Skip to content

Instantly share code, notes, and snippets.

@elia
Created May 9, 2012 15:24
Show Gist options
  • Select an option

  • Save elia/2645475 to your computer and use it in GitHub Desktop.

Select an option

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!
# (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