<commit_before># encoding: utf-8

class DiscussionRelationship < ActiveRecord::Base
  belongs_to :user
  belongs_to :discussion

  after_save do |relationship|
    relationship.update_user_caches!
  end

  after_destroy do |relationship|
    relationship.update_user_caches!
  end

  class << self
    # Define a relationship with a discussion
    def define(user, discussion, options={})
      relationship = self.find(:first, :conditions => ['user_id = ? AND discussion_id = ?', user.id, discussion.id])
      relationship ||= DiscussionRelationship.create(:user_id => user.id, :discussion_id => discussion.id)
      relationship.update_attributes(options.merge({:trusted => discussion.trusted}))
      relationship.save
      relationship
    end

    # Find participated discussions for a user
    def find_participated(user, options={})
      self.find_discussions(user, {:participated => true}.merge(options))
    end

    # Find followed discussions for a user
    def find_following(user, options={})
      self.find_discussions(user, {:following => true}.merge(options))
    end

    # Find favorite discussions for a user
    def find_favorite(user, options={})
      self.find_discussions(user, {:favorite => true}.merge(options))
    end

    def find_discussions(user, options={})
      paginate = options.has_key?(:page)

      find_conditions = [:participated, :following, :favorite].inject(Hash.new) do |cond, key|
        cond[key] = ((options[key]) ? true : false) if options.has_key?(key)
        cond
      end.merge({:user_id => user.id})

      find_conditions[:trusted] = false unless options[:trusted]
      find_options = {}

      if paginate
        limit     = options[:limit] || Discussion::DISCUSSIONS_PER_PAGE
        discussions_count = self.count(:all, :conditions => find_conditions)
        num_pages = (discussions_count.to_f/limit).ceil
        page      = (options[:page] || 1).to_i
        page      = 1 if page < 1
        page      = num_pages if page > num_pages
        offset    = limit * (page - 1)
        offset    = 0 if offset < 0

        find_options = {
          :limit => limit,
          :offset => offset
        }.merge(find_options)
      end

      join_string = "INNER JOIN discussion_relationships ON discussion_relationships.discussion_id = discussions.id"
      join_string += " AND " + find_conditions.map{|k,v| "discussion_relationships.#{k.to_s} = #{v}"}.join(" AND ")

      find_options = {
        :select     => 'discussions.*',
        #:conditions => find_conditions,
        :joins      => join_string,
        :order      => 'discussions.sticky DESC, discussions.last_post_at DESC',
        :include    => [:poster, :last_poster, :category]
      }.merge(find_options)

      discussions = Discussion.find(:all, find_options)
      if paginate
        Pagination.apply(discussions, Pagination::Paginater.new(:total_count => discussions_count, :page => page, :per_page => limit))
      end
      discussions
    end
  end

  def update_user_caches!
    self.user.update_attributes({
      :participated_count => DiscussionRelationship.count(:all, :conditions => ['user_id = ? AND participated = ?', self.user.id, true]),
      :following_count    => DiscussionRelationship.count(:all, :conditions => ['user_id = ? AND following = ?', self.user.id, true]),
      :favorites_count    => DiscussionRelationship.count(:all, :conditions => ['user_id = ? AND favorite = ?', self.user.id, true])
    })
  end
end
<commit_msg>DiscussionRelationship.find_discussions rewritten with Arel, now works with SQLite<commit_after># encoding: utf-8

class DiscussionRelationship < ActiveRecord::Base
  belongs_to :user
  belongs_to :discussion

  after_save do |relationship|
    relationship.update_user_caches!
  end

  after_destroy do |relationship|
    relationship.update_user_caches!
  end

  class << self
    # Define a relationship with a discussion
    def define(user, discussion, options={})
      relationship = self.find(:first, :conditions => ['user_id = ? AND discussion_id = ?', user.id, discussion.id])
      relationship ||= DiscussionRelationship.create(:user_id => user.id, :discussion_id => discussion.id)
      relationship.update_attributes(options.merge({:trusted => discussion.trusted}))
      relationship.save
      relationship
    end

    # Find participated discussions for a user
    def find_participated(user, options={})
      self.find_discussions(user, {:participated => true}.merge(options))
    end

    # Find followed discussions for a user
    def find_following(user, options={})
      self.find_discussions(user, {:following => true}.merge(options))
    end

    # Find favorite discussions for a user
    def find_favorite(user, options={})
      self.find_discussions(user, {:favorite => true}.merge(options))
    end

    def find_discussions(user, options={})
      conditions = options.select do |k, v|
        [:participated, :following, :favorite].include?(k)
      end
      conditions[:user_id] = user.id
      conditions[:trusted] = false unless options[:trusted]

      discussions = Discussion
        .select('discussions.*')
        .order('sticky DESC, last_post_at DESC')
        .includes(:poster, :last_poster, :category)
        .joins(:discussion_relationships)
        .where(:discussion_relationships => conditions)

      if options.has_key?(:page)
        limit     = options[:limit] || Discussion::DISCUSSIONS_PER_PAGE
        discussions_count = self.count(:all, :conditions => conditions)
        num_pages = (discussions_count.to_f/limit).ceil
        page      = (options[:page] || 1).to_i
        page      = 1 if page < 1
        page      = num_pages if page > num_pages
        offset    = limit * (page - 1)
        offset    = 0 if offset < 0

        discussions = discussions
          .limit(limit)
          .offset(offset)
      end

      discussions = discussions.all

      if options.has_key?(:page)
        Pagination.apply(
          discussions,
          Pagination::Paginater.new(
            :total_count => discussions_count,
            :page => page,
            :per_page => limit
          )
        )
      end
      discussions
    end
  end

  def update_user_caches!
    self.user.update_attributes({
      :participated_count => DiscussionRelationship.count(:all, :conditions => ['user_id = ? AND participated = ?', self.user.id, true]),
      :following_count    => DiscussionRelationship.count(:all, :conditions => ['user_id = ? AND following = ?', self.user.id, true]),
      :favorites_count    => DiscussionRelationship.count(:all, :conditions => ['user_id = ? AND favorite = ?', self.user.id, true])
    })
  end
end

{'fraction_non_alphanumeric': 0.118773373223635, 'fraction_numerical': 0.0020942408376963353, 'mean_word_length': 3.0035928143712574, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7541480', 'n_tokens_mistral': 2006, 'n_tokens_neox': 1902, 'n_words': 441}