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.



233
234
235
236
# File 'lib/clot/form_for.rb', line 233

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

Instance Method Details

#error_message(attr, msg) ⇒ Object



269
270
271
272
273
274
275
# File 'lib/clot/form_for.rb', line 269

def error_message(attr, msg)
  unless attr == :base
    "#{attr} - #{msg}"
  else
    msg
  end
end

#render(context) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/clot/form_for.rb', line 239

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

  result = ""
  if @model 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

    result += '<div class="errorExplanation" id="errorExplanation"><h2>' + @default_message + '</h2><ul>'

    @model.errors.each do |attr, msg|
      result += "<li>#{error_message(attr, msg)}</li>"
    end

    result += "</ul></div>"
  end
  result
end