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



175
176
177
178
# File 'lib/active_sorting/model.rb', line 175

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



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/active_sorting/model.rb', line 143

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



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

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



134
135
136
# File 'lib/active_sorting/model.rb', line 134

def active_sorting_value
  send(self.class.active_sorting_field)
end

#active_sorting_value=(new_value) ⇒ Object



138
139
140
# File 'lib/active_sorting/model.rb', line 138

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