Class: ActionView::Helpers::InstanceTag

Inherits:
Object
  • Object
show all
Includes:
DateHelper, FormOptionsHelper, FormTagHelper, 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

Constants included from FormOptionsHelper

FormOptionsHelper::COUNTRIES

Constants included from ERB::Util

ERB::Util::HTML_ESCAPE, ERB::Util::JSON_ESCAPE

Constants included from TagHelper

TagHelper::BOOLEAN_ATTRIBUTES

Constants included from DateHelper

DateHelper::DEFAULT_PREFIX

Instance Attribute Summary collapse

Class Method 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 ERB::Util

#html_escape, j, json_escape

Methods included from TagHelper

#cdata_section, #escape_once

Methods included from FormTagHelper

#check_box_tag, #field_set_tag, #file_field_tag, #form_tag, #hidden_field_tag, #image_submit_tag, #label_tag, #password_field_tag, #radio_button_tag, #select_tag, #submit_tag, #text_area_tag, #text_field_tag

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, #time_select

Constructor Details

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

Returns a new instance of InstanceTag.



504
505
506
507
508
509
510
511
512
513
514
515
# File 'lib/action_view/helpers/form_helper.rb', line 504

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!(/\[\]$/,"")
    if object ||= @template_object.instance_variable_get("@#{Regexp.last_match.pre_match}") and object.respond_to?(:to_param)
      @auto_index = object.to_param
    else
      raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}"
    end
  end
end

Instance Attribute Details

#method_nameObject (readonly)

Returns the value of attribute method_name.



498
499
500
# File 'lib/action_view/helpers/form_helper.rb', line 498

def method_name
  @method_name
end

#object_nameObject (readonly)

Returns the value of attribute object_name.



498
499
500
# File 'lib/action_view/helpers/form_helper.rb', line 498

def object_name
  @object_name
end

Class Method Details

.check_box_checked?(value, checked_value) ⇒ Boolean

Returns:

  • (Boolean)


632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'lib/action_view/helpers/form_helper.rb', line 632

def check_box_checked?(value, checked_value)
  case value
  when TrueClass, FalseClass
    value
  when NilClass
    false
  when Integer
    value != 0
  when String
    value == checked_value
  when Array
    value.include?(checked_value)
  else
    value.to_i != 0
  end
end

.radio_button_checked?(value, checked_value) ⇒ Boolean

Returns:

  • (Boolean)


649
650
651
# File 'lib/action_view/helpers/form_helper.rb', line 649

def radio_button_checked?(value, checked_value)
  value.to_s == checked_value.to_s
end

.value(object, method_name) ⇒ Object



620
621
622
# File 'lib/action_view/helpers/form_helper.rb', line 620

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

.value_before_type_cast(object, method_name) ⇒ Object



624
625
626
627
628
629
630
# File 'lib/action_view/helpers/form_helper.rb', line 624

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

Instance Method Details

#column_typeObject



271
272
273
# File 'lib/action_view/helpers/active_record_helper.rb', line 271

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

#content_tag(name, value, options) ⇒ Object



228
229
230
231
232
233
234
# File 'lib/action_view/helpers/active_record_helper.rb', line 228

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



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

alias_method :content_tag_without_error_wrapping, :content_tag

#error_messageObject



267
268
269
# File 'lib/action_view/helpers/active_record_helper.rb', line 267

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

#error_wrapping(html_tag, has_error) ⇒ Object



263
264
265
# File 'lib/action_view/helpers/active_record_helper.rb', line 263

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

#objectObject



603
604
605
606
607
608
609
# File 'lib/action_view/helpers/form_helper.rb', line 603

def object
  @object || @template_object.instance_variable_get("@#{@object_name}")
rescue NameError
  # As @object_name may contain the nested syntax (item[subobject]) we
  # need to fallback to nil.
  nil
end

#tag(name, options) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/action_view/helpers/active_record_helper.rb', line 219

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



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

alias_method :tag_without_error_wrapping, :tag

#to_boolean_select_tag(options = {}) ⇒ Object



586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/action_view/helpers/form_helper.rb', line 586

def to_boolean_select_tag(options = {})
  options = options.stringify_keys
  add_default_name_and_id(options)
  value = value(object)
  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



571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/action_view/helpers/form_helper.rb', line 571

def to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0")
  options = options.stringify_keys
  options["type"]     = "checkbox"
  options["value"]    = checked_value
  if options.has_key?("checked")
    cv = options.delete "checked"
    checked = cv == true || cv == "checked"
  else
    checked = self.class.check_box_checked?(value(object), checked_value)
  end
  options["checked"] = "checked" if checked
  add_default_name_and_id(options)
  tag("input", options) << tag("input", "name" => options["name"], "type" => "hidden", "value" => options['disabled'] && checked ? checked_value : unchecked_value)
end

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



393
394
395
396
397
398
399
400
# File 'lib/action_view/helpers/form_options_helper.rb', line 393

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)
  value = value(object)
  (
    "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



599
600
601
# File 'lib/action_view/helpers/form_helper.rb', line 599

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

#to_country_select_tag(priority_countries, options, html_options) ⇒ Object



402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/action_view/helpers/form_options_helper.rb', line 402

def to_country_select_tag(priority_countries, options, html_options)
  ActiveSupport::Deprecation.warn("country_select will be removed from 2.2.0.  http://www.rubyonrails.org/deprecation/list-of-countries has more information.", caller)
  html_options = html_options.stringify_keys
  add_default_name_and_id(html_options)
  value = value(object)
  ("select",
    add_options(
      country_options_for_select(value, priority_countries),
      options, value
    ), html_options
  )
end

#to_date_select_tag(options = {}, html_options = {}) ⇒ Object



601
602
603
# File 'lib/action_view/helpers/date_helper.rb', line 601

def to_date_select_tag(options = {}, html_options = {})
  date_or_time_select(options.merge(:discard_hour => true), html_options)
end

#to_date_select_tag_without_error_wrappingObject



236
237
238
# File 'lib/action_view/helpers/active_record_helper.rb', line 236

def to_date_select_tag(options = {}, html_options = {})
  date_or_time_select(options.merge(:discard_hour => true), html_options)
end

#to_datetime_select_tag(options = {}, html_options = {}) ⇒ Object



609
610
611
# File 'lib/action_view/helpers/date_helper.rb', line 609

def to_datetime_select_tag(options = {}, html_options = {})
  date_or_time_select(options, html_options)
end

#to_datetime_select_tag_without_error_wrappingObject



245
246
247
# File 'lib/action_view/helpers/active_record_helper.rb', line 245

def to_datetime_select_tag(options = {}, html_options = {})
  date_or_time_select(options, html_options)
end

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



527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/action_view/helpers/form_helper.rb', line 527

def to_input_field_tag(field_type, options = {})
  options = options.stringify_keys
  options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("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(object) unless field_type == "file"
  options["value"] &&= html_escape(options["value"])
  add_default_name_and_id(options)
  tag("input", options)
end

#to_label_tag(text = nil, options = {}) ⇒ Object



517
518
519
520
521
522
523
524
525
# File 'lib/action_view/helpers/form_helper.rb', line 517

def to_label_tag(text = nil, options = {})
  options = options.stringify_keys
  name_and_id = options.dup
  add_default_name_and_id(name_and_id)
  options.delete("index")
  options["for"] ||= name_and_id["id"]
  content = (text.blank? ? nil : text.to_s) || method_name.humanize
  label_tag(name_and_id["id"], content, options)
end

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



541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/action_view/helpers/form_helper.rb', line 541

def to_radio_button_tag(tag_value, options = {})
  options = DEFAULT_RADIO_OPTIONS.merge(options.stringify_keys)
  options["type"]     = "radio"
  options["value"]    = tag_value
  if options.has_key?("checked")
    cv = options.delete "checked"
    checked = cv == true || cv == "checked"
  else
    checked = self.class.radio_button_checked?(value(object), tag_value)
  end
  options["checked"]  = "checked" if checked
  pretty_tag_value    = tag_value.to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase
  options["id"]     ||= defined?(@auto_index) ?
    "#{tag_id_with_index(@auto_index)}_#{pretty_tag_value}" :
    "#{tag_id}_#{pretty_tag_value}"
  add_default_name_and_id(options)
  tag("input", options)
end

#to_select_tag(choices, options, html_options) ⇒ Object



385
386
387
388
389
390
391
# File 'lib/action_view/helpers/form_options_helper.rb', line 385

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

#to_tag(options = {}) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/action_view/helpers/active_record_helper.rb', line 198

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, :decimal
      to_input_field_tag("text", options)
    when :date
      to_date_select_tag(options)
    when :datetime, :timestamp
      to_datetime_select_tag(options)
    when :time
      to_time_select_tag(options)
    when :boolean
      to_boolean_select_tag(options)
  end
end

#to_text_area_tag(options = {}) ⇒ Object



560
561
562
563
564
565
566
567
568
569
# File 'lib/action_view/helpers/form_helper.rb', line 560

def to_text_area_tag(options = {})
  options = DEFAULT_TEXT_AREA_OPTIONS.merge(options.stringify_keys)
  add_default_name_and_id(options)

  if size = options.delete("size")
    options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split)
  end

  ("textarea", html_escape(options.delete('value') || value_before_type_cast(object)), options)
end

#to_time_select_tag(options = {}, html_options = {}) ⇒ Object



605
606
607
# File 'lib/action_view/helpers/date_helper.rb', line 605

def to_time_select_tag(options = {}, html_options = {})
  date_or_time_select(options.merge(:discard_year => true, :discard_month => true), html_options)
end

#to_time_select_tag_without_error_wrappingObject



254
255
256
# File 'lib/action_view/helpers/active_record_helper.rb', line 254

def to_time_select_tag(options = {}, html_options = {})
  date_or_time_select(options.merge(:discard_year => true, :discard_month => true), html_options)
end

#to_time_zone_select_tag(priority_zones, options, html_options) ⇒ Object



415
416
417
418
419
420
421
422
423
424
425
# File 'lib/action_view/helpers/form_options_helper.rb', line 415

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

#value(object) ⇒ Object



611
612
613
# File 'lib/action_view/helpers/form_helper.rb', line 611

def value(object)
  self.class.value(object, @method_name)
end

#value_before_type_cast(object) ⇒ Object



615
616
617
# File 'lib/action_view/helpers/form_helper.rb', line 615

def value_before_type_cast(object)
  self.class.value_before_type_cast(object, @method_name)
end