Skip to content

Instantly share code, notes, and snippets.

@ToniRib
Last active March 8, 2016 20:50
Show Gist options
  • Save ToniRib/c6fcd9f0c3689b23b503 to your computer and use it in GitHub Desktop.
Save ToniRib/c6fcd9f0c3689b23b503 to your computer and use it in GitHub Desktop.
Lighting Talk Mod 3 - ActiveSupport::Concern

ActiveRecord::Concerns - How To Share Scopes/Class Methods Between Modules

Intro

  • What is that 'concerns' folder that lives under models that you always delete? Why does it exist? You're about to find out!
  • Mostly a code-along lightning talk
  • Background: You have a scope that needs to be shared across multiple models and you don't want to repeat your code
  • Example: Rails Engine -> successful_transactions scope for Merchant and Customer

Code Along

  • Show Rails Engine as it is prior to any refactoring ('successful_transactions' scope in both classes)
  • Create the ActiveRecord::Concern for a SuccessfulTransactions module (point out that things must be wrapped in the 'included' block)
  • Include the module in both models
  • Add the 'it_behaves_like' statement to the spec file for each class
  • Run the tests and show that it works!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment