Module: ActiveRecord::Orderable::InstanceMethods

Defined in:
lib/ar-orderable.rb

Instance Method Summary collapse

Instance Method Details

#all_orderableObject

returns all elements in current scope



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ar-orderable.rb', line 77

def all_orderable
  if self.class.orderable_scope.any?
    scope = self.class.orderable_scope.inject({}) do |where, scope_name|
      where[scope_name] = self[scope_name]
      where
    end
    self.class.where(scope)
  else
    self.class.scoped
  end
end

#move_down(options = {}) ⇒ Object



72
73
74
# File 'lib/ar-orderable.rb', line 72

def move_down options = {}
  move_to(self[self.class.orderable_column] + 1, options) if self[self.class.orderable_column]
end

#move_to(nr, options = {}) ⇒ Object

Moves Item to given position



58
59
60
61
62
63
64
65
66
# File 'lib/ar-orderable.rb', line 58

def move_to nr, options = {}
  if options[:skip_callbacks].nil? ? self.class.skip_callbacks_for_orderable : options[:skip_callbacks]
    self[self.class.orderable_column] = nr
    self.send(:pre_save_ordering)
    self.class.raw_orderable_update(self.id, nr)
  else
    self.update_attribute(self.class.orderable_column, nr)
  end
end

#move_up(options = {}) ⇒ Object



68
69
70
# File 'lib/ar-orderable.rb', line 68

def move_up options = {}
  move_to(self[self.class.orderable_column] - 1, options) if self[self.class.orderable_column]
end