5

私は液体テンプレートをDBに保存しており、レンダリングする前に、テンプレートに必要なすべてのパラメーターが提供されているかどうかを確認したいと思います-今では次のようなものを見つけました:

parsed = Liquid::Template.parse(string_with_template)
required = parsed.instance_values["root"].instance_values["nodelist"].select{ |v| v.is_a?(Liquid::Variable) }.map(&:name)

そして、レンダリングする前に私は関数を持っています

def has_all_required?(liquid_params, required)
  keys = liquid_params.keys
  required.each{|e| return false unless keys.include?(e) }
  return true
end

この検証を達成するためのよりクリーンな方法はありますか?

すべての提案をありがとう、Santuxus

4

1 に答える 1

2

テンプレートを作成するときに、似たようなことをして、テンプレート本体に対してカスタムバリデーターを使用しました。

validates :body, :presence => true, :email_template => true

次に、テンプレートタイプに対してフィールドを検証するEmailTemplateValidatorがあります。

def validate_each(object, attribute, value)
    case object.template_type
    when 'registration'
        # registration_welcome emails MUST include the activation_url token
        if !value.include?('{{activation_url}}')
            object.errors[attribute] << 'Registration emails must include the {{activation_url}} token'
        end
    end    

終わり

次に、アプリに含まれている必要のあるトークンを含む新しいテンプレートがアプリで必要になるため、新しいケースブロックをバリデーターに追加する計画です。

于 2011-05-03T14:14:15.410 に答える