Skip to content
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

Define additional methods for all attributes #361

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
53 changes: 28 additions & 25 deletions lib/attr_encrypted/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,43 @@ def attributes=(*args)
protected

# <tt>attr_encrypted</tt> method
def attr_encrypted(*attrs)
def attr_encrypted(*attributes)
super
options = attrs.extract_options!
attr = attrs.pop
attribute attr if ::ActiveRecord::VERSION::STRING >= "5.1.0"
options.merge! encrypted_attributes[attr]

define_method("#{attr}_was") do
attribute_was(attr)
end
options = attributes.extract_options!

attributes.each do |attr|
attribute attr if ::ActiveRecord::VERSION::STRING >= "5.1.0"
options.merge! encrypted_attributes[attr]

if ::ActiveRecord::VERSION::STRING >= "4.1"
define_method("#{attr}_changed?") do |options = {}|
attribute_changed?(attr, options)
define_method("#{attr}_was") do
attribute_was(attr)
end
else
define_method("#{attr}_changed?") do
attribute_changed?(attr)

if ::ActiveRecord::VERSION::STRING >= "4.1"
define_method("#{attr}_changed?") do |options = {}|
attribute_changed?(attr, options)
end
else
define_method("#{attr}_changed?") do
attribute_changed?(attr)
end
end
end

define_method("#{attr}_change") do
attribute_change(attr)
end
define_method("#{attr}_change") do
attribute_change(attr)
end

define_method("#{attr}_with_dirtiness=") do |value|
attribute_will_change!(attr) if value != __send__(attr)
__send__("#{attr}_without_dirtiness=", value)
end
define_method("#{attr}_with_dirtiness=") do |value|
attribute_will_change!(attr) if value != __send__(attr)
__send__("#{attr}_without_dirtiness=", value)
end

alias_method "#{attr}_without_dirtiness=", "#{attr}="
alias_method "#{attr}=", "#{attr}_with_dirtiness="
alias_method "#{attr}_without_dirtiness=", "#{attr}="
alias_method "#{attr}=", "#{attr}_with_dirtiness="

alias_method "#{attr}_before_type_cast", attr
alias_method "#{attr}_before_type_cast", attr
end
end

def attribute_instance_methods_as_symbols
Expand Down
15 changes: 14 additions & 1 deletion test/active_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ def create_tables
self.verbose = false
create_table :people do |t|
t.string :encrypted_email
t.string :encrypted_name
t.string :password
t.string :encrypted_credentials
t.binary :salt
t.binary :key_iv
t.string :encrypted_email_salt
t.string :encrypted_name_salt
t.string :encrypted_credentials_salt
t.string :encrypted_email_iv
t.string :encrypted_name_iv
t.string :encrypted_credentials_iv
end
create_table :accounts do |t|
Expand Down Expand Up @@ -57,7 +60,7 @@ class UploadedFile; end

class Person < ActiveRecord::Base
self.attr_encrypted_options[:mode] = :per_attribute_iv_and_salt
attr_encrypted :email, key: SECRET_KEY
attr_encrypted :email, :name, key: SECRET_KEY
attr_encrypted :credentials, key: Proc.new { |user| Encryptor.encrypt(value: user.salt, key: SECRET_KEY, iv: user.key_iv) }, marshal: true

after_initialize :initialize_salt_and_credentials
Expand Down Expand Up @@ -337,4 +340,14 @@ def test_should_evaluate_proc_based_mode
refute_equal address.encrypted_zipcode, zipcode
assert_equal address.zipcode, zipcode
end

def test_additional_methods_defined_for_all_attributes
assert Person.method_defined?(:email_was)
assert Person.method_defined?(:email_changed?)
assert Person.method_defined?(:email_change)

assert Person.method_defined?(:name_was)
assert Person.method_defined?(:name_changed?)
assert Person.method_defined?(:name_change)
end
end