Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #6 from slowjack2k/develop
Browse files Browse the repository at this point in the history
fixed an issue with existing has_many associated objects and updates of ...
  • Loading branch information
slowjack2k committed Jul 3, 2014
2 parents a5c063d + a4d5270 commit 250a024
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
29 changes: 29 additions & 0 deletions lib/activerecord_to_poro/metadata_enabled_ar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,38 @@ def _from_attrs_with_metadata(attrs={}, pre_created_object= nil)
record = pre_created_object || record_by_primary_key || new

record.tap do |new_obj|

new_obj.attributes = attrs

_patch_has_many_members(attrs, new_obj) unless new_obj.new_record?
end

end

def _patch_has_many_members(attrs, new_obj)
has_many_attrs = attrs.slice(* _has_many_attr_names(new_obj))

has_many_attrs.each_pair do |name, updated_records|

new_obj.public_send(name).each do |attached_record|
record_with_updated_values = updated_records.find {|r| r == attached_record}
next unless record_with_updated_values

_apply_change_set_to_record(attached_record, record_with_updated_values.changes)
end

end
end

def _has_many_attr_names(obj_or_class)
class_to_check = obj_or_class.respond_to?(:reflect_on_all_associations) ? obj_or_class : obj_or_class.class
class_to_check.reflect_on_all_associations(:has_many).map(&:name)
end

def _apply_change_set_to_record(attached_record, changes)
changes.each_pair do |attr_name, (_, new_value)|
attached_record.public_send("#{attr_name}=", new_value)
end
end

def _as_scope(attr)
Expand Down
8 changes: 4 additions & 4 deletions lib/activerecord_to_poro/object_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ def load_result_source=(new_load_result)
end

def dump_result_source=(new_dump_result)
@dump_result_source = new_dump_result.tap do |source|
unless source.respond_to? :_from_attrs_with_metadata
source.send(:extend, MetadataEnabledAr)
end
unless new_dump_result.respond_to? :_from_attrs_with_metadata
new_dump_result.send(:extend, MetadataEnabledAr)
end

@dump_result_source = new_dump_result
end

def add_default_mapping_for_current_class
Expand Down
2 changes: 1 addition & 1 deletion lib/activerecord_to_poro/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ActiverecordToPoro
VERSION = "0.0.4"
VERSION = "0.0.5"
end
11 changes: 11 additions & 0 deletions spec/acceptance/map_ar_associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@
expect(mapper.dump(poro).roles.last.permissions.size).to eq 1
end

scenario "updates an associated ActiveRecord object from a modified poro object" do
poro = mapper.load(a_active_record_object)
expect(mapper.dump(poro).roles.size).to eq 2

role_to_change = poro.roles.first
new_name = "#{role_to_change.name}_new"
role_to_change.name = new_name

expect(mapper.dump(poro).roles.first.name).to eq new_name
end

scenario "lazy loads associated objects" do
expect(a_active_record_object).not_to receive :salutation
expect(a_active_record_object).not_to receive :roles
Expand Down

0 comments on commit 250a024

Please sign in to comment.