brick 1.0.140 → 1.0.141

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: 9f2d0e5e8ee3fb56acdbdee808b2a6ebe8cbf8b43457fd10b46f6197d82ef054
4
+ data.tar.gz: f21cca0ba332706e77d7f21245aeef6aa922668fbd41020e4edecd6672fdf4e0
5
5
  SHA512:
6
- metadata.gz: 534bde690d45d11d8371d0e666df86badc47879242bcf67a219aeff6dc71fb1ac668b02bba5059f2fceafa63ddb6312cf3e42e993c5ba1de47ebf276abd9bb7c
7
- data.tar.gz: 18e0c976540749fbc93fd89be05735d2e9544db37d04a3738bc06e82fdc2811d9478398fa66881d17e9e18cb44e78720f66cc8db74660ddd6b5e23e523ea2db7
6
+ metadata.gz: b2a88167018d956d7066eaae2bb0fa05e69a1efe751b725477a6b13ad575fad7e89237fc1749138533723103f1a63018a23f6ba83cb1b6bed972e22c3bb89321
7
+ data.tar.gz: a6fc5f155686488fac49aea2eb58bc734021ea2be01fd505acbe29c357f888f65b17ebea78bae4c22ce83e4847eaef456c8a5329193753b8ac756a724d998ed1
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
  []
@@ -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 = 141
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.141
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-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord