Class: EntryStateManager

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

Overview

Class with methods related to changing the read/unread state of entries.

Class Method Summary collapse

Class Method Details

.change_entries_state(entry, state, user, whole_feed: false, whole_folder: false, all_entries: false) ⇒ Object

Change the read or unread state of an entry, for a given user.

Receives as arguments:

  • the entry to be changed state

  • the state in which to put it. Supported values are only “read” and “unread”; this method

does nothing if a different value is passed

  • the user for which the state will be set.

  • whole_feed (optional): boolean to indicate whether other entries in the same feed *older* than

the one passed as argument are to be changed state as well.

  • whole_folder (optional): boolean to indicate whether other entries in the same folder *older* than

the one passed as argument are to be changed state as well.

  • all_entries (optional): boolean to indicate whether *ALL* entries from all subscribed feeds *older*

than the one passed as argument are to be changed state as well.

If the update_feed or update_folder optional named arguments are passed as true, entries in the same feed/folder as the passed entry which either:

  • have an older publish date than the passed entry

  • or have the same publish date but a smaller id

are considered to be older and therefore set as read or unread depending on the “state” argument.


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/entry_state_manager.rb', line 29

def self.change_entries_state(entry, state, user, whole_feed: false, whole_folder: false, all_entries: false)
  if state == 'read'
    read = true
  elsif state == 'unread'
    read = false
  else
    return nil
  end

  if !whole_feed && !whole_folder && !all_entries
    # Update a single entry
    entry_state = EntryState.find_by user_id: user.id, entry_id: entry.id
    entry_state.update read: read
    # Update unread entries count for the feed
    if read
      SubscriptionsManager.feed_decrement_count entry.feed, user
    else
      SubscriptionsManager.feed_increment_count entry.feed, user
    end
  else
    change_feed_entries_state entry, read, user if whole_feed
    change_folder_entries_state entry, read, user if whole_folder
    change_all_entries_state entry, read, user if all_entries
  end

  return nil
end