declare_schema 1.4.0.colin.4 → 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: b48f50b03282da270199e8bb46e4e7a2d69068a8753203364835db206692a94a
4
- data.tar.gz: b72de21ed3123f0b4e9527d5e5ee805742b2c21628adeabd4213925858062914
3
+ metadata.gz: 1bdf7e72cc4b08040aaaa9036e520edf7c2be76a0a2466fc08215c8d222ec966
4
+ data.tar.gz: 63a8e595acec695a3a88ead0daeb46e871353645747ec2c03e6175d7d7c35161
5
5
  SHA512:
6
- metadata.gz: 24edb7af1a43c955d5bc3664d755ea30d225bf1bce15e98765693ee7de2da87b4d66c42b0c6f6818b5a81b9485536da4ccd824319b91744eee6837c7317d80ca
7
- data.tar.gz: 996169b7db3b3a8426968e8924181a0c588b7035d00a563fb78708eb8e47e2384f20b5838a2bc491ccbef3f45c6e3f462c3e0095261477841cfe666b320acfd2
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.4)
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
@@ -16,6 +16,7 @@ module DeclareSchema
16
16
  # attr_types holds the type class for any attribute reader (i.e. getter
17
17
  # method) that returns rich-types
18
18
  inheriting_cattr_reader attr_types: HashWithIndifferentAccess.new
19
+ inheriting_cattr_reader attr_order: []
19
20
 
20
21
  # field_specs holds FieldSpec objects for every declared
21
22
  # field. Note that attribute readers are created (by ActiveRecord)
@@ -64,7 +65,7 @@ module DeclareSchema
64
65
  end
65
66
 
66
67
  # tell the migration generator to ignore the named index. Useful for existing indexes, or for indexes
67
- # that can't be automatically generated (for example: a prefix index in MySQL)
68
+ # that can't be automatically generated.
68
69
  def ignore_index(index_name)
69
70
  ignore_indexes << index_name.to_s
70
71
  end
@@ -80,6 +81,7 @@ module DeclareSchema
80
81
  _add_validations_for_field(name, type, args, options)
81
82
  _add_index_for_field(name, args, options)
82
83
  field_specs[name] = ::DeclareSchema::Model::FieldSpec.new(self, name, type, position: field_specs.size, **options)
84
+ attr_order << name unless attr_order.include?(name)
83
85
  end
84
86
 
85
87
  def index_definitions_with_primary_key
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DeclareSchema
4
- VERSION = "1.4.0.colin.4"
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.4
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-13 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