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 }
DEFAULT_TEXT_AREA_OPTIONS =
{ "wrap" => "virtual", "cols" => 40, "rows" => 20 }

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

Methods included from TagHelper

#end_form_tag, #form_tag

Methods included from DateHelper

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

Constructor Details

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

Returns a new instance of InstanceTag.



128
129
130
131
# File 'lib/action_view/helpers/form_helper.rb', line 128

def initialize(object_name, method_name, template_object, local_binding = nil)
  @object_name, @method_name = object_name, method_name
  @template_object, @local_binding = template_object, local_binding
end

Instance Attribute Details

#method_nameObject (readonly)

Returns the value of attribute method_name.



123
124
125
# File 'lib/action_view/helpers/form_helper.rb', line 123

def method_name
  @method_name
end

#object_nameObject (readonly)

Returns the value of attribute object_name.



123
124
125
# File 'lib/action_view/helpers/form_helper.rb', line 123

def object_name
  @object_name
end

Instance Method Details

#column_typeObject



178
179
180
# File 'lib/action_view/helpers/active_record_helper.rb', line 178

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

#content_tag(name, value, options) ⇒ Object



144
145
146
147
148
149
150
# File 'lib/action_view/helpers/active_record_helper.rb', line 144

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



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

alias_method :content_tag_without_error_wrapping, :content_tag

#error_messageObject



174
175
176
# File 'lib/action_view/helpers/active_record_helper.rb', line 174

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

#error_wrapping(html_tag, has_error) ⇒ Object



170
171
172
# File 'lib/action_view/helpers/active_record_helper.rb', line 170

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

#objectObject



186
187
188
# File 'lib/action_view/helpers/form_helper.rb', line 186

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

#tag(name, options) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/action_view/helpers/active_record_helper.rb', line 135

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



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

alias_method :tag_without_error_wrapping, :tag

#to_boolean_select_tag(options = {}) ⇒ Object



175
176
177
178
179
180
181
182
183
184
# File 'lib/action_view/helpers/form_helper.rb', line 175

def to_boolean_select_tag(options = {})
  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



158
159
160
161
162
163
# File 'lib/action_view/helpers/form_helper.rb', line 158

def to_check_box_tag(options = {}, checked_value = "1", unchecked_value = "0")
  options.merge!({ "checked" => "checked" }) if !value.nil? && ((value.is_a?(TrueClass) || value.is_a?(FalseClass)) ? value : value.to_i > 0)
  options.merge!({ "type" => "checkbox", "value" => checked_value })
  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



204
205
206
207
208
209
# File 'lib/action_view/helpers/form_options_helper.rb', line 204

def to_collection_select_tag(collection, value_method, text_method, options, html_options)
  add_default_name_and_id(html_options)
  (
    "select", add_blank_option(options_from_collection_for_select(collection, value_method, text_method, value), options[:include_blank]), html_options
  )
end

#to_country_select_tag(priority_countries, options, html_options) ⇒ Object



211
212
213
214
# File 'lib/action_view/helpers/form_options_helper.rb', line 211

def to_country_select_tag(priority_countries, options, html_options)
  add_default_name_and_id(html_options)
  ("select", add_blank_option(country_options_for_select(value, priority_countries), options[:include_blank]), html_options)
end

#to_date_select_tag(options = {}) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/action_view/helpers/date_helper.rb', line 214

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

  date_select = ""
  
  if options[:month_before_year]
    date_select << select_month(date, options_with_prefix.call(2)) unless options[:discard_month]
    date_select << select_year(date, options_with_prefix.call(1))
  else
    date_select << select_year(date, options_with_prefix.call(1))
    date_select << select_month(date, options_with_prefix.call(2)) unless options[:discard_month]
  end

  date_select << select_day(date, options_with_prefix.call(3))   unless options[:discard_day] || options[:discard_month]

  return date_select
end

#to_date_select_tag_without_error_wrappingObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/action_view/helpers/active_record_helper.rb', line 152

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

  date_select = ""
  
  if options[:month_before_year]
    date_select << select_month(date, options_with_prefix.call(2)) unless options[:discard_month]
    date_select << select_year(date, options_with_prefix.call(1))
  else
    date_select << select_year(date, options_with_prefix.call(1))
    date_select << select_month(date, options_with_prefix.call(2)) unless options[:discard_month]
  end

  date_select << select_day(date, options_with_prefix.call(3))   unless options[:discard_day] || options[:discard_month]

  return date_select
end

#to_date_tagObject



165
166
167
168
169
170
171
172
173
# File 'lib/action_view/helpers/form_helper.rb', line 165

def to_date_tag()
  defaults = { "discard_type" => true }
  date     = value || Date.today
  options  = Proc.new { |position| defaults.update({ :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



235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/action_view/helpers/date_helper.rb', line 235

def to_datetime_select_tag(options = {})
  defaults = { :discard_type => true }
  options  = defaults.merge(options) 
  options_with_prefix = Proc.new { |position| options.update({ :prefix => "#{@object_name}[#{@method_name}(#{position}i)]" }) }
  datetime = options[:include_blank] ? (value || 0) : (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]
  
  return datetime_select
end

#to_datetime_select_tag_without_error_wrappingObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/action_view/helpers/active_record_helper.rb', line 161

def to_datetime_select_tag(options = {})
  defaults = { :discard_type => true }
  options  = defaults.merge(options) 
  options_with_prefix = Proc.new { |position| options.update({ :prefix => "#{@object_name}[#{@method_name}(#{position}i)]" }) }
  datetime = options[:include_blank] ? (value || 0) : (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]
  
  return datetime_select
end

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



133
134
135
136
137
138
139
140
141
# File 'lib/action_view/helpers/form_helper.rb', line 133

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

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



143
144
145
146
147
148
149
150
# File 'lib/action_view/helpers/form_helper.rb', line 143

def to_radio_button_tag(tag_value, options={})
  html_options = DEFAULT_FIELD_OPTIONS.merge(options)
  html_options.merge!({ "checked" => "checked" }) if value == tag_value
  html_options.merge!({ "type" => "radio", "value"=> tag_value.to_s })
                 
  add_default_name_and_id(html_options)
  tag("input", html_options)
end

#to_select_tag(choices, options, html_options) ⇒ Object



199
200
201
202
# File 'lib/action_view/helpers/form_options_helper.rb', line 199

def to_select_tag(choices, options, html_options)
  add_default_name_and_id(html_options)
  ("select", add_blank_option(options_for_select(choices, value), options[:include_blank]), html_options)
end

#to_tag(options = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/action_view/helpers/active_record_helper.rb', line 116

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
      to_datetime_select_tag(options)
    when :boolean
      to_boolean_select_tag(options)
  end
end

#to_text_area_tag(options = {}) ⇒ Object



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

def to_text_area_tag(options = {})
  options = DEFAULT_TEXT_AREA_OPTIONS.merge(options)
  add_default_name_and_id(options)
  ("textarea", html_escape(value_before_type_cast), options)
end

#valueObject



190
191
192
# File 'lib/action_view/helpers/form_helper.rb', line 190

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

#value_before_type_castObject



194
195
196
197
198
199
200
# File 'lib/action_view/helpers/form_helper.rb', line 194

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