Class: RoarExtensions::PaginatedCollectionPresenter

Inherits:
Object
  • Object
show all
Includes:
Presenter
Defined in:
lib/roar_extensions/paginated_collection_presenter.rb

Instance Method Summary collapse

Methods included from Presenter

included

Constructor Details

#initialize(record, base_path) ⇒ PaginatedCollectionPresenter

Returns a new instance of PaginatedCollectionPresenter.



19
20
21
22
23
24
25
# File 'lib/roar_extensions/paginated_collection_presenter.rb', line 19

def initialize(record, base_path)
  unless record.respond_to?(:next_page)
    record = KaminariShim.new(record)
  end
  super(record)
  @base_path = base_path
end

Instance Method Details



27
28
29
30
31
32
33
# File 'lib/roar_extensions/paginated_collection_presenter.rb', line 27

def page_link(page_number)
  if page_number == 1
    @base_path
  elsif !page_number.nil?
    "#{@base_path}?page=#{page_number}"
  end
end

#to_hash(options = {}) ⇒ Object

Hack to push the :include and :exclude options to the collection results



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/roar_extensions/paginated_collection_presenter.rb', line 38

def to_hash(options = {})
  opt_include = options.delete(:include)
  opt_exclude = options.delete(:exclude)

  res = to_hash_without_entries(options)
  res["paginated_collection"]["entries"] = record.collect.map do |e|
    entry_include = opt_include && opt_include.map {|name| get_actual_property_name(e, name)}
    entry_exclude = opt_exclude && opt_exclude.map {|name| get_actual_property_name(e, name)}
    e.to_hash(options.merge(:include => entry_include,
                            :exclude => entry_exclude))
  end
  res
end

#to_hash_without_entriesObject



35
# File 'lib/roar_extensions/paginated_collection_presenter.rb', line 35

alias_method :to_hash_without_entries, :to_hash