Skip to content

Instantly share code, notes, and snippets.

@patbenatar
Last active December 11, 2015 14:49
Show Gist options
  • Save patbenatar/4617063 to your computer and use it in GitHub Desktop.
Save patbenatar/4617063 to your computer and use it in GitHub Desktop.
class Order
include Stats::Host
# The above would mix this in:
has_one :stat, class_name: "Order::Stat"
after_commit :create_stat, on: :create
def create_stat
stat.create!
end
end
class Earning
include Stats::Provider
stats_for :order,
observe: [:amount, :earner_type]
# The above would mix this in:
after_commit :refresh_stat, on: :create
after_commit :refresh_stat, on: :update, if: :should_refresh_stat?
after_commit :refresh_stat, on: :destroy
def refresh_stat
order.stat.refresh
end
def should_refresh_stat?
amount_changed? || earner_type_changed?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment