Class: EntriesPagination

Inherits:
Object
  • Object
show all
Defined in:
lib/entries_pagination.rb

Overview

This class has methods related to retrieving entries from the database

Class Method Summary collapse

Class Method Details

.feed_entries(feed, user, include_read: false, page: nil) ⇒ Object

Retrieve entries from the feed passed as argument, that are in the passed state for the passed user.

Receives as arguments:

  • feed from which to retrieve entries.

  • user for whom the read/unread state of each entry will be considered.

  • include_read (optional): boolean that indicates whether to include both read and unread entries

(if true) or just unread entries (if false). By default this argument is false.

  • page (optional): results page to return.

Entries are ordered by published (first), created_at (second) and id (third). If the page argument is nil, all entries are returned. If it has a value, entries are paginated and the requested page is returned. Results pagination is achieved with the Kaminari gem, which uses a default page size of 25 results.

If successful, returns an ActiveRecord::Relation with the entries.


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/entries_pagination.rb', line 22

def self.feed_entries(feed, user, include_read: false, page: nil)
  if include_read && !page.present?
    entries =  feed.entries.order 'entries.published desc, entries.created_at desc, entries.id desc'
  elsif include_read && page.present?
    entries =  feed.entries.order('entries.published desc, entries.created_at desc, entries.id desc').page page
  else
    entries = unread_feed_entries feed, user, page: page
  end

  return entries
end

.folder_entries(folder, user, include_read: false, page: nil) ⇒ Object

Retrieve entries in the folder passed as argument, that are in the passed state for the passed user. In this context, “entries in the folder” means “entries from all feeds in the folder”.

Receives as arguments:

  • the folder from which to retrieve entries. The special value

“all” means that unread entries should be retrieved from ALL subscribed feeds.

  • user for whom the read/unread state of each entry will be considered.

  • include_read (optional): boolean that indicates whether to include both read and unread entries

(if true) or just unread entries (if false). By default this argument is false.

  • page (optional): results page to return.

Entries are ordered by published (first), created_at (second) and id (third). If the page argument is nil, all entries are returned. If it has a value, entries are paginated and the requested page is returned. Results pagination is achieved with the Kaminari gem, which uses a default page size of 25 results.

If successful, returns an ActiveRecord::Relation with the entries.


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/entries_pagination.rb', line 52

def self.folder_entries(folder, user, include_read: false, page: nil)
  if folder == Folder::ALL_FOLDERS
    if include_read && !page.present?
      entries = user.entries.order 'entries.published desc, entries.created_at desc, entries.id desc'
    elsif include_read && page.present?
      entries = user.entries.order('entries.published desc, entries.created_at desc, entries.id desc').page page
    else
      entries = all_unread_entries user, page: page
    end
  else
    if include_read && !page.present?
      entries = folder.entries.order 'entries.published desc, entries.created_at desc, entries.id desc'
    elsif include_read && page.present?
      entries = folder.entries.order('entries.published desc, entries.created_at desc, entries.id desc').page page
    else
      entries = unread_folder_entries folder, user, page: page
    end
  end

  return entries
end