Class: ActionView::Helpers::InstanceTag

Inherits:
Object
  • Object
show all
Includes:
DateHelper, FormOptionsHelper, TagHelper
Defined in:
lib/action_view/helpers/date_helper.rb,
lib/action_view/helpers/form_helper.rb,
lib/action_view/helpers/form_options_helper.rb,
lib/action_view/helpers/active_record_helper.rb

Overview

:nodoc:

Constant Summary collapse

DEFAULT_FIELD_OPTIONS =
{ "size" => 30 }.freeze
DEFAULT_RADIO_OPTIONS =
{ }.freeze
DEFAULT_TEXT_AREA_OPTIONS =
{ "cols" => 40, "rows" => 20 }.freeze
DEFAULT_DATE_OPTIONS =
{ :discard_type => true }.freeze

Constants included from FormOptionsHelper

FormOptionsHelper::COUNTRIES

Constants included from DateHelper

DateHelper::DEFAULT_PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FormOptionsHelper

#collection_select, #country_options_for_select, #country_select, #option_groups_from_collection_for_select, #options_for_select, #options_from_collection_for_select, #select, #time_zone_options_for_select, #time_zone_select

Methods included from TagHelper

#cdata_section

Methods included from DateHelper

#date_select, #datetime_select, #distance_of_time_in_words, #select_date, #select_datetime, #select_day, #select_hour, #select_minute, #select_month, #select_second, #select_time, #select_year, #time_ago_in_words

Constructor Details

#initialize(object_name, method_name, template_object, local_binding = nil, object = nil) ⇒ InstanceTag

Returns a new instance of InstanceTag.


236
237
238
239
240
241
242
243
# File 'lib/action_view/helpers/form_helper.rb', line 236

def initialize(object_name, method_name, template_object, local_binding = nil, object = nil)
  @object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup
  @template_object, @local_binding = template_object, local_binding
  @object = object
  if @object_name.sub!(/\[\]$/,"")
    @auto_index = @template_object.instance_variable_get("@#{Regexp.last_match.pre_match}").id_before_type_cast
  end
end

Instance Attribute Details

#method_nameObject (readonly)

Returns the value of attribute method_name


229
230
231
# File 'lib/action_view/helpers/form_helper.rb', line 229

def method_name
  @method_name
end

#object_nameObject (readonly)

Returns the value of attribute object_name


229
230
231
# File 'lib/action_view/helpers/form_helper.rb', line 229

def object_name
  @object_name
end

Instance Method Details

#column_typeObject


195
196
197
# File 'lib/action_view/helpers/active_record_helper.rb', line 195

def column_type
  object.send("column_for_attribute", @method_name).type
end

#content_tag(name, value, options) ⇒ Object


161
162
163
164
165
166
167
# File 'lib/action_view/helpers/active_record_helper.rb', line 161

def (name, value, options)
  if object.respond_to?("errors") && object.errors.respond_to?("on")
    error_wrapping((name, value, options), object.errors.on(@method_name))
  else
    (name, value, options)
  end
end

#content_tag_without_error_wrappingObject


160
# File 'lib/action_view/helpers/active_record_helper.rb', line 160

alias_method :content_tag_without_error_wrapping, :content_tag

#error_messageObject


191
192
193
# File 'lib/action_view/helpers/active_record_helper.rb', line 191

def error_message
  object.errors.on(@method_name)
end

#error_wrapping(html_tag, has_error) ⇒ Object


187
188
189
# File 'lib/action_view/helpers/active_record_helper.rb', line 187

def error_wrapping(html_tag, has_error)
  has_error ? Base.field_error_proc.call(html_tag, self) : html_tag
end

#objectObject


327
328
329
# File 'lib/action_view/helpers/form_helper.rb', line 327

def object
  @object || @template_object.instance_variable_get("@#{@object_name}")
end

#tag(name, options) ⇒ Object


152
153
154
155
156
157
158
# File 'lib/action_view/helpers/active_record_helper.rb', line 152

def tag(name, options)
  if object.respond_to?("errors") && object.errors.respond_to?("on")
    error_wrapping(tag_without_error_wrapping(name, options), object.errors.on(@method_name))
  else
    tag_without_error_wrapping(name, options)
  end
end

#tag_without_error_wrappingObject


151
# File 'lib/action_view/helpers/active_record_helper.rb', line 151

alias_method :tag_without_error_wrapping, :tag

#to_boolean_select_tag(options = {}) ⇒ Object


311
312
313
314
315
316
317
318
319
320
321
# File 'lib/action_view/helpers/form_helper.rb', line 311

def to_boolean_select_tag(options = {})
  options = options.stringify_keys
  add_default_name_and_id(options)
  tag_text = "<select"
  tag_text << tag_options(options)
  tag_text << "><option value=\"false\""
  tag_text << " selected" if value == false
  tag_text << ">False</option><option value=\"true\""
  tag_text << " selected" if value
  tag_text << ">True</option></select>"
end

#to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object


277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/action_view/helpers/form_helper.rb', line 277

def to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0")
  options = options.stringify_keys
  options["type"]     = "checkbox"
  options["value"]    = checked_value
  checked = case value
    when TrueClass, FalseClass
      value
    when NilClass
      false
    when Integer
      value != 0
    when String
      value == checked_value
    else
      value.to_i != 0
    end
  if checked || options["checked"] == "checked"
    options["checked"] = "checked"
  else
    options.delete("checked")
  end
  add_default_name_and_id(options)
  tag("input", options) << tag("input", "name" => options["name"], "type" => "hidden", "value" => unchecked_value)
end

#to_collection_select_tag(collection, value_method, text_method, options, html_options) ⇒ Object


306
307
308
309
310
311
312
# File 'lib/action_view/helpers/form_options_helper.rb', line 306

def to_collection_select_tag(collection, value_method, text_method, options, html_options)
  html_options = html_options.stringify_keys
  add_default_name_and_id(html_options)
  (
    "select", add_options(options_from_collection_for_select(collection, value_method, text_method, value), options, value), html_options
  )
end

#to_content_tag(tag_name, options = {}) ⇒ Object


323
324
325
# File 'lib/action_view/helpers/form_helper.rb', line 323

def (tag_name, options = {})
  (tag_name, value, options)
end

#to_country_select_tag(priority_countries, options, html_options) ⇒ Object


314
315
316
317
318
# File 'lib/action_view/helpers/form_options_helper.rb', line 314

def to_country_select_tag(priority_countries, options, html_options)
  html_options = html_options.stringify_keys
  add_default_name_and_id(html_options)
  ("select", add_options(country_options_for_select(value, priority_countries), options, value), html_options)
end

#to_date_select_tag(options = {}) ⇒ Object


257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/action_view/helpers/date_helper.rb', line 257

def to_date_select_tag(options = {})
  defaults = { :discard_type => true }
  options  = defaults.merge(options)
  options_with_prefix = Proc.new { |position| options.merge(:prefix => "#{@object_name}[#{@method_name}(#{position}i)]") }
  date     = options[:include_blank] ? (value || 0) : (value || Date.today)

  date_select = ''
  options[:order]   = [:month, :year, :day] if options[:month_before_year] # For backwards compatibility
  options[:order] ||= [:year, :month, :day]

  position = {:year => 1, :month => 2, :day => 3}

  discard = {}
  discard[:year]  = true if options[:discard_year]
  discard[:month] = true if options[:discard_month]
  discard[:day]   = true if options[:discard_day] or options[:discard_month]

  options[:order].each do |param|
    date_select << self.send("select_#{param}", date, options_with_prefix.call(position[param])) unless discard[param]
  end

  date_select
end

#to_date_select_tag_without_error_wrappingObject


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/action_view/helpers/active_record_helper.rb', line 169

def to_date_select_tag(options = {})
  defaults = { :discard_type => true }
  options  = defaults.merge(options)
  options_with_prefix = Proc.new { |position| options.merge(:prefix => "#{@object_name}[#{@method_name}(#{position}i)]") }
  date     = options[:include_blank] ? (value || 0) : (value || Date.today)

  date_select = ''
  options[:order]   = [:month, :year, :day] if options[:month_before_year] # For backwards compatibility
  options[:order] ||= [:year, :month, :day]

  position = {:year => 1, :month => 2, :day => 3}

  discard = {}
  discard[:year]  = true if options[:discard_year]
  discard[:month] = true if options[:discard_month]
  discard[:day]   = true if options[:discard_day] or options[:discard_month]

  options[:order].each do |param|
    date_select << self.send("select_#{param}", date, options_with_prefix.call(position[param])) unless discard[param]
  end

  date_select
end

#to_date_tagObject


302
303
304
305
306
307
308
309
# File 'lib/action_view/helpers/form_helper.rb', line 302

def to_date_tag()
  defaults = DEFAULT_DATE_OPTIONS.dup
  date     = value || Date.today
  options  = Proc.new { |position| defaults.merge(:prefix => "#{@object_name}[#{@method_name}(#{position}i)]") }
  html_day_select(date, options.call(3)) +
  html_month_select(date, options.call(2)) +
  html_year_select(date, options.call(1))
end

#to_datetime_select_tag(options = {}) ⇒ Object


281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/action_view/helpers/date_helper.rb', line 281

def to_datetime_select_tag(options = {})
  defaults = { :discard_type => true }
  options  = defaults.merge(options)
  options_with_prefix = Proc.new { |position| options.merge(:prefix => "#{@object_name}[#{@method_name}(#{position}i)]") }
  datetime = options[:include_blank] ? (value || nil) : (value || Time.now)

  datetime_select  = select_year(datetime, options_with_prefix.call(1))
  datetime_select << select_month(datetime, options_with_prefix.call(2)) unless options[:discard_month]
  datetime_select << select_day(datetime, options_with_prefix.call(3)) unless options[:discard_day] || options[:discard_month]
  datetime_select << ' &mdash; ' + select_hour(datetime, options_with_prefix.call(4)) unless options[:discard_hour]
  datetime_select << ' : ' + select_minute(datetime, options_with_prefix.call(5)) unless options[:discard_minute] || options[:discard_hour]

  datetime_select
end

#to_datetime_select_tag_without_error_wrappingObject


178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/action_view/helpers/active_record_helper.rb', line 178

def to_datetime_select_tag(options = {})
  defaults = { :discard_type => true }
  options  = defaults.merge(options)
  options_with_prefix = Proc.new { |position| options.merge(:prefix => "#{@object_name}[#{@method_name}(#{position}i)]") }
  datetime = options[:include_blank] ? (value || nil) : (value || Time.now)

  datetime_select  = select_year(datetime, options_with_prefix.call(1))
  datetime_select << select_month(datetime, options_with_prefix.call(2)) unless options[:discard_month]
  datetime_select << select_day(datetime, options_with_prefix.call(3)) unless options[:discard_day] || options[:discard_month]
  datetime_select << ' &mdash; ' + select_hour(datetime, options_with_prefix.call(4)) unless options[:discard_hour]
  datetime_select << ' : ' + select_minute(datetime, options_with_prefix.call(5)) unless options[:discard_minute] || options[:discard_hour]

  datetime_select
end

#to_input_field_tag(field_type, options = {}) ⇒ Object


245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/action_view/helpers/form_helper.rb', line 245

def to_input_field_tag(field_type, options = {})
  options = options.stringify_keys
  options["size"] ||= options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"]
  options = DEFAULT_FIELD_OPTIONS.merge(options)
  if field_type == "hidden"
    options.delete("size")
  end
  options["type"] = field_type
  options["value"] ||= value_before_type_cast unless field_type == "file"
  add_default_name_and_id(options)
  tag("input", options)
end

#to_radio_button_tag(tag_value, options = {}) ⇒ Object


258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/action_view/helpers/form_helper.rb', line 258

def to_radio_button_tag(tag_value, options = {})
  options = DEFAULT_RADIO_OPTIONS.merge(options.stringify_keys)
  options["type"]     = "radio"
  options["value"]    = tag_value
  options["checked"]  = "checked" if value.to_s == tag_value.to_s
  pretty_tag_value    = tag_value.to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase
  options["id"]       = @auto_index ?             
    "#{@object_name}_#{@auto_index}_#{@method_name}_#{pretty_tag_value}" :
    "#{@object_name}_#{@method_name}_#{pretty_tag_value}"
  add_default_name_and_id(options)
  tag("input", options)
end

#to_select_tag(choices, options, html_options) ⇒ Object


299
300
301
302
303
304
# File 'lib/action_view/helpers/form_options_helper.rb', line 299

def to_select_tag(choices, options, html_options)
  html_options = html_options.stringify_keys
  add_default_name_and_id(html_options)
  selected_value = options.has_key?(:selected) ? options[:selected] : value
  ("select", add_options(options_for_select(choices, selected_value), options, value), html_options)
end

#to_tag(options = {}) ⇒ Object


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/action_view/helpers/active_record_helper.rb', line 133

def to_tag(options = {})
  case column_type
    when :string
      field_type = @method_name.include?("password") ? "password" : "text"
      to_input_field_tag(field_type, options)
    when :text
      to_text_area_tag(options)
    when :integer, :float
      to_input_field_tag("text", options)
    when :date
      to_date_select_tag(options)
    when :datetime, :timestamp
      to_datetime_select_tag(options)
    when :boolean
      to_boolean_select_tag(options)
  end
end

#to_text_area_tag(options = {}) ⇒ Object


271
272
273
274
275
# File 'lib/action_view/helpers/form_helper.rb', line 271

def to_text_area_tag(options = {})
  options = DEFAULT_TEXT_AREA_OPTIONS.merge(options.stringify_keys)
  add_default_name_and_id(options)
  ("textarea", html_escape(options.delete('value') || value_before_type_cast), options)
end

#to_time_zone_select_tag(priority_zones, options, html_options) ⇒ Object


320
321
322
323
324
325
326
327
328
329
# File 'lib/action_view/helpers/form_options_helper.rb', line 320

def to_time_zone_select_tag(priority_zones, options, html_options)
  html_options = html_options.stringify_keys
  add_default_name_and_id(html_options)
  ("select",
    add_options(
      time_zone_options_for_select(value, priority_zones, options[:model] || TimeZone),
      options, value
    ), html_options
  )
end

#valueObject


331
332
333
334
335
# File 'lib/action_view/helpers/form_helper.rb', line 331

def value
  unless object.nil?
    object.send(@method_name)
  end
end

#value_before_type_castObject


337
338
339
340
341
342
343
# File 'lib/action_view/helpers/form_helper.rb', line 337

def value_before_type_cast
  unless object.nil?
    object.respond_to?(@method_name + "_before_type_cast") ?
      object.send(@method_name + "_before_type_cast") :
      object.send(@method_name)
  end
end