activerecord-exclusive-arc 0.2.1 → 0.2.2

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: 27e01ec54aa223f1e36904771298125b451731632cca44ae6d0d15627cbcf879
4
- data.tar.gz: 97990f81b596836909e67609a2d9745d6be13f0852373829656d2218454f6029
3
+ metadata.gz: b3824d9259213c59c55ee5287cc9bd3b8e76924be405a269c6752434811c2599
4
+ data.tar.gz: 2e8b6ad42b5b4cad774e7ef56e21c54fa9a0e8223fce05762ecb60cf791b4b87
5
5
  SHA512:
6
- metadata.gz: 448c3910176f2f9163d0cfac054482fa2c6dc96b3e67d1093668626e2dbc94cfdeb59adcc371f3890af35541f9e4db7b8f4db0cd0108db09cd6bfe68de4954be
7
- data.tar.gz: ec36d41ce0896b6505f76eb5112dd999db1cb334d9d0b956d82d7f043efba1e4999ef32ddbf0a384c140b5dcba01dfbaf7606b4638ac94381fbc8408751a7d3a
6
+ metadata.gz: 28d0736dfd28b87e367ab62d9ac1c39cc556bc76d999b390a4b6220b88ac566bcac66c6fda36b0237a5442180b44602c72e6ef837ed99a4f281b0e42da4aac81
7
+ data.tar.gz: e62da6627153c7853dde9bba360ec654ea28baf088bb4db6261fc77dd88fc78d1bf4b5f5d2c2d777682d0233e7f6ab72567ed1790e8813670b4964ec69a063a8
@@ -1,3 +1,3 @@
1
1
  module ExclusiveArc
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -31,10 +31,16 @@ class ExclusiveArcGenerator < ActiveRecord::Generators::Base
31
31
  model_file_path,
32
32
  class_name.demodulize,
33
33
  <<~RB
34
- #{indents}include ExclusiveArc::Model
35
34
  #{indents}has_exclusive_arc #{model_exclusive_arcs}
36
35
  RB
37
36
  )
37
+ inject_into_class(
38
+ model_file_path,
39
+ class_name.demodulize,
40
+ <<~RB
41
+ #{indents}include ExclusiveArc::Model
42
+ RB
43
+ )
38
44
  end
39
45
 
40
46
  no_tasks do
@@ -44,12 +50,30 @@ class ExclusiveArcGenerator < ActiveRecord::Generators::Base
44
50
  string
45
51
  end
46
52
 
53
+ def add_references
54
+ belong_tos.map do |reference|
55
+ add_reference(reference)
56
+ end.join("\n")
57
+ end
58
+
47
59
  def add_reference(reference)
48
- string = "add_reference :#{table_name}, :#{reference}"
49
- type = reference_type(reference)
50
- string += ", type: :#{type}" unless /int/.match?(type.downcase)
51
- string += ", foreign_key: true" unless options[:skip_foreign_key_constraints]
52
- string += ", index: {where: \"#{reference}_id IS NOT NULL\"}" unless options[:skip_foreign_key_indexes]
60
+ foreign_key = foreign_key_name(reference)
61
+ type = reference_type(reference).downcase
62
+ if foreign_key == "#{reference}_id"
63
+ string = " add_reference :#{table_name}, :#{reference}"
64
+ string += ", type: :#{type}" unless /int/.match?(type)
65
+ string += ", foreign_key: true" unless options[:skip_foreign_key_constraints]
66
+ string += ", index: {where: \"#{foreign_key} IS NOT NULL\"}" unless options[:skip_foreign_key_indexes]
67
+ else
68
+ string = " add_column :#{table_name}, :#{foreign_key}, :#{type}"
69
+ unless options[:skip_foreign_key_constraints]
70
+ referenced_table_name = reference_table_name(reference)
71
+ string += "\n add_foreign_key :#{table_name}, :#{referenced_table_name}, column: :#{foreign_key}"
72
+ end
73
+ unless options[:skip_foreign_key_indexes]
74
+ string += "\n add_index :#{table_name}, :#{foreign_key}, where: \"#{foreign_key} IS NOT NULL\""
75
+ end
76
+ end
53
77
  string
54
78
  end
55
79
 
@@ -62,15 +86,38 @@ class ExclusiveArcGenerator < ActiveRecord::Generators::Base
62
86
  end
63
87
 
64
88
  def reference_type(reference)
65
- klass = reference.singularize.classify.constantize
89
+ klass = class_name.constantize.reflections[reference].klass
66
90
  klass.columns.find { |col| col.name == klass.primary_key }.sql_type
67
91
  rescue
68
92
  "bigint"
69
93
  end
70
94
 
95
+ def reference_table_name(reference)
96
+ class_name.constantize.reflections[reference].klass.table_name
97
+ rescue
98
+ reference.tableize
99
+ end
100
+
101
+ def foreign_key_name(reference)
102
+ class_name.constantize.reflections[reference].foreign_key
103
+ rescue
104
+ "#{reference}_id"
105
+ end
106
+
107
+ def add_check_constraint
108
+ return if options[:skip_check_constraint]
109
+ <<-RUBY
110
+ add_check_constraint(
111
+ :#{table_name},
112
+ "#{check_constraint}",
113
+ name: :#{arc}
114
+ )
115
+ RUBY
116
+ end
117
+
71
118
  def check_constraint
72
119
  reference_checks = belong_tos.map do |reference|
73
- "CASE WHEN #{reference}_id IS NULL THEN 0 ELSE 1 END"
120
+ "CASE WHEN #{foreign_key_name(reference)} IS NULL THEN 0 ELSE 1 END"
74
121
  end
75
122
  condition = options[:optional] ? "<= 1" : "= 1"
76
123
  "(#{reference_checks.join(" + ")}) #{condition}"
@@ -1,10 +1,6 @@
1
1
  class <%= migration_class_name %> < ActiveRecord::Migration[<%= ActiveRecord::Migration.current_version %>]
2
2
  def change
3
- <% belong_tos.each do |reference| %><%= add_reference(reference) %>
4
- <% end %><% unless options[:skip_check_constraint] %>add_check_constraint(
5
- :<%= table_name %>,
6
- "<%= check_constraint %>",
7
- name: :<%= arc %>
8
- )<% end %>
3
+ <%= add_references %>
4
+ <%= add_check_constraint %>
9
5
  end
10
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-exclusive-arc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - justin talbott
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-08 00:00:00.000000000 Z
11
+ date: 2023-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord