brick 1.0.200 → 1.0.201

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: 261f446a557861aebb3e8f2ce1451895f7034c903dd0713bc5bef7f7506765a2
4
- data.tar.gz: e123ca1f0a0ee229d3e2e7baabea6bd76ed76848aa226753a97e4fb69b32ca74
3
+ metadata.gz: 52b363a56451567ff44ebee4510f850eb6f1df3d09bf05392c75a3c0444daa73
4
+ data.tar.gz: bd39b40ba7fbeae5f9225ba8a9591f53082f60ee6f89f68b9bc83199656c75fd
5
5
  SHA512:
6
- metadata.gz: df36018bfc5eaafd092546c165a7f829c4e7e00b1f52729e65812e8a6721a8d5e81a1b3d421c6ab23010d4c86be8af628874c80725e293d7e0646554decdbea7
7
- data.tar.gz: 9e3b7c34512805c3afc7c71fb8a71d5a02da2514a4f24b70c45f618fa567a7840b8afbe358af706bffcd1e277da2dda24df884c5dc4ada20a0f2007da04eff8a
6
+ metadata.gz: e095c942770c458142026d918aa7084853d23963c0802dbda7a7773b504cc2a6a751369cbf58214453aa83e004230b8bab9ff7c2d1040bffc9f28a5b371c894e
7
+ data.tar.gz: 4d15b729fea0b416cf75fc18a6ce11c28eea989438d831de94710d8878ee9cb2174df7a5075028922691acceea9af2322591f4219b8dbca16b7a25716c55ee09
@@ -334,10 +334,10 @@ module ActiveRecord
334
334
  end
335
335
 
336
336
  # Providing a relation object allows auto-modules built from table name prefixes to work
337
- def self._brick_index(mode = nil, separator = nil, relation = nil)
337
+ def self._brick_index(mode = nil, separator = nil, relation = nil, not_path = nil)
338
338
  return if abstract_class?
339
339
 
340
- ::Brick._brick_index(table_name, mode, separator, relation)
340
+ ::Brick._brick_index(table_name, mode, separator, relation, not_path)
341
341
  end
342
342
 
343
343
  def self.brick_import_template
@@ -1345,7 +1345,7 @@ end
1345
1345
  ((filename = ActiveSupport::Dependencies.search_for_file(desired_classname.underscore) ||
1346
1346
  (self != Object && ActiveSupport::Dependencies.search_for_file((desired_classname = requested).underscore))
1347
1347
  ) && (require_dependency(filename) || true) &&
1348
- ((possible = self.const_get(args.first)) && possible.name == desired_classname)
1348
+ (!anonymous? && (possible = self.const_get(args.first)) && possible.name == desired_classname)
1349
1349
  ) ||
1350
1350
 
1351
1351
  # If any class has turned up so far (and we're not in the middle of eager loading)
@@ -1366,13 +1366,14 @@ end
1366
1366
  end
1367
1367
  end
1368
1368
  class_name = ::Brick.namify(requested)
1369
+ is_avo_present = Object.const_defined?('Avo') && ::Avo.respond_to?(:railtie_namespace)
1369
1370
  # CONTROLLER
1370
1371
  result = if ::Brick.enable_controllers? &&
1371
1372
  is_controller && (plural_class_name = class_name[0..-11]).length.positive?
1372
1373
  # Otherwise now it's up to us to fill in the gaps
1373
1374
  controller_class_name = +''
1374
1375
  full_class_name = +''
1375
- unless self == Object
1376
+ unless self == Object || (is_avo_present && self.name == 'Avo')
1376
1377
  controller_class_name << ((split_self_name&.first && split_self_name.join('::')) || self.name)
1377
1378
  full_class_name << "::#{controller_class_name}"
1378
1379
  controller_class_name << '::'
@@ -1432,58 +1433,43 @@ end
1432
1433
  base_module.const_set(class_name.to_sym, (built_module = Module.new))
1433
1434
  [built_module, "module #{possible_module}; end\n"]
1434
1435
 
1435
- # AVO Resource
1436
- elsif base_module == Object && Object.const_defined?('Avo') && ::Avo.respond_to?(:railtie_namespace) && requested.end_with?('Resource') &&
1436
+ # AVO 2.x Resource
1437
+ elsif base_module == Object && is_avo_present && requested.end_with?('Resource') &&
1437
1438
  # Expect that anything called MotorResource or SpinaResource could be from those administrative gems
1438
- requested.length > 8 && ['MotorResource', 'SpinaResource'].exclude?(requested)
1439
- if (model = Object.const_get(requested[0..-9])) && model < ActiveRecord::Base
1440
- require 'generators/avo/resource_generator'
1441
- field_generator = Generators::Avo::ResourceGenerator.new([''])
1442
- field_generator.instance_variable_set(:@model, model)
1443
- fields = field_generator.send(:generate_fields)&.split("\n")
1444
- &.each_with_object([]) do |f, s|
1445
- if (f = f.strip).start_with?('field ')
1446
- f = f[6..-1].split(',')
1447
- s << [f.first[1..-1].to_sym, [f[1][1..-1].split(': :').map(&:to_sym)].to_h]
1448
- end
1449
- end || []
1450
- built_resource = Class.new(Avo::BaseResource) do |new_resource_class|
1451
- self.model_class = model
1452
- self.title = :brick_descrip
1453
- self.includes = []
1454
- if (!model.is_view? && mod_pk = model.primary_key)
1455
- field((mod_pk.is_a?(Array) ? mod_pk.first : mod_pk).to_sym, { as: :id })
1456
- end
1457
- # Create a call such as: field :name, as: :text
1458
- fields.each do |f|
1459
- # Add proper types if this is a polymorphic belongs_to
1460
- if f.last == { as: :belongs_to } &&
1461
- (fk = ::Brick.relations[model.table_name][:fks].find { |k, v| v[:assoc_name] == f.first.to_s }) &&
1462
- fk.last.fetch(:polymorphic, nil)
1463
- poly_types = fk.last.fetch(:inverse_table, nil)&.each_with_object([]) do |poly_table, s|
1464
- s << Object.const_get(::Brick.relations[poly_table][:class_name])
1465
- end
1466
- if poly_types.present?
1467
- f.last[:polymorphic_as] = f.first
1468
- f.last[:types] = poly_types
1469
- end
1470
- end
1471
- self.send(:field, *f)
1472
- end
1473
- end
1474
- Object.const_set(requested.to_sym, built_resource)
1475
- [built_resource, nil]
1439
+ requested.length > 8 && ['MotorResource', 'SpinaResource'].exclude?(requested) &&
1440
+ (model = Object.const_get(requested[0..-9])) && model < ActiveRecord::Base
1441
+ built_resource = Class.new(Avo::BaseResource) do
1442
+ self.model_class = model
1443
+ self.title = :brick_descrip
1444
+ self.includes = []
1445
+ ::Brick::ADD_AVO_FIELDS.call(self, model)
1476
1446
  end
1447
+ base_module.const_set(requested.to_sym, built_resource)
1448
+ [built_resource, nil]
1449
+
1450
+ # AVO 3.x Resource
1451
+ elsif is_avo_present && self.name == 'Avo::Resources' &&
1452
+ (model = begin
1453
+ (model = Object.const_get(requested)) && model < ActiveRecord::Base
1454
+ model
1455
+ rescue
1456
+ end)
1457
+ [::Brick.avo_3x_resource(model, requested), nil]
1477
1458
 
1478
1459
  # MODEL
1479
1460
  elsif ::Brick.enable_models?
1461
+ # Avo sometimes tries to find a model class inside of the Avo namespace
1462
+ if is_avo_present && self.name == 'Avo'
1463
+ name = (base_module = Object).name
1464
+ end
1465
+ name ||= base_module.name
1480
1466
  # Custom inheritable Brick base model?
1481
1467
  class_name = (inheritable_name = class_name)[5..-1] if class_name.start_with?('Brick')
1482
1468
  Object.send(:build_model, relations, base_module, name, class_name, inheritable_name)
1483
1469
  end
1484
1470
  if result
1485
1471
  built_class, code = result
1486
- puts "\n#{code}\n"
1472
+ puts "\n#{code}\n" if code
1487
1473
  built_class
1488
1474
  elsif !schema_name && ::Brick.config.sti_namespace_prefixes&.key?("::#{class_name}")
1489
1475
  # module_prefixes = type_name.split('::')
@@ -1512,6 +1498,54 @@ end
1512
1498
  end
1513
1499
  end
1514
1500
 
1501
+ module Brick
1502
+ def self.avo_3x_resource(model, requested)
1503
+ built_resource = Class.new(Avo::BaseResource) do
1504
+ self.model_class = model
1505
+ self.title = :brick_descrip
1506
+ self.includes = []
1507
+ define_method :fields do # Have to be inside of a fields method
1508
+ ::Brick::ADD_AVO_FIELDS.call(self, model)
1509
+ end
1510
+ end
1511
+ ::Avo::BaseResource.const_set(requested.to_sym, built_resource)
1512
+ built_resource
1513
+ end
1514
+ end
1515
+
1516
+ ::Brick::ADD_AVO_FIELDS = lambda do |obj, model|
1517
+ require 'generators/avo/resource_generator'
1518
+ field_generator = Generators::Avo::ResourceGenerator.new([''])
1519
+ field_generator.instance_variable_set(:@model, model)
1520
+ flds = field_generator.send(:generate_fields)&.split("\n")
1521
+ &.each_with_object([]) do |f, s|
1522
+ if (f = f.strip).start_with?('field ')
1523
+ f = f[6..-1].split(',')
1524
+ s << [f.first[1..-1].to_sym, [f[1][1..-1].split(': :').map(&:to_sym)].to_h]
1525
+ end
1526
+ end || []
1527
+ if (!model.is_view? && mod_pk = model.primary_key)
1528
+ obj.field((mod_pk.is_a?(Array) ? mod_pk.first : mod_pk).to_sym, **{ as: :id })
1529
+ end
1530
+ # Create a call such as: field :name, as: :text
1531
+ flds.each do |f|
1532
+ # Add proper types if this is a polymorphic belongs_to
1533
+ if f.last == { as: :belongs_to } &&
1534
+ (fk = ::Brick.relations[model.table_name][:fks].find { |k, v| v[:assoc_name] == f.first.to_s }) &&
1535
+ fk.last.fetch(:polymorphic, nil)
1536
+ poly_types = fk.last.fetch(:inverse_table, nil)&.each_with_object([]) do |poly_table, s|
1537
+ s << Object.const_get(::Brick.relations[poly_table][:class_name])
1538
+ end
1539
+ if poly_types.present?
1540
+ f.last[:polymorphic_as] = f.first
1541
+ f.last[:types] = poly_types
1542
+ end
1543
+ end
1544
+ kwargs = f.last.is_a?(Hash) ? f.pop : {}
1545
+ obj.send(:field, *f, **kwargs)
1546
+ end
1547
+ end
1548
+
1515
1549
  class Object
1516
1550
  class << self
1517
1551
 
@@ -2602,8 +2636,9 @@ class Object
2602
2636
  assoc_parts[0].downcase! if assoc_parts[0] =~ /^[A-Z0-9_]+$/
2603
2637
  assoc_name = assoc_parts.join('.')
2604
2638
  else
2605
- class_name_parts = ::Brick.namify(hm_assoc[:inverse_table], :underscore).split('.')
2606
- needs_class = assoc_name.singularize.camelize != class_name_parts.last.singularize.camelize
2639
+ last_class_name_part = ::Brick.relations[hm_assoc[:inverse_table]].fetch(:class_name, nil)&.split('::')&.last ||
2640
+ ::Brick.namify(hm_assoc[:inverse_table], :underscore).split('.').last.singularize.camelize
2641
+ needs_class = assoc_name.singularize.camelize != last_class_name_part
2607
2642
  end
2608
2643
  [assoc_name, needs_class]
2609
2644
  end
@@ -2672,6 +2707,9 @@ else
2672
2707
  rescue ActiveRecord::NoDatabaseError
2673
2708
  # ::Brick.is_db_present = false
2674
2709
  end
2710
+ if Object.const_defined?('Avo') && ::Avo.respond_to?(:railtie_namespace)
2711
+ Module.class_exec &::Brick::ADD_CONST_MISSING
2712
+ end
2675
2713
  conn
2676
2714
  end
2677
2715
  end
@@ -296,13 +296,13 @@ function linkSchemas() {
296
296
  end
297
297
 
298
298
  module UrlHelpers
299
- alias _brick_resource_path resource_path
299
+ alias _brick_resources_path resources_path
300
300
  # Accommodate STI resources
301
- def resource_path(model:, resource:, **args)
302
- resource ||= if (klass = model&.class)
301
+ def resources_path(resource:, **kwargs)
302
+ resource ||= if (klass = resource.model_class)
303
303
  Avo::App.resources.find { |r| r.model_class > klass }
304
304
  end
305
- _brick_resource_path(model: model, resource: resource, **args)
305
+ _brick_resources_path(resource: resource, **kwargs)
306
306
  end
307
307
  end
308
308
 
@@ -329,31 +329,50 @@ function linkSchemas() {
329
329
  # end
330
330
  # end
331
331
 
332
- (self::App.respond_to?(:eager_load) ? App : DynamicRouter).class_exec do
333
- class << self
334
- alias _brick_eager_load eager_load
335
- def eager_load(entity)
336
- _brick_eager_load(entity)
337
- if entity == :resources
338
- possible_schema, _x1, _x2 = ::Brick.get_possible_schemas
339
- if possible_schema
340
- orig_tenant = Apartment::Tenant.current
341
- Apartment::Tenant.switch!(possible_schema)
342
- end
343
- existing = Avo::BaseResource.descendants.each_with_object({}) do |r, s|
344
- s[r.name[0..-9]] = nil if r.name.end_with?('Resource')
345
- end
346
- ::Brick.relations.each do |k, v|
347
- unless k.is_a?(Symbol) || existing.key?(class_name = v[:class_name]) || Brick.config.exclude_tables.include?(k) ||
348
- class_name.blank? || class_name.include?('::') ||
349
- ['ActiveAdminComment', 'MotorAlert', 'MotorAlertLock', 'MotorApiConfig', 'MotorAudit', 'MotorConfig', 'MotorDashboard', 'MotorForm', 'MotorNote', 'MotorNoteTag', 'MotorNoteTagTag', 'MotorNotification', 'MotorQuery', 'MotorReminder', 'MotorResource', 'MotorTag', 'MotorTaggableTag'].include?(class_name)
350
- Object.const_get("#{class_name}Resource")
351
- end
352
- end
353
- Apartment::Tenant.switch!(orig_tenant) if orig_tenant
332
+ if self.const_defined?('Resources') &&
333
+ self::Resources.const_defined?('ResourceManager') # Avo 3.x?
334
+ self::Resources::ResourceManager.class_exec do
335
+ class << self
336
+ alias _brick_fetch_resources fetch_resources
337
+ def fetch_resources
338
+ Avo._brick_avo_resources
339
+ _brick_fetch_resources
340
+ end
341
+ end
342
+ end
343
+ else # Avo 2.x
344
+ (self::App.respond_to?(:eager_load) ? App : DynamicRouter).class_exec do
345
+ class << self
346
+ alias _brick_eager_load eager_load
347
+ def eager_load(entity)
348
+ Avo._brick_avo_resources(true) if entity == :resources
349
+ _brick_eager_load(entity)
350
+ end
351
+ end
352
+ end
353
+ end
354
+
355
+ def self._brick_avo_resources(is_2x = nil)
356
+ possible_schema, _x1, _x2 = ::Brick.get_possible_schemas
357
+ if possible_schema
358
+ orig_tenant = Apartment::Tenant.current
359
+ Apartment::Tenant.switch!(possible_schema)
360
+ end
361
+ existing = Avo::BaseResource.descendants.each_with_object({}) do |r, s|
362
+ s[r.name[0..-9]] = nil if r.name.end_with?('Resource')
363
+ end
364
+ ::Brick.relations.each do |k, v|
365
+ unless k.is_a?(Symbol) || existing.key?(class_name = v[:class_name]) || Brick.config.exclude_tables.include?(k) ||
366
+ class_name.blank? || class_name.include?('::') ||
367
+ ['ActiveAdminComment', 'MotorAlert', 'MotorAlertLock', 'MotorApiConfig', 'MotorAudit', 'MotorConfig', 'MotorDashboard', 'MotorForm', 'MotorNote', 'MotorNoteTag', 'MotorNoteTagTag', 'MotorNotification', 'MotorQuery', 'MotorReminder', 'MotorResource', 'MotorTag', 'MotorTaggableTag'].include?(class_name)
368
+ if is_2x # Avo 2.x?
369
+ "::#{class_name}Resource".constantize
370
+ else # Avo 3.x
371
+ ::Brick.avo_3x_resource(Object.const_get(class_name), class_name) unless ::Avo::BaseResource.constants.include?(class_name.to_sym)
354
372
  end
355
373
  end
356
374
  end
375
+ Apartment::Tenant.switch!(orig_tenant) if orig_tenant
357
376
  end
358
377
 
359
378
  # Add our schema link Javascript code when the TurboFrameWrapper is rendered so it ends up on all index / show / etc
@@ -409,18 +428,21 @@ window.addEventListener(\"popstate\", linkSchemas);
409
428
  class Fields::IndexComponent
410
429
  alias _brick_resource_view_path resource_view_path
411
430
  def resource_view_path
412
- return if @resource.model&.class&.is_view?
431
+ mdl_class = @resource.respond_to?(:model_class) ? @resource.model_class : @resource.model&.class
432
+ return if mdl_class&.is_view?
413
433
 
414
434
  _brick_resource_view_path
415
435
  end
416
436
  end
417
437
 
418
438
  module Concerns::HasFields
419
- class_methods do
420
- alias _brick_field field
421
- def field(name, *args, **kwargs, &block)
422
- kwargs.merge!(args.pop) if args.last.is_a?(Hash)
423
- _brick_field(name, **kwargs, &block)
439
+ class << self
440
+ if respond_to?(:field)
441
+ alias _brick_field field
442
+ def field(name, *args, **kwargs, &block)
443
+ kwargs.merge!(args.pop) if args.last.is_a?(Hash)
444
+ _brick_field(name, **kwargs, &block)
445
+ end
424
446
  end
425
447
  end
426
448
  end
@@ -592,11 +592,11 @@ function onImagesLoaded(event) {
592
592
  relation = ::Brick.relations.fetch(rel_name || klass.table_name, nil)
593
593
  if (klass_or_obj&.is_a?(Class) && klass_or_obj < ActiveRecord::Base) ||
594
594
  (klass_or_obj&.is_a?(ActiveRecord::Base) && klass_or_obj.new_record? && (klass_or_obj = klass_or_obj.class))
595
- path = (proc = kwargs[:index_proc]) ? proc.call(klass_or_obj, relation) : "#{app_routes.path_for(controller: klass_or_obj.base_class._brick_index(nil, '/', relation), action: :index)}#{filter}"
595
+ path = (proc = kwargs[:index_proc]) ? proc.call(klass_or_obj, relation) : "#{app_routes.path_for(controller: klass_or_obj.base_class._brick_index(nil, '/', relation, true), action: :index)}#{filter}"
596
596
  lt_args = [text || "Index for #{klass_or_obj.name.pluralize}", path]
597
597
  else
598
598
  # If there are multiple incoming parameters then last one is probably the actual ID, and first few might be some nested tree of stuff leading up to it
599
- path = (proc = kwargs[:show_proc]) ? proc.call(klass_or_obj, relation) : "#{app_routes.path_for(controller: klass_or_obj.class.base_class._brick_index(nil, '/', relation), action: :show, id: klass_or_obj)}#{filter}"
599
+ path = (proc = kwargs[:show_proc]) ? proc.call(klass_or_obj, relation) : "#{app_routes.path_for(controller: klass_or_obj.class.base_class._brick_index(nil, '/', relation, true), action: :show, id: klass_or_obj)}#{filter}"
600
600
  lt_args = [text || "Show this #{klass_or_obj.class.name}", path]
601
601
  end
602
602
  kwargs.delete(:visited)
@@ -462,7 +462,7 @@ ORDER BY 1, 2, c.internal_column_id, acc.position"
462
462
  v[:resource] = proposed_name_parts.last.underscore
463
463
  if colliding_thing
464
464
  message_start = if colliding_thing.is_a?(Module) && Object.const_defined?(:Rails) &&
465
- colliding_thing.constants.find { |c| colliding_thing.const_get(c) < Rails::Application }
465
+ colliding_thing.constants.find { |c| colliding_thing.const_get(c) < ::Rails::Application }
466
466
  "The module for the Rails application itself, \"#{colliding_thing.name}\","
467
467
  else
468
468
  "Non-AR #{colliding_thing.class.name.downcase} \"#{colliding_thing.name}\""
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 200
8
+ TINY = 201
9
9
 
10
10
  # PRE is nil unless it's a pre-release (beta, RC, etc.)
11
11
  PRE = nil
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.200
4
+ version: 1.0.201
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-01-26 00:00:00.000000000 Z
11
+ date: 2024-02-03 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.1.6
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