brick 1.0.140 → 1.0.142

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: 995f6ed970029309b465f80193521b91c63335e12400cbb21e0ef0d60fe60dd2
4
- data.tar.gz: c610df65f26a9e9bf4f081c8d0172fea6fc69cda350d26cf5287362237e7ceca
3
+ metadata.gz: 4e907aa4b41603045845a12d80de47e9ad714768929225b8bec209ddd9b92212
4
+ data.tar.gz: 227137ba8ec83040eec6dbf76c3541f6438b7427c586690f7430572cdd59eb8a
5
5
  SHA512:
6
- metadata.gz: 534bde690d45d11d8371d0e666df86badc47879242bcf67a219aeff6dc71fb1ac668b02bba5059f2fceafa63ddb6312cf3e42e993c5ba1de47ebf276abd9bb7c
7
- data.tar.gz: 18e0c976540749fbc93fd89be05735d2e9544db37d04a3738bc06e82fdc2811d9478398fa66881d17e9e18cb44e78720f66cc8db74660ddd6b5e23e523ea2db7
6
+ metadata.gz: b68b20ad2ef66293dd7bdfd329c1ea341fb535e6240128beb1d5cd0dec99503ecc95b44b1adfde24f1493e4d7edb20ea61c7a4c9a1e5f6502b95c3c512261208
7
+ data.tar.gz: 45722aa4054d96a858bab394a0e207e37d92ab415eb2a5832807e95d855e1c662139074bde9cf545d662a062fbe834ccd228d9595061c1dfde73990e6a3b3feb
data/lib/brick/config.rb CHANGED
@@ -348,7 +348,7 @@ module Brick
348
348
  end
349
349
 
350
350
  def always_load_fields
351
- @mutex.synchronize { @always_load_fields || Hash.new { |h, k| h[k] = [] } }
351
+ @mutex.synchronize { @always_load_fields || {} }
352
352
  end
353
353
 
354
354
  def always_load_fields=(field_set)
@@ -296,7 +296,7 @@ module ActiveRecord
296
296
 
297
297
  # Providing a relation object allows auto-modules built from table name prefixes to work
298
298
  def self._brick_index(mode = nil, separator = '_', relation = nil)
299
- return if abstract_class
299
+ return if abstract_class?
300
300
 
301
301
  tbl_parts = ((mode == :singular) ? table_name.singularize : table_name).split('.')
302
302
  tbl_parts.shift if ::Brick.apartment_multitenant && tbl_parts.length > 1 && tbl_parts.first == ::Brick.apartment_default_tenant
@@ -574,9 +574,14 @@ module ActiveRecord
574
574
  "'<#{typ.end_with?('_TYP') ? typ[0..-5] : typ}>' AS #{col.name}"
575
575
  end
576
576
  end
577
- else # Having some select columns chosen, add any missing always_load_fields for this model
578
- ::Brick.config.always_load_fields.fetch(klass.name, nil)&.each do |alf|
579
- selects << alf unless selects.include?(alf)
577
+ else # Having some select columns chosen, add any missing always_load_fields for this model ...
578
+ this_model = klass
579
+ loop do
580
+ ::Brick.config.always_load_fields.fetch(this_model.name, nil)&.each do |alf|
581
+ selects << alf unless selects.include?(alf)
582
+ end
583
+ # ... plus any and all STI superclasses it may inherit from
584
+ break if (this_model = this_model.superclass).abstract_class? || this_model == ActiveRecord::Base
580
585
  end
581
586
  end
582
587
 
@@ -637,7 +642,8 @@ module ActiveRecord
637
642
  col_prefix = 'br_cc_' if brick_col_names
638
643
  if (cc_part_idx = cc_part.length - 1).zero?
639
644
  col_alias = "#{col_prefix}#{k}__#{table_name.tr('.', '_')}_#{cc_part.first}"
640
- else
645
+ elsif brick_col_names ||
646
+ used_col_aliases.key?(col_alias = k.to_s) # This sets a simpler custom column name if possible
641
647
  while cc_part_idx > 0 &&
642
648
  (col_alias = "#{col_prefix}#{k}__#{cc_part[cc_part_idx..-1].map(&:to_s).join('__').tr('.', '_')}") &&
643
649
  used_col_aliases.key?(col_alias)
@@ -965,19 +971,36 @@ JOIN (SELECT #{hm_selects.map { |s| "#{'br_t0.' if from_clause}#{s}" }.join(', '
965
971
  [self.select(selects), descrip_cols]
966
972
  end
967
973
 
968
- def brick_uniq
974
+ # Accommodate when a relation gets queried for a model, and in that model it has an #after_initialize block
975
+ # which references attributes that were not originally included as part of the select_values.
976
+ def brick_(method, *args, brick_orig_relation: nil, **kwargs, &block)
969
977
  begin
970
- uniq
978
+ send(method, *args, **kwargs, &block) # method will be something like :uniq or :each
971
979
  rescue ActiveModel::MissingAttributeError => e
972
- # If this model has an #after_initialize then it might try to reference attributes we haven't brought in
973
980
  if (err_msg = e.message).start_with?('missing attribute: ') &&
974
981
  klass.column_names.include?(col_name = e.message[19..-1])
975
982
  (dup_rel = dup).select_values << col_name
976
- ret = dup_rel.brick_uniq
977
- puts "*** WARNING: Missing field!
978
- Might want to add this in your brick.rb:
979
- ::Brick.always_load_fields = { #{klass.name.inspect} => [#{col_name.inspect}]}"
980
- ::Brick.config.always_load_fields[klass.name] << col_name
983
+ ret = dup_rel.brick_(method, *args, brick_orig_relation: (brick_orig_relation ||= self), **kwargs, &block)
984
+ always_loads = (::Brick.config.always_load_fields ||= {})
985
+
986
+ # Find the most parent STI superclass for this model, and apply an always_load_fields entry for this missing column
987
+ has_field = false
988
+ this_model = klass
989
+ loop do
990
+ has_field = true if always_loads.key?(this_model.name) && always_loads[this_model.name]&.include?(col_name)
991
+ break if has_field || (next_model = this_model.superclass).abstract_class? || next_model == ActiveRecord::Base
992
+ this_model = next_model
993
+ end
994
+ unless has_field
995
+ (brick_orig_relation || self).instance_variable_set(:@brick_new_alf, ((always_loads[this_model.name] ||= []) << col_name))
996
+ end
997
+
998
+ if self.object_id == brick_orig_relation.object_id
999
+ puts "*** WARNING: Missing field#{'s' if @brick_new_alf.length > 1}!
1000
+ Might want to add this in your brick.rb:
1001
+ ::Brick.always_load_fields = { #{klass.name.inspect} => #{@brick_new_alf.inspect} }"
1002
+ remove_instance_variable(:@brick_new_alf)
1003
+ end
981
1004
  ret
982
1005
  else
983
1006
  []
@@ -658,11 +658,11 @@ window.addEventListener(\"popstate\", linkSchemas);
658
658
  else
659
659
  [[fk_name, pk.length == 1 ? pk.first : pk.inspect]]
660
660
  end
661
- if hm_assoc.options.key?(:as)
662
- poly_type = if hm_assoc.active_record.column_names.include?(hm_assoc.active_record.inheritance_column)
661
+ if hm_assoc.options.key?(:as) && !(hmaar = hm_assoc.active_record).abstract_class?
662
+ poly_type = if hmaar.column_names.include?(hmaar.inheritance_column)
663
663
  '[sti_type]'
664
664
  else
665
- hm_assoc.active_record.name
665
+ hmaar.name
666
666
  end
667
667
  # %%% Might only need hm_assoc.type and not the first part :)
668
668
  type_col = hm_assoc.inverse_of&.foreign_type || hm_assoc.type
@@ -1649,7 +1649,7 @@ end
1649
1649
  bt << (option_detail = [[\"(No #\{bt_name\} chosen)\", '^^^brick_NULL^^^']])
1650
1650
  # %%% Accommodate composite keys for obj.pk at the end here
1651
1651
  collection, descrip_cols = bt_class&.order(Arel.sql(\"#\{bt_class.table_name}.#\{obj_pk = bt_class.primary_key}\"))&.brick_list
1652
- collection&.each do |obj|
1652
+ collection&.brick_(:each) do |obj|
1653
1653
  option_detail << [
1654
1654
  obj.brick_descrip(
1655
1655
  descrip_cols&.first&.map { |col| obj.send(col.last) },
@@ -1729,7 +1729,7 @@ end
1729
1729
  else # We get an array back when AR < 4.2
1730
1730
  collection2 = collection.to_a.compact
1731
1731
  end
1732
- collection2 = collection2.brick_uniq
1732
+ collection2 = collection2.brick_(:uniq)
1733
1733
  if collection2.empty? %>
1734
1734
  <tr><td>(none)</td></tr>
1735
1735
  <% else
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 140
8
+ TINY = 142
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.140
4
+ version: 1.0.142
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorin Thwaits
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-15 00:00:00.000000000 Z
11
+ date: 2023-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord