Class: ActionView::Helpers::InstanceTag

Inherits:
Object
  • Object
show all
Defined in:
lib/enum_column/active_record_helper.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#__to_tag_enumObject



38
# File 'lib/enum_column/active_record_helper.rb', line 38

alias __to_tag_enum to_tag

#enum_valuesObject

Gets the list of values for the column.



102
103
104
# File 'lib/enum_column/active_record_helper.rb', line 102

def enum_values
  object.send("column_for_attribute", @method_name).values
end

#to_enum_radio_tag(options = {}) ⇒ Object

Creates a set of radio buttons and labels.

Raises:

  • (ArgumentError)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/enum_column/active_record_helper.rb', line 77

def to_enum_radio_tag(options = {})
  # Remove when we no longer support 1.1.
  begin
    v = value(object)
  rescue ArgumentError
    v = value
  end
  add_default_name_and_id(options)
  values = enum_values
  raise ArgumentError, "No values for enum select tag" unless values
  tag_text = ''
  template = options.dup
  template.delete('checked')
  values.each do |enum|
    opts = template.dup
    opts['checked'] = 'checked' if v and v == enum
    opts['id'] = "#{opts['id']}_#{enum}"
    tag_text << "<label>#{enum}: "
    tag_text << to_radio_button_tag(enum, opts)
    tag_text << "</label>"
  end
  tag_text
end

#to_enum_select_tag(options = {}) ⇒ Object

Create a select tag and one option for each of the enumeration values.

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/enum_column/active_record_helper.rb', line 52

def to_enum_select_tag(options = {})
  # Remove when we no longer support 1.1.
  begin
    v = value(object)
  rescue ArgumentError
    v = value
  end
  add_default_name_and_id(options)
  tag_text = "<select"
  tag_text << tag_options(options)
  tag_text << ">"
  values = enum_values
  raise ArgumentError, "No values for enum select tag" unless values
  if options[:include_blank]
    tag_text << "<option value=\"\"></option>\n"
  end
  values.each do |enum|
    tag_text << "<option value=\"#{enum}\""
    tag_text << ' selected="selected"' if v and v == enum
    tag_text << ">#{enum}</option>"
  end
  tag_text << "</select>"
end

#to_tag(options = {}) ⇒ Object

Add the enumeration tag support. Defaults using the select tag to display the options.



42
43
44
45
46
47
48
# File 'lib/enum_column/active_record_helper.rb', line 42

def to_tag(options = {})
  if column_type == :enum
    to_enum_select_tag(options)
  else
    __to_tag_enum(options)
  end
end