Skip to content

Commit

Permalink
chore[Op#40437]: improve var naming; make less generic
Browse files Browse the repository at this point in the history
  • Loading branch information
akabiru committed Oct 25, 2024
1 parent a302f3f commit 80cea6e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
9 changes: 5 additions & 4 deletions app/models/concerns/reactable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def grouped_emoji_reactions_by_reactable(reactable_id:, reactable_type:)
grouped_emoji_reactions(reactable_id:, reactable_type:).each_with_object({}) do |row, hash|
hash[row.reactable_id] ||= {}
hash[row.reactable_id][row.reaction.to_sym] = {
count: row.count,
users: row.user_details.map { |(id, name)| { id:, name: } }
count: row.reactions_count,
users: row.reacting_users.map { |(id, name)| { id:, name: } }
}
end
end
Expand All @@ -65,11 +65,12 @@ def grouped_emoji_reactions(reactable_id:, reactable_type:)

def emoji_reactions_group_selection_sql
<<~SQL.squish
emoji_reactions.reactable_id, emoji_reactions.reaction, COUNT(emoji_reactions.id) as count,
emoji_reactions.reactable_id, emoji_reactions.reaction,
COUNT(emoji_reactions.id) as reactions_count,
json_agg(
json_build_array(users.id, #{user_name_concat_format_sql})
ORDER BY emoji_reactions.created_at
) as user_details,
) as reacting_users,
MIN(emoji_reactions.created_at) as first_created_at
SQL
end
Expand Down
16 changes: 8 additions & 8 deletions spec/models/concerns/reactable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,35 +135,35 @@
result = Journal.grouped_emoji_reactions(reactable_id: work_package.journal_ids, reactable_type: "Journal")

expect(result[0].reaction).to eq("thumbs_up")
expect(result[0].count).to eq(2)
expect(result[0].user_details).to eq([[user1.id, user1.name], [user2.id, user2.name]])
expect(result[0].reactions_count).to eq(2)
expect(result[0].reacting_users).to eq([[user1.id, user1.name], [user2.id, user2.name]])

expect(result[1].reaction).to eq("thumbs_down")
expect(result[1].count).to eq(1)
expect(result[1].user_details).to eq([[user2.id, user2.name]])
expect(result[1].reactions_count).to eq(1)
expect(result[1].reacting_users).to eq([[user2.id, user2.name]])
end

context "when user format is set to :username", with_settings: { user_format: :username } do
it "returns grouped emoji reactions with usernames" do
result = Journal.grouped_emoji_reactions(reactable_id: work_package.journal_ids, reactable_type: "Journal")

expect(result[0].user_details).to eq([[user1.id, user1.login], [user2.id, user2.login]])
expect(result[0].reacting_users).to eq([[user1.id, user1.login], [user2.id, user2.login]])
end
end

context "when user format is set to :firstname", with_settings: { user_format: :firstname } do
it "returns grouped emoji reactions with first and last names" do
result = Journal.grouped_emoji_reactions(reactable_id: wp_journal2.id, reactable_type: "Journal")

expect(result[0].user_details).to eq([[user2.id, user2.firstname]])
expect(result[0].reacting_users).to eq([[user2.id, user2.firstname]])
end
end

context "when user format is set to :lastname_coma_firstname", with_settings: { user_format: :lastname_coma_firstname } do
it "returns grouped emoji reactions with last coma firstname" do
result = Journal.grouped_emoji_reactions(reactable_id: wp_journal1.id, reactable_type: "Journal")

expect(result[0].user_details).to eq(
expect(result[0].reacting_users).to eq(
[
[user1.id, "#{user1.lastname}, #{user1.firstname}"],
[user2.id, "#{user2.lastname}, #{user2.firstname}"]
Expand All @@ -176,7 +176,7 @@
it "returns grouped emoji reactions with last firstname" do
result = Journal.grouped_emoji_reactions(reactable_id: wp_journal1.id, reactable_type: "Journal")

expect(result[0].user_details).to eq(
expect(result[0].reacting_users).to eq(
[
[user1.id, "#{user1.lastname}#{user1.firstname}"],
[user2.id, "#{user2.lastname}#{user2.firstname}"]
Expand Down

0 comments on commit 80cea6e

Please sign in to comment.