-
Couldn't load subscription status.
- Fork 132
WIP: track changes on embedded_one #191
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ def attributes | |
| @attributes[k] = format_field(k, v) | ||
| end | ||
| end | ||
| insert_embeds_one_changes_on_child if trackable_class.tracked_embeds_one.present? && changes.empty? | ||
| @attributes | ||
| end | ||
|
|
||
|
|
@@ -27,6 +28,22 @@ def insert_embeds_one_changes(relation, value) | |
| @attributes[relation][1] = value[1][paranoia_field].present? ? {} : format_embeds_one_relation(relation, value[1]) | ||
| end | ||
|
|
||
| def insert_embeds_one_changes_on_child | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be renamed and refactored to return changes on embeds. The code for non-embeds should be changed to return changes on non-embeds, finally |
||
| trackable_class.tracked_embeds_one.each do |rel| | ||
| rel_class = trackable_class.embeds_one_class(rel) | ||
| paranoia_field = Mongoid::History.trackable_class_settings(rel_class)[:paranoia_field] | ||
| paranoia_field = rel_class.aliased_fields.key(paranoia_field) || paranoia_field | ||
| rel = aliased_fields.key(rel) || rel | ||
| obj = trackable.send(rel) | ||
| next if !obj || (obj.respond_to?(paranoia_field) && obj.public_send(paranoia_field).present?) | ||
| @attributes[rel] = {} | ||
| obj.changes.each do |k, v| | ||
| @attributes[rel] = [{ k => v.first }, { k => v.last }] | ||
| end | ||
| end | ||
| @attributes | ||
| end | ||
|
|
||
| def insert_embeds_many_changes(relation, value) | ||
| relation = trackable_class.database_field_name(relation) | ||
| relation_class = trackable_class.embeds_many_class(relation) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| require 'spec_helper' | ||
|
|
||
| describe Mongoid::History::Tracker do | ||
| before :all do | ||
| # Child model (will be embedded in Parent) | ||
| class Child | ||
| include Mongoid::Document | ||
| include Mongoid::History::Trackable | ||
|
|
||
| field :name | ||
| embedded_in :parent, inverse_of: :child | ||
| end | ||
|
|
||
| # Parent model (embeds one Child) | ||
| class Parent | ||
| include Mongoid::Document | ||
| include Mongoid::History::Trackable | ||
|
|
||
| field :name, type: String | ||
| embeds_one :child | ||
|
|
||
| track_history on: %i[fields embedded_relations], | ||
| version_field: :version, | ||
| track_create: true, | ||
| track_update: true, | ||
| track_destroy: false | ||
| end | ||
| end | ||
|
|
||
| it 'should be able to track history for nested embedded documents in parent' do | ||
| p = Parent.new(name: 'bowser') | ||
| p.child = Child.new(name: 'todd') | ||
| p.save! | ||
|
|
||
| expect(p.history_tracks.length).to eq(1) | ||
| change = p.history_tracks.last | ||
| expect(change.modified['name']).to eq('bowser') | ||
| expect(change.modified['child']['name']).to eq('todd') | ||
|
|
||
| p.update_attributes(name: 'brow') | ||
| expect(p.history_tracks.length).to eq(2) | ||
|
|
||
| p.child.name = 'mario' | ||
| p.save! | ||
|
|
||
| expect(p.history_tracks.length).to eq(3) | ||
| expect(p.history_tracks.last.original['child']['name']).to eq('todd') | ||
| expect(p.history_tracks.last.modified['child']['name']).to eq('mario') | ||
| end | ||
|
|
||
| after :all do | ||
| Object.send(:remove_const, :Parent) | ||
| Object.send(:remove_const, :Child) | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems strange that you'd have the
changes.empty?check here. Maybe write a spec that changes fields, then embeds, then both. Then we're keeping things in@attributes, so I imagine this really should be something like