Module: Resort::Sortable::ClassMethods
- Defined in:
- lib/resort.rb
Overview
Class methods to be used from the model class.
Instance Method Summary collapse
-
#first_in_order ⇒ ActiveRecord::Base
Returns the first element of the list.
-
#last_in_order ⇒ ActiveRecord::Base
Returns the last element of the list.
-
#ordered ⇒ Array<ActiveRecord::Base>
Returns eager-loaded Components in order.
Instance Method Details
#first_in_order ⇒ ActiveRecord::Base
Returns the first element of the list.
76 77 78 |
# File 'lib/resort.rb', line 76 def first_in_order scoped.where(:first => true).first end |
#last_in_order ⇒ ActiveRecord::Base
Returns the last element of the list.
83 84 85 |
# File 'lib/resort.rb', line 83 def last_in_order scoped.where(:next_id => nil).first end |
#ordered ⇒ Array<ActiveRecord::Base>
Returns eager-loaded Components in order.
OPTIMIZE: Use IdentityMap when available
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/resort.rb', line 92 def ordered ordered_elements = [] elements = {} scoped.each do |element| if ordered_elements.empty? && element.first? ordered_elements << element else elements[element.id] = element end end raise "Multiple or no first items in the list where found. Consider defining a siblings method" if ordered_elements.length != 1 && elements.length > 0 elements.length.times do ordered_elements << elements[ordered_elements.last.next_id] end ordered_elements.compact end |