Class: Selections::FormBuilderExtensions::SelectionTag
- Inherits:
-
Object
- Object
- Selections::FormBuilderExtensions::SelectionTag
- Defined in:
- lib/selections/form_builder_extensions.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#field ⇒ Object
readonly
Returns the value of attribute field.
-
#field_id ⇒ Object
readonly
Returns the value of attribute field_id.
-
#form ⇒ Object
readonly
Returns the value of attribute form.
-
#html_options ⇒ Object
readonly
Returns the value of attribute html_options.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#selection ⇒ Object
readonly
Returns the value of attribute selection.
-
#system_code_name ⇒ Object
readonly
Returns the value of attribute system_code_name.
Instance Method Summary collapse
- #blank_content ⇒ Object
- #default_item ⇒ Object
- #error_message ⇒ Object
- #include_blank? ⇒ Boolean
-
#initialize(form, object, field, options, html_options) ⇒ SelectionTag
constructor
A new instance of SelectionTag.
- #items ⇒ Object
- #radio_tag ⇒ Object
- #select_tag ⇒ Object
- #selected_item ⇒ Object
- #system_code ⇒ Object
Constructor Details
#initialize(form, object, field, options, html_options) ⇒ SelectionTag
Returns a new instance of SelectionTag.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/selections/form_builder_extensions.rb', line 38 def initialize(form, object, field, , ) @form = form @object = object @field = field @html_options = || {} @system_code_name = [:system_code] || field @selection = Selections.model @field_id ||= (field.to_s + "_id").to_sym @options = || {} end |
Instance Attribute Details
#field ⇒ Object (readonly)
Returns the value of attribute field.
36 37 38 |
# File 'lib/selections/form_builder_extensions.rb', line 36 def field @field end |
#field_id ⇒ Object (readonly)
Returns the value of attribute field_id.
36 37 38 |
# File 'lib/selections/form_builder_extensions.rb', line 36 def field_id @field_id end |
#form ⇒ Object (readonly)
Returns the value of attribute form.
36 37 38 |
# File 'lib/selections/form_builder_extensions.rb', line 36 def form @form end |
#html_options ⇒ Object (readonly)
Returns the value of attribute html_options.
36 37 38 |
# File 'lib/selections/form_builder_extensions.rb', line 36 def @html_options end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
36 37 38 |
# File 'lib/selections/form_builder_extensions.rb', line 36 def object @object end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
36 37 38 |
# File 'lib/selections/form_builder_extensions.rb', line 36 def @options end |
#selection ⇒ Object (readonly)
Returns the value of attribute selection.
36 37 38 |
# File 'lib/selections/form_builder_extensions.rb', line 36 def selection @selection end |
#system_code_name ⇒ Object (readonly)
Returns the value of attribute system_code_name.
36 37 38 |
# File 'lib/selections/form_builder_extensions.rb', line 36 def system_code_name @system_code_name end |
Instance Method Details
#blank_content ⇒ Object
111 112 113 |
# File 'lib/selections/form_builder_extensions.rb', line 111 def blank_content [:blank_content] || 'none' end |
#default_item ⇒ Object
107 108 109 |
# File 'lib/selections/form_builder_extensions.rb', line 107 def default_item items.where(is_default: true).first.try(:id).to_s end |
#error_message ⇒ Object
95 96 97 |
# File 'lib/selections/form_builder_extensions.rb', line 95 def "Could not find system_code of '#{system_code_name}' or '#{form.object_name}_#{system_code_name}'" end |
#include_blank? ⇒ Boolean
50 51 52 53 54 55 56 |
# File 'lib/selections/form_builder_extensions.rb', line 50 def include_blank? if [:include_blank].nil? !!((object.try(:new_record?) || !object.send(field_id))) && default_item.blank? else !![:include_blank] end end |
#items ⇒ Object
63 64 65 |
# File 'lib/selections/form_builder_extensions.rb', line 63 def items @items ||= system_code.children.filter_archived_except_selected(object.send(field_id)) end |
#radio_tag ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/selections/form_builder_extensions.rb', line 79 def radio_tag if system_code items.unshift(selection.new(name: blank_content)) if include_blank? items.inject('') do |build, item| = item.id ? .merge(value: item.id.to_s) : [:checked] = selected_item == item.id.to_s && !item.new_record? build + form.label(field_id, ) do form.(field_id, item.id, ) + item.name end end.html_safe else end end |
#select_tag ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/selections/form_builder_extensions.rb', line 67 def select_tag if system_code #TODO add default style #html_options[:style] ||= [:include_blank] = include_blank? [:selected] = selected_item form.select field_id, items.map { |item| [item.name, item.id] }, , else end end |
#selected_item ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/selections/form_builder_extensions.rb', line 99 def selected_item if object.new_record? && object.send(field_id).blank? default_item else object.send(field_id).to_s end end |
#system_code ⇒ Object
58 59 60 61 |
# File 'lib/selections/form_builder_extensions.rb', line 58 def system_code @system_code ||= selection.where(system_code: "#{form.object_name}_#{system_code_name}").first @system_code ||= selection.where(system_code: system_code_name.to_s).first end |