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
197 198 199 200 |
# File 'lib/active_sorting/model.rb', line 197 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
165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/active_sorting/model.rb', line 165 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
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/active_sorting/model.rb', line 179 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
156 157 158 |
# File 'lib/active_sorting/model.rb', line 156 def active_sorting_value send(self.class.active_sorting_field) end |
#active_sorting_value=(new_value) ⇒ Object
160 161 162 |
# File 'lib/active_sorting/model.rb', line 160 def active_sorting_value=(new_value) send("#{self.class.active_sorting_field}=", new_value) end |