Module: SelectableAttrRails::Helpers::SelectHelper::Base

Defined in:
lib/selectable_attr_rails/helpers/select_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

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, options = {}, html_options = {}, &block)
  html_options = {:size => 5, :multiple => 'multiple'}.update(html_options || {})
  options = update_enum_select_options(options, object_name, method)
  object = options.delete(:object)
  base_name = options.delete(:base_name)
  entry_hash_array = options.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, options, html_options, &block)
end

#select_with_attr_enumeable(object_name, method, *args, &block) ⇒ Object

def select_with_attr_enumeable(object, method, choices, options = {}, html_options = {})

Raises:

  • (ArgumentError)


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)
  options, html_options = *args
  options = update_enum_select_options(options, object_name, method)
  object, base_name = options[:object], options[:base_name]
  return multi_enum_select(object_name, method, options, html_options, &block) if object.respond_to?("#{base_name}_hash_array")
  return single_enum_select(object_name, method, options, html_options, &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, options = {}, html_options = {}, &block)
  options = update_enum_select_options(options, object_name, method)
  object = options[:object] # options.delete(:object)
  base_name = options.delete(:base_name)
  entry_hash_array = options.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, options, html_options || {}, &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 update_enum_select_options(options, object_name, method)
  options ||= {}
  object = (options[:object] ||= instance_variable_get("@#{object_name}"))
  options[:base_name] ||= object.class.enum_base_name(method.to_s)
  options
end