<commit_before>##
# This class has methods related to managing the relationships between feeds and folders.

class FolderManager

  ##
  # 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.

  def self.folder_feeds(folder, user, include_read: false)
    if folder == Folder::NO_FOLDER
      feeds = Feed.arel_table
      folders = Folder.arel_table
      feed_subscriptions = Arel::Table.new :feed_subscriptions
      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<commit_msg>Get an Arel::Table object from the ActiveRecord::Base#arel_table method instead of new. Removes a deprecation warning.<commit_after>##
# This class has methods related to managing the relationships between feeds and folders.

class FolderManager

  ##
  # 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.

  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
{'fraction_non_alphanumeric': 0.08331656268866976, 'fraction_numerical': 0.0004024954719259408, 'mean_word_length': 2.5198300283286117, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4473048', 'n_tokens_mistral': 1465, 'n_tokens_neox': 1401, 'n_words': 386}