Class: ActionView::Helpers::InstanceTag
- Inherits:
-
Object
- Object
- ActionView::Helpers::InstanceTag
show all
- Includes:
- 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
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 ERB::Util
ERB::Util::HTML_ESCAPE, ERB::Util::JSON_ESCAPE
Constants included
from TagHelper
TagHelper::BOOLEAN_ATTRIBUTES
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#column_type ⇒ Object
-
#content_tag(name, value, options) ⇒ Object
-
#content_tag_without_error_wrapping ⇒ Object
-
#error_message ⇒ Object
-
#error_wrapping(html_tag, has_error) ⇒ Object
-
#initialize(object_name, method_name, template_object, object = nil) ⇒ InstanceTag
constructor
A new instance of InstanceTag.
-
#object ⇒ Object
-
#tag(name, options) ⇒ Object
-
#tag_without_error_wrapping ⇒ Object
-
#to_boolean_select_tag(options = {}) ⇒ Object
-
#to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
-
#to_collection_select_tag(collection, value_method, text_method, options, html_options) ⇒ Object
-
#to_content_tag(tag_name, options = {}) ⇒ Object
-
#to_date_select_tag(options = {}, html_options = {}) ⇒ Object
-
#to_date_select_tag_without_error_wrapping ⇒ Object
-
#to_datetime_select_tag(options = {}, html_options = {}) ⇒ Object
-
#to_datetime_select_tag_without_error_wrapping ⇒ Object
-
#to_grouped_collection_select_tag(collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options) ⇒ Object
-
#to_input_field_tag(field_type, options = {}) ⇒ Object
-
#to_label_tag(text = nil, options = {}) ⇒ Object
-
#to_radio_button_tag(tag_value, options = {}) ⇒ Object
-
#to_select_tag(choices, options, html_options) ⇒ Object
-
#to_tag(options = {}) ⇒ Object
-
#to_text_area_tag(options = {}) ⇒ Object
-
#to_time_select_tag(options = {}, html_options = {}) ⇒ Object
-
#to_time_select_tag_without_error_wrapping ⇒ Object
-
#to_time_zone_select_tag(priority_zones, options, html_options) ⇒ Object
-
#value(object) ⇒ Object
-
#value_before_type_cast(object) ⇒ Object
#collection_select, #grouped_collection_select, #grouped_options_for_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
#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
Constructor Details
#initialize(object_name, method_name, template_object, object = nil) ⇒ InstanceTag
Returns a new instance of InstanceTag.
712
713
714
715
716
717
718
719
720
721
722
723
|
# File 'lib/action_view/helpers/form_helper.rb', line 712
def initialize(object_name, method_name, template_object, object = nil)
@object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup
@template_object = template_object
@object = object
if @object_name.sub!(/\[\]$/,"") || @object_name.sub!(/\[\]\]$/,"]")
if (object ||= @template_object.instance_variable_get("@#{Regexp.last_match.pre_match}")) && 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_name ⇒ Object
Returns the value of attribute method_name.
706
707
708
|
# File 'lib/action_view/helpers/form_helper.rb', line 706
def method_name
@method_name
end
|
#object_name ⇒ Object
Returns the value of attribute object_name.
706
707
708
|
# File 'lib/action_view/helpers/form_helper.rb', line 706
def object_name
@object_name
end
|
Class Method Details
.check_box_checked?(value, checked_value) ⇒ Boolean
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
|
# File 'lib/action_view/helpers/form_helper.rb', line 840
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
|
857
858
859
|
# File 'lib/action_view/helpers/form_helper.rb', line 857
def radio_button_checked?(value, checked_value)
value.to_s == checked_value.to_s
end
|
.value(object, method_name) ⇒ Object
828
829
830
|
# File 'lib/action_view/helpers/form_helper.rb', line 828
def value(object, method_name)
object.send method_name unless object.nil?
end
|
.value_before_type_cast(object, method_name) ⇒ Object
832
833
834
835
836
837
838
|
# File 'lib/action_view/helpers/form_helper.rb', line 832
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_type ⇒ Object
300
301
302
|
# File 'lib/action_view/helpers/active_record_helper.rb', line 300
def column_type
object.send(:column_for_attribute, @method_name).type
end
|
#content_tag(name, value, options) ⇒ Object
257
258
259
260
261
262
263
|
# File 'lib/action_view/helpers/active_record_helper.rb', line 257
def content_tag(name, value, options)
if object.respond_to?(:errors) && object.errors.respond_to?(:on)
error_wrapping(content_tag_without_error_wrapping(name, value, options), object.errors.on(@method_name))
else
content_tag_without_error_wrapping(name, value, options)
end
end
|
#content_tag_without_error_wrapping ⇒ Object
256
|
# File 'lib/action_view/helpers/active_record_helper.rb', line 256
alias_method :content_tag_without_error_wrapping, :content_tag
|
#error_message ⇒ Object
296
297
298
|
# File 'lib/action_view/helpers/active_record_helper.rb', line 296
def error_message
object.errors.on(@method_name)
end
|
#error_wrapping(html_tag, has_error) ⇒ Object
292
293
294
|
# File 'lib/action_view/helpers/active_record_helper.rb', line 292
def error_wrapping(html_tag, has_error)
has_error ? Base.field_error_proc.call(html_tag, self) : html_tag
end
|
811
812
813
814
815
816
817
|
# File 'lib/action_view/helpers/form_helper.rb', line 811
def object
@object || @template_object.instance_variable_get("@#{@object_name}")
rescue NameError
nil
end
|
#tag(name, options) ⇒ Object
248
249
250
251
252
253
254
|
# File 'lib/action_view/helpers/active_record_helper.rb', line 248
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_wrapping ⇒ Object
247
|
# File 'lib/action_view/helpers/active_record_helper.rb', line 247
alias_method :tag_without_error_wrapping, :tag
|
#to_boolean_select_tag(options = {}) ⇒ Object
794
795
796
797
798
799
800
801
802
803
804
805
|
# File 'lib/action_view/helpers/form_helper.rb', line 794
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
|
# File 'lib/action_view/helpers/form_helper.rb', line 777
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)
hidden = tag("input", "name" => options["name"], "type" => "hidden", "value" => options['disabled'] && checked ? checked_value : unchecked_value)
checkbox = tag("input", options)
hidden + checkbox
end
|
#to_collection_select_tag(collection, value_method, text_method, options, html_options) ⇒ Object
536
537
538
539
540
541
542
543
544
545
|
# File 'lib/action_view/helpers/form_options_helper.rb', line 536
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)
disabled_value = options.has_key?(:disabled) ? options[:disabled] : nil
selected_value = options.has_key?(:selected) ? options[:selected] : value
content_tag(
"select", add_options(options_from_collection_for_select(collection, value_method, text_method, :selected => selected_value, :disabled => disabled_value), options, value), html_options
)
end
|
#to_content_tag(tag_name, options = {}) ⇒ Object
807
808
809
|
# File 'lib/action_view/helpers/form_helper.rb', line 807
def to_content_tag(tag_name, options = {})
content_tag(tag_name, value(object), options)
end
|
#to_date_select_tag(options = {}, html_options = {}) ⇒ Object
906
907
908
|
# File 'lib/action_view/helpers/date_helper.rb', line 906
def to_date_select_tag(options = {}, html_options = {})
datetime_selector(options, html_options).select_date
end
|
#to_date_select_tag_without_error_wrapping ⇒ Object
265
266
267
|
# File 'lib/action_view/helpers/active_record_helper.rb', line 265
def to_date_select_tag(options = {}, html_options = {})
datetime_selector(options, html_options).select_date
end
|
#to_datetime_select_tag(options = {}, html_options = {}) ⇒ Object
914
915
916
|
# File 'lib/action_view/helpers/date_helper.rb', line 914
def to_datetime_select_tag(options = {}, html_options = {})
datetime_selector(options, html_options).select_datetime
end
|
#to_datetime_select_tag_without_error_wrapping ⇒ Object
274
275
276
|
# File 'lib/action_view/helpers/active_record_helper.rb', line 274
def to_datetime_select_tag(options = {}, html_options = {})
datetime_selector(options, html_options).select_datetime
end
|
#to_grouped_collection_select_tag(collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options) ⇒ Object
547
548
549
550
551
552
553
554
|
# File 'lib/action_view/helpers/form_options_helper.rb', line 547
def to_grouped_collection_select_tag(collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
html_options = html_options.stringify_keys
add_default_name_and_id(html_options)
value = value(object)
content_tag(
"select", add_options(option_groups_from_collection_for_select(collection, group_method, group_label_method, option_key_method, option_value_method, value), options, value), html_options
)
end
|
737
738
739
740
741
742
743
744
745
746
747
748
749
|
# File 'lib/action_view/helpers/form_helper.rb', line 737
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
725
726
727
728
729
730
731
732
733
734
735
|
# File 'lib/action_view/helpers/form_helper.rb', line 725
def to_label_tag(text = nil, options = {})
options = options.stringify_keys
tag_value = options.delete("value")
name_and_id = options.dup
name_and_id["id"] = name_and_id["for"]
add_default_name_and_id_for_value(tag_value, 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
|
751
752
753
754
755
756
757
758
759
760
761
762
763
764
|
# File 'lib/action_view/helpers/form_helper.rb', line 751
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
add_default_name_and_id_for_value(tag_value, options)
tag("input", options)
end
|
#to_select_tag(choices, options, html_options) ⇒ Object
527
528
529
530
531
532
533
534
|
# File 'lib/action_view/helpers/form_options_helper.rb', line 527
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
disabled_value = options.has_key?(:disabled) ? options[:disabled] : nil
content_tag("select", add_options(options_for_select(choices, :selected => selected_value, :disabled => disabled_value), options, selected_value), html_options)
end
|
#to_tag(options = {}) ⇒ Object
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
# File 'lib/action_view/helpers/active_record_helper.rb', line 227
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
766
767
768
769
770
771
772
773
774
775
|
# File 'lib/action_view/helpers/form_helper.rb', line 766
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
content_tag("textarea", html_escape(options.delete('value') || value_before_type_cast(object)), options)
end
|
#to_time_select_tag(options = {}, html_options = {}) ⇒ Object
910
911
912
|
# File 'lib/action_view/helpers/date_helper.rb', line 910
def to_time_select_tag(options = {}, html_options = {})
datetime_selector(options, html_options).select_time
end
|
#to_time_select_tag_without_error_wrapping ⇒ Object
283
284
285
|
# File 'lib/action_view/helpers/active_record_helper.rb', line 283
def to_time_select_tag(options = {}, html_options = {})
datetime_selector(options, html_options).select_time
end
|
#to_time_zone_select_tag(priority_zones, options, html_options) ⇒ Object
556
557
558
559
560
561
562
563
564
565
566
|
# File 'lib/action_view/helpers/form_options_helper.rb', line 556
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)
content_tag("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
819
820
821
|
# File 'lib/action_view/helpers/form_helper.rb', line 819
def value(object)
self.class.value(object, @method_name)
end
|
#value_before_type_cast(object) ⇒ Object
823
824
825
|
# File 'lib/action_view/helpers/form_helper.rb', line 823
def value_before_type_cast(object)
self.class.value_before_type_cast(object, @method_name)
end
|