Skip to content

Commit

Permalink
Add spec for model with module name give
Browse files Browse the repository at this point in the history
  • Loading branch information
chimame committed Sep 22, 2017
1 parent 5c1c543 commit ebe3766
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 1 deletion.
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
8 changes: 8 additions & 0 deletions spec/dummy_app/app/models/Family/child.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Family
class Child < ActiveRecord::Base
has_paper_trail

belongs_to :parent
has_many :grandsons
end
end
8 changes: 8 additions & 0 deletions spec/dummy_app/app/models/Family/family.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Family
class Family < ActiveRecord::Base
has_paper_trail

belongs_to :parent
belongs_to :grandson
end
end
13 changes: 13 additions & 0 deletions spec/dummy_app/app/models/Family/grandson.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Family
class Grandson < ActiveRecord::Base
has_paper_trail

if ActiveRecord.gem_version >= Gem::Version.new("5.0")
belongs_to :child, optional: true
else
belongs_to :child
end
has_many :families
has_many :parents, through: :families
end
end
12 changes: 12 additions & 0 deletions spec/dummy_app/app/models/Family/parent.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Family
class Parent < ActiveRecord::Base
has_paper_trail

has_one :partner
has_many :children
has_many :families
has_many :grandsons, through: :families

accepts_nested_attributes_for :children
end
end
7 changes: 7 additions & 0 deletions spec/dummy_app/app/models/Family/partner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Family
class Partner < ActiveRecord::Base
has_paper_trail

belongs_to :parent
end
end
27 changes: 27 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,33 @@ 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 :families do |t|
t.references :parent
t.references :grandson
end

create_table :parents do |t|
t.string :name
t.references :family
end

create_table :partners do |t|
t.string :name
t.references :parent
end

create_table :children do |t|
t.string :name
t.references :parent
end

create_table :grandsons do |t|
t.string :name
t.references :family
t.references :child
t.timestamps
end
end

def down
Expand Down
35 changes: 35 additions & 0 deletions spec/dummy_app/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@
t.string "name"
end

create_table "children", force: :cascade do |t|
t.string "name"
t.integer "parent_id"
t.index ["parent_id"], name: "index_children_on_parent_id"
end

create_table "citations", force: :cascade do |t|
t.integer "quotation_id"
end
Expand Down Expand Up @@ -111,6 +117,13 @@
t.integer "editor_id"
end

create_table "families", force: :cascade do |t|
t.integer "parent_id"
t.integer "grandson_id"
t.index ["grandson_id"], name: "index_families_on_grandson_id"
t.index ["parent_id"], name: "index_families_on_parent_id"
end

create_table "fluxors", force: :cascade do |t|
t.integer "widget_id"
t.string "name"
Expand All @@ -132,6 +145,16 @@
t.datetime "updated_at"
end

create_table "grandsons", force: :cascade do |t|
t.string "name"
t.integer "family_id"
t.integer "child_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["child_id"], name: "index_grandsons_on_child_id"
t.index ["family_id"], name: "index_grandsons_on_family_id"
end

create_table "legacy_widgets", force: :cascade do |t|
t.string "name"
t.integer "version"
Expand Down Expand Up @@ -173,6 +196,18 @@
t.string "name"
end

create_table "parents", force: :cascade do |t|
t.string "name"
t.integer "family_id"
t.index ["family_id"], name: "index_parents_on_family_id"
end

create_table "partners", force: :cascade do |t|
t.string "name"
t.integer "parent_id"
t.index ["parent_id"], name: "index_partners_on_parent_id"
end

create_table "people", force: :cascade do |t|
t.string "name"
t.string "time_zone"
Expand Down
95 changes: 95 additions & 0 deletions spec/paper_trail/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,29 @@
end
end
end

context "module name is given" do
let(:parent_with_partner) do
parent = Family::Parent.new(name: "parent1")
parent.build_partner(name: "partner1")
parent.save!
parent
end

context "change partner" do
before do
parent_with_partner.name = "parent2"
parent_with_partner.partner.name = "partner2"
parent_with_partner.save!
end

it "reify partner" do
previous_parent = parent_with_partner.versions.last.reify(has_one: true)
previous_partner = previous_parent.partner
expect(previous_partner.name).to eq "partner1"
end
end
end
end

context "a has_many association" do
Expand Down Expand Up @@ -284,6 +307,30 @@
end
end
end

context "module name is given" do
let(:parent_with_children) do
parent = Family::Parent.new(name: "parent1")
parent.children.build(name: "child1")
parent.save!
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 +748,30 @@
end
end
end

context "module name is given" do
let(:parent_with_grandsons) do
parent = Family::Parent.new(name: "parent1")
parent.grandsons.build(name: "grandson1")
parent.save!
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 +906,30 @@
end
end
end

context "module name is given" do
let(:parent_with_children) do
parent = Family::Parent.new(name: "parent1")
parent.children.build(name: "child1")
parent.save!
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
end

context "has_and_belongs_to_many associations" do
Expand Down

0 comments on commit ebe3766

Please sign in to comment.