declare_schema 1.4.0.colin.5 → 1.4.0.colin.7

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: 0f8226540260a887af839da5afcb4641217c03ac5a6eb2d7a00da2806188f1ce
4
+ data.tar.gz: 7eeb464b5bfc9b1fbf88446696ede88b8ea582060658421cf2772e62a6205797
5
5
  SHA512:
6
- metadata.gz: d8e261efe79f5b6fdbb86f1fec4a81445e65baf664d26dd6db2c3e85d2d94c305967a4e0f9f5dcce0106daab83480975034dd20c47a319b9365a60b031ec7d49
7
- data.tar.gz: 22da299dd84f766df2668d4a81f86d3398be03cab012ac098fc1d7d1fab10d65e6ecc0b3fec126ff7c4b57ee18df6c5cab0219fc681b146891f98c06131e3f74
6
+ metadata.gz: 2294e94f2165ec156807fbf9ba98dea10f191bfda1b4c630f5444ab08ee7f80323e86a9af80e968e20e579e21ba24ddef520312a7f0a146d850b6f8927aa50aa
7
+ data.tar.gz: 2742ce98af1fb6ef8eb4ba1fa40563a635abb584cc1860efd373a474c6dce9fc1a27b630141f94ea6a23f36602d8381b08d6a33b13b6fd3d352b40df85f37daf
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.7)
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
@@ -57,11 +57,8 @@ module DeclareSchema
57
57
  index(fields.flatten, unique: true, name: ::DeclareSchema::Model::IndexDefinition::PRIMARY_KEY_NAME)
58
58
  end
59
59
 
60
- def constraint(fkey, **options)
61
- fkey_s = fkey.to_s
62
- unless constraint_specs.any? { |constraint_spec| constraint_spec.foreign_key == fkey_s }
63
- constraint_specs << DeclareSchema::Model::ForeignKeyDefinition.new(self, fkey, **options)
64
- end
60
+ def constraint(foreign_key, **options)
61
+ constraint_specs << DeclareSchema::Model::ForeignKeyDefinition.new(self, foreign_key.to_s, **options)
65
62
  end
66
63
 
67
64
  # tell the migration generator to ignore the named index. Useful for existing indexes, or for indexes
@@ -152,9 +149,9 @@ module DeclareSchema
152
149
 
153
150
  super
154
151
 
155
- refl = reflections[name.to_s] or raise "Couldn't find reflection #{name} in #{reflections.keys}"
156
- fkey = refl.foreign_key or raise "Couldn't find foreign_key for #{name} in #{refl.inspect}"
157
- fkey_id_column_options = column_options.dup
152
+ reflection = reflections[name.to_s] or raise "Couldn't find reflection #{name} in #{reflections.keys}"
153
+ foreign_key = reflection.foreign_key or raise "Couldn't find foreign_key for #{name} in #{reflection.inspect}"
154
+ foreign_key_id_column_options = column_options.dup
158
155
 
159
156
  # Note: the foreign key limit: should match the primary key limit:. (If there is a foreign key constraint,
160
157
  # those limits _must_ match.) We'd like to call _infer_fk_limit and get the limit right from the PK.
@@ -165,31 +162,31 @@ module DeclareSchema
165
162
  # The one downside of this approach is that application code that asks the field_spec for the declared
166
163
  # foreign key limit: will always get 8 back even if this is a grandfathered foreign key that points to
167
164
  # a limit: 4 primary key. It seems unlikely that any application code would do this.
168
- fkey_id_column_options[:pre_migration] = ->(field_spec) do
169
- if (inferred_limit = _infer_fk_limit(fkey, refl))
165
+ foreign_key_id_column_options[:pre_migration] = ->(field_spec) do
166
+ if (inferred_limit = _infer_fk_limit(foreign_key, reflection))
170
167
  field_spec.sql_options[:limit] = inferred_limit
171
168
  end
172
169
  end
173
170
 
174
- declare_field(fkey.to_sym, :bigint, **fkey_id_column_options)
171
+ declare_field(foreign_key.to_sym, :bigint, **foreign_key_id_column_options)
175
172
 
176
- if refl.options[:polymorphic]
173
+ if reflection.options[:polymorphic]
177
174
  foreign_type = options[:foreign_type] || "#{name}_type"
178
175
  _declare_polymorphic_type_field(foreign_type, column_options)
179
- index([foreign_type, fkey], **index_options) if index_options
176
+ index([foreign_type, foreign_key], **index_options) if index_options
180
177
  else
181
- index(fkey, **index_options) if index_options
182
- constraint(fkey, **fk_options) if fk_options[:constraint_name] != false
178
+ index(foreign_key, **index_options) if index_options
179
+ constraint(foreign_key, **fk_options) if fk_options[:constraint_name] != false
183
180
  end
184
181
  end
185
182
 
186
- def _infer_fk_limit(fkey, refl)
187
- if refl.options[:polymorphic]
188
- if (fkey_column = columns_hash[fkey.to_s]) && fkey_column.type == :integer
189
- fkey_column.limit
183
+ def _infer_fk_limit(foreign_key, reflection)
184
+ if reflection.options[:polymorphic]
185
+ if (foreign_key_column = columns_hash[foreign_key.to_s]) && foreign_key_column.type == :integer
186
+ foreign_key_column.limit
190
187
  end
191
188
  else
192
- klass = refl.klass or raise "Couldn't find belongs_to klass for #{name} in #{refl.inspect}"
189
+ klass = reflection.klass or raise "Couldn't find belongs_to klass for #{name} in #{reflection.inspect}"
193
190
  if (pk_id_type = klass._table_options&.[](:id))
194
191
  if pk_id_type == :integer
195
192
  4
@@ -320,11 +317,11 @@ module DeclareSchema
320
317
  end
321
318
 
322
319
  attr_types[name] ||
323
- if (refl = reflections[name.to_s])
324
- if refl.macro.in?([:has_one, :belongs_to]) && !refl.options[:polymorphic]
325
- refl.klass
320
+ if (reflection = reflections[name.to_s])
321
+ if reflection.macro.in?([:has_one, :belongs_to]) && !reflection.options[:polymorphic]
322
+ reflection.klass
326
323
  else
327
- refl
324
+ reflection
328
325
  end
329
326
  end ||
330
327
  if (col = _column(name.to_s))
@@ -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.7"
5
5
  end
@@ -426,7 +426,7 @@ module Generators
426
426
 
427
427
  new_table_name = model.table_name
428
428
  existing_indexes = ::DeclareSchema::Model::IndexDefinition.for_model(model, old_table_name)
429
- model_indexes_with_equivalents = model.index_definitions_with_primary_key
429
+ model_indexes_with_equivalents = model.index_definitions_with_primary_key.to_a
430
430
  model_indexes = model_indexes_with_equivalents.map do |i|
431
431
  if i.explicit_name.nil?
432
432
  if (existing = existing_indexes.find { |e| i != e && e.equivalent?(i) })
@@ -486,7 +486,7 @@ module Generators
486
486
  ::DeclareSchema.default_generate_foreign_keys or return []
487
487
 
488
488
  existing_fks = ::DeclareSchema::Model::ForeignKeyDefinition.for_model(model, old_table_name)
489
- model_fks = model.constraint_specs
489
+ model_fks = model.constraint_specs.to_a
490
490
 
491
491
  fks_to_drop = existing_fks - model_fks
492
492
  fks_to_add = model_fks - existing_fks
@@ -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.7
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