brick 1.0.208 → 1.0.210

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b119de0df1fa94e7c3a07315295c2d5fcf2871ec01e44986db6ddee784b82cf3
4
- data.tar.gz: ba567e471502273780baeb78ed456e3b81319ce63560ccd7eaaf7ec15cb0ea21
3
+ metadata.gz: 803d4790408a0b789ff8086b5b81346581071b260cbc528039fd8cfa8894b7ac
4
+ data.tar.gz: 4503e1b9feba62d33e83f3fadc25a34b70bf9c661dc750187a3c391e3b24d5e8
5
5
  SHA512:
6
- metadata.gz: 6f0e173346e205727f94e1e33d182f2a783cc20d3496a8acb7592da9c931a1c4a9e6f32da540d78647f75b00b504df03845e17816cfd589a6dc69d73a81f56b5
7
- data.tar.gz: 02b36babcf6958b35cf38ad8caf2147fffaf5a438f2674a541f46f0b9813ec46df05a5fe1fafcbaba96ad63a654db429d4dc245d1a1cb688b1e20070b197a374
6
+ metadata.gz: 1fe28c2a9ca30ef663f28fe79e65e1b2787686c6fce6d5738d9b6addd6070437411d5210e1ba641b28eae5215fb37756f3faa80f246d823d5d553c9db990a427
7
+ data.tar.gz: daa9bb88c21467075717062c1ede0ab38af4207bf96d9a2495ce84f99850a4d0cb09f4c1c95017cb3862ef915fab85036397375d43518f7bee2d7bd6f1255aed
@@ -72,7 +72,7 @@ module ActiveRecord
72
72
 
73
73
  def real_model(params)
74
74
  if params && (sub_model = params.fetch(type_col = inheritance_column, nil))
75
- sub_model = sub_model.first if sub_model.is_a?(Array) # Support the params style that gets returned from #brick_select
75
+ sub_model = sub_model.first if sub_model.is_a?(Array) # Support the params style that gets returned from #_brick_querying
76
76
  # Make sure the chosen model is really the same or a subclass of this model
77
77
  (possible_model = sub_model.constantize) <= self ? possible_model : self
78
78
  else
@@ -491,17 +491,12 @@ module ActiveRecord
491
491
  [order_by, order_by_txt]
492
492
  end
493
493
 
494
- def self.brick_select(*args, params: {}, brick_col_names: false, **kwargs)
495
- selects = if args[0].is_a?(Array)
496
- other_args = args[1..-1]
497
- args[0]
498
- else
499
- other_args = []
500
- args
501
- end
502
- (relation = all).brick_select(selects, *other_args,
503
- params: params, brick_col_names: brick_col_names, **kwargs)
504
- relation.select(selects)
494
+ def self.brick_select(*args, **kwargs)
495
+ all.brick_select(*args, **kwargs)
496
+ end
497
+
498
+ def self.brick_pluck(*args, withhold_ids: true, **kwargs)
499
+ all.brick_pluck(*args, withhold_ids: withhold_ids, **kwargs)
505
500
  end
506
501
 
507
502
  def self.brick_where(*args)
@@ -542,33 +537,47 @@ module ActiveRecord
542
537
 
543
538
  # Links from ActiveRecord association pathing names over to the real table
544
539
  # correlation names that get chosen when the AREL AST tree is walked.
545
- def brick_links
540
+ def brick_links(do_dup = true)
546
541
  # Touching AREL AST walks the JoinDependency tree, and in that process uses our
547
542
  # "brick_links" patch to find how every AR chain of association names relates to exact
548
- # table correlation names chosen by AREL. We use a duplicate relation object for this
549
- # because an important side-effect of referencing the AST is that the @arel instance
550
- # variable gets set, and this is a signal to ActiveRecord that a relation has now
551
- # become immutable. (We aren't quite ready for our "real deal" relation object to be
552
- # set in stone ... still need to add .select(), and possibly .where() and .order()
553
- # things ... also if there are any HM counts then an OUTER JOIN for each of them out
554
- # to a derived table to do that counting. All of these things need to know proper
555
- # table correlation names, which will now become available from brick_links on the
556
- # rel_dupe object.)
543
+ # table correlation names chosen by AREL. Unless a relation has already had its AST
544
+ # tree built out, we will use a duplicate relation object for this, because an important
545
+ # side-effect of referencing the AST is that the @arel instance variable gets set. This
546
+ # is a signal to ActiveRecord that a relation has now become immutable. (When Brick is
547
+ # still in the middle of calculating its query, we aren't quite ready for the relation
548
+ # object to be set in stone ... still need to add .select(), and possibly .where() and
549
+ # .order() things ... also if there are any HM counts then an OUTER JOIN for each of
550
+ # them out to a derived table to do that counting. All of these things need to know
551
+ # proper table correlation names, which will now become available from brick_links on
552
+ # the rel_dupe object.)
557
553
  @_brick_links ||= begin
558
554
  # If it's a CollectionProxy (which inherits from Relation) then need to dig
559
555
  # out the core Relation object which is found in the association scope.
560
- rel_dupe = (is_a?(ActiveRecord::Associations::CollectionProxy) ? scope : self).dup
556
+ brick_rel = is_a?(ActiveRecord::Associations::CollectionProxy) ? scope : self
557
+ brick_rel = (@_brick_rel_dup ||= brick_rel.dup) if do_dup
561
558
  # Start out with a hash that has only the root table name
562
- rel_dupe.instance_variable_set(:@_brick_links, bl = { '' => table_name })
563
- rel_dupe.arel.ast # Walk the AST tree in order to capture all the other correlation names
559
+ brick_rel.instance_variable_set(:@_brick_links, bl = { '' => table_name })
560
+ brick_rel.arel.ast if do_dup # Walk the AST tree in order to capture all the other correlation names
564
561
  bl
565
562
  end
566
563
  end
567
564
 
568
- def brick_select(*args, params: {}, order_by: nil, translations: {},
569
- join_array: ::Brick::JoinArray.new,
570
- cust_col_override: nil,
571
- brick_col_names: true)
565
+ def brick_select(*args, **kwargs)
566
+ selects = args[0].is_a?(Array) ? args[0] : args
567
+ _brick_querying(selects, **kwargs)
568
+ select(selects)
569
+ end
570
+
571
+ def brick_pluck(*args, withhold_ids: true, **kwargs)
572
+ selects = args[0].is_a?(Array) ? args[0] : args
573
+ _brick_querying(selects, withhold_ids: withhold_ids, **kwargs)
574
+ pluck(selects)
575
+ end
576
+
577
+ def _brick_querying(*args, withhold_ids: nil, params: {}, order_by: nil, translations: {},
578
+ join_array: ::Brick::JoinArray.new,
579
+ cust_col_override: nil,
580
+ brick_col_names: nil)
572
581
  selects = args[0].is_a?(Array) ? args[0] : args
573
582
  if selects.present? && cust_col_override.nil? # See if there's any fancy ones in the select list
574
583
  idx = 0
@@ -652,13 +661,13 @@ module ActiveRecord
652
661
  "'<#{typ.end_with?('_TYP') ? typ[0..-5] : typ}>' AS #{col.name}"
653
662
  end
654
663
  end
655
- else # Having some select columns chosen, add any missing always_load_fields for this model ...
664
+ elsif !withhold_ids # Having some select columns chosen, add any missing always_load_fields for this model ...
656
665
  this_model = klass
657
666
  loop do
658
667
  ::Brick.config.always_load_fields.fetch(this_model.name, nil)&.each do |alf|
659
668
  selects << alf unless selects.include?(alf)
660
669
  end
661
- # ... plus any and all STI superclasses it may inherit from
670
+ # ... plus ALF fields from any and all STI superclasses it may inherit from
662
671
  break if (this_model = this_model.superclass).abstract_class? || this_model == ActiveRecord::Base
663
672
  end
664
673
  end
@@ -671,15 +680,16 @@ module ActiveRecord
671
680
  end
672
681
  end
673
682
 
674
- core_selects = selects.dup
683
+ # core_selects = selects.dup
675
684
  id_for_tables = Hash.new { |h, k| h[k] = [] }
676
685
  field_tbl_names = Hash.new { |h, k| h[k] = {} }
677
686
  used_col_aliases = {} # Used to make sure there is not a name clash
678
687
 
679
688
  # CUSTOM COLUMNS
680
689
  # ==============
681
- (cust_col_override || klass._br_cust_cols).each do |k, cc|
682
- if respond_to?(k) # Name already taken?
690
+ (cust_col_override || (!withhold_ids && klass._br_cust_cols))&.each do |k, cc|
691
+ brick_links # Intentionally create a relation duplicate
692
+ if @_brick_rel_dup.respond_to?(k) # Name already taken?
683
693
  # %%% Use ensure_unique here in this kind of fashion:
684
694
  # cnstr_name = ensure_unique(+"(brick) #{for_tbl}_#{pri_tbl}", nil, bts, hms)
685
695
  # binding.pry
@@ -728,12 +738,14 @@ module ActiveRecord
728
738
  selects << "#{_br_quoted_name(tbl_name)}.#{_br_quoted_name(cc_part.last)} AS #{_br_quoted_name(col_alias)}"
729
739
  cc_part << col_alias
730
740
  end
731
- # Add a key column unless we've already got it
732
- if key_alias && !used_col_aliases.key?(key_alias)
733
- selects << "#{_br_quoted_name(key_tbl_name)}.#{_br_quoted_name(dest_pk)} AS #{_br_quoted_name(key_alias)}"
734
- used_col_aliases[key_alias] = nil
741
+ unless withhold_ids
742
+ # Add a key column unless we've already got it
743
+ if key_alias && !used_col_aliases.key?(key_alias)
744
+ selects << "#{_br_quoted_name(key_tbl_name)}.#{_br_quoted_name(dest_pk)} AS #{_br_quoted_name(key_alias)}"
745
+ used_col_aliases[key_alias] = nil
746
+ end
747
+ cc[2] = key_alias ? [key_klass, key_alias] : nil
735
748
  end
736
- cc[2] = key_alias ? [key_klass, key_alias] : nil
737
749
  end
738
750
 
739
751
  # LEFT OUTER JOINs
@@ -749,7 +761,7 @@ module ActiveRecord
749
761
  # %%% Strangely in Rails 7.1 on a slower system then very rarely brick_link comes back nil...
750
762
  brick_link = brick_links[sel_col.first]
751
763
  field_tbl_name = brick_link&.split('.')&.last ||
752
- # ... so here's a best-effort guess for what the table name might be.
764
+ # ... so if it is nil then here's a best-effort guess as to what the table name might be.
753
765
  klass.reflect_on_association(sel_col.first)&.klass&.table_name
754
766
  # If it's Oracle, quote any AREL aliases that had been applied
755
767
  field_tbl_name = "\"#{field_tbl_name}\"" if ::Brick.is_oracle && brick_links.values.include?(field_tbl_name)
@@ -1020,9 +1032,10 @@ JOIN (SELECT #{hm_selects.map { |s| _br_quoted_name("#{'br_t0.' if from_clause}#
1020
1032
  selects << 'customer_id' if klass.name == 'Pay::Subscription' && Pay::Subscription.columns_hash.key?('customer_id')
1021
1033
 
1022
1034
  pieces, my_dsl = klass.brick_parse_dsl(join_array = ::Brick::JoinArray.new, [], translations = {}, false, nil, true)
1023
- brick_select(
1035
+ _brick_querying(
1024
1036
  selects, where_values_hash, nil, translations: translations, join_array: join_array,
1025
- cust_col_override: { '_br' => (descrip_cols = [pieces, my_dsl]) }
1037
+ cust_col_override: { '_br' => (descrip_cols = [pieces, my_dsl]) },
1038
+ brick_col_names: true
1026
1039
  )
1027
1040
  order_values = "#{_br_quoted_name(klass.table_name)}.#{_br_quoted_name(klass.primary_key)}"
1028
1041
  [self.select(selects), descrip_cols]
@@ -2244,9 +2257,10 @@ class Object
2244
2257
 
2245
2258
  ar_relation = ActiveRecord.version < Gem::Version.new('4') ? real_model.preload : real_model.all
2246
2259
  params['_brick_is_api'] = true if (is_api = request.format == :js || current_api_root)
2247
- @_brick_params = ar_relation.brick_select((selects ||= []), params: params, order_by: order_by,
2248
- translations: (translations = {}),
2249
- join_array: (join_array = ::Brick::JoinArray.new))
2260
+ @_brick_params = ar_relation._brick_querying((selects ||= []), params: params, order_by: order_by,
2261
+ translations: (translations = {}),
2262
+ join_array: (join_array = ::Brick::JoinArray.new),
2263
+ brick_col_names: true)
2250
2264
 
2251
2265
  if is_api # Asking for JSON?
2252
2266
  # Apply column renaming
@@ -2340,7 +2354,7 @@ class Object
2340
2354
  _, order_by_txt = model._brick_calculate_ordering(default_ordering(table_name, pk, true)) if pk
2341
2355
  code << " def index\n"
2342
2356
  code << " @#{plural_table_name} = #{model.name}#{pk&.present? ? ".order(#{order_by_txt.join(', ')})" : '.all'}\n"
2343
- code << " @#{plural_table_name}.brick_select(params)\n"
2357
+ code << " @#{plural_table_name}._brick_querying(params, brick_col_names: true)\n"
2344
2358
  code << " end\n"
2345
2359
 
2346
2360
  is_pk_string = nil
@@ -57,7 +57,7 @@ module Brick::Rails::FormBuilder
57
57
  out << ::Brick::Rails.hide_bcrypt(val, nil, 1000)
58
58
  elsif col_type == :string
59
59
  if model.respond_to?(:uploaders) && model.uploaders.key?(col.name&.to_sym) &&
60
- (url = self.object.send(col.name)&.url) # Carrierwave image?
60
+ (url = self.object.send(col.name)&.url) # Carrierwave image?
61
61
  out << "<img src=\"#{url}\" title=\"#{val}\">"
62
62
  elsif model.respond_to?(:enumerized_attributes) && (opts = (attr = model.enumerized_attributes[method])&.options).present?
63
63
  enum_html_options = attr.kind_of?(Enumerize::Multiple) ? html_options.merge({ multiple: true, size: opts.length + 1 }) : html_options
@@ -232,15 +232,15 @@ module Brick::Rails::FormTags
232
232
  out << if klass._brick_monetized_attributes&.include?(col_name)
233
233
  val ? Money.new(val.to_i).format : ''
234
234
  elsif klass.respond_to?(:uploaders) && klass.uploaders.key?(col_name.to_sym) &&
235
- (url = obj.send(col.name)&.url) && # Carrierwave image?
236
- # And either not restricting Carrierwave, or under the defined Carrierwave image limit?
237
- (!(limit_carrierwave = ::Brick.config.limit_carrierwave) ||
238
- (limit_carrierwave.is_a?(Numeric) &&
239
- (carrierwave_count = instance_variable_get(:@_carrierwave_count) || 0) &&
240
- ((carrierwave_count += 1) < limit_carrierwave) &&
241
- instance_variable_set(:@_carrierwave_count, carrierwave_count)
242
- )
243
- )
235
+ (url = obj.send(col.name)&.url) && # Has a Carrierwave URL? ...
236
+ # ... and either not restricting Carrierwave, or under the defined Carrierwave attachment limit?
237
+ (!(limit_carrierwave = ::Brick.config.limit_carrierwave) ||
238
+ (limit_carrierwave.is_a?(Numeric) &&
239
+ (carrierwave_count = instance_variable_get(:@_carrierwave_count) || 0) &&
240
+ ((carrierwave_count += 1) < limit_carrierwave) &&
241
+ instance_variable_set(:@_carrierwave_count, carrierwave_count)
242
+ )
243
+ )
244
244
  "<img class=\"thumbImg\" src=\"#{url}\" title=\"#{val}\">"
245
245
  else
246
246
  lat_lng = if [:float, :decimal].include?(col.type) &&
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 208
8
+ TINY = 210
9
9
 
10
10
  # PRE is nil unless it's a pre-release (beta, RC, etc.)
11
11
  PRE = nil
data/lib/brick.rb CHANGED
@@ -1512,7 +1512,7 @@ module ActiveRecord
1512
1512
  module QueryMethods
1513
1513
  private
1514
1514
 
1515
- if private_instance_methods.include?(:build_join_query)
1515
+ if private_instance_methods.include?(:build_join_query) # AR 5.0 - 6.0
1516
1516
  alias _brick_build_join_query build_join_query
1517
1517
  def build_join_query(manager, buckets, *args) # , **kwargs)
1518
1518
  # %%% Better way to bring relation into the mix
@@ -1523,7 +1523,7 @@ module ActiveRecord
1523
1523
  _brick_build_join_query(manager, buckets, *args) # , **kwargs)
1524
1524
  end
1525
1525
 
1526
- elsif private_instance_methods.include?(:select_association_list)
1526
+ elsif private_instance_methods.include?(:select_association_list) # AR >= 6.1
1527
1527
  alias _brick_select_association_list select_association_list
1528
1528
  def select_association_list(associations, stashed_joins = nil)
1529
1529
  result = _brick_select_association_list(associations, stashed_joins)
@@ -1531,7 +1531,7 @@ module ActiveRecord
1531
1531
  result
1532
1532
  end
1533
1533
 
1534
- # else # Rails 4.1 ? and older
1534
+ # else # AR 4.2 and older
1535
1535
  # alias _brick_build_joins build_joins
1536
1536
  # def build_joins(manager, joins)
1537
1537
  # result = _brick_build_joins(manager, joins)
@@ -1551,12 +1551,12 @@ module ActiveRecord
1551
1551
  # Capture the table alias name that was chosen
1552
1552
  # if (relation = node.instance_variable_get(:@assocs)&.instance_variable_get(:@relation))
1553
1553
  # link_path = node.instance_variable_get(:@link_path)
1554
- # relation.brick_links[link_path] = result.first.table_alias || result.first.table_name
1554
+ # relation.brick_links(false)[link_path] = result.first.table_alias || result.first.table_name
1555
1555
  # end
1556
1556
  result
1557
1557
  end
1558
1558
  end
1559
- else # For AR >= 4.2
1559
+ else # For AR >= 4.1
1560
1560
  class JoinDependency
1561
1561
  # An intelligent .eager_load() and .includes() that creates t0_r0 style aliases only for the columns
1562
1562
  # used in .select(). To enable this behaviour, include the flag :_brick_eager_load as the first
@@ -1652,7 +1652,7 @@ module ActiveRecord
1652
1652
  # Capture the table alias name that was chosen
1653
1653
  if (relation = node.instance_variable_get(:@assocs)&.instance_variable_get(:@relation))
1654
1654
  link_path = node.instance_variable_get(:@link_path)
1655
- relation.brick_links[link_path] = result.first.table_alias || result.first.table_name
1655
+ relation.brick_links(false)[link_path] = result.first.table_alias || result.first.table_name
1656
1656
  end
1657
1657
  result
1658
1658
  end
@@ -1663,7 +1663,7 @@ module ActiveRecord
1663
1663
  # Capture the table alias name that was chosen
1664
1664
  if (relation = child.instance_variable_get(:@assocs)&.instance_variable_get(:@relation))
1665
1665
  link_path = child.instance_variable_get(:@link_path)
1666
- relation.brick_links[link_path] = if child.table.is_a?(Arel::Nodes::TableAlias)
1666
+ relation.brick_links(false)[link_path] = if child.table.is_a?(Arel::Nodes::TableAlias)
1667
1667
  child.table.right
1668
1668
  else
1669
1669
  # Was: result.first&.left&.table_alias || child.table_name
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.208
4
+ version: 1.0.210
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-03-17 00:00:00.000000000 Z
11
+ date: 2024-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord