Reputation: 8030
If I make something like this:
class ActiveRecord::Base
def self.encrypt(*attr_names)
encrypter = Encrypter.new(attr_names)
before_save encrypter
after_save encrypter
after_find encrypter
define_method(:after_find) { }
end
end
require
somewhere?require
?Upvotes: 4
Views: 1698
Reputation: 4117
The rails-ish way of doing what is you're trying to do is:
create a file in lib/encryptable.rb (or app/models/concerns if you're on rails 4) which defines a module with your methods.
Then in your models you can do include Encryptable
or (for all models) in an initializer:
ActiveRecord::Base.class_eval do
include Encryptable
end
read more about rails 4 concerns here: How to use concerns in Rails 4
Upvotes: 5