Class: Erector::FormFor::Input

Inherits:
Widget
  • Object
show all
Defined in:
lib/erector/form_for.rb

Constant Summary collapse

EQUIVALENCIES =
{:string => :text, :datetime_local => :'datetime-local', :phone => :tel, :fax => :tel}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, kind, object, object_name, column, opts) ⇒ Input

Returns a new instance of Input.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/erector/form_for.rb', line 80

def initialize app, kind, object, object_name, column, opts
  @type        = kind.to_sym 
  @app         = app
  @column      = column
  @object_name = object_name
  opts[:name]  ||= object_name ? "#{object_name}[#{column}]" : column
  opts[:id]    ||= [object_name, column].compact.join('-').dasherize
  opts[:value] ||= Hash === object ? object[column] : object.send(column)
  @label = opts.delete(:label) || default_label
  super opts
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



78
79
80
# File 'lib/erector/form_for.rb', line 78

def id
  @id
end

Instance Method Details

#contentObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/erector/form_for.rb', line 99

def content
  case @type
  when :boolean
    input :type => :hidden, :value => 0, :name => @name
    label :for => id do
      input assigns.merge(:type => :checkbox, :value => 1)
      text @label
    end
  when :text
    label @label, :for => id
    textarea @value, assigns
  when :select
    label @label, :for => id
    assigns.delete(:collection) || raise("Missing parameter :collection")
    assigns.delete(:value) 
    select assigns do
      @collection.each do |value, text|
        option text || value, :value => value, :selected => value.to_s == @value.to_s ? 'selected' : nil
      end
    end
  when :radio
    assigns.delete(:collection) || raise("Missing parameter :collection")
    assigns.delete(:value) 
    fieldset do
      label @label
      ol do
        @collection.each do |value, label_text|
          li do
            label do
              input :value => value, :id => "#{@id}-#{value}".downcase.dasherize, :checked => value.to_s == @value.to_s ? 'checked' : nil
              text " #{label_text || value}"
            end
          end
        end
      end
    end
  else
    label @label, :for => id
    input assigns.merge(:type => EQUIVALENCIES[@type] || @type)
  end
end

#default_labelObject



92
93
94
95
96
97
# File 'lib/erector/form_for.rb', line 92

def default_label
  return @column.to_s.titleize unless @app.respond_to?(:t)
  @app.t.form_for.labels[@object_name][@column] |
    @app.t.models.user.attributes[@column] |
    @column.to_s.titleize
end