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
-
#active_sorting_callback_before_validation ⇒ Object
Callbacks Generates a new code based on where given options.
-
#active_sorting_center_item(n1, n2) ⇒ Object
Centers an item between the where given two positions.
-
#active_sorting_next_step ⇒ Object
Generate the next stepping.
- #active_sorting_value ⇒ Object
- #active_sorting_value=(new_value) ⇒ Object
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_validation ⇒ Object
Callbacks Generates a new code based on where given options
194 195 196 197 |
# File 'lib/active_sorting/model.rb', line 194 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
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/active_sorting/model.rb', line 162 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_step ⇒ Object
Generate the next stepping
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/active_sorting/model.rb', line 176 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 equal step return self.class.active_sorting_step if max.nil? # Increment by the step value configured max + self.class.active_sorting_step end |
#active_sorting_value ⇒ Object
153 154 155 |
# File 'lib/active_sorting/model.rb', line 153 def active_sorting_value send(self.class.active_sorting_field) end |
#active_sorting_value=(new_value) ⇒ Object
157 158 159 |
# File 'lib/active_sorting/model.rb', line 157 def active_sorting_value=(new_value) send("#{self.class.active_sorting_field}=", new_value) end |