Class: PageWrapper::Model
- Inherits:
-
Object
- Object
- PageWrapper::Model
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::SerializerSupport
- Defined in:
- lib/page_wrapper/model.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
Instance Method Summary collapse
- #fetch_records(page, options) ⇒ Object
- #get_options_from(params) ⇒ Object
-
#initialize(params) ⇒ Model
constructor
A new instance of Model.
- #per_page_default ⇒ Object
- #per_page_max ⇒ Object
Constructor Details
#initialize(params) ⇒ Model
Returns a new instance of Model.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/page_wrapper/model.rb', line 28 def initialize(params) page = params[:id].to_i = (params) records = fetch_records(page, ) @attributes = .merge( id: records.current_page, pages: records.num_pages, first: records.first_page?, last: records.last_page?, total: records.total_count ) end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
8 9 10 |
# File 'lib/page_wrapper/model.rb', line 8 def attributes @attributes end |
Class Method Details
.fields ⇒ Object
10 11 12 |
# File 'lib/page_wrapper/model.rb', line 10 def self.fields [:id, :pages, :first, :last, :total, :per_page, :query, :sort, :dir] end |
Instance Method Details
#fetch_records(page, options) ⇒ Object
41 42 43 |
# File 'lib/page_wrapper/model.rb', line 41 def fetch_records(page, ) raise "Overide me" end |
#get_options_from(params) ⇒ Object
45 46 47 48 |
# File 'lib/page_wrapper/model.rb', line 45 def (params) per_page = [params.fetch(:per_page, per_page_default).to_i, per_page_max].min params.slice(:query, :sort, :dir).merge(per_page: per_page) end |
#per_page_default ⇒ Object
20 21 22 |
# File 'lib/page_wrapper/model.rb', line 20 def per_page_default 10 end |
#per_page_max ⇒ Object
24 25 26 |
# File 'lib/page_wrapper/model.rb', line 24 def per_page_max 100 end |