brick 1.0.46 → 1.0.47

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: 1442fb46672a819689a1f15aa16e2dd53647a057ea38948431746d3d8baf66f0
4
- data.tar.gz: 38d26e09e226d37e87737cba35f76ee3b2413dc4474ac8ecba582297205789aa
3
+ metadata.gz: 4180c0164d7f086112339696df9d8fef2aeebcd32d9b81faf22d76ddc11815bc
4
+ data.tar.gz: 823673bcbecc342f5df15e196ee65ad6f6ac0b75415514079c055af06d0ac7f6
5
5
  SHA512:
6
- metadata.gz: 8cb9e747cffb1e33f045f07656891b8e17ac4349661f897aa01886bae6fb7c2e4687f1021e04173266ab60cf330ec71f58514aeb670669f30ea8c6dd4140e943
7
- data.tar.gz: 54166ef84adaeab00c2d6408e9a9a6c5da5998bd278b0db490943d2d8da2701938cdc87a4c40ff65ac2cbe7196a7eb014a4b3b08dfd2ff5aa2ec426af00d38ab
6
+ metadata.gz: 1d17b1abd64051066b71839095be1e672b897ad707a2d10f4ca4429131c894ee178711c873cef59957111704437b86b6322b8909ab5cc41cc955547146377dd8
7
+ data.tar.gz: e210becff69096e73b9b7fa09bb4e1be6c36cd2982305b2c803c9c00e9d2b5b085857ea6edacc62498557cde928e2afbbb42ebbd137180385ec3a8a0c5efdf85
@@ -596,7 +596,7 @@ Module.class_exec do
596
596
  # See if a file is there in the same way that ActiveSupport::Dependencies#load_missing_constant
597
597
  # checks for it in ~/.rvm/gems/ruby-2.7.5/gems/activesupport-5.2.6.2/lib/active_support/dependencies.rb
598
598
 
599
- if (base_model = ::Brick.config.sti_namespace_prefixes&.fetch("::#{name}::", nil)&.constantize) || # Are we part of an auto-STI namespace? ...
599
+ if (base_model = ::Brick.config.sti_namespace_prefixes&.fetch("::#{self.name}::", nil)&.constantize) || # Are we part of an auto-STI namespace? ...
600
600
  self != Object # ... or otherwise already in some namespace?
601
601
  schema_name = [(singular_schema_name = name.underscore),
602
602
  (schema_name = singular_schema_name.pluralize),
@@ -773,33 +773,47 @@ class Object
773
773
  end # class definition
774
774
  # Having this separate -- will this now work out better?
775
775
  built_model.class_exec do
776
- hmts&.each do |hmt_fk, fks|
776
+ hmts&.each do |hmt_fk, hms|
777
777
  hmt_fk = hmt_fk.tr('.', '_')
778
- fks.each do |fk|
779
- # %%% Will not work with custom has_many name
780
- through = ::Brick.config.schema_behavior[:multitenant] ? fk.first[:assoc_name] : fk.first[:inverse_table].tr('.', '_').pluralize
781
- hmt_name = if fks.length > 1
782
- if fks[0].first[:inverse][:assoc_name] == fks[1].first[:inverse][:assoc_name] # Same BT names pointing back to us? (Most common scenario)
783
- "#{hmt_fk}_through_#{fk.first[:assoc_name]}"
778
+ hms.each do |hm|
779
+ # %%% Need to confirm that HMTs work when they are built from has_manys with custom names
780
+ through = ::Brick.config.schema_behavior[:multitenant] ? hm.first[:assoc_name] : hm.first[:inverse_table].tr('.', '_').pluralize
781
+ options = {}
782
+ hmt_name = if hms.length > 1
783
+ if hms[0].first[:inverse][:assoc_name] == hms[1].first[:inverse][:assoc_name] # Same BT names pointing back to us? (Most common scenario)
784
+ "#{hmt_fk}_through_#{hm.first[:assoc_name]}"
784
785
  else # Use BT names to provide uniqueness
785
- through = fk.first[:alternate_name].pluralize
786
- singular_assoc_name = fk.first[:inverse][:assoc_name].singularize
786
+ if self.name.underscore.singularize == hm.first[:alternate_name]
787
+ # Has previously been:
788
+ # # If it folds back on itself then look at the other side
789
+ # # (At this point just infer the source be the inverse of the first has_many that
790
+ # # we find that is not ourselves. If there are more than two then uh oh, can't
791
+ # # yet handle that rare circumstance!)
792
+ # other = hms.find { |hm1| hm1 != hm } # .first[:fk]
793
+ # options[:source] = other.first[:inverse][:assoc_name].to_sym
794
+ # And also has been:
795
+ # hm.first[:inverse][:assoc_name].to_sym
796
+ options[:source] = hm.last.to_sym
797
+ else
798
+ through = hm.first[:alternate_name].pluralize
799
+ end
800
+ singular_assoc_name = hm.first[:inverse][:assoc_name].singularize
787
801
  "#{singular_assoc_name}_#{hmt_fk}"
788
802
  end
789
803
  else
790
804
  hmt_fk
791
805
  end
792
- options = { through: through.to_sym }
806
+ options[:through] = through.to_sym
793
807
  if relation[:fks].any? { |k, v| v[:assoc_name] == hmt_name }
794
- hmt_name = "#{hmt_name.singularize}_#{fk.first[:assoc_name]}"
808
+ hmt_name = "#{hmt_name.singularize}_#{hm.first[:assoc_name]}"
795
809
  # Was:
796
- # options[:class_name] = fk.first[:inverse_table].singularize.camelize
797
- # options[:foreign_key] = fk.first[:fk].to_sym
798
- far_assoc = relations[fk.first[:inverse_table]][:fks].find { |_k, v| v[:assoc_name] == fk.last }
810
+ # options[:class_name] = hm.first[:inverse_table].singularize.camelize
811
+ # options[:foreign_key] = hm.first[:fk].to_sym
812
+ far_assoc = relations[hm.first[:inverse_table]][:fks].find { |_k, v| v[:assoc_name] == hm.last }
799
813
  options[:class_name] = far_assoc.last[:inverse_table].singularize.camelize
800
814
  options[:foreign_key] = far_assoc.last[:fk].to_sym
801
815
  end
802
- options[:source] = fk.last.to_sym unless hmt_name.singularize == fk.last
816
+ options[:source] ||= hm.last.to_sym unless hmt_name.singularize == hm.last
803
817
  code << " has_many :#{hmt_name}#{options.map { |opt| ", #{opt.first}: #{opt.last.inspect}" }.join}\n"
804
818
  self.send(:has_many, hmt_name.to_sym, **options)
805
819
  end
@@ -105,13 +105,26 @@ module Brick
105
105
  hms_columns = [] # Used for 'index'
106
106
  skip_klass_hms = ::Brick.config.skip_index_hms[model_name] || {}
107
107
  hms_headers = hms.each_with_object([]) do |hm, s|
108
- hm_stuff = [(hm_assoc = hm.last), "H#{hm_assoc.macro == :has_one ? 'O' : 'M'}#{'T' if hm_assoc.options[:through]}", (assoc_name = hm.first)]
108
+ hm_stuff = [(hm_assoc = hm.last),
109
+ "H#{hm_assoc.macro == :has_one ? 'O' : 'M'}#{'T' if hm_assoc.options[:through]}",
110
+ (assoc_name = hm.first)]
109
111
  hm_fk_name = if hm_assoc.options[:through]
110
- associative = associatives[hm_assoc.name]
111
- associative && "'#{associative.name}.#{associative.foreign_key}'"
112
- else
113
- hm_assoc.foreign_key
114
- end
112
+ associative = associatives[hm.first]
113
+ tbl_nm = if hm_assoc.options[:source]
114
+ associative.klass.reflect_on_association(hm_assoc.options[:source]).inverse_of&.name
115
+ else
116
+ associative.name
117
+ end
118
+ # If there is no inverse available for the source belongs_to association, make one based on the class name
119
+ unless tbl_nm
120
+ tbl_nm = associative.class_name.underscore
121
+ tbl_nm.slice!(0) if tbl_nm[0] == ('/')
122
+ tbl_nm = tbl_nm.tr('/', '_').pluralize
123
+ end
124
+ "'#{tbl_nm}.#{associative.foreign_key}'"
125
+ else
126
+ hm_assoc.foreign_key
127
+ end
115
128
  case args.first
116
129
  when 'index'
117
130
  hms_columns << if hm_assoc.macro == :has_many
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 46
8
+ TINY = 47
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
@@ -168,8 +168,12 @@ module Brick
168
168
  skip_hms = {}
169
169
  associatives = hms.each_with_object({}) do |hmt, s|
170
170
  if (through = hmt.last.options[:through])
171
- skip_hms[through] = nil
172
- s[hmt.first] = hms[through] # End up with a hash of HMT names pointing to join-table associations
171
+ skip_hms[through] = nil # if hms[through]
172
+ # binding.pry if !hms[through]
173
+ # End up with a hash of HMT names pointing to join-table associations
174
+ # Last part was: hmt.last.name
175
+ # Changed up because looking for: hms[:issue_issue_duplicates]
176
+ s[hmt.first] = hms[through] # || hms["#{(opt = hmt.last.options)[:through].to_s.singularize}_#{opt[:source].to_s.pluralize}".to_sym]
173
177
  elsif hmt.last.inverse_of.nil?
174
178
  puts "SKIPPING #{hmt.last.name.inspect}"
175
179
  # %%% If we don't do this then below associative.name will find that associative is 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.46
4
+ version: 1.0.47
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-07-15 00:00:00.000000000 Z
11
+ date: 2022-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord