Created
May 9, 2012 15:24
-
-
Save elia/2645475 to your computer and use it in GitHub Desktop.
Wipe out all your ActiveRecord methods and expose only the api you need and want to support!
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
| # (c) Copyright 2012 Elia Schito, released under the MIT license | |
| # | |
| # = ActiveRecord::ClearInterface | |
| # | |
| # Wipe out all your AR methods and expose only the api you need | |
| # and want to support! | |
| # | |
| # Example: | |
| # | |
| # | |
| # class Message < ActiveRecord::Base | |
| # include ActiveRecord::ClearInterface | |
| # | |
| # attr_accessible :subject, :body | |
| # | |
| # def save_to_db | |
| # save | |
| # end | |
| # | |
| # validates :subject, :body, :user, presence: true | |
| # end | |
| # | |
| # And joyfully the new interface is tiny! | |
| # | |
| # # BEFORE | |
| # (Message.new.public_methods - Object.new.public_methods).size # => 250 | |
| # | |
| # # AFTER | |
| # (Message.new.public_methods - Object.new.public_methods).size # => 67 | |
| # Message.new.save # => protected method `save' called for #<Message:0x007f9c24b22d80> | |
| # | |
| module ActiveRecord::ClearInterface | |
| extend ActiveSupport::Concern | |
| included do | |
| allowed_methods = | |
| Object.public_instance_methods + | |
| ActiveModel::Naming.public_instance_methods + | |
| ActiveModel::Conversion.public_instance_methods + | |
| ActiveModel::Validations.public_instance_methods + | |
| [:persisted?, :logger] | |
| protected *(ActiveRecord::Base.public_instance_methods - allowed_methods) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment