lib/dm-is-select/is/select.rb in kematzy-dm-is-select-0.0.2 vs lib/dm-is-select/is/select.rb in kematzy-dm-is-select-0.0.3

- old
+ new

@@ -15,11 +15,11 @@ # # ==== Params # # * :field_name => the name of the field values shown in select # * :options - # * :is_tree => whether if the current Model is an is :tree model + # * :is_tree => whether if the current Model is an is :tree model. (Defaults to false) # # ==== Examples # # # is :select, :name @@ -34,11 +34,11 @@ def is_select(select_field = :name, options = {}) raise ArgumentError, "The :select_field, must be an existing attribute in the Model. Got [ #{select_field.inspect} ]" unless properties.any?{ |p| p.name == select_field.to_sym } @select_options = { # add specical features if we are working with Tree Model - :is_tree => false, + :is_tree => false }.merge(options) @select_field = select_field @@ -78,30 +78,42 @@ # => 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 # # # @api public - def items_for_select_menu(options={}) + def items_for_select_menu(options = {}) + # clean out the various parts + html_options = options.only(:prompt, :divider, :show_root, :root_text) + sql_options = options.except(:prompt, :divider, :show_root, :root_text) + # puts "sql_options=[#{sql_options.inspect}] [#{__FILE__}:#{__LINE__}]" + # puts "html_options=[#{html_options.inspect}] [#{__FILE__}:#{__LINE__}]" + options = { :prompt => "Select #{self.name}", :divider => true, - :order => [self.select_field.to_sym], :show_root => true, :root_text => "Top Level #{self.name}", - }.merge(options) + }.merge(html_options) + sql_options = { + :order => [self.select_field.to_sym], + }.merge(sql_options) + mi = self.select_options[:is_tree] ? - all(:parent_id => 0, :order => options[:order] ) : - all(:order => options[:order]) + all({ :parent_id => 0 }.merge(sql_options) ) : + all(sql_options) res = [] if options[:prompt] res << [options[:prompt],nil] res << [" ------ ",'nil'] if options[:divider]