Module: DataMapper::Is::Select::ClassMethods
- Defined in:
- lib/dm-is-select/is/select.rb
Instance Attribute Summary collapse
-
#select_field ⇒ Object
readonly
Returns the value of attribute select_field.
-
#select_options ⇒ Object
readonly
Returns the value of attribute select_options.
Instance Method Summary collapse
-
#items_for_select_menu(options = {}) ⇒ Object
Provides the Model content in a ready to use
<select>
options array.
Instance Attribute Details
#select_field ⇒ Object (readonly)
Returns the value of attribute select_field.
52 53 54 |
# File 'lib/dm-is-select/is/select.rb', line 52 def select_field @select_field end |
#select_options ⇒ Object (readonly)
Returns the value of attribute select_options.
52 53 54 |
# File 'lib/dm-is-select/is/select.rb', line 52 def @select_options end |
Instance Method Details
#items_for_select_menu(options = {}) ⇒ Object
Provides the Model content in a ready to use <select>
options array
Params
-
:options
-
:prompt [String/Boolean] => The text shown on the
<select>
field in the browser. (Defaults to “Select NameOfYourModel”) -
:divider [Boolean] => Whether to add a divider/separator between the prompt and the main options. (Defaults to
true
) -
:order [Array] => A normal DM order declaration. (Defaults to [:name] or the name of the select_field declared)
-
:show_root [Boolean] => Whether to add the Top Level Parent in the choices. (Defaults to
true
) -
:root_text [String] => The text to show as the Parent item in select list. (Defaults to “Top Level NameOfYourModel”)
-
Examples
Category.items_for_select_menu
=> [ ['Select Category',nil], ['---', nil], ['Category 1',1] ,....]
Category.items_for_select_menu(:prompt => "Custom Prompt")
=> [ ['Custom Prompt',nil],...]
Category.items_for_select_menu(:prompt => false)
=> [ ['Category 1',1] ,...]
Category.items_for_select_menu(:divider => false )
=> array without the ['---', nil] node
Category.items_for_select_menu(:order => [ :id.desc ] )
=> array with the order reversed. (Prompts & divider always comes first)
Category.items_for_select_menu(:publish_status => "on", :order => [ :id.desc ] )
=> returns only those items that matches the query params or just an empty Select Menu
If your model is a Tree:
Category.items_for_select_menu(:root_text => "Custom Root Text") # sets the text for the Top Level (root) Parent
=> [ ..., ['Custom Root Text', 0],...]
Category.items_for_select_menu(:show_root => false) # removes the Top Level (root) Parent from the
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/dm-is-select/is/select.rb', line 95 def ( = {}) # clean out the various parts = .only(:prompt, :divider, :show_root, :root_text) = .except(:prompt, :divider, :show_root, :root_text) # puts "sql_options=[#{sql_options.inspect}] [#{__FILE__}:#{__LINE__}]" # puts "html_options=[#{html_options.inspect}] [#{__FILE__}:#{__LINE__}]" = { :prompt => "Select #{self.name}", :divider => true, :show_root => true, :root_text => "Top Level #{self.name}", }.merge() = { :order => [self.select_field.to_sym], }.merge() mi = self.[:is_tree] ? all({ :parent_id => 0 }.merge() ) : all() res = [] if [:prompt] res << [[:prompt],nil] res << [" ------ ",'nil'] if [:divider] if self.[:is_tree] if [:show_root] res << [[:root_text], 0] res << [" ------ ",'nil'] if [:divider] end end end if self.[:is_tree] mi.each do |x| res << [x.send(self.select_field), x.id] unless x.children.blank? x.children.each do |child| res << ["-- #{child.send(self.select_field)}", child.id] child.children.each do |grand_child| res << ["-- -- #{grand_child.send(self.select_field)}", grand_child.id] end unless child.children.blank? end end end else mi.each do |x| res << [x.send(self.select_field), x.id] end end res end |