-
Notifications
You must be signed in to change notification settings - Fork 896
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
Fix get version data with full name of class #996
Merged
jaredbeck
merged 7 commits into
paper-trail-gem:master
from
chimame:fix_association_can_not_be_loaded
Oct 5, 2017
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5c1c543
Fix get version data with full name of class
chimame 3f714e7
Add spec for model with module name give
chimame aea6b54
Modify version was not created correctly for spec
chimame 775dae5
Modify model definition for module name spec
chimame 25f23c7
Modify schema.rb
chimame 275c4e3
Delete table name prefix
chimame 7c34f5c
Change directory name to lower case
chimame File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Family | ||
class Family < ActiveRecord::Base | ||
has_paper_trail | ||
|
||
if ActiveRecord.gem_version >= Gem::Version.new("5.0") | ||
belongs_to :parent, class_name: "Family::Person", foreign_key: :parent_id, optional: true | ||
else | ||
belongs_to :parent, class_name: "Family::Person", foreign_key: :parent_id | ||
end | ||
if ActiveRecord.gem_version >= Gem::Version.new("5.0") | ||
belongs_to :grandson, class_name: "Family::Person", foreign_key: :grandson_id, optional: true | ||
else | ||
belongs_to :grandson, class_name: "Family::Person", foreign_key: :grandson_id | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Family | ||
def self.table_name_prefix | ||
"family_" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Family | ||
class Person < ActiveRecord::Base | ||
has_paper_trail | ||
|
||
has_many :families, class_name: "Family::Family", foreign_key: :parent_id | ||
has_many :children, class_name: "Family::Person", foreign_key: :parent_id | ||
has_many :grandsons, through: :families | ||
has_one :mentee, class_name: "Family::Person", foreign_key: :partner_id | ||
if ActiveRecord.gem_version >= Gem::Version.new("5.0") | ||
belongs_to :parent, class_name: "Family::Person", foreign_key: :parent_id, optional: true | ||
else | ||
belongs_to :parent, class_name: "Family::Person", foreign_key: :parent_id | ||
end | ||
if ActiveRecord.gem_version >= Gem::Version.new("5.0") | ||
belongs_to :menter, class_name: "Family::Person", foreign_key: :partner_id, optional: true | ||
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. "Mentor" is the correct spelling. English is silly. |
||
else | ||
belongs_to :menter, class_name: "Family::Person", foreign_key: :partner_id | ||
end | ||
|
||
accepts_nested_attributes_for :mentee | ||
accepts_nested_attributes_for :children | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,31 @@ | |
end | ||
end | ||
end | ||
|
||
context "module name is given" do | ||
let(:parent_with_partner) do | ||
parent = Family::Person.new(name: "parent1") | ||
parent.build_mentee(name: "partner1") | ||
parent.save! | ||
Timecop.travel(1.second.since) | ||
parent | ||
end | ||
|
||
context "change partner" do | ||
before do | ||
parent_with_partner.update_attributes( | ||
name: "parent2", | ||
mentee_attributes: { id: parent_with_partner.mentee.id, name: "partner2" } | ||
) | ||
end | ||
|
||
it "reify partner" do | ||
previous_parent = parent_with_partner.versions.last.reify(has_one: true) | ||
previous_partner = previous_parent.mentee | ||
expect(previous_partner.name).to eq "partner1" | ||
end | ||
end | ||
end | ||
end | ||
|
||
context "a has_many association" do | ||
|
@@ -284,6 +309,31 @@ | |
end | ||
end | ||
end | ||
|
||
context "module name is given" do | ||
let(:parent_with_children) do | ||
parent = Family::Person.new(name: "parent1") | ||
parent.children.build(name: "child1") | ||
parent.save! | ||
Timecop.travel(1.second.since) | ||
parent | ||
end | ||
|
||
context "create new children" do | ||
before do | ||
parent_with_children.name = "parent2" | ||
parent_with_children.children.build(name: "child2") | ||
parent_with_children.save! | ||
end | ||
|
||
it "reify children" do | ||
previous_parent = parent_with_children.versions.last.reify(has_many: true) | ||
previous_children = previous_parent.children | ||
expect(previous_children.size).to eq 1 | ||
expect(previous_children.first.name).to eq "child1" | ||
end | ||
end | ||
end | ||
end | ||
|
||
context "has_many through associations" do | ||
|
@@ -701,6 +751,31 @@ | |
end | ||
end | ||
end | ||
|
||
context "module name is given" do | ||
let(:parent_with_grandsons) do | ||
parent = Family::Person.new(name: "parent1") | ||
parent.grandsons.build(name: "grandson1") | ||
parent.save! | ||
Timecop.travel(1.second.since) | ||
parent | ||
end | ||
|
||
context "create new grandsons" do | ||
before do | ||
parent_with_grandsons.name = "parent2" | ||
parent_with_grandsons.grandsons.build(name: "grandson2") | ||
parent_with_grandsons.save! | ||
end | ||
|
||
it "reify grandsons" do | ||
previous_parent = parent_with_grandsons.versions.last.reify(has_many: true) | ||
previous_grandsons = previous_parent.grandsons | ||
expect(previous_grandsons.size).to eq 1 | ||
expect(previous_grandsons.first.name).to eq "grandson1" | ||
end | ||
end | ||
end | ||
end | ||
|
||
context "belongs_to associations" do | ||
|
@@ -835,6 +910,31 @@ | |
end | ||
end | ||
end | ||
|
||
context "module name is given" do | ||
let(:parent_with_children) do | ||
parent = Family::Person.new(name: "parent1") | ||
parent.children.build(name: "child1") | ||
parent.save! | ||
Timecop.travel(1.second.since) | ||
parent | ||
end | ||
|
||
context "change children" do | ||
before do | ||
parent_with_children.update_attributes!( | ||
name: "parent2", | ||
children_attributes: { id: parent_with_children.children.first.id, name: "child2" } | ||
) | ||
end | ||
|
||
it "reify parent" do | ||
previous_children = parent_with_children.children. | ||
first.versions.last.reify(belongs_to: true) | ||
expect(previous_children.parent.name).to eq "parent1" | ||
end | ||
end | ||
end | ||
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. Thank you for testing all four association types. |
||
end | ||
|
||
context "has_and_belongs_to_many associations" do | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Neat! I didn't know about this method. I will be able to use this at work.