Module: Leaf::Finders::Base
- Defined in:
- lib/leaf/finders/base.rb
Overview
Database-agnostic finder module
Out of the box, leaf supports hooking in the Sequel ORM
Instance Method Summary collapse
-
#paginate(*args, &block) ⇒ Object
This is the main paginating finder.
-
#paginated_each(options = {}, &block) ⇒ Object
Iterates through all records by loading one page at a time.
- #per_page ⇒ Object
- #per_page=(limit) ⇒ Object
Instance Method Details
#paginate(*args, &block) ⇒ Object
This is the main paginating finder.
Special parameters for paginating finders
-
:page
– REQUIRED, but defaults to 1 if false or nil -
:per_page
– defaults toCurrentModel.per_page
(which is 30 if not overridden) -
:total_entries
– use only if you manually count total entries -
:count
– additional options that are passed on tocount
-
:finder
– name of the finder method to use (default: “find”)
All other options (conditions
, order
, …) are forwarded to find
and count
calls.
28 29 30 31 32 33 34 35 36 |
# File 'lib/leaf/finders/base.rb', line 28 def paginate(*args, &block) = args.pop page, per_page, total_entries = () Leaf::Collection.create(page, per_page, total_entries) do |pager| = .except :page, :per_page, :total_entries wp_query(, pager, args, &block) end end |
#paginated_each(options = {}, &block) ⇒ Object
Iterates through all records by loading one page at a time. This is useful for migrations or any other use case where you don’t want to load all the records in memory at once.
It uses paginate
internally; therefore it accepts all of its options. You can specify a starting page with :page
(default is 1). Default :order
is "id"
, override if necessary.
Jamis Buck describes this and also uses a more efficient way for MySQL.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/leaf/finders/base.rb', line 48 def paginated_each( = {}, &block) = { :order => 'id', :page => 1 }.merge [:page] = [:page].to_i [:total_entries] = 0 # skip the individual count queries total = 0 begin collection = paginate() total += collection.each(&block).size [:page] += 1 end until collection.size < collection.per_page total end |
#per_page ⇒ Object
9 10 11 |
# File 'lib/leaf/finders/base.rb', line 9 def per_page @per_page ||= 30 end |
#per_page=(limit) ⇒ Object
13 14 15 |
# File 'lib/leaf/finders/base.rb', line 13 def per_page=(limit) @per_page = limit.to_i end |