Module: Pading::PageScopeMethods
- Defined in:
- lib/pading/activerecord/page_scope_menthods.rb
Instance Method Summary collapse
-
#current_page ⇒ Object
获取当前page.
- #first_page? ⇒ Boolean
- #last_page? ⇒ Boolean
- #next_page ⇒ Object
- #per(num, max_per_page: nil) ⇒ Object
- #prev_page ⇒ Object
- #total_count(column_name = :all, _options = nil) ⇒ Object
- #total_pages ⇒ Object
Instance Method Details
#current_page ⇒ Object
获取当前page
16 17 18 19 20 21 |
# File 'lib/pading/activerecord/page_scope_menthods.rb', line 16 def current_page # offset_value limit_value 元编程自动生成的查询 # limit_value 每一页多少个 # offset_value 从第几个开始查询 (offset_value / limit_value) + 1 end |
#first_page? ⇒ Boolean
53 54 55 |
# File 'lib/pading/activerecord/page_scope_menthods.rb', line 53 def first_page? current_page == 1 end |
#last_page? ⇒ Boolean
57 58 59 |
# File 'lib/pading/activerecord/page_scope_menthods.rb', line 57 def last_page? current_page == total_pages end |
#next_page ⇒ Object
45 46 47 |
# File 'lib/pading/activerecord/page_scope_menthods.rb', line 45 def next_page current_page + 1 unless last_page? end |
#per(num, max_per_page: nil) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/pading/activerecord/page_scope_menthods.rb', line 4 def per(num, max_per_page: nil) if (n = num.to_i ) < 0 || !(/^\d/ =~ num.to_s) self elsif n.zero? # zero 是否为0 limit(n) else limit(n).offset(offset_value / limit_value * n) end end |
#prev_page ⇒ Object
49 50 51 |
# File 'lib/pading/activerecord/page_scope_menthods.rb', line 49 def prev_page current_page - 1 unless first_page? end |
#total_count(column_name = :all, _options = nil) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pading/activerecord/page_scope_menthods.rb', line 24 def total_count(column_name = :all, = nil) return @total_count if defined?(@total_count) && @total_count c = except(:offset, :limit, :order) # TODO references_eager_loaded_tables? API 没有这个方法的描述 c = c.except(:includes) unless references_eager_loaded_tables? c = c.count(column_name) @total_count = if c.is_a?(Hash) || c.is_a?(ActiveSupport::OrderedHash) c.count elsif c.respond_to? :count c.count(column_name) else c end end |
#total_pages ⇒ Object
40 41 42 |
# File 'lib/pading/activerecord/page_scope_menthods.rb', line 40 def total_pages (total_count.to_f / limit_value).ceil end |