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

Fix get version data with full name of class #996

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).

### Fixed

- None
- Fix Associations
supports revision of association with module name.

## 7.1.3 (2017-09-19)

Expand Down
2 changes: 1 addition & 1 deletion lib/paper_trail/reifiers/belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def load_record(assoc, id, options, version)
# @api private
def load_version(assoc, id, transaction_id, version_at)
assoc.klass.paper_trail.version_class.
where("item_type = ?", assoc.class_name).
where("item_type = ?", assoc.klass.name).
where("item_id = ?", id).
where("created_at >= ? OR transaction_id = ?", version_at, transaction_id).
order("id").limit(1).first
Expand Down
2 changes: 1 addition & 1 deletion lib/paper_trail/reifiers/has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def load_versions_for_hm_association(assoc, model, version_table, tx_id, version
select("MIN(version_id)").
where("foreign_key_name = ?", assoc.foreign_key).
where("foreign_key_id = ?", model.id).
where("#{version_table}.item_type = ?", assoc.class_name).
where("#{version_table}.item_type = ?", assoc.klass.name).
where("created_at >= ? OR transaction_id = ?", version_at, tx_id).
group("item_id").
to_sql
Expand Down
2 changes: 1 addition & 1 deletion lib/paper_trail/reifiers/has_many_through.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def collection_through_belongs_to(through_collection, assoc, options, tx_id)
def load_versions_for_hmt_association(assoc, ids, tx_id, version_at)
version_id_subquery = assoc.klass.paper_trail.version_class.
select("MIN(id)").
where("item_type = ?", assoc.class_name).
where("item_type = ?", assoc.klass.name).
where("item_id IN (?)", ids).
where(
"created_at >= ? OR transaction_id = ?",
Expand Down
2 changes: 1 addition & 1 deletion lib/paper_trail/reifiers/has_one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def load_version_for_has_one(assoc, model, transaction_id, version_at)
model.class.paper_trail.version_class.joins(:version_associations).
where("version_associations.foreign_key_name = ?", assoc.foreign_key).
where("version_associations.foreign_key_id = ?", model.id).
where("#{version_table_name}.item_type = ?", assoc.class_name).
where("#{version_table_name}.item_type = ?", assoc.klass.name).
where("created_at >= ? OR transaction_id = ?", version_at, transaction_id).
order("#{version_table_name}.id ASC").
first
Expand Down
16 changes: 16 additions & 0 deletions spec/dummy_app/app/models/Family/family.rb
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
5 changes: 5 additions & 0 deletions spec/dummy_app/app/models/family.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Family
def self.table_name_prefix
"family_"
end
Copy link
Member

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.

end
23 changes: 23 additions & 0 deletions spec/dummy_app/app/models/family/person.rb
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
Copy link
Member

Choose a reason for hiding this comment

The 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
11 changes: 11 additions & 0 deletions spec/dummy_app/db/migrate/20110208155312_set_up_test_tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,17 @@ def up
t.datetime :created_at
end
add_index :custom_primary_key_record_versions, %i[item_type item_id], name: "idx_cust_pk_item"

create_table :family_families do |t|
t.integer :parent_id
t.integer :grandson_id
end

create_table :family_people do |t|
t.string :name
t.integer :parent_id
t.integer :partner_id
end
end

def down
Expand Down
11 changes: 11 additions & 0 deletions spec/dummy_app/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@
t.integer "editor_id"
end

create_table "family_families", force: :cascade do |t|
t.integer "parent_id"
t.integer "grandson_id"
end

create_table "family_people", force: :cascade do |t|
t.string "name"
t.integer "parent_id"
t.integer "partner_id"
end

create_table "fluxors", force: :cascade do |t|
t.integer "widget_id"
t.string "name"
Expand Down
100 changes: 100 additions & 0 deletions spec/paper_trail/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down