brick 1.0.17 → 1.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/brick/config.rb +1 -1
- data/lib/brick/extensions.rb +347 -74
- data/lib/brick/frameworks/rails/engine.rb +140 -109
- data/lib/brick/join_array.rb +227 -0
- data/lib/brick/version_number.rb +1 -1
- data/lib/brick.rb +36 -2
- data/lib/generators/brick/install_generator.rb +10 -10
- metadata +3 -2
data/lib/brick.rb
CHANGED
@@ -102,14 +102,37 @@ module Brick
|
|
102
102
|
end
|
103
103
|
|
104
104
|
def get_bts_and_hms(model)
|
105
|
-
model.reflect_on_all_associations.each_with_object([{}, {}]) do |a, s|
|
105
|
+
bts, hms = model.reflect_on_all_associations.each_with_object([{}, {}]) do |a, s|
|
106
|
+
# So that we can map an association name to any special alias name used in an AREL query
|
107
|
+
ans = (model._assoc_names[a.name] ||= [])
|
108
|
+
next if !const_defined?(a.name.to_s.singularize.camelize) && ::Brick.config.exclude_tables.include?(a.plural_name)
|
109
|
+
|
110
|
+
ans << a.klass unless ans.include?(a.klass)
|
106
111
|
case a.macro
|
107
112
|
when :belongs_to
|
108
113
|
s.first[a.foreign_key] = [a.name, a.klass]
|
109
|
-
when :has_many, :has_one
|
114
|
+
when :has_many, :has_one # This gets has_many as well as has_many :through
|
115
|
+
# %%% weed out ones that don't have an available model to reference
|
110
116
|
s.last[a.name] = a
|
111
117
|
end
|
112
118
|
end
|
119
|
+
# Mark has_manys that go to an associative ("join") table so that they are skipped in the UI,
|
120
|
+
# as well as any possible polymorphic associations
|
121
|
+
skip_hms = {}
|
122
|
+
associatives = hms.each_with_object({}) do |hmt, s|
|
123
|
+
if (through = hmt.last.options[:through])
|
124
|
+
skip_hms[through] = nil
|
125
|
+
s[hmt.first] = hms[through] # End up with a hash of HMT names pointing to join-table associations
|
126
|
+
elsif hmt.last.inverse_of.nil?
|
127
|
+
puts "SKIPPING #{hmt.last.name.inspect}"
|
128
|
+
# %%% If we don't do this then below associative.name will find that associative is nil
|
129
|
+
skip_hms[hmt.last.name] = nil
|
130
|
+
end
|
131
|
+
end
|
132
|
+
skip_hms.each do |k, _v|
|
133
|
+
puts hms.delete(k).inspect
|
134
|
+
end
|
135
|
+
[bts, hms, associatives]
|
113
136
|
end
|
114
137
|
|
115
138
|
# Switches Brick auto-models on or off, for all threads
|
@@ -254,6 +277,17 @@ module Brick
|
|
254
277
|
ars.each { |fk| ::Brick._add_bt_and_hm(fk[0..2]) }
|
255
278
|
@_additional_references_loaded = true
|
256
279
|
end
|
280
|
+
|
281
|
+
# Find associative tables that can be set up for has_many :through
|
282
|
+
::Brick.relations.each do |_key, tbl|
|
283
|
+
tbl_cols = tbl[:cols].keys
|
284
|
+
fks = tbl[:fks].each_with_object({}) { |fk, s| s[fk.last[:fk]] = [fk.last[:assoc_name], fk.last[:inverse_table]] if fk.last[:is_bt]; s }
|
285
|
+
# Aside from the primary key and the metadata columns created_at, updated_at, and deleted_at, if this table only has
|
286
|
+
# foreign keys then it can act as an associative table and thus be used with has_many :through.
|
287
|
+
if fks.length > 1 && (tbl_cols - fks.keys - (::Brick.config.metadata_columns || []) - (tbl[:pkey].values.first || [])).length.zero?
|
288
|
+
fks.each { |fk| tbl[:hmt_fks][fk.first] = fk.last }
|
289
|
+
end
|
290
|
+
end
|
257
291
|
end
|
258
292
|
|
259
293
|
|
@@ -18,7 +18,7 @@ module Brick
|
|
18
18
|
desc 'Generates an initializer file for configuring Brick'
|
19
19
|
|
20
20
|
def create_initializer_file
|
21
|
-
unless File.
|
21
|
+
unless File.exist?(filename = 'config/initializers/brick.rb')
|
22
22
|
# See if we can make suggestions for additional_references
|
23
23
|
resembles_fks = []
|
24
24
|
possible_additional_references = (relations = ::Brick.relations).each_with_object([]) do |v, s|
|
@@ -47,7 +47,7 @@ module Brick
|
|
47
47
|
if (relations.fetch(f_table = col_down, nil) ||
|
48
48
|
relations.fetch(f_table = ActiveSupport::Inflector.pluralize(col_down), nil)) &&
|
49
49
|
# Looks pretty promising ... just make sure a model file isn't present
|
50
|
-
!File.
|
50
|
+
!File.exist?("app/models/#{ActiveSupport::Inflector.singularize(v.first)}.rb")
|
51
51
|
s << "['#{v.first}', '#{col}', '#{f_table}']"
|
52
52
|
else
|
53
53
|
resembles_fks << "#{v.first}.#{col}"
|
@@ -58,24 +58,24 @@ module Brick
|
|
58
58
|
end
|
59
59
|
|
60
60
|
bar = case possible_additional_references.length
|
61
|
-
|
61
|
+
when 0
|
62
62
|
+"# Brick.additional_references = [['orders', 'customer_id', 'customer'],
|
63
63
|
# ['customer', 'region_id', 'regions']]"
|
64
|
-
|
65
|
-
|
64
|
+
when 1
|
65
|
+
+"# # Here is a possible additional reference that has been auto-identified for the #{ActiveRecord::Base.connection.current_database} database:
|
66
66
|
# Brick.additional_references = [[#{possible_additional_references.first}]"
|
67
|
-
|
68
|
-
|
67
|
+
else
|
68
|
+
+"# # Here are possible additional references that have been auto-identified for the #{ActiveRecord::Base.connection.current_database} database:
|
69
69
|
# Brick.additional_references = [
|
70
70
|
# #{possible_additional_references.join(",\n# ")}
|
71
71
|
# ]"
|
72
|
-
|
72
|
+
end
|
73
73
|
if resembles_fks.length > 0
|
74
74
|
bar << "\n# # Columns named somewhat like a foreign key which you may want to consider:
|
75
75
|
# # #{resembles_fks.join(', ')}"
|
76
76
|
end
|
77
77
|
|
78
|
-
|
78
|
+
create_file(filename, "# frozen_string_literal: true
|
79
79
|
|
80
80
|
# # Settings for the Brick gem
|
81
81
|
# # (By default this auto-creates models, controllers, views, and routes on-the-fly.)
|
@@ -159,7 +159,7 @@ module Brick
|
|
159
159
|
# Brick.default_route_fallback = 'customers' # This defaults to \"customers/index\"
|
160
160
|
# Brick.default_route_fallback = 'orders/outstanding' # Example of a non-RESTful route
|
161
161
|
# Brick.default_route_fallback = '' # Omits setting a default route in the absence of any other
|
162
|
-
"
|
162
|
+
")
|
163
163
|
end
|
164
164
|
end
|
165
165
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: brick
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.20
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lorin Thwaits
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -221,6 +221,7 @@ files:
|
|
221
221
|
- lib/brick/frameworks/rails/controller.rb
|
222
222
|
- lib/brick/frameworks/rails/engine.rb
|
223
223
|
- lib/brick/frameworks/rspec.rb
|
224
|
+
- lib/brick/join_array.rb
|
224
225
|
- lib/brick/serializers/json.rb
|
225
226
|
- lib/brick/serializers/yaml.rb
|
226
227
|
- lib/brick/util.rb
|