module ActiveRecord::Orderable::ClassMethods
Attributes
orderable_column[RW]
orderable_scope[RW]
skip_callbacks_for_orderable[RW]
Public Instance Methods
acts_as_orderable(options = {})
click to toggle source
@:column [string] column name @:scope [string] column name to scope by @:scope Array column names to scope by acts_as_orderable
:column => “order_nr”
# File lib/ar-orderable.rb, line 14 def acts_as_orderable options = {} return unless self.connection.table_exists?(self.table_name) self.orderable_column = (options[:column] || "order_nr").to_s self.skip_callbacks_for_orderable = options[:skip_callbacks] if self.columns_hash.keys.include? self.orderable_column self.orderable_scope = Array(options[: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()
click to toggle source
updates all unordered items puts them into the end of list
# 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)
click to toggle source
# 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