brick 1.0.78 → 1.0.79

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: 52f0716e20661f909922bdcddf3401b79b87c33c7b118d1581dfdf9861dd702c
4
- data.tar.gz: 45323c512a8c7490b0ac715595ead6e6299d06a442b9c723318b90bcdce6106d
3
+ metadata.gz: eedc546e7e9693459cf228ee376650cdf53c0325e55d50ef24bbf46d421f8738
4
+ data.tar.gz: c4feda4b5db8334363d05f3418dc172f0dbf408c21192222008a5b9a174ae448
5
5
  SHA512:
6
- metadata.gz: 2ef6ac2089e1150bc9c74c6625fc3e7627f5ee06a42d1a2d5fe43790d99156abacc27ba2e042044ee42171f352718d99ae8b468ec9dca4350cd6631bead51ce7
7
- data.tar.gz: 8c4da365eb99677930a9f35f0cd5981b91adc790a13ecf55b5369da03fdf1592cccd01f911457cf204aa8769767d5a8537d856ff7080dec75543524b3e680904
6
+ metadata.gz: 0c9713099c057b9c9041121576a4f38d5949213926a47d597f88f4a9021ce14cd3df2a30c14b8b929b92ba4549ec4492bdfd8de13cb0a4f710e911c00eab67b6
7
+ data.tar.gz: 27ca4aa6d40e165aaca5b09cf5b2ce97737b26adc8424b1d25d81b9813100679e9e06a61279c4fd8b96d750a89a4c4cb8eafbfe2d425a4d4c6868644820f297d
@@ -614,15 +614,16 @@ module ActiveRecord
614
614
  count_column = if hm.options[:through]
615
615
  # Build the chain of JOINs going to the final destination HMT table
616
616
  # (Usually just one JOIN, but could be many.)
617
- x = [hmt_assoc = hm]
617
+ hmt_assoc = hm
618
+ x = []
618
619
  x.unshift(hmt_assoc) while hmt_assoc.options[:through] && (hmt_assoc = klass.reflect_on_association(hmt_assoc.options[:through]))
619
- from_clause = +"#{x.first.klass.table_name} br_t0"
620
+ from_clause = +"#{x.first.table_name} br_t0"
620
621
  fk_col = x.shift.foreign_key
621
622
  link_back = [klass.primary_key] # %%% Inverse path back to the original object -- used to build out a link with a filter
622
623
  idx = 0
623
624
  bail_out = nil
624
- x[0..-2].map do |a|
625
- from_clause << "\n LEFT OUTER JOIN #{a.klass.table_name} br_t#{idx += 1} "
625
+ x.map do |a|
626
+ from_clause << "\n LEFT OUTER JOIN #{a.table_name} br_t#{idx += 1} "
626
627
  from_clause << if (src_ref = a.source_reflection).macro == :belongs_to
627
628
  "ON br_t#{idx}.id = br_t#{idx - 1}.#{a.foreign_key}"
628
629
  elsif src_ref.options[:as]
@@ -637,18 +638,22 @@ module ActiveRecord
637
638
  "ON br_t#{idx}.#{a.foreign_key} = br_t#{idx - 1}.id"
638
639
  end
639
640
  link_back.unshift(a.source_reflection.name)
640
- [a.klass.table_name, a.foreign_key, a.source_reflection.macro]
641
+ [a.table_name, a.foreign_key, a.source_reflection.macro]
641
642
  end
642
643
  next if bail_out
643
- # count_column is determined from the last HMT member
644
- if (src_ref = x.last.source_reflection).macro == :belongs_to # Traditional HMT using an associative table
645
- "br_t#{idx}.#{x.last.foreign_key}"
644
+ # count_column is determined from the originating HMT member
645
+ if (src_ref = hm.source_reflection).nil?
646
+ puts "*** Warning: Could not determine destination model for this HMT association in model #{klass.name}:\n has_many :#{hm.name}, through: :#{hm.options[:through]}"
647
+ nix << k
648
+ bail_out = true
649
+ elsif src_ref.macro == :belongs_to # Traditional HMT using an associative table
650
+ "br_t#{idx}.#{hm.foreign_key}"
646
651
  else # A HMT that goes HM -> HM, something like Categories -> Products -> LineItems
647
652
  "br_t#{idx}.#{src_ref.active_record.primary_key}"
648
653
  end
649
654
  else
650
- fk_col = hm.foreign_key
651
- poly_type = hm.inverse_of.foreign_type if hm.options.key?(:as)
655
+ fk_col = (inv = hm.inverse_of)&.foreign_key || hm.foreign_key
656
+ poly_type = inv.foreign_type if hm.options.key?(:as)
652
657
  pk = hm.klass.primary_key
653
658
  (pk.is_a?(Array) ? pk.first : pk) || '*'
654
659
  end
@@ -684,11 +689,11 @@ module ActiveRecord
684
689
  end
685
690
  unless from_clause
686
691
  hm_table_name = if is_mysql
687
- "`#{hm.klass.table_name}`"
692
+ "`#{hm.table_name}`"
688
693
  elsif is_postgres || is_mssql
689
- "\"#{(hm.klass.table_name).gsub('.', '"."')}\""
694
+ "\"#{(hm.table_name).gsub('.', '"."')}\""
690
695
  else
691
- hm.klass.table_name
696
+ hm.table_name
692
697
  end
693
698
  end
694
699
  group_bys = ::Brick.is_oracle || is_mssql ? selects : (1..selects.length).to_a
@@ -1383,7 +1388,7 @@ class Object
1383
1388
  ordering = params['_brick_order']&.split(',')&.map(&:to_sym) || Object.send(:default_ordering, table_name, pk)
1384
1389
  order_by, _ = model._brick_calculate_ordering(ordering, true) # Don't do the txt part
1385
1390
 
1386
- @_brick_params = (ar_relation = model.all).brick_select(params, (selects = []), nil,
1391
+ @_brick_params = (ar_relation = model.all).brick_select(params, (selects = []), order_by,
1387
1392
  translations = {},
1388
1393
  join_array = ::Brick::JoinArray.new)
1389
1394
  # %%% Add custom HM count columns
@@ -1658,7 +1663,10 @@ module ActiveRecord::ConnectionHandling
1658
1663
  # .default_schema are specified then we can work with non-tenanted models more appropriately
1659
1664
  if (apartment = Object.const_defined?('Apartment')) &&
1660
1665
  File.exist?(apartment_initializer = ::Rails.root.join('config/initializers/apartment.rb'))
1661
- load apartment_initializer
1666
+ unless @_apartment_loaded
1667
+ load apartment_initializer
1668
+ @_apartment_loaded = true
1669
+ end
1662
1670
  apartment_excluded = Apartment.excluded_models
1663
1671
  end
1664
1672
  # Only for Postgres (Doesn't work in sqlite3 or MySQL)
@@ -813,6 +813,24 @@ erDiagram
813
813
  hms_keys << (assoc_name = (assoc = hm.first).name.to_s)
814
814
  "#{assoc_name.inspect} => [#{(assoc.options[:through] && !assoc.through_reflection).inspect}, #{assoc.klass.name}, #{hm[1].inspect}, #{hm[2].inspect}]"
815
815
  end.join(', ')}}
816
+ # If the resource is missing, has the user simply created an inappropriately pluralised name for a table?
817
+ @#{table_name} ||= if dym_list = instance_variables.reject do |entry|
818
+ entry.to_s.start_with?('@_') ||
819
+ ['@cache_hit', '@marked_for_same_origin_verification', '@view_renderer', '@view_flow', '@output_buffer', '@virtual_path'].include?(entry.to_s)
820
+ end
821
+ msg = \"Can't find resource \\\"#{table_name}\\\".\"
822
+ # Can't be sure otherwise of what is up, so check DidYouMean and offer a suggestion.
823
+ if (dym = DidYouMean::SpellChecker.new(dictionary: dym_list).correct('@#{table_name}')).present?
824
+ msg << \"\nIf you meant \\\"#\{found_dym = dym.first[1..-1]}\\\" then to avoid this message add this entry into inflections.rb:\n\"
825
+ msg << \" inflect.singular('#\{found_dym}', '#{obj_name}')\"
826
+ puts
827
+ puts \"WARNING: #\{msg}\"
828
+ puts
829
+ @#{table_name} = instance_variable_get(dym.first.to_sym)
830
+ else
831
+ raise ActiveRecord::RecordNotFound.new(msg)
832
+ end
833
+ end
816
834
  col_keys = @#{table_name}.columns.each_with_object([]) do |col, s|
817
835
  col_name = col.name
818
836
  next if @_brick_incl&.exclude?(col_name) ||
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 78
8
+ TINY = 79
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
@@ -179,13 +179,21 @@ module Brick
179
179
 
180
180
  case a.macro
181
181
  when :belongs_to
182
- s.first[a.foreign_key] = if a.polymorphic?
183
- primary_tables = relations[model.table_name][:fks].find { |_k, fk| fk[:assoc_name] == a.name.to_s }&.last&.fetch(:inverse_table, [])
184
- models = primary_tables&.map { |table| table.singularize.camelize.constantize }
185
- [a.name, models, true]
186
- else
187
- [a.name, a.klass]
188
- end
182
+ if a.polymorphic?
183
+ rel_poly_bt = relations[model.table_name][:fks].find { |_k, fk| fk[:assoc_name] == a.name.to_s }
184
+ if (primary_tables = rel_poly_bt&.last&.fetch(:inverse_table, [])).is_a?(Array)
185
+ models = primary_tables&.map { |table| table.singularize.camelize.constantize }
186
+ s.first[a.foreign_key] = [a.name, models, true]
187
+ else
188
+ # This will come up when using Devise invitable when invited_by_class_name is not
189
+ # specified because in that circumstance it adds a polymorphic :invited_by association,
190
+ # along with appropriate invited_by_type and invited_by_id columns.
191
+ puts "Missing any real indication as to which models \"has_many\" this polymorphic BT in model #{a.active_record.name}:"
192
+ puts " belongs_to :#{a.name}, polymorphic: true"
193
+ end
194
+ else
195
+ s.first[a.foreign_key] = [a.name, a.klass]
196
+ end
189
197
  when :has_many, :has_one # This gets has_many as well as has_many :through
190
198
  # %%% weed out ones that don't have an available model to reference
191
199
  s.last[a.name] = a
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.78
4
+ version: 1.0.79
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lorin Thwaits
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-14 00:00:00.000000000 Z
11
+ date: 2022-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord