Skip to content

Commit

Permalink
Merge pull request #36 from evman182/eraffel/FixAssociations
Browse files Browse the repository at this point in the history
Remove association override
  • Loading branch information
evman182 authored Feb 20, 2024
2 parents edcb8d7 + d9d580d commit d502ba4
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 15 deletions.
4 changes: 2 additions & 2 deletions gemfiles/Gemfile.6.1.mysql.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PATH
remote: ..
specs:
temporal_tables (3.0.0)
rails (>= 6.0, < 7.2)
temporal_tables (3.0.1)
rails (>= 6.1, < 7.2)

GEM
remote: https://rubygems.org/
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/Gemfile.6.1.pg.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PATH
remote: ..
specs:
temporal_tables (3.0.0)
rails (>= 6.0, < 7.2)
temporal_tables (3.0.1)
rails (>= 6.1, < 7.2)

GEM
remote: https://rubygems.org/
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/Gemfile.7.0.mysql.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PATH
remote: ..
specs:
temporal_tables (3.0.0)
rails (>= 6.0, < 7.2)
temporal_tables (3.0.1)
rails (>= 6.1, < 7.2)

GEM
remote: https://rubygems.org/
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/Gemfile.7.0.pg.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PATH
remote: ..
specs:
temporal_tables (3.0.0)
rails (>= 6.0, < 7.2)
temporal_tables (3.0.1)
rails (>= 6.1, < 7.2)

GEM
remote: https://rubygems.org/
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/Gemfile.7.1.mysql.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PATH
remote: ..
specs:
temporal_tables (3.0.0)
rails (>= 6.0, < 7.2)
temporal_tables (3.0.1)
rails (>= 6.1, < 7.2)

GEM
remote: https://rubygems.org/
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/Gemfile.7.1.pg.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PATH
remote: ..
specs:
temporal_tables (3.0.0)
rails (>= 6.0, < 7.2)
temporal_tables (3.0.1)
rails (>= 6.1, < 7.2)

GEM
remote: https://rubygems.org/
Expand Down
3 changes: 1 addition & 2 deletions lib/temporal_tables/temporal_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ def self.temporalize_associations! # rubocop:disable Metrics/MethodLength, Metri
association.macro, association.name,
**association.options.merge(
class_name: clazz.name,
foreign_key: association.foreign_key,
primary_key: clazz.orig_class.primary_key
foreign_key: association.foreign_key
)
)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/temporal_tables/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module TemporalTables
VERSION = '3.0.0'
VERSION = '3.0.1'
end
16 changes: 16 additions & 0 deletions spec/basic_history_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,19 @@
end
end
end

describe Hamster do
context 'with tables that have non-default primary key names' do
let(:hamster) { Hamster.create name: 'Fluffy' }
let(:wheel) { HamsterWheel.create hamster: hamster }

it 'can create instance of class with nested class name with history entries' do
expect(hamster).not_to be_nil
expect(wheel).not_to be_nil
expect(hamster.hamster_wheel).not_to be_nil
hamster_history = Hamster.history.at(Time.now.utc).first
expect(hamster_history).not_to be_nil
expect(hamster_history.hamster_wheel).not_to be_nil
end
end
end
7 changes: 7 additions & 0 deletions spec/internal/app/models/hamster.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class Hamster < ActiveRecord::Base
self.primary_key = :uuid

has_one :hamster_wheel, foreign_key: :hamster_uuid, inverse_of: :hamster
end
5 changes: 5 additions & 0 deletions spec/internal/app/models/hamster_wheel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class HamsterWheel < ActiveRecord::Base
belongs_to :hamster, foreign_key: :hamster_uuid, primary_key: :uuid, inverse_of: :hamster_wheel
end
18 changes: 18 additions & 0 deletions spec/internal/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,22 @@
t.belongs_to :bird, type: (postgres ? :uuid : :integer)
t.integer :height
end

if postgres
create_table :hamsters, id: false do |t|
t.column :uuid, :uuid, default: 'gen_random_uuid()'
t.string :name
end
execute 'ALTER TABLE hamsters ADD PRIMARY KEY (uuid);'
add_temporal_table :hamsters
else
create_table :hamsters, primary_key: :uuid, temporal: true do |t|
t.string :name
end
end

create_table :hamster_wheels, id: (postgres ? :uuid : :integer), temporal: true do |t|
t.column :hamster_uuid, (postgres ? :uuid : :bigint), null: false
t.foreign_key :hamsters, column: :hamster_uuid, primary_key: :uuid
end
end

0 comments on commit d502ba4

Please sign in to comment.