buren / association_count

Small gem to include association counts where they are needed.
MIT License
3 stars 2 forks source link

Other aggregations? #6

Open lostapathy opened 5 years ago

lostapathy commented 5 years ago

Would you have any interest in a PR that adds support for other aggregations, or would you rather leave the scope of this gem as-is?

It would be really awesome to be able to have (roughly):

class Invoice < ActiveRecord::Base
  has_many :charges
  can_aggregate :charges, :total_charge_amount, "sum(quantity * amount)"
end

class Charge < ActiveRecord::Base
  belongs_to :invoice
end

Invoice.include_total_charge_amount

Or perhaps have various helpers such as this to have a nicer, if more limited, DSL.

can_sum :charges, :total_charge_amount, "quantity * amount"
buren commented 5 years ago

I like it.

I did some, very minor, research trying to find other gems that already does this (rubytoolbox, google..) and found a few things that are vaguely related, but nothing that does what you mention.

It might be something that would be useful and possible to maintain. I'll try experiment a bit first.

Thanks again @lostapathy I'll try to post back to this thread soon. (Be sure to @-mention me again here soon if I don't get back to you.)

lostapathy commented 5 years ago

@buren I experimented with this a bit before I put this in, and definitely think it's doable. I will try to turn that into a PR for you in the next couple days.