cat-in-136 / redmine_hearts

a redmine plugin which provides intra-Redmine Like/Fav reactions
https://www.redmine.org/plugins/redmine_hearts
GNU General Public License v2.0
33 stars 3 forks source link

list on a project "/project/:project_id/hearts/" #1

Closed cat-in-136 closed 6 years ago

cat-in-136 commented 6 years ago

It is useful to show all the "Like!" on a project.

But I could not write query...

cat-in-136 commented 6 years ago

I assumed following code. But it does not work because of belongs_to :heartable, :polymorphic => true.

Heart.where(:heartable => {:project => @project}).
    group(:heartable_type, :heartable_id).
    order(:created_at => :desc).
    offset(@offset).
    limit(@limit).
    pluck(:heartable)

Or, it is better to add project_id to the Heart model? (but i don't know how to implement it.)

mintow commented 6 years ago

Thanks for your work. I like the "like" plugin. Can I see the "like" data in the issue list?

cat-in-136 commented 6 years ago

Candidate code to get "Like!" on the specific project @project.

scope = [
  Heart.where(:heartable => Board.where(:project_id => @project.id)),
  Heart.where(:heartable => Issue.where(:project_id => @project.id)),
  Heart.where(:heartable => Message.joins(:board).where(:boards => {:project_id => @project.id})),
  Heart.where(:heartable => News.where(:project_id => @project.id)),
  Heart.where(:heartable => Wiki.where(:project_id => @project.id)),
  Heart.where(:heartable => WikiPage.joins(:wiki).where(:wikis => {:project_id => @project.id})),
  Heart.where(:heartable => Journal.where(:journalized => Issue.where(:project_id => @project.id))),
].reduce { |scope1, scope2|
  Heart.where(
    Heart.arel_table.grouping(scope1.where_values.reduce(:and)).or(
      Heart.arel_table.grouping(scope2.where_values.reduce(:and))
    )
  ).tap { |scope| scope.bind_values = scope1.bind_values + scope2.bind_values }
}
hearts = scope.
  group(:heartable_type, :heartable_id).
  order(:created_at => :desc).
  offset(@offset).
  limit(@limit)
cat-in-136 commented 6 years ago

The development for this enhancement is on-going on the hearts-per-project branch.