Class: PageWrapper::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::AttributeMethods, ActiveModel::SerializerSupport
Defined in:
lib/page_wrapper/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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
  options = get_options_from(params)
  records = fetch_records(page, options)
  @attributes = options.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

#attributesObject (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

.fieldsObject



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, options)
  raise "Overide me"
end

#get_options_from(params) ⇒ Object



45
46
47
48
# File 'lib/page_wrapper/model.rb', line 45

def get_options_from(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_defaultObject



20
21
22
# File 'lib/page_wrapper/model.rb', line 20

def per_page_default
  10
end

#per_page_maxObject



24
25
26
# File 'lib/page_wrapper/model.rb', line 24

def per_page_max
  100
end