brick 1.0.180 → 1.0.182

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: c5cc0c73d63cbebcbad81388f8ae444c72dafa57a38c1f2a95b3b7f5bd4a621a
4
- data.tar.gz: 8670884c20ac46ee01d28c97fabe97694a2f3862761228e16c0422790a937cc8
3
+ metadata.gz: 390cb3f8a466f1d94c0e8e7472017753fb09f8c9909a71134545e25b47edfb21
4
+ data.tar.gz: 9398ee17aa8a5b8741adf784ebde9421f6419f93a421ec0ba5893f028b192383
5
5
  SHA512:
6
- metadata.gz: 62d010ea2765fe14369e18db8b123426f5ace62025b053b453656870223073b75f95add727e5cb2073f78d0f58a88cb293904bcbee08726b0ba09947c16f3709
7
- data.tar.gz: 25dbb2878198256df74cc36934bb7fcaab24a3dfd0b9cac55de8d04e2fcfce8ff0158458cd2d76004e354020addd6b38de5001ab79bebecb9f8afaa1f3e5133f
6
+ metadata.gz: 75c2d9f206d58f5296862fcdec776b3a5daa82e2c7405ff3fa46bf386e61cfac39ddf9cf55d7ab3eb54ddf23434c5086fd99e6d9eca0543ed63d9946892ea140
7
+ data.tar.gz: 1818e986519622fd6c51c25e0153e003a476c7e823f14ff4cc9aabdf4cfb1adae7fe8f67ff1e9ca1a181694e028aa3b0c985afad8ab88b5c1d839be3aba70ed0
@@ -115,14 +115,30 @@ if Object.const_defined?('ActionPack') && !ActionPack.respond_to?(:version)
115
115
  end
116
116
  end
117
117
  end
118
- if Bundler.locked_gems&.dependencies.key?('action_view')
118
+ if Object.const_defined?('Bundler') && Bundler.locked_gems&.dependencies.key?('action_view')
119
119
  require 'action_view' # Needed for Rails <= 4.0
120
- if Object.const_defined?('ActionView') && !ActionView.respond_to?(:version)
121
- module ActionView
120
+ module ::ActionView
121
+ if Object.const_defined?('ActionView') && !ActionView.respond_to?(:version)
122
122
  def self.version
123
123
  ActionPack.version
124
124
  end
125
125
  end
126
+ if self.version < ::Gem::Version.new('5.2')
127
+ module Helpers
128
+ module TextHelper
129
+ # Older versions of #pluralize lack the all-important .to_s
130
+ def pluralize(count, singular, plural_arg = nil, plural: plural_arg, locale: I18n.locale)
131
+ word = if (count == 1 || count.to_s =~ /^1(\.0+)?$/)
132
+ singular
133
+ else
134
+ plural || singular.pluralize(locale)
135
+ end
136
+
137
+ "#{count || 0} #{word}"
138
+ end
139
+ end
140
+ end
141
+ end
126
142
  end
127
143
  end
128
144
 
@@ -1545,8 +1545,7 @@ class Object
1545
1545
  is_sti = true
1546
1546
  else
1547
1547
  # Class for auto-generated models to inherit from
1548
- base_model = (::Brick.config.models_inherit_from ||= (app.config.brick.fetch(:models_inherit_from, nil) ||
1549
- begin
1548
+ base_model = (::Brick.config.models_inherit_from ||= (begin
1550
1549
  ::ApplicationRecord
1551
1550
  rescue StandardError => ex
1552
1551
  ::ActiveRecord::Base
@@ -1610,9 +1609,14 @@ class Object
1610
1609
  if has_pk
1611
1610
  code << " # Primary key: #{_brick_primary_key.join(', ')}\n" unless _brick_primary_key == ['id']
1612
1611
  elsif our_pks&.present?
1613
- if our_pks.length > 1 && respond_to?(:'primary_keys=') # Using the composite_primary_keys gem?
1614
- new_model_class.primary_keys = our_pks
1615
- code << " self.primary_keys = #{our_pks.map(&:to_sym).inspect}\n"
1612
+ pk_mutator = if respond_to?(:'primary_keys=')
1613
+ 'primary_keys=' # Using the composite_primary_keys gem
1614
+ elsif ActiveRecord.version >= Gem::Version.new('7.1')
1615
+ 'primary_key=' # Rails 7.1+?
1616
+ end
1617
+ if our_pks.length > 1 && pk_mutator
1618
+ new_model_class.send(pk_mutator, our_pks)
1619
+ code << " self.#{pk_mutator[0..-2]} = #{our_pks.map(&:to_sym).inspect}\n"
1616
1620
  else
1617
1621
  new_model_class.primary_key = (pk_sym = our_pks.first.to_sym)
1618
1622
  code << " self.primary_key = #{pk_sym.inspect}\n"
@@ -1790,14 +1794,19 @@ class Object
1790
1794
  singular_table_parts.shift
1791
1795
  end
1792
1796
  options[:class_name] = "::#{assoc[:primary_class]&.name || singular_table_parts.map(&:camelize).join('::')}" if need_class_name
1793
- # Work around a bug in CPK where self-referencing belongs_to associations double up their foreign keys
1794
1797
  if need_fk # Funky foreign key?
1795
- options[:foreign_key] = if assoc[:fk].is_a?(Array)
1796
- assoc_fk = assoc[:fk].uniq
1797
- assoc_fk.length < 2 ? assoc_fk.first : assoc_fk
1798
- else
1799
- assoc[:fk].to_sym
1800
- end
1798
+ options_fk_key = :foreign_key
1799
+ if assoc[:fk].is_a?(Array)
1800
+ # #uniq works around a bug in CPK where self-referencing belongs_to associations double up their foreign keys
1801
+ if (assoc_fk = assoc[:fk].uniq).length > 1
1802
+ options_fk_key = :query_constraints if ActiveRecord.version >= ::Gem::Version.new('7.1')
1803
+ options[options_fk_key] = assoc_fk
1804
+ else
1805
+ options[options_fk_key] = assoc_fk.first
1806
+ end
1807
+ else
1808
+ options[options_fk_key] = assoc[:fk].to_sym
1809
+ end
1801
1810
  end
1802
1811
  if inverse_assoc_name && (need_class_name || need_fk || need_inverse_of) &&
1803
1812
  (klass = options[:class_name]&.constantize) && (ian = inverse_assoc_name.tr('.', '_').to_sym) &&
@@ -1404,12 +1404,6 @@ end
1404
1404
  #{erd_markup}
1405
1405
 
1406
1406
  <%= # Consider getting the name from the association -- hm.first.name -- if a more \"friendly\" alias should be used for a screwy table name
1407
- cols = {#{hms_keys = []
1408
- hms_headers.map do |hm|
1409
- hms_keys << (assoc_name = (assoc = hm.first).name.to_s)
1410
- "#{assoc_name.inspect} => [#{(assoc.options[:through] && !assoc.through_reflection).inspect}, #{assoc.klass.name}, #{hm[1].inspect}, #{hm[2].inspect}]"
1411
- end.join(', ')}}
1412
-
1413
1407
  # If the resource is missing, has the user simply created an inappropriately pluralised name for a table?
1414
1408
  @#{table_name} ||= if (dym_list = instance_variables.reject do |entry|
1415
1409
  entry.to_s.start_with?('@_') ||
@@ -1429,6 +1423,13 @@ end
1429
1423
  end
1430
1424
  end
1431
1425
 
1426
+ # Starts as being just has_many columns, and will be augmented later with all the other columns
1427
+ cols = {#{hms_keys = []
1428
+ hms_headers.map do |hm|
1429
+ hms_keys << (assoc_name = (assoc = hm.first).name.to_s)
1430
+ "#{assoc_name.inspect} => [#{(assoc.options[:through] && !assoc.through_reflection).inspect}, #{assoc.klass.name}, #{hm[1].inspect}, #{hm[2].inspect}]"
1431
+ end.join(', ')}}
1432
+
1432
1433
  # %%% Why in the Canvas LMS app does ActionView::Helpers get cleared / reloaded, or otherwise lose access to #brick_grid ???
1433
1434
  # Possible fix if somewhere we can implement the #include with:
1434
1435
  # (ActiveSupport.const_defined?('Reloader') ? ActiveSupport : ActionDispatch)::Reloader.to_prepare do ... end
@@ -1436,8 +1437,8 @@ end
1436
1437
  # Rails.application.reloader.to_prepare do ... end
1437
1438
  self.class.class_exec { include ::Brick::Rails::FormTags } unless respond_to?(:brick_grid)
1438
1439
  # Write out the mega-grid
1439
- brick_grid(@#{table_name}, @_brick_bt_descrip, @_brick_sequence, @_brick_incl, @_brick_excl,
1440
- cols, poly_cols, bts, #{hms_keys.inspect}, {#{hms_columns.join(', ')}}) %>
1440
+ brick_grid(@#{table_name}, @_brick_sequence, @_brick_incl, @_brick_excl,
1441
+ cols, bt_descrip: @_brick_bt_descrip, poly_cols: poly_cols, bts: bts, hms_keys: #{hms_keys.inspect}, hms_cols: {#{hms_columns.join(', ')}}) %>
1441
1442
 
1442
1443
  #{"<hr><%= link_to(\"New #{new_path_name = "new_#{path_obj_name}_path"
1443
1444
  obj_name}\", #{new_path_name}, { class: '__brick' }) if respond_to?(:#{new_path_name}) %>" unless @_brick_model.is_view?}
@@ -1,7 +1,7 @@
1
1
  module Brick::Rails::FormTags
2
2
  # Our super speedy grid
3
- def brick_grid(relation = nil, bt_descrip = nil, sequence = nil, inclusions = nil, exclusions = nil,
4
- cols = {}, poly_cols = nil, bts = {}, hms_keys = [], hms_cols = {},
3
+ def brick_grid(relation = nil, sequence = nil, inclusions = nil, exclusions = nil,
4
+ cols = {}, bt_descrip: nil, poly_cols: nil, bts: {}, hms_keys: [], hms_cols: {},
5
5
  show_header: nil, show_row_count: nil, show_erd_button: nil, show_new_button: nil, show_avo_button: nil, show_aa_button: nil)
6
6
  # When a relation is not provided, first see if one exists which matches the controller name
7
7
  unless (relation ||= instance_variable_get("@#{controller_name}".to_sym))
@@ -158,7 +158,7 @@ module Brick::Rails::FormTags
158
158
  pk.map { |pk_part| obj.send(pk_part.to_sym) }), { class: 'big-arrow' })}</td>\n" if pk.present?
159
159
  sequence.each_with_index do |col_name, idx|
160
160
  val = obj.attributes[col_name]
161
- bt = bts[col_name]
161
+ bt = bts[col_name] || composite_bt_names[col_name]
162
162
  out << '<td'
163
163
  (classes ||= []) << 'col-sticky' if idx < nfc
164
164
  (classes ||= []) << 'dimmed' unless cols.key?(col_name) || (cust_col = cust_cols[col_name]) ||
@@ -166,7 +166,7 @@ module Brick::Rails::FormTags
166
166
  (classes ||= []) << 'right' if val.is_a?(Numeric) && !bt
167
167
  out << " class=\"#{classes.join(' ')}\"" if classes&.present?
168
168
  out << '>'
169
- if (bt || composite_bt_names[col_name])
169
+ if bt
170
170
  if bt[2] && obj.respond_to?(poly_id_col = "#{bt.first}_id") # Polymorphic?
171
171
  if (poly_id = obj.send(poly_id_col))
172
172
  bt_class = obj.send(klass.brick_foreign_type(bt.first))
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 180
8
+ TINY = 182
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
@@ -203,23 +203,25 @@ module Brick
203
203
  end
204
204
 
205
205
  model_cols = model.columns_hash
206
- pk_type = if (mpk = model.primary_key).is_a?(Array)
207
- # Composite keys should really use: model.primary_key.map { |pk_part| model_cols[pk_part].type }
208
- model_cols[mpk.first].type
209
- else
210
- mpk && model_cols[mpk].type
211
- end
212
206
  bts, hms = model.reflect_on_all_associations.each_with_object([{}, {}]) do |a, s|
213
- # %%% The time will come when we will support type checking of composite foreign keys!
214
- # binding.pry if a.foreign_key.is_a?(Array)
215
207
  if a.belongs_to? && !a.polymorphic? && ::Brick.config.polymorphics.fetch(full_assoc_name = "#{model.table_name}.#{a.name}", nil)
216
208
  puts "Based on inclusion in ::Brick.polymorphics, marking association #{full_assoc_name} as being polymorphic."
217
209
  a.options[:polymorphic] = true
218
210
  end
219
- unless a.polymorphic? || (!a.belongs_to? && (through = a.options[:through])) ||
220
- (a.klass && ::Brick.config.exclude_tables.exclude?(a.klass.table_name) &&
221
- (!a.belongs_to? || (same_type = (fk_type = model_cols[a.foreign_key]&.type) == pk_type))
222
- )
211
+ if !a.polymorphic? && (a.belongs_to? || (through = a.options[:through])) &&
212
+ !(a.klass && ::Brick.config.exclude_tables.exclude?(a.klass.table_name) &&
213
+ (!a.belongs_to? ||
214
+ ((fk_type = a.foreign_key.is_a?(Array) ? a.foreign_key.map { |fk_part| model_cols[fk_part.to_s].type } : model_cols[a.foreign_key.to_s].type) &&
215
+ (primary_cols = a.klass.columns_hash) &&
216
+ (pk_type = a.klass.primary_key.is_a?(Array) ? a.klass.primary_key.map { |pk_part| primary_cols[pk_part.to_s].type } : primary_cols[a.klass.primary_key].type) &&
217
+ (same_type = (pk_type == fk_type))
218
+ )
219
+ )
220
+ )
221
+ # unless a.polymorphic? || (!a.belongs_to? && (through = a.options[:through])) ||
222
+ # (a.klass && ::Brick.config.exclude_tables.exclude?(a.klass.table_name) &&
223
+ # (!a.belongs_to? || (same_type = (fk_type = model_cols[a.foreign_key.to_s]&.type) == pk_type))
224
+ # )
223
225
  if same_type == false # We really do want to test specifically for false here, and not nil!
224
226
  puts "WARNING:
225
227
  Foreign key column #{a.klass.table_name}.#{a.foreign_key} is #{fk_type}, but the primary key it relates to, #{a.active_record.table_name}.#{a.active_record.primary_key}, is #{pk_type}.
@@ -280,7 +282,8 @@ module Brick
280
282
  next
281
283
  end
282
284
  else
283
- if !a.options.key?(:as) && a.klass.column_names.exclude?(a.foreign_key.to_s)
285
+ this_fks = this_fk.is_a?(Array) ? this_fk : [this_fk.to_s]
286
+ if !a.options.key?(:as) && (this_fks - a.klass.column_names).length.positive?
284
287
  options = ", #{a.options.map { |k, v| "#{k.inspect} => #{v.inspect}" }.join(', ')}" if a.options.present?
285
288
  puts "WARNING: Model #{model.name} has this association:
286
289
  has_many :#{a.name}#{options}
@@ -1544,7 +1547,7 @@ ActiveSupport.on_load(:active_record) do
1544
1547
  else
1545
1548
  {}
1546
1549
  end
1547
- _original_initialize(klass, **kwargs)
1550
+ _original_initialize(klass, *args, **kwargs)
1548
1551
  end
1549
1552
  end
1550
1553
  end
@@ -2036,11 +2039,13 @@ if ActiveRecord.version < ::Gem::Version.new('6.0') && ruby_version >= ::Gem::Ve
2036
2039
  end
2037
2040
 
2038
2041
  # AR >= 5.0 on Ruby >= 3.0
2039
- ::ActiveRecord::Type::AdapterSpecificRegistry.class_exec do
2040
- alias :_brick_add_modifier :add_modifier
2041
- def add_modifier(*args, **kwargs)
2042
- kwargs.merge!(args.pop) if args.length > 2 && args.last.is_a?(Hash)
2043
- _brick_add_modifier(*args, **kwargs)
2042
+ if ActiveRecord.version >= ::Gem::Version.new('5.0')
2043
+ ::ActiveRecord::Type::AdapterSpecificRegistry.class_exec do
2044
+ alias :_brick_add_modifier :add_modifier
2045
+ def add_modifier(*args, **kwargs)
2046
+ kwargs.merge!(args.pop) if args.length > 2 && args.last.is_a?(Hash)
2047
+ _brick_add_modifier(*args, **kwargs)
2048
+ end
2044
2049
  end
2045
2050
  end
2046
2051
 
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.180
4
+ version: 1.0.182
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-10-08 00:00:00.000000000 Z
11
+ date: 2023-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -164,6 +164,20 @@ dependencies:
164
164
  - - "~>"
165
165
  - !ruby/object:Gem::Version
166
166
  version: 1.42.0
167
+ - !ruby/object:Gem::Dependency
168
+ name: rswag-ui
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
167
181
  - !ruby/object:Gem::Dependency
168
182
  name: mysql2
169
183
  requirement: !ruby/object:Gem::Requirement