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

Feature/rails 5 1 compatibility #1

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
63 changes: 32 additions & 31 deletions lib/delegates_attributes_to.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
module DelegatesAttributesToWithExtras
def assign_multiparameter_attributes(pairs)
delegated_pairs = {}
original_pairs = []

pairs.each do |name, value|
# it splits multiparameter attribute
# 'published_at(2i)' => ['published_at(2i)', 'published_at', '(2i)']
# 'published_at' => ['published_at', 'published_at', nil ]
__, delegated_attribute, suffix = name.match(/^(\w+)(\([0-9]*\w\))?$/).to_a
association, attribute = self.class.delegated_attributes[delegated_attribute]

if association
(delegated_pairs[association] ||= {})["#{attribute}#{suffix}"] = value
else
original_pairs << [name, value]
end
end

delegated_pairs.each do |association, attributes|
association_object = send(association) || send("build_#{association}")
# let association_object handle its multiparameter attributes
association_object.attributes = attributes
end

super(original_pairs)
end
end

module DelegatesAttributesTo
prepend DelegatesAttributesToWithExtras

DEFAULT_REJECTED_COLUMNS = ['created_at','created_on','updated_at','updated_on','lock_version','type','id','position','parent_id','lft','rgt'].freeze
DIRTY_SUFFIXES = ["_changed?", "_change", "_will_change!", "_was"].freeze
Expand All @@ -7,8 +37,6 @@ def self.included(base)
base.extend ClassMethods
base.send :include, InstanceMethods

base.alias_method_chain :assign_multiparameter_attributes, :delegation

base.class_attribute :default_rejected_delegate_columns
base.default_rejected_delegate_columns = DEFAULT_REJECTED_COLUMNS.dup

Expand Down Expand Up @@ -90,33 +118,6 @@ module InstanceMethods

private

def assign_multiparameter_attributes_with_delegation(pairs)
delegated_pairs = {}
original_pairs = []

pairs.each do |name, value|
# it splits multiparameter attribute
# 'published_at(2i)' => ['published_at(2i)', 'published_at', '(2i)']
# 'published_at' => ['published_at', 'published_at', nil ]
__, delegated_attribute, suffix = name.match(/^(\w+)(\([0-9]*\w\))?$/).to_a
association, attribute = self.class.delegated_attributes[delegated_attribute]

if association
(delegated_pairs[association] ||= {})["#{attribute}#{suffix}"] = value
else
original_pairs << [name, value]
end
end

delegated_pairs.each do |association, attributes|
association_object = send(association) || send("build_#{association}")
# let association_object handle its multiparameter attributes
association_object.attributes = attributes
end

assign_multiparameter_attributes_without_delegation(original_pairs)
end

def changed_attributes
result = {}
self.class.delegated_attributes.each do |delegated_attribute, (association, attribute)|
Expand All @@ -135,8 +136,8 @@ def changed_attributes
result.merge! delegated_attribute => association_changed_attributes[attribute]
end
changed_attributes = super
changed_attributes.merge!(result)
changed_attributes
result.merge! changed_attributes
result
end
end
end
Expand Down