Volodymyr
Volodymyr

Reputation: 1186

Active Record search condition duplication - how avoid thats?

Here have class where I need collect some statistics about Likes, Comments, Content, Views

class UsersActivityTable

  def initialize(user_ids, content_ids, start_date, end_date)
    @user_ids, @content_ids, @start_date, @end_date = user_ids, content_ids,  start_date,  end_date
  end

  def rows
    @rows ||= @user_ids.map do |user_id|
                OpenStruct.new( :full_name => sum_users(user_id),
                                :contributed => sum_contributed(user_id),
                                :interacted => sum_interacted(user_id)
                              )
               end
  end

  private

  def sum_users(user_id)
    @sum_users ||= Hash[ User.value_of(:id, :first_name, :last_name).map{|usr| [usr[0], "#{usr[1]} #{usr[2]}"] } ]
    @sum_users[user_id]
  end

  def sum_contributed(user_id)
    @sum_contributed ||= Content.where(:user_id => @user_ids, :id => @content_ids)
                                .where('date(created_at) between ? and ? ', @start_date, @end_date)
                                .group(:user_id)
                                .count
    @sum_contributed[user_id].to_i
  end

  def sum_interacted(user_id)
    sum_comments(user_id).to_i + sum_views(user_id).to_i + sum_likes(user_id).to_i
  end

  def sum_comments(user_id)
    @sum_comments ||= Comment.where(:user_id => @user_ids, :content_id => @content_ids)
                             .where('date(created_at) between ? and ? ', @start_date, @end_date)
                             .group(:user_id)
                             .count
    @sum_comments[user_id]
  end

  def sum_likes(user_id)
    @sum_likes ||= Like.where(:user_id => @user_ids, :content_id => @content_ids)
                       .where('date(created_at) between ? and ? ', @start_date, @end_date)
                       .group(:user_id)
                       .count
    @sum_likes[user_id]
  end

  def sum_views(user_id)
    @sum_views ||= View.where(:viewable_type => 'Content', :user_id => @user_ids, :viewable_id => @content_ids)
                       .where('date(created_at) between ? and ? ', @start_date, @end_date)
                       .group(:user_id)
                       .count
    @sum_views[user_id]
  end
end

As you can see there is some duplication:

.where('date(created_at) between ? and ? ', @start_date, @end_date)
.group(:user_id)
.count

I perform it for every entity. Is there any way to apply condition lambda or something?

What I think is possible - is define method on collection instance and call it:

# pseudo code
group_results = lambda{|el| el.where('date(created_at) between ? and ? ', @start_date, @end_date)
                             .group(:user_id)
                             .count }
Content.where(...).tap{|collection| collection.defune_method .... }.group_results

Upvotes: 0

Views: 65

Answers (1)

John Paul Ashenfelter
John Paul Ashenfelter

Reputation: 3143

Sometimes for non-trivial statistics, one find_by_sql that returns everything you want is more efficient and possibly even a hair clearer than complex ruby.

YMMV of course :)

Upvotes: 1

Related Questions