Created
September 30, 2011 22:53
-
-
Save rossta/1255232 to your computer and use it in GitHub Desktop.
Pattern for extending ActiveRecord models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app/models/user_extensions/admin.rb | |
| module UserExtensions | |
| module Admin | |
| extend ActiveSupport::Concern | |
| ADMIN = 'admin' | |
| module ClassMethods | |
| def acts_as_admin(options = {}) | |
| attr_accessor :admin_password_confirmation | |
| validates_presence_of :admin_password | |
| end | |
| # more fat user admin class methods | |
| end | |
| module InstanceMethods | |
| def admin? | |
| role == ADMIN | |
| end | |
| # more fat user admin instance methods | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app/models/gravatar/user.rb | |
| module Gravatar | |
| class User < ::User | |
| include UserExtensions::Gravatar | |
| default_scope select(%w[ id name gravatar_id ]) | |
| # additional slim user instance methods | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app/models/user.rb | |
| class User < ActiveRecord::Base | |
| include UserExtensions::Admin | |
| include UserExtensions::Memberships | |
| // lots more fat code | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment