Class: Clot::ErrorMessagesFor

Inherits:
Liquid::Tag
  • Object
show all
Includes:
TagHelper
Defined in:
lib/clot/form_for.rb

Instance Method Summary collapse

Methods included from TagHelper

#resolve_value, #split_params

Constructor Details

#initialize(name, params, tokens) ⇒ ErrorMessagesFor

Returns a new instance of ErrorMessagesFor.



192
193
194
195
# File 'lib/clot/form_for.rb', line 192

def initialize(name, params, tokens)
  @_params = split_params(params)
  super
end

Instance Method Details

#render(context) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/clot/form_for.rb', line 198

def render(context)
  @params = @_params.clone
  @model = context[@params.shift]

  if @model and @model.respond_to?(:errors) and @model.errors.count > 0
    @suffix = @model.errors.count > 1 ? "s" : ""
    @default_message = @model.errors.count.to_s + " error#{@suffix} occurred while processing information"

    @params.each do |pair|
      pair = pair.split /:/
      value = resolve_value(pair[1], context)

      case pair[0]
        when "header_message" then
          @default_message = value
      end
    end

    readable_errors = @model.errors.map{ |target, msg| "#{target}: #{msg}" }

    { :message => @default_message, :errors => readable_errors }.to_json
  end
end