Class: Mack::Database::Paginator
- Defined in:
- lib/mack-data_mapper/paginator.rb
Instance Method Summary collapse
-
#paginate ⇒ Object
Implements the paginate method from the Mack::Database::Paginator spec in mack-orm.
Instance Method Details
#paginate ⇒ Object
Implements the paginate method from the Mack::Database::Paginator spec in mack-orm.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/mack-data_mapper/paginator.rb', line 7 def paginate order_clause = [self..delete(:order)].flatten.compact self.total_results = self.klass.count(self.) self.total_pages = (self.total_results.to_f / self.results_per_page).ceil self.current_page = self.total_pages if self.current_page > self.total_pages if order_clause.empty? self.klass.key.each do |k| order_clause << k.name.to_sym.asc end end self..reverse_merge!({ :order => order_clause }) offset = (self.current_page - 1) * self.results_per_page offset = 0 if offset < 0 self..merge!({ :limit => self.results_per_page, :offset => offset }) self.results = self.klass.all(self.) self end |