Module: ActiveSorting::Model

Defined in:
lib/active_sorting/model.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
# File 'lib/active_sorting/model.rb', line 3

def self.included(base)
  base.extend ClassMethods
  # Track all sortable arguments
  base.class_attribute :active_sorting_options
end

Instance Method Details

#active_sorting_callback_before_validationObject

Callbacks Generates a new code based on where given options



191
192
193
194
# File 'lib/active_sorting/model.rb', line 191

def active_sorting_callback_before_validation
  field_name = self.class.active_sorting_field
  send("#{field_name}=", active_sorting_next_step) if send(field_name).nil?
end

#active_sorting_center_item(n1, n2) ⇒ Object

Centers an item between the where given two positions



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/active_sorting/model.rb', line 159

def active_sorting_center_item(n1, n2)
  delta = (n1 - n2).abs
  smaller = [n1, n2].min
  if delta == 1
    new_position = smaller + delta
  elsif delta > 1
    new_position = smaller + (delta / 2)
  end
  self.active_sorting_value = new_position
  save!
  self
end

#active_sorting_next_stepObject

Generate the next stepping



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/active_sorting/model.rb', line 173

def active_sorting_next_step
  conditions = {}
  self.class.active_sorting_scope.each do |s|
    conditions[s] = send(s)
  end
  # Get the maximum value for the sortable field name
  max = self.class
            .unscoped
            .where(conditions)
            .maximum(self.class.active_sorting_field)
  # First value will always be 0
  return 0 if max.nil?
  # Increment by the step value configured
  max + self.class.active_sorting_step
end

#active_sorting_valueObject



150
151
152
# File 'lib/active_sorting/model.rb', line 150

def active_sorting_value
  send(self.class.active_sorting_field)
end

#active_sorting_value=(new_value) ⇒ Object



154
155
156
# File 'lib/active_sorting/model.rb', line 154

def active_sorting_value=(new_value)
  send("#{self.class.active_sorting_field}=", new_value)
end