Skip to content

Instantly share code, notes, and snippets.

@rossta
Created September 30, 2011 22:53
Show Gist options
  • Select an option

  • Save rossta/1255232 to your computer and use it in GitHub Desktop.

Select an option

Save rossta/1255232 to your computer and use it in GitHub Desktop.
Pattern for extending ActiveRecord models
# 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
# 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
# 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