Skip to content

Only include instance methods once attr_encrypted is specified #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 45 additions & 39 deletions lib/attr_encrypted/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,50 @@
module AttrEncrypted
module Adapters
module ActiveRecord
def self.extended(base) # :nodoc:
base.class_eval do

# https://github.com/attr-encrypted/attr_encrypted/issues/68
alias_method :reload_without_attr_encrypted, :reload
def reload(*args, &block)
result = reload_without_attr_encrypted(*args, &block)
self.class.encrypted_attributes.keys.each do |attribute_name|
instance_variable_set("@#{attribute_name}", nil)
protected
# <tt>attr_encrypted</tt> method
def attr_encrypted(*attrs)
self.class_eval do
# https://github.com/attr-encrypted/attr_encrypted/issues/68
alias_method :reload_without_attr_encrypted, :reload
def reload(*args, &block)
result = reload_without_attr_encrypted(*args, &block)
self.class.encrypted_attributes.keys.each do |attribute_name|
instance_variable_set("@#{attribute_name}", nil)
end
result
end
result
end

attr_encrypted_options[:encode] = true
attr_encrypted_options[:encode] = true

class << self
alias_method :method_missing_without_attr_encrypted, :method_missing
alias_method :method_missing, :method_missing_with_attr_encrypted
end
class << self
alias_method :method_missing_without_attr_encrypted, :method_missing
alias_method :method_missing, :method_missing_with_attr_encrypted
end

def perform_attribute_assignment(method, new_attributes, *args)
return if new_attributes.blank?
def perform_attribute_assignment(method, new_attributes, *args)
return if new_attributes.blank?

send method, new_attributes.reject { |k, _| self.class.encrypted_attributes.key?(k.to_sym) }, *args
send method, new_attributes.reject { |k, _| !self.class.encrypted_attributes.key?(k.to_sym) }, *args
end
private :perform_attribute_assignment
send method, new_attributes.reject { |k, _| self.class.encrypted_attributes.key?(k.to_sym) }, *args
send method, new_attributes.reject { |k, _| !self.class.encrypted_attributes.key?(k.to_sym) }, *args
end
private :perform_attribute_assignment

if ::ActiveRecord::VERSION::STRING > "3.1"
alias_method :assign_attributes_without_attr_encrypted, :assign_attributes
def assign_attributes(*args)
perform_attribute_assignment :assign_attributes_without_attr_encrypted, *args
if ::ActiveRecord::VERSION::STRING > "3.1"
alias_method :assign_attributes_without_attr_encrypted, :assign_attributes
def assign_attributes(*args)
perform_attribute_assignment :assign_attributes_without_attr_encrypted, *args
end
end
end

alias_method :attributes_without_attr_encrypted=, :attributes=
def attributes=(*args)
perform_attribute_assignment :attributes_without_attr_encrypted=, *args
alias_method :attributes_without_attr_encrypted=, :attributes=
def attributes=(*args)
perform_attribute_assignment :attributes_without_attr_encrypted=, *args
end
end
end
end

protected

# <tt>attr_encrypted</tt> method
def attr_encrypted(*attrs)
super

options = attrs.extract_options!
attr = attrs.pop
attribute attr if ::ActiveRecord::VERSION::STRING >= "5.1.0"
Expand Down Expand Up @@ -136,8 +133,17 @@ def method_missing_with_attr_encrypted(method, *args, &block)
end
end

ActiveSupport.on_load(:active_record) do
extend AttrEncrypted
extend AttrEncrypted::Adapters::ActiveRecord
if defined?(ActiveSupport.on_load)
ActiveSupport.on_load(:active_record) do
extend AttrEncrypted
extend AttrEncrypted::Adapters::ActiveRecord
end
else
Rails.configuration.to_prepare do
ActiveRecord::Base.class_eval do
extend AttrEncrypted
extend AttrEncrypted::Adapters::ActiveRecord
end
end
end
end