Module: SelectableAttrRails::Helpers::SelectHelper::Base
- Defined in:
- lib/selectable_attr_rails/helpers/select_helper.rb
Class Method Summary collapse
Instance Method Summary collapse
- #multi_enum_select(object_name, method, options = {}, html_options = {}, &block) ⇒ Object
-
#select_with_attr_enumeable(object_name, method, *args, &block) ⇒ Object
def select_with_attr_enumeable(object, method, choices, options = {}, html_options = {}).
- #single_enum_select(object_name, method, options = {}, html_options = {}, &block) ⇒ Object
- #update_enum_select_options(options, object_name, method) ⇒ Object
Class Method Details
.included(base) ⇒ Object
4 5 6 7 8 |
# File 'lib/selectable_attr_rails/helpers/select_helper.rb', line 4 def self.included(base) base.module_eval do alias_method_chain :select, :attr_enumeable end end |
Instance Method Details
#multi_enum_select(object_name, method, options = {}, html_options = {}, &block) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/selectable_attr_rails/helpers/select_helper.rb', line 36 def multi_enum_select(object_name, method, = {}, = {}, &block) = {:size => 5, :multiple => 'multiple'}.update( || {}) = (, object_name, method) object = .delete(:object) base_name = .delete(:base_name) entry_hash_array = .delete(:entry_hash_array) || object.send("#{base_name}_hash_array") container = entry_hash_array.map{|hash| [hash[:name].to_s, hash[:id].to_s]} attr = "#{base_name}_ids" select_without_attr_enumeable(object_name, attr, container, , , &block) end |
#select_with_attr_enumeable(object_name, method, *args, &block) ⇒ Object
def select_with_attr_enumeable(object, method, choices, options = {}, html_options = {})
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/selectable_attr_rails/helpers/select_helper.rb', line 11 def select_with_attr_enumeable(object_name, method, *args, &block) if args.length > 3 raise ArgumentError, "argument must be " << "(object, method, choices, options = {}, html_options = {}) or " << "(object, method, options = {}, html_options = {})" end return select_without_attr_enumeable(object_name, method, *args, &block) if args.length == 3 return select_without_attr_enumeable(object_name, method, *args, &block) if args.first.is_a?(Array) , = *args = (, object_name, method) object, base_name = [:object], [:base_name] return multi_enum_select(object_name, method, , , &block) if object.respond_to?("#{base_name}_hash_array") return single_enum_select(object_name, method, , , &block) if object.class.respond_to?("#{base_name}_hash_array") raise ArgumentError, "invaliad argument" end |
#single_enum_select(object_name, method, options = {}, html_options = {}, &block) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/selectable_attr_rails/helpers/select_helper.rb', line 27 def single_enum_select(object_name, method, = {}, = {}, &block) = (, object_name, method) object = [:object] # options.delete(:object) base_name = .delete(:base_name) entry_hash_array = .delete(:entry_hash_array) || object.class.send("#{base_name}_hash_array") container = entry_hash_array.map{|hash| [hash[:name].to_s, hash[:id]]} select_without_attr_enumeable(object_name, method, container, , || {}, &block) end |
#update_enum_select_options(options, object_name, method) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/selectable_attr_rails/helpers/select_helper.rb', line 47 def (, object_name, method) ||= {} object = ([:object] ||= instance_variable_get("@#{object_name}")) [:base_name] ||= object.class.enum_base_name(method.to_s) end |