Class: Lolita::Configuration::Page
- Defined in:
- lib/lolita/configuration/page.rb
Instance Attribute Summary collapse
-
#per_page(value = nil) ⇒ Object
Records per page (also block setter).
Instance Method Summary collapse
-
#asc(column) ⇒ Object
Define new sort column with ascending sort direction.
-
#desc(column) ⇒ Object
Define new sort columns with descending sort direction.
- #get_page ⇒ Object
-
#initialize(dbi) ⇒ Page
constructor
A new instance of Page.
-
#last_page ⇒ Object
Return last paginated page ====Example list.paginate(2) # call page to avoid another call to db list.page.
-
#paginate(*args) ⇒ Object
Paginate Options: *
:per_page- record count per page, default uses list.per_page *:page- what page to show *:sort_columns- sort columns for page. -
#sort_columns ⇒ Object
Return all sort columns.
Constructor Details
#initialize(dbi) ⇒ Page
Returns a new instance of Page.
8 9 10 11 |
# File 'lib/lolita/configuration/page.rb', line 8 def initialize(dbi) @dbi=dbi @sort_columns=[] end |
Instance Attribute Details
#per_page(value = nil) ⇒ Object
Records per page (also block setter)
14 15 16 17 18 19 20 21 |
# File 'lib/lolita/configuration/page.rb', line 14 def per_page(value=nil) if value @per_page=value self else @per_page end end |
Instance Method Details
#asc(column) ⇒ Object
Define new sort column with ascending sort direction
24 25 26 27 |
# File 'lib/lolita/configuration/page.rb', line 24 def asc(column) @sort_columns<<[column.to_sym,:asc] self end |
#desc(column) ⇒ Object
Define new sort columns with descending sort direction
30 31 32 33 |
# File 'lib/lolita/configuration/page.rb', line 30 def desc(column) @sort_columns<<[column.to_sym,:desc] self end |
#get_page ⇒ Object
71 72 73 |
# File 'lib/lolita/configuration/page.rb', line 71 def get_page() @page=@dbi.paginate((@page_options||{}).merge(@last_options)) end |
#last_page ⇒ Object
Return last paginated page
Example
list.paginate(2)
# call page to avoid another call to db
list.page
80 81 82 |
# File 'lib/lolita/configuration/page.rb', line 80 def last_page @page end |
#paginate(*args) ⇒ Object
Paginate Options:
-
:per_page- record count per page, default uses list.per_page -
:page- what page to show -
:sort_columns- sort columns for page. See #sort_columns -
:asc- column to sort in ascending directionlist.paginate(1,:asc=>:created_at) -
:desc- column to sort in descending direction
Example
list.paginate(1)
list.paginate
list.paginate(:per_page=>2,:page=>1)
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/lolita/configuration/page.rb', line 58 def paginate *args =args ? args. : {} hold=.delete(:hold) () [:page]||=((args && args.first) || 1) [:per_page]||=@per_page || 10 @last_options= @last_options[:sort]=self.sort_columns.empty? ? nil : self.sort_columns unless hold get_page() end end |
#sort_columns ⇒ Object
Return all sort columns. Each column is an Array where first element is column name and second is direction
Example
page=Lolita::Configuration::Page.new(@dbi)
page.asc(:created_at)
page.sort_columns #=> [[:created_at,:asc]]
41 42 43 |
# File 'lib/lolita/configuration/page.rb', line 41 def sort_columns @sort_columns end |