declare_schema 1.4.0.colin.5 → 1.4.0.colin.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 770c10f063190aa951ca0ab0956c1988876e9a60c235e08ac9013d6a2f55dc3d
4
- data.tar.gz: e0935d656bc3dd48e21c41edfbfbaf8fa416dee00bac305fcaa8b08a85bc0c02
3
+ metadata.gz: 1bdf7e72cc4b08040aaaa9036e520edf7c2be76a0a2466fc08215c8d222ec966
4
+ data.tar.gz: 63a8e595acec695a3a88ead0daeb46e871353645747ec2c03e6175d7d7c35161
5
5
  SHA512:
6
- metadata.gz: d8e261efe79f5b6fdbb86f1fec4a81445e65baf664d26dd6db2c3e85d2d94c305967a4e0f9f5dcce0106daab83480975034dd20c47a319b9365a60b031ec7d49
7
- data.tar.gz: 22da299dd84f766df2668d4a81f86d3398be03cab012ac098fc1d7d1fab10d65e6ecc0b3fec126ff7c4b57ee18df6c5cab0219fc681b146891f98c06131e3f74
6
+ metadata.gz: eca71720644bbf4783dc90bab55604b154f2feb6ab5e05c72822675c9dc2993368041074f2f3a719188298577820e460ec44815a3b7a3ee19b46f00f5448e19d
7
+ data.tar.gz: 35851323ffd8cd93b0f634d03f3164a83087cee588a45da74f4499e5ebd1d48dcea50fb21dfe42eb2ed807ce3cf307a40de79b4f5a8fbb99c0017268d20932d6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- declare_schema (1.4.0.colin.5)
4
+ declare_schema (1.4.0.colin.6)
5
5
  rails (>= 5.0)
6
6
 
7
7
  GEM
@@ -7,7 +7,7 @@ module DeclareSchema
7
7
  class ForeignKeyDefinition
8
8
  include Comparable
9
9
 
10
- attr_reader :constraint_name, :model, :foreign_key, :foreign_key_name, :parent_table_name, :child_table_name, :options, :on_delete_cascade
10
+ attr_reader :constraint_name, :model, :foreign_key, :foreign_key_name, :child_table_name, :options, :on_delete_cascade
11
11
 
12
12
 
13
13
  def initialize(model, foreign_key, **options)
@@ -19,6 +19,17 @@ module DeclareSchema
19
19
  @parent_table_name = options[:parent_table]&.to_s
20
20
  @foreign_key_name = options[:foreign_key]&.to_s || @foreign_key
21
21
 
22
+ @parent_class_name =
23
+ case class_name = options[:class_name]
24
+ when String, Symbol
25
+ class_name.to_s
26
+ when Class
27
+ @parent_class = class_name
28
+ @parent_class.name
29
+ when nil
30
+ @foreign_key.sub(/_id\z/, '').camelize
31
+ end
32
+
22
33
  @constraint_name = options[:constraint_name]&.to_s.presence ||
23
34
  model.connection.index_name(model.table_name, column: @foreign_key_name)
24
35
  @on_delete_cascade = options[:dependent] == :delete
@@ -44,21 +55,13 @@ module DeclareSchema
44
55
  end
45
56
 
46
57
  # returns the parent class as a Class object
47
- # or nil if no :class_name option given
58
+ # lazy loaded so that we don't require the parent class until we need it
48
59
  def parent_class
49
- if (class_name = options[:class_name])
50
- if class_name.is_a?(Class)
51
- class_name
52
- else
53
- class_name.to_s.constantize
54
- end
55
- end
60
+ @parent_class ||= @parent_class_name.constantize
56
61
  end
57
62
 
58
63
  def parent_table_name
59
- @parent_table_name ||=
60
- parent_class&.try(:table_name) ||
61
- foreign_key.sub(/_id\z/, '').camelize.constantize.table_name
64
+ @parent_table_name ||= parent_class.table_name
62
65
  end
63
66
 
64
67
  def <=>(rhs)
@@ -67,14 +70,14 @@ module DeclareSchema
67
70
 
68
71
  alias eql? ==
69
72
 
73
+ def hash
74
+ key.hash
75
+ end
76
+
70
77
  private
71
78
 
72
79
  def key
73
- @key ||= [@child_table_name, parent_table_name, @foreign_key_name, @on_delete_cascade].map(&:to_s)
74
- end
75
-
76
- def hash
77
- key.hash
80
+ @key ||= [@child_table_name, @parent_class_name, @foreign_key_name, @on_delete_cascade].map(&:to_s)
78
81
  end
79
82
  end
80
83
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeclareSchema
4
- VERSION = "1.4.0.colin.5"
4
+ VERSION = "1.4.0.colin.6"
5
5
  end
@@ -60,6 +60,24 @@ RSpec.describe DeclareSchema::Model::ForeignKeyDefinition do
60
60
  end
61
61
  end
62
62
 
63
+ describe `#<=>` do
64
+ context 'when class name not passed' do
65
+ let(:options) { { foreign_key: :the_network_id, constraint_name: :constraint_1, dependent: :delete } }
66
+
67
+ it 'compares equal without requring the parent class' do
68
+ expect(subject <=> subject).to eq(0)
69
+ end
70
+ end
71
+
72
+ context 'when class name passed' do
73
+ let(:options) { { foreign_key: :the_network_id, class_name: 'TheNetwork', constraint_name: :constraint_1 } }
74
+
75
+ it 'compares equal without requring the parent class' do
76
+ expect(subject <=> subject).to eq(0)
77
+ end
78
+ end
79
+ end
80
+
63
81
  context 'when constraint name passed as empty string' do
64
82
  let(:options) { { constraint_name: "" } }
65
83
  it 'defaults to rails constraint name' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: declare_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0.colin.5
4
+ version: 1.4.0.colin.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Invoca Development adapted from hobo_fields by Tom Locke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-14 00:00:00.000000000 Z
11
+ date: 2024-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails