Skip to content

Instantly share code, notes, and snippets.

@PerezIgnacio
Last active February 3, 2020 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PerezIgnacio/f9eb9b8b9cf243453abc909839f843a0 to your computer and use it in GitHub Desktop.
Save PerezIgnacio/f9eb9b8b9cf243453abc909839f843a0 to your computer and use it in GitHub Desktop.
User Concern example
module Chargeable
extend ActiveSupport::Concern
def charge_order(amount, discount, products)
create_order(amount, discount, products)
send_order_charge
# ...
end
private
def create_order(amount, discount, products)
# altough we could pass the user as parameter, this concern incorrectly
# assumes that the user model includes the concern, an implicitness we should avoid
@order = Order.create!(amount: amount, discount: discount, products: products, user: self)
end
def send_order_charge
# logic related to payment process, usually calling an external API
end
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment