brick 1.0.202 → 1.0.204
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/brick/extensions.rb +21 -15
- data/lib/brick/frameworks/rails/engine.rb +1 -1
- data/lib/brick/version_number.rb +1 -1
- data/lib/generators/brick/controllers_generator.rb +1 -2
- data/lib/generators/brick/migration_builder.rb +18 -13
- data/lib/generators/brick/models_generator.rb +1 -1
- data/lib/generators/brick/seeds_generator.rb +7 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9bf1dd73cdb0040884c92622e0a7dab4c36232440b5f70e74c2f9f362843373d
|
4
|
+
data.tar.gz: 19fd7bbfd6088ed1606b6c2a5ad2fa67cb9dd3b6217313bdfaa368b4fc1ca5f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb16cd17c3ad18718164242957e71353ccb5708650136a44517a13255b65d40dc47d6b579defd92d76b9b6a77c696a386880db70f6c2d1495a0f198f64866dbc
|
7
|
+
data.tar.gz: 97e22cd5f88a8008313362558ccb5ab7829772568573f0622e2eba61640d5a59ae44135d4870878f90bd750d496ed8aeef0e22badffe4168b142580911650b11
|
data/lib/brick/extensions.rb
CHANGED
@@ -126,10 +126,11 @@ module ActiveRecord
|
|
126
126
|
return @_brick_primary_key if instance_variable_defined?(:@_brick_primary_key)
|
127
127
|
|
128
128
|
pk = begin
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
129
|
+
primary_key
|
130
|
+
rescue
|
131
|
+
superclass.respond_to?(:primary_key) && superclass.primary_key
|
132
|
+
end
|
133
|
+
pk = pk.is_a?(String) ? [pk] : pk.dup || []
|
133
134
|
pk.map! { |pk_part| pk_part =~ /^[A-Z0-9_]+$/ ? pk_part.downcase : pk_part } unless connection.adapter_name == 'MySQL2'
|
134
135
|
# Just return [] if we're missing any part of the primary key. (PK is usually just "id")
|
135
136
|
if relation && pk.present?
|
@@ -1551,7 +1552,7 @@ class Object
|
|
1551
1552
|
|
1552
1553
|
private
|
1553
1554
|
|
1554
|
-
def build_model(relations, base_module, base_name, class_name, inheritable_name = nil)
|
1555
|
+
def build_model(relations, base_module, base_name, class_name, inheritable_name = nil, is_generator = nil)
|
1555
1556
|
tnp = ::Brick.config.table_name_prefixes&.find { |p| p.last == base_module.name }
|
1556
1557
|
# return [base_module, ''] if !base_module.is_a?(Class) && base_name == tnp&.last
|
1557
1558
|
|
@@ -1585,12 +1586,12 @@ class Object
|
|
1585
1586
|
# Maybe, just maybe there's a database table that will satisfy this need
|
1586
1587
|
matching = ::Brick.table_name_lookup&.fetch(class_name, nil)
|
1587
1588
|
if (matching ||= [table_name, singular_table_name, plural_class_name, model_name, table_name.titleize].find { |m| relations.key?(schema_name ? "#{schema_name}.#{m}" : m) })
|
1588
|
-
build_model_worker(schema_name, inheritable_name, model_name, singular_table_name, table_name, relations, matching)
|
1589
|
+
build_model_worker(schema_name, inheritable_name, model_name, singular_table_name, table_name, relations, matching, is_generator)
|
1589
1590
|
end
|
1590
1591
|
end
|
1591
1592
|
end
|
1592
1593
|
|
1593
|
-
def build_model_worker(schema_name, inheritable_name, model_name, singular_table_name, table_name, relations, matching)
|
1594
|
+
def build_model_worker(schema_name, inheritable_name, model_name, singular_table_name, table_name, relations, matching, is_generator = nil)
|
1594
1595
|
if ::Brick.apartment_multitenant &&
|
1595
1596
|
schema_name == ::Brick.apartment_default_tenant
|
1596
1597
|
relation = relations["#{schema_name}.#{matching}"]
|
@@ -1638,11 +1639,11 @@ class Object
|
|
1638
1639
|
hmts = nil
|
1639
1640
|
if (schema_module || Object).const_defined?((chosen_name = (inheritable_name || model_name)).to_sym)
|
1640
1641
|
possible = (schema_module || Object).const_get(chosen_name)
|
1641
|
-
return possible unless possible == schema_module
|
1642
|
+
return possible unless possible == schema_module || is_generator
|
1642
1643
|
end
|
1643
1644
|
code = +"class #{full_name} < #{base_model.name}\n"
|
1644
1645
|
built_model = Class.new(base_model) do |new_model_class|
|
1645
|
-
(schema_module || Object).const_set(chosen_name, new_model_class)
|
1646
|
+
(schema_module || Object).const_set(chosen_name, new_model_class) unless is_generator
|
1646
1647
|
@_brick_relation = relation
|
1647
1648
|
if inheritable_name
|
1648
1649
|
new_model_class.define_singleton_method :inherited do |subclass|
|
@@ -1662,7 +1663,7 @@ class Object
|
|
1662
1663
|
code << " has_secure_password\n"
|
1663
1664
|
end
|
1664
1665
|
# Accommodate singular or camel-cased table names such as "order_detail" or "OrderDetails"
|
1665
|
-
code << " self.table_name = '#{self.table_name = matching}'\n" if inheritable_name ||
|
1666
|
+
code << " self.table_name = '#{self.table_name = matching}'\n" if (inheritable_name || model_name).underscore.pluralize != matching
|
1666
1667
|
if (inh_col = ::Brick.config.sti_type_column.find { |_k, v| v.include?(matching) }&.first)
|
1667
1668
|
new_model_class.inheritance_column = inh_col
|
1668
1669
|
code << " self.inheritance_column = '#{inh_col}'\n"
|
@@ -2618,9 +2619,9 @@ class Object
|
|
2618
2619
|
|
2619
2620
|
def _brick_get_hm_assoc_name(relation, hm_assoc, source = nil)
|
2620
2621
|
assoc_name, needs_class = if (relation[:hm_counts][hm_assoc[:inverse_table]]&.> 1) &&
|
2621
|
-
hm_assoc[:alternate_name] != (source || name
|
2622
|
+
hm_assoc[:alternate_name] != (source || name&.underscore)
|
2622
2623
|
plural = "#{hm_assoc[:assoc_name]}_#{ActiveSupport::Inflector.pluralize(hm_assoc[:alternate_name])}"
|
2623
|
-
new_alt_name = (hm_assoc[:alternate_name] == name
|
2624
|
+
new_alt_name = (hm_assoc[:alternate_name] == name&.underscore) ? "#{hm_assoc[:assoc_name].singularize}_#{plural}" : plural
|
2624
2625
|
# %%% In rare cases might even need to add a number at the end for uniqueness
|
2625
2626
|
# uniq = 1
|
2626
2627
|
# while same_name = relation[:fks].find { |x| x.last[:assoc_name] == hm_assoc[:assoc_name] && x.last != hm_assoc }
|
@@ -3008,9 +3009,14 @@ module Brick
|
|
3008
3009
|
|
3009
3010
|
def _brick_index(tbl_name, mode = nil, separator = nil, relation = nil, not_path = nil)
|
3010
3011
|
separator ||= '_'
|
3011
|
-
|
3012
|
-
|
3013
|
-
|
3012
|
+
relation ||= ::Brick.relations.fetch(tbl_name, nil)
|
3013
|
+
if mode == :migration
|
3014
|
+
res_name = tbl_name
|
3015
|
+
else
|
3016
|
+
res_name = (tbl_name_parts = tbl_name.split('.'))[0..-2].first
|
3017
|
+
res_name << '.' if res_name
|
3018
|
+
(res_name ||= +'') << relation&.fetch(:resource, nil) || tbl_name_parts.last
|
3019
|
+
end
|
3014
3020
|
|
3015
3021
|
res_parts = ((mode == :singular) ? res_name.singularize : res_name).split('.')
|
3016
3022
|
res_parts.shift if ::Brick.apartment_multitenant && res_parts.length > 1 && res_parts.first == ::Brick.apartment_default_tenant
|
@@ -627,7 +627,7 @@ window.addEventListener(\"popstate\", linkSchemas);
|
|
627
627
|
alias :_brick_lookup_context :lookup_context
|
628
628
|
def lookup_context(*args)
|
629
629
|
ret = _brick_lookup_context(*args)
|
630
|
-
@_lookup_context.instance_variable_set(:@_brick_req_params, params) if
|
630
|
+
@_lookup_context.instance_variable_set(:@_brick_req_params, params) if self.class < AbstractController::Base && params
|
631
631
|
ret
|
632
632
|
end
|
633
633
|
end
|
data/lib/brick/version_number.rb
CHANGED
@@ -34,7 +34,6 @@ module Brick
|
|
34
34
|
|
35
35
|
tbl_parts = rel.first.split('.')
|
36
36
|
tbl_parts.shift if [::Brick.default_schema, 'public'].include?(tbl_parts.first)
|
37
|
-
tbl_parts[-1] = tbl_parts[-1].pluralize
|
38
37
|
begin
|
39
38
|
s << ControllerOption.new(tbl_parts.join('/').camelize, rel.last[:class_name].constantize)
|
40
39
|
rescue
|
@@ -64,7 +63,7 @@ module Brick
|
|
64
63
|
else
|
65
64
|
Object
|
66
65
|
end
|
67
|
-
controller_parts[controller_parts.length - 1] = (controller_name = "#{controller_parts.last
|
66
|
+
controller_parts[controller_parts.length - 1] = (controller_name = "#{controller_parts.last}Controller")
|
68
67
|
_built_controller, code = Object.send(:build_controller, namespace, controller_name, controller_name, controller_option.model, relations)
|
69
68
|
path = ['controllers']
|
70
69
|
path.concat(controller_parts.map(&:underscore))
|
@@ -110,7 +110,7 @@ module Brick
|
|
110
110
|
end
|
111
111
|
end
|
112
112
|
# Start the timestamps back the same number of minutes from now as expected number of migrations to create
|
113
|
-
current_mig_time = Time.now - (schemas.length + chosen.length).minutes
|
113
|
+
current_mig_time = [Time.now - (schemas.length + chosen.length).minutes]
|
114
114
|
done = []
|
115
115
|
fks = {}
|
116
116
|
stuck = {}
|
@@ -139,12 +139,14 @@ module Brick
|
|
139
139
|
# puts snag_fks.inspect
|
140
140
|
stuck[tbl] = snags
|
141
141
|
end
|
142
|
-
end
|
142
|
+
end
|
143
|
+
).present?
|
143
144
|
fringe.each do |tbl|
|
144
145
|
mig = gen_migration_columns(relations, tbl, (tbl_parts = tbl.split('.')), (add_fks = []), built_schemas, mig_path, current_mig_time,
|
145
|
-
key_type, is_4x_rails, ar_version, do_fks_last)
|
146
|
+
key_type, is_4x_rails, ar_version, do_fks_last, versions_to_create)
|
146
147
|
after_fks.concat(add_fks) if do_fks_last
|
147
|
-
|
148
|
+
current_mig_time[0] += 1.minute
|
149
|
+
versions_to_create << migration_file_write(mig_path, "create_#{::Brick._brick_index(tbl, nil, 'x')}", current_mig_time, ar_version, mig)
|
148
150
|
end
|
149
151
|
done.concat(fringe)
|
150
152
|
chosen -= done
|
@@ -154,9 +156,10 @@ module Brick
|
|
154
156
|
# Write out any more tables that haven't been done yet
|
155
157
|
chosen.each do |tbl|
|
156
158
|
mig = gen_migration_columns(relations, tbl, (tbl_parts = tbl.split('.')), (add_fks = []), built_schemas, mig_path, current_mig_time,
|
157
|
-
key_type, is_4x_rails, ar_version, do_fks_last)
|
159
|
+
key_type, is_4x_rails, ar_version, do_fks_last, versions_to_create)
|
158
160
|
after_fks.concat(add_fks)
|
159
|
-
|
161
|
+
current_mig_time[0] += 1.minute
|
162
|
+
versions_to_create << migration_file_write(mig_path, "create_#{::Brick._brick_index(tbl, :migration, 'x')}", current_mig_time, ar_version, mig)
|
160
163
|
end
|
161
164
|
done.concat(chosen)
|
162
165
|
chosen.clear
|
@@ -188,7 +191,8 @@ module Brick
|
|
188
191
|
end\n"
|
189
192
|
end
|
190
193
|
mig << +" end\n"
|
191
|
-
|
194
|
+
current_mig_time[0] += 1.minute
|
195
|
+
versions_to_create << migration_file_write(mig_path, 'create_brick_fks.rbx', current_mig_time, ar_version, mig)
|
192
196
|
puts "Have written out a final migration called 'create_brick_fks.rbx' which creates #{after_fks.length} foreign keys.
|
193
197
|
This file extension (.rbx) will cause it not to run yet when you do a 'rails db:migrate'.
|
194
198
|
The idea here is to do all data loading first, and then rename that migration file back
|
@@ -255,7 +259,7 @@ module Brick
|
|
255
259
|
private
|
256
260
|
|
257
261
|
def gen_migration_columns(relations, tbl, tbl_parts, add_fks, built_schemas, mig_path, current_mig_time,
|
258
|
-
key_type, is_4x_rails, ar_version, do_fks_last)
|
262
|
+
key_type, is_4x_rails, ar_version, do_fks_last, versions_to_create)
|
259
263
|
return unless (relation = relations.fetch(tbl, nil))&.fetch(:cols, nil)&.present?
|
260
264
|
|
261
265
|
mig = +''
|
@@ -278,7 +282,8 @@ module Brick
|
|
278
282
|
end
|
279
283
|
unless schema.blank? || built_schemas.key?(schema)
|
280
284
|
mig = +" def change\n create_schema(:#{schema}) unless schema_exists?(:#{schema})\n end\n"
|
281
|
-
|
285
|
+
current_mig_time[0] += 1.minute
|
286
|
+
versions_to_create << migration_file_write(mig_path, "create_db_schema_#{schema.underscore}", current_mig_time, ar_version, mig)
|
282
287
|
built_schemas[schema] = nil
|
283
288
|
end
|
284
289
|
|
@@ -389,11 +394,11 @@ module Brick
|
|
389
394
|
end
|
390
395
|
end
|
391
396
|
if possible_ts.length == 2 && # Both created_at and updated_at
|
392
|
-
|
393
|
-
|
397
|
+
# Rails 5 and later timestamps default to NOT NULL
|
398
|
+
(possible_ts.first.last == is_4x_rails && possible_ts.last.last == is_4x_rails)
|
394
399
|
mig << "\n t.timestamps\n"
|
395
400
|
else # Just one or the other, or a nullability mismatch
|
396
|
-
possible_ts.each { |ts| emit_column('timestamp', ts.first, nil) }
|
401
|
+
possible_ts.each { |ts| mig << emit_column('timestamp', ts.first, nil) }
|
397
402
|
end
|
398
403
|
mig << " end\n"
|
399
404
|
if pk_is_also_fk
|
@@ -424,7 +429,7 @@ module Brick
|
|
424
429
|
end
|
425
430
|
|
426
431
|
def migration_file_write(mig_path, name, current_mig_time, ar_version, mig)
|
427
|
-
File.open("#{mig_path}/#{version = current_mig_time.strftime('%Y%m%d%H%M00')}_#{name}#{'.rb' unless name.index('.')}", "w") do |f|
|
432
|
+
File.open("#{mig_path}/#{version = current_mig_time.first.strftime('%Y%m%d%H%M00')}_#{name}#{'.rb' unless name.index('.')}", "w") do |f|
|
428
433
|
f.write "class #{name.split('.').first.camelize} < ActiveRecord::Migration#{ar_version}\n"
|
429
434
|
f.write mig
|
430
435
|
f.write "end\n"
|
@@ -62,7 +62,7 @@ module Brick
|
|
62
62
|
else
|
63
63
|
Object
|
64
64
|
end
|
65
|
-
_built_model, code = Object.send(:build_model, relations, base_module, base_module.name, model_parts.last)
|
65
|
+
_built_model, code = Object.send(:build_model, relations, base_module, base_module.name, model_parts.last, nil, true)
|
66
66
|
path = ['models']
|
67
67
|
path.concat(model_parts.map(&:underscore))
|
68
68
|
dir = +"#{::Rails.root}/app"
|
@@ -27,7 +27,7 @@ module Brick
|
|
27
27
|
# Generate a list of viable models that can be chosen
|
28
28
|
# First start with any existing models that have been defined ...
|
29
29
|
existing_models = ActiveRecord::Base.descendants.each_with_object({}) do |m, s|
|
30
|
-
s[m.table_name] = SeedModel.new(m.table_name, m, false) if !m.abstract_class? && m.table_exists?
|
30
|
+
s[m.table_name] = SeedModel.new(m.table_name, m, false) if !m.abstract_class? && !m.is_view? && m.table_exists?
|
31
31
|
end
|
32
32
|
models = (existing_models.values +
|
33
33
|
# ... then add models which can be auto-built by Brick
|
@@ -123,12 +123,15 @@ module Brick
|
|
123
123
|
pk_val = obj.send(pkey_cols.first)
|
124
124
|
fk_vals = []
|
125
125
|
data = []
|
126
|
-
relation[:cols].each do |col,
|
126
|
+
relation[:cols].each do |col, _col_type|
|
127
127
|
next if !(fk = fkeys.find { |assoc| col == assoc[:fk] }) &&
|
128
128
|
pkey_cols.include?(col)
|
129
129
|
|
130
|
-
|
131
|
-
val = val.
|
130
|
+
begin
|
131
|
+
if (val = obj.send(col)) && (val.is_a?(Time) || val.is_a?(Date))
|
132
|
+
val = val.to_s
|
133
|
+
end
|
134
|
+
rescue StandardError => e # ActiveRecord::Encryption::Errors::Configuration
|
132
135
|
end
|
133
136
|
if fk
|
134
137
|
inv_tbl = fk[:inverse_table].gsub('.', '__')
|
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.204
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lorin Thwaits
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-02-
|
11
|
+
date: 2024-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -285,7 +285,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
285
285
|
- !ruby/object:Gem::Version
|
286
286
|
version: 1.3.6
|
287
287
|
requirements: []
|
288
|
-
rubygems_version: 3.
|
288
|
+
rubygems_version: 3.2.33
|
289
289
|
signing_key:
|
290
290
|
specification_version: 4
|
291
291
|
summary: Create a Rails app from data alone
|