brick 1.0.123 → 1.0.124

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: baad039242ee5623097ad6170fec07e5feb0eb2fed6d41a9079f0775cccaceea
4
- data.tar.gz: 603377f6b2c4437c448630bce6581d3650b8dfc13187e82afbaab1d425d171e2
3
+ metadata.gz: 44fce70a53b4467d019ca42a9826f44a4849d472dcbfc2fe324f961708b70a18
4
+ data.tar.gz: 2674709f203786560b41c1bf1e2062336cf88282d168848303e9920b398662e3
5
5
  SHA512:
6
- metadata.gz: 1f4e48c1a437684929a81ace3e75ab15bb627c006680dba5a7454825fba358142560712f016bf944575aefeb27655716c3212d72a2b9b5a083a09cd2dfa8537c
7
- data.tar.gz: ea9c376866230e4c2bab7b6c34dfc435a8cf81b568bc22af3a0e69497accca1620152ae311eab6c96649b79ecedf978b880afa88888dc6d1c4d3700bc3ef3060
6
+ metadata.gz: 337760a0382fd37a64026a07d80e708f9f475f73e8ca420d05341b731c977ab9153556803f10314a4851730e28a22ce4847705e2d8ad81121a8f33736f505f12
7
+ data.tar.gz: 04a1e5ec1cc503173da16be708d611f9fb1dd6f3965e6e848dfd1e310f3e1c94b396c8e693982d8f3c3589b5d8ffbfee70fd52593918e12ecd8309c8e110b4c5
@@ -638,6 +638,7 @@ module ActiveRecord
638
638
  # Add derived table JOIN for the has_many counts
639
639
  nix = []
640
640
  klass._br_hm_counts.each do |k, hm|
641
+ num_bt_things = 0
641
642
  count_column = if hm.options[:through]
642
643
  # Build the chain of JOINs going to the final destination HMT table
643
644
  # (Usually just one JOIN, but could be many.)
@@ -668,6 +669,8 @@ module ActiveRecord
668
669
  from_clause << if (src_ref = a.source_reflection).macro == :belongs_to
669
670
  nm = hmt_assoc.source_reflection.inverse_of&.name
670
671
  link_back << nm
672
+ num_bt_things += 1
673
+ # puts "BT #{a.table_name}"
671
674
  "ON br_t#{idx}.id = br_t#{idx - 1}.#{a.foreign_key}"
672
675
  elsif src_ref.options[:as]
673
676
  "ON br_t#{idx}.#{src_ref.type} = '#{src_ref.active_record.name}'" + # "polymorphable_type"
@@ -686,6 +689,7 @@ module ActiveRecord
686
689
  "br_t#{idx - 1}.#{a.foreign_key} = br_t#{idx}.id"
687
690
  end
688
691
  else # Standard has_many or has_one
692
+ # puts "HM #{a.table_name}"
689
693
  # binding.pry unless (
690
694
  nm = hmt_assoc.source_reflection.inverse_of&.name
691
695
  # )
@@ -708,7 +712,13 @@ module ActiveRecord
708
712
  # binding.pry if link_back.length > 2
709
713
  "br_t#{idx}.#{hm.foreign_key}"
710
714
  else # A HMT that goes HM -> HM, something like Categories -> Products -> LineItems
711
- # binding.pry if link_back.length > 2
715
+ # %%% Currently flaky, so will revisit this soon, probably while implementing the whole link_back architecture
716
+ if num_bt_things > 1
717
+ # binding.pry
718
+ nix << k
719
+ next
720
+ end
721
+
712
722
  "br_t#{idx}.#{src_ref.active_record.primary_key}"
713
723
  end
714
724
  else
@@ -2498,9 +2508,9 @@ module Brick
2498
2508
  # rubocop:enable Style/CommentedKeyword
2499
2509
 
2500
2510
  class << self
2501
- def _add_bt_and_hm(fk, relations, is_polymorphic = false, is_optional = false)
2511
+ def _add_bt_and_hm(fk, relations, polymorphic_class = nil, is_optional = false)
2502
2512
  bt_assoc_name = ::Brick.namify(fk[2], :downcase)
2503
- unless is_polymorphic
2513
+ unless polymorphic_class
2504
2514
  bt_assoc_name = if bt_assoc_name.underscore.end_with?('_id')
2505
2515
  bt_assoc_name[-3] == '_' ? bt_assoc_name[0..-4] : bt_assoc_name[0..-3]
2506
2516
  elsif bt_assoc_name.downcase.end_with?('id') && bt_assoc_name.exclude?('_')
@@ -2555,7 +2565,7 @@ module Brick
2555
2565
  puts "Brick: Additional reference #{fk.inspect} refers to non-existent #{'table'.pluralize(missing.length)} #{missing.join(' and ')}. (Available tables include #{tables.join(', ')}.)"
2556
2566
  return
2557
2567
  end
2558
- unless (cols = relations[fk[1]][:cols]).key?(fk[2]) || (is_polymorphic && cols.key?("#{fk[2]}_id") && cols.key?("#{fk[2]}_type"))
2568
+ unless (cols = relations[fk[1]][:cols]).key?(fk[2]) || (polymorphic_class && cols.key?("#{fk[2]}_id") && cols.key?("#{fk[2]}_type"))
2559
2569
  columns = cols.map { |k, v| "#{k} (#{v.first.split(' ').first})" }
2560
2570
  puts "Brick: Additional reference #{fk.inspect} refers to non-existent column #{fk[2]}. (Columns present in #{fk[1]} are #{columns.join(', ')}.)"
2561
2571
  return
@@ -2572,9 +2582,10 @@ module Brick
2572
2582
  return unless bts # Rails 5.0 and older can have bts end up being nil
2573
2583
 
2574
2584
  if (assoc_bt = bts[cnstr_name])
2575
- if is_polymorphic
2585
+ if polymorphic_class
2576
2586
  # Assuming same fk (don't yet support composite keys for polymorphics)
2577
2587
  assoc_bt[:inverse_table] << fk[4]
2588
+ assoc_bt[:polymorphic] << polymorphic_class
2578
2589
  else # Expect we could have a composite key going
2579
2590
  if assoc_bt[:fk].is_a?(String)
2580
2591
  assoc_bt[:fk] = [assoc_bt[:fk], fk[2]] unless fk[2] == assoc_bt[:fk]
@@ -2584,10 +2595,10 @@ module Brick
2584
2595
  assoc_bt[:assoc_name] = "#{assoc_bt[:assoc_name]}_#{fk[2]}"
2585
2596
  end
2586
2597
  else
2587
- inverse_table = [primary_table] if is_polymorphic
2598
+ inverse_table = [primary_table] if polymorphic_class
2588
2599
  assoc_bt = bts[cnstr_name] = { is_bt: true, fk: fk[2], assoc_name: bt_assoc_name, inverse_table: inverse_table || primary_table }
2589
2600
  assoc_bt[:optional] = true if is_optional
2590
- assoc_bt[:polymorphic] = true if is_polymorphic
2601
+ assoc_bt[:polymorphic] = [polymorphic_class] if polymorphic_class
2591
2602
  end
2592
2603
  if is_class
2593
2604
  # For use in finding the proper :source for a HMT association that references an STI subclass
@@ -2613,7 +2624,7 @@ module Brick
2613
2624
  end
2614
2625
  assoc_hm = hms[hm_cnstr_name] = { is_bt: false, fk: fk[2], assoc_name: fk_namified.pluralize, alternate_name: bt_assoc_name,
2615
2626
  inverse_table: inv_tbl, inverse: assoc_bt }
2616
- assoc_hm[:polymorphic] = true if is_polymorphic
2627
+ assoc_hm[:polymorphic] = true if polymorphic_class
2617
2628
  hm_counts = relation.fetch(:hm_counts) { relation[:hm_counts] = {} }
2618
2629
  this_hm_count = hm_counts[fk[1]] = hm_counts.fetch(fk[1]) { 0 } + 1
2619
2630
  end
@@ -170,11 +170,8 @@ module Brick::Rails::FormTags
170
170
  if klass
171
171
  type_col = klass.inheritance_column # Usually 'type'
172
172
  filter_parts << "#{type_col}=#{sti_type}" if sti_type && klass.column_names.include?(type_col)
173
- path_params = request.path_parameters.dup
174
- path_params.delete(:controller)
175
- path_params.delete(:action)
173
+ path_params = request.path_parameters
176
174
  pk = (klass.primary_key || ActiveRecord::Base.primary_key).to_sym
177
- # Used to also have this but it's a bit too permissive to identify a primary key: (path_params.length == 1 && path_params.values.first) ||
178
175
  if ((id = (path_params[pk] || path_params[:id] || path_params["#{klass.name.underscore}_id".to_sym])) && (obj = klass.find_by(pk => id))) ||
179
176
  (['show', 'edit', 'update', 'destroy'].include?(action_name) && (obj = klass.first))
180
177
  obj
@@ -183,7 +180,18 @@ module Brick::Rails::FormTags
183
180
  # %%% If there is a polymorphic association that might relate to stuff in the path_params,
184
181
  # try to identify an appropriate ___able_id and ___able_type filter
185
182
  ((klass.column_names - [pk.to_s]) & path_params.keys.map(&:to_s)).each do |path_param|
186
- filter_parts << "#{path_param}=#{path_params[path_param.to_sym]}"
183
+ next if [:controller, :action].include?(path_param)
184
+
185
+ foreign_id = path_params[path_param.to_sym]
186
+ # Need to convert a friendly_id slug to a real ID?
187
+ if Object.const_defined?('FriendlyId') &&
188
+ (assoc = klass.reflect_on_all_associations.find { |a| a.belongs_to? && a.foreign_key == path_param }) &&
189
+ (assoc_klass = assoc.klass).instance_variable_get(:@friendly_id_config) &&
190
+ (new_id = assoc_klass.where(assoc_klass.friendly_id_config.query_field => foreign_id)
191
+ .pluck(assoc_klass.primary_key).first)
192
+ foreign_id = new_id
193
+ end
194
+ filter_parts << "#{path_param}=#{foreign_id}"
187
195
  end
188
196
  klass
189
197
  end
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 123
8
+ TINY = 124
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
@@ -208,7 +208,7 @@ module Brick
208
208
  if a.polymorphic?
209
209
  rel_poly_bt = relations[model.table_name][:fks].find { |_k, fk| fk[:assoc_name] == a.name.to_s }
210
210
  if (primary_tables = rel_poly_bt&.last&.fetch(:inverse_table, [])).is_a?(Array)
211
- models = primary_tables&.map { |table| table.singularize.camelize.constantize }
211
+ models = rel_poly_bt[1][:polymorphic]&.map { |table| table.singularize.camelize.constantize }
212
212
  s.first[a.foreign_key.to_s] = [a.name, models, true]
213
213
  else
214
214
  # This will come up when using Devise invitable when invited_by_class_name is not
@@ -523,8 +523,13 @@ module Brick
523
523
  table_name, poly = k.split('.')
524
524
  v ||= ActiveRecord::Base.execute_sql("SELECT DISTINCT #{poly}_type AS typ FROM #{table_name}").each_with_object([]) { |result, s| s << result['typ'] if result['typ'] }
525
525
  v.each do |type|
526
- if relations.key?(primary_table = type.underscore.pluralize)
527
- ::Brick._add_bt_and_hm([nil, table_name, poly, nil, primary_table, "(brick) #{table_name}_#{poly}"], relations, true, is_optional)
526
+ # Allow polymorphic BT to relate to an STI subclass
527
+ base_type = ::Brick.config.sti_namespace_prefixes["::#{type}"] ||
528
+ ::Brick.config.sti_namespace_prefixes.find { |k, _v| type.start_with?(k[2..-1]) }&.last&.[](2..-1)
529
+ if relations.key?(primary_table = (base_type || type).underscore.pluralize)
530
+ ::Brick._add_bt_and_hm([nil, table_name, poly, nil, primary_table, "(brick) #{table_name}_#{poly}"], relations,
531
+ type, # Polymorphic class
532
+ is_optional)
528
533
  else
529
534
  missing_stis[primary_table] = type unless ::Brick.existing_stis.key?(type)
530
535
  end
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.123
4
+ version: 1.0.124
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-03-24 00:00:00.000000000 Z
11
+ date: 2023-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord