Class: FolderManager

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

Overview

This class has methods related to managing the relationships between feeds and folders.

Class Method Summary collapse

Class Method Details

.folder_feeds(folder, user, include_read: false) ⇒ Object

Returns feeds in the passed folder.

Accepts as arguments:

  • The folder from which feeds must be retrieved. This argument can be:

    • a Folder instance. Feeds in this folder will be returned. In this case folder must be owned by the passed user,

otherwise an error is raised

- the special Folder::NO_FOLDER value. In this case feeds subscribed by the passed user which are not in any folder

will be returned

  • The user for whom feeds will be retrieved.

  • include_read (optional). A boolean argument that defaults to false. If this argument is true, all feeds in the

folder will be returned. If it's false, only feeds with unread entries will be returned.

The returned feeds are guaranteed to be subscribed by the passed user.


21
22
23
24
25
26
27
28
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
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/folder_manager.rb', line 21

def self.folder_feeds(folder, user, include_read: false)
  if folder == Folder::NO_FOLDER
    feeds = Feed.arel_table
    folders = Folder.arel_table
    feed_subscriptions = FeedSubscription.arel_table
    feeds_folders = Arel::Table.new :feeds_folders

    feeds_in_folders_condition = feeds_folders.join(folders).on(folders[:id].eq(feeds_folders[:folder_id])).
                              where(folders[:user_id].eq(user.id)).
                              where(feeds_folders[:feed_id].eq(feeds[:id])).
                            project(feeds_folders[Arel.star])

    if include_read
      subscribed_feeds_sql = feeds.join(feed_subscriptions).on(feeds[:id].eq(feed_subscriptions[:feed_id])).
                                where(feed_subscriptions[:user_id].eq(user.id)).
                              project(feeds[Arel.star])
    else
      subscribed_feeds_sql = feeds.join(feed_subscriptions).on(feeds[:id].eq(feed_subscriptions[:feed_id])).
                                where(feed_subscriptions[:user_id].eq(user.id)).
                                where(feed_subscriptions[:unread_entries].gt(0)).
                              project(feeds[Arel.star])
    end

    feeds_not_in_folders_sql = subscribed_feeds_sql.where(feeds_in_folders_condition.exists.not)
                                .order(feeds[:title])

    feeds_list = Feed.find_by_sql feeds_not_in_folders_sql.to_sql
  else
    # Validate that folder belongs to user
    if !user.folders.include? folder
      Rails.logger.error "User #{user.id} - #{user.email} tried to list feeds in folder #{folder.id} - #{folder.title} which he does not own"
      raise FolderNotOwnedByUserError.new
    end

    if include_read
      feeds_list = folder.feeds.order(:title)
    else
      feeds_list = folder.feeds.joins(:feed_subscriptions)
                    .where(feed_subscriptions: {user_id: user.id})
                    .where('feed_subscriptions.unread_entries > 0')
                    .order(:title)
    end
  end

  return feeds_list
end

.move_feed_to_folder(feed, user, folder: nil, folder_title: nil) ⇒ Object

Move a feed into a folder, for a given user.

Receives as arguments:

  • feed: mandatory. Feed to be moved.

  • user: mandatory. User who is moving the feed. User must be subscribed to the feed and own the folder.

  • folder: optional named argument. If present, moves the feed to this folder, which must be owned by

the passed user; in this case, ignores the folder_title argument. Accepts the special value “none”, which means that the feed will be removed from its current folder without moving it to another one.

  • folder_title: optional named argument. If present, and the folder argument is not present, create a new folder with this

title (owned by the passed user) and move the feed to it.

Returns the folder instance to which the feed has been moved, or nil if “none” has been passed in the “folder” argument.

Raises a NotSubscribedError if the user is not subscribed to the feed.


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/folder_manager.rb', line 87

def self.move_feed_to_folder(feed, user, folder: nil, folder_title: nil)
  if !user.feeds.exists? feed.id
    Rails.logger.error "User #{user.id} - #{user.email} tried to change folder for feed #{feed.id} #{feed.fetch_url} to which he is not subscribed"
    raise NotSubscribedError.new
  end

  if folder.present? && folder != Folder::NO_FOLDER
    folder = move_feed_to_existing_folder feed, folder, user
  elsif  folder == Folder::NO_FOLDER
    folder = remove_feed_from_folder feed, user
  else
    folder = move_feed_to_new_folder feed, folder_title, user
  end

  return folder
end