Module: ActiveRecord::Orderable::ClassMethods
- Defined in:
- lib/ar-orderable.rb
Instance Attribute Summary collapse
-
#orderable_column ⇒ Object
Returns the value of attribute orderable_column.
-
#orderable_scope ⇒ Object
Returns the value of attribute orderable_scope.
-
#skip_callbacks_for_orderable ⇒ Object
Returns the value of attribute skip_callbacks_for_orderable.
Instance Method Summary collapse
-
#acts_as_orderable(options = {}) ⇒ Object
@:column [string] column name @:scope [string] column name to scope by @:scope Array column names to scope by acts_as_orderable :column => “order_nr”.
-
#order_unordered ⇒ Object
updates all unordered items puts them into the end of list.
- #raw_orderable_update(id, nr) ⇒ Object
Instance Attribute Details
#orderable_column ⇒ Object
Returns the value of attribute orderable_column.
8 9 10 |
# File 'lib/ar-orderable.rb', line 8 def orderable_column @orderable_column end |
#orderable_scope ⇒ Object
Returns the value of attribute orderable_scope.
8 9 10 |
# File 'lib/ar-orderable.rb', line 8 def orderable_scope @orderable_scope end |
#skip_callbacks_for_orderable ⇒ Object
Returns the value of attribute skip_callbacks_for_orderable.
8 9 10 |
# File 'lib/ar-orderable.rb', line 8 def skip_callbacks_for_orderable @skip_callbacks_for_orderable end |
Instance Method Details
#acts_as_orderable(options = {}) ⇒ Object
@:column [string] column name @:scope [string] column name to scope by @:scope Array column names to scope by acts_as_orderable :column => “order_nr”
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ar-orderable.rb', line 14 def acts_as_orderable = {} return unless self.connection.table_exists?(self.table_name) self.orderable_column = ([:column] || "order_nr").to_s self.skip_callbacks_for_orderable = [:skip_callbacks] if self.columns_hash.keys.include? self.orderable_column self.orderable_scope = Array([:scope]) self.before_save :pre_save_ordering self.before_destroy :pre_destroy_ordering self.default_scope { order(self.orderable_column) } include ActiveRecord::Orderable::InstanceMethods else msg = "[IMPORTANT] ActiveRecord::Orderable plugin: class #{self} has missing column '#{self.orderable_column}'" if defined?(RAILS_DEFAULT_LOGGER) RAILS_DEFAULT_LOGGER.error msg elsif defined?(Rails.logger) Rails.logger.error msg elsif Rails.env == "development" puts msg end end end |
#order_unordered ⇒ Object
updates all unordered items puts them into the end of list
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/ar-orderable.rb', line 37 def order_unordered self.reset_column_information self.group(self.orderable_scope).each do |obj| unordered_conditions = "#{self.orderable_column} IS NULL OR #{self.table_name}.#{self.orderable_column} = 0" ordered_conditions = "#{self.orderable_column} IS NOT NULL AND #{self.table_name}.#{self.orderable_column} != 0" order_nr = obj.all_orderable.order(self.orderable_column).last[self.orderable_column] || 0 obj.all_orderable.where(unordered_conditions).find_each do |item| order_nr += 1 raw_orderable_update(item.id, order_nr) end end end |
#raw_orderable_update(id, nr) ⇒ Object
50 51 52 |
# File 'lib/ar-orderable.rb', line 50 def raw_orderable_update id, nr self.connection.execute("update #{self.table_name} set #{self.orderable_column} = #{nr.to_i} where #{self.table_name}.id = #{id.to_i};") end |