Module: Clot::ModelTag

Included in:
CheckBox, CollectionSelect, FileField, Label, ModelMultiDateTag, PasswordField, TextArea, TextField
Defined in:
lib/clot/model_form_tags.rb

Instance Method Summary collapse

Instance Method Details

#render(context) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/clot/model_form_tags.rb', line 52

def render(context)
  result = super(context)
  #if @errors.include? @attribute_name
  #  result = "<div class=\"fieldWithErrors\">#{result}</div>"
  #end
  result
end

#set_primary_attributes(context) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/clot/model_form_tags.rb', line 3

def set_primary_attributes(context)
  @item = context['form_model']
  if @item

    @attribute_name =  resolve_value(@params.shift,context)
    @first_attr = context['form_class_name']
  else
    @first_attr =  @params.shift

    if @params[0] && ! @params[0].match(/:/)
      @attribute_name =  resolve_value(@params.shift,context)
    end
    @item = context[@first_attr]
  end
  attribute_names = @attribute_name.split('.')

  unless @item.source.respond_to?(:"#{attribute_names[0]}=")
    raise "#{attribute_names[0]} is not a valid form field for #{@first_attr.camelize}."
  end

  if attribute_names.size == 3
    @name_string = @first_attr + "[" + attribute_names[0].to_s + "_attributes][" + attribute_names[1].to_s + "_attributes][" + attribute_names[2].to_s + "]"
    @id_string = @first_attr + "_" + attribute_names[0].to_s + "_attributes_" + attribute_names[1].to_s + "_" + attribute_names[2].to_s
    @value_string = ""
    if @item
      @subitem = @item[attribute_names[0]][attribute_names[1]]
      if @subitem
        @value_string = @subitem[attribute_names[2].to_sym]
      end
    end
  elsif attribute_names.size == 2
    @name_string = @first_attr + "[" + attribute_names[0].to_s + "_attributes][" + attribute_names[1].to_s + "]"
    @id_string = @first_attr + "_" + attribute_names.join('_')
    @value_string = ""
    if @item
      @subitem = @item[attribute_names[0]]
      if @subitem
        @value_string = @subitem[attribute_names[1].to_sym]
      end
    end
  else
    @id_string = "#{@first_attr}_#{@attribute_name}"
    @name_string = "#{@first_attr}[#{@attribute_name}]"
    @value_string = @item[@attribute_name.to_sym]
  end
  @errors = context['form_errors'] || []

end