brick 1.0.151 → 1.0.152

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: 1d8abfeb02b561a0ac8214de3c4791992947cab9f288f9f57c816a883dc4592a
4
- data.tar.gz: 1ec0f9d11eea9b1741e454ac04aa2c51380a0d9676f4e84a5de2419d3ef751bd
3
+ metadata.gz: 4b4101e6f3437aaf0cb79eec97a94b9df6655d9db8a4773e761768556d2ec31f
4
+ data.tar.gz: 543bad78f1a359af9126373928789d868530b7ed09c17dbf18bf52a012a37763
5
5
  SHA512:
6
- metadata.gz: 758e2ab6e10589689abdb77df64731e3df2fff341998e8f815bd5f180a73df89fb1282155a9520cc43102a49b1f7283114c02146329161e2324fc2fa79c6048e
7
- data.tar.gz: 9a77d3d4318943225427c59e676c4a067c95a22351eaea88ed14bbc6da60ee834498117704bc0fe1dd493f6e10ab65a27db4bff3b060440f4e7dfaf3b31ad184
6
+ metadata.gz: 84d77e9a74428eea4e90f0a40b6e2a6dc824623a6b8975659698b4c79b73ec2ec5f5f26409c5d3d7f0c89c33e6c0aafb73a0c69b8cb3900654d10c528f3d4699
7
+ data.tar.gz: 556ad657abeb7a2368940a8b651ef85e6d3c410684e3ab7a088d741ea851e5c31b51bbe8f8baaa017231f56eb7fa177928782b68c6ee7b00c053d1267ac67a1f
data/lib/brick/config.rb CHANGED
@@ -204,6 +204,15 @@ module Brick
204
204
  @mutex.synchronize { @has_ones = hos }
205
205
  end
206
206
 
207
+ # Associations upon which to add #accepts_nested_attributes_for logic
208
+ def nested_attributes
209
+ @mutex.synchronize { @nested_attributes }
210
+ end
211
+
212
+ def nested_attributes=(anaf)
213
+ @mutex.synchronize { @nested_attributes = anaf }
214
+ end
215
+
207
216
  # Polymorphic associations
208
217
  def polymorphics
209
218
  @mutex.synchronize { @polymorphics }
@@ -344,6 +344,28 @@ module ActiveRecord
344
344
  def _br_cust_cols
345
345
  @_br_cust_cols ||= {}
346
346
  end
347
+
348
+ def _brick_find_permits(model_or_assoc, current_permits, done_permits = [])
349
+ unless done_permits.include?(model_or_assoc)
350
+ done_permits << model_or_assoc
351
+ self.reflect_on_all_associations.select { |assoc| !assoc.belongs_to? }.each_with_object([]) do |assoc, s|
352
+ if assoc.options[:through]
353
+ current_permits << { "#{assoc.name.to_s.singularize}_ids".to_sym => [] }
354
+ s << "#{assoc.name.to_s.singularize}_ids: []"
355
+ end
356
+ if self.instance_methods.include?(:"#{assoc.name}_attributes=")
357
+ # Support nested attributes which use the friendly_id gem
358
+ assoc.klass._brick_nested_friendly_id if Object.const_defined?('FriendlyId') &&
359
+ assoc.klass.instance_variable_get(:@friendly_id_config)
360
+ new_attrib_text = assoc.klass._brick_find_permits(assoc, (new_permits = assoc.klass.columns_hash.keys.map(&:to_sym)), done_permits)
361
+ new_permits << :_destroy
362
+ current_permits << { "#{assoc.name}_attributes".to_sym => new_permits }
363
+ s << "#{assoc.name}_attributes: #{new_attrib_text}"
364
+ end
365
+ end
366
+ end
367
+ current_permits
368
+ end
347
369
  end
348
370
 
349
371
  # Search for custom column, BT, HM, and HMT DSL stuff
@@ -1639,18 +1661,23 @@ class Object
1639
1661
  else
1640
1662
  need_fk = "#{ActiveSupport::Inflector.singularize(assoc[:inverse][:inverse_table].split('.').last)}_id" != assoc[:fk]
1641
1663
  end
1664
+ singular_assoc_name = ActiveSupport::Inflector.singularize(assoc_name.tr('.', '_'))
1642
1665
  has_ones = ::Brick.config.has_ones&.fetch(full_name, nil)
1643
- if has_ones&.key?(singular_assoc_name = ActiveSupport::Inflector.singularize(assoc_name.tr('.', '_')))
1644
- assoc_name = if (custom_assoc_name = has_ones[singular_assoc_name])
1645
- need_class_name = custom_assoc_name != singular_assoc_name
1646
- custom_assoc_name
1647
- else
1648
- singular_assoc_name
1649
- end
1650
- :has_one
1651
- else
1652
- :has_many
1653
- end
1666
+ macro = if has_ones&.key?(singular_assoc_name)
1667
+ assoc_name = if (custom_assoc_name = has_ones[singular_assoc_name])
1668
+ need_class_name = custom_assoc_name != singular_assoc_name
1669
+ custom_assoc_name
1670
+ else
1671
+ singular_assoc_name
1672
+ end
1673
+ :has_one
1674
+ else
1675
+ :has_many
1676
+ end
1677
+ # Auto-create an accepts_nested_attributes_for for this HM?
1678
+ is_anaf = (anaf = ::Brick.config.nested_attributes&.fetch(full_name, nil)) &&
1679
+ (anaf.is_a?(Array) ? anaf.include?(assoc_name) : anaf == assoc_name)
1680
+ macro
1654
1681
  end
1655
1682
  # Figure out if we need to specially call out the class_name and/or foreign key
1656
1683
  # (and if either of those then definitely also a specific inverse_of)
@@ -1686,6 +1713,10 @@ class Object
1686
1713
  assoc_name = assoc_name.tr('.', '_').to_sym
1687
1714
  code << " #{macro} #{assoc_name.inspect}#{options.map { |k, v| ", #{k}: #{v.inspect}" }.join}\n"
1688
1715
  self.send(macro, assoc_name, **options)
1716
+ if is_anaf
1717
+ code << " accepts_nested_attributes_for #{assoc_name.inspect}\n"
1718
+ self.send(:accepts_nested_attributes_for, assoc_name)
1719
+ end
1689
1720
  end
1690
1721
 
1691
1722
  def default_ordering(table_name, pk)
@@ -2194,6 +2225,9 @@ class Object
2194
2225
  end
2195
2226
  end
2196
2227
  end
2228
+ if (upd_hash ||= upd_params).fetch(model.inheritance_column, nil).strip == ''
2229
+ upd_hash[model.inheritance_column] = nil
2230
+ end
2197
2231
  obj.send(:update, upd_hash || upd_params)
2198
2232
  end
2199
2233
 
@@ -2241,7 +2275,7 @@ class Object
2241
2275
 
2242
2276
  if is_need_params
2243
2277
  code << " def #{params_name}\n"
2244
- permits_txt = self._brick_find_permits(model, (permits = model.columns_hash.keys.map(&:to_sym)))
2278
+ permits_txt = model._brick_find_permits(model, permits = model.columns_hash.keys.map(&:to_sym))
2245
2279
  code << " params.require(:#{require_name = model.name.underscore.tr('/', '_')
2246
2280
  }).permit(#{permits_txt.map(&:inspect).join(', ')})\n"
2247
2281
  code << " end\n"
@@ -2257,27 +2291,6 @@ class Object
2257
2291
  [built_controller, code]
2258
2292
  end
2259
2293
 
2260
- def _brick_find_permits(model, current_permits, done_permits = [])
2261
- unless done_permits.include?(self)
2262
- done_permits << self
2263
- model.reflect_on_all_associations.select { |assoc| assoc.macro == :has_many }.each_with_object([]) do |assoc, s|
2264
- if assoc.options[:through]
2265
- current_permits << { "#{assoc.name.to_s.singularize}_ids".to_sym => [] }
2266
- s << "#{assoc.name.to_s.singularize}_ids: []"
2267
- elsif assoc.active_record.instance_methods.include?(:"#{assoc.name}_attributes=")
2268
- # Support nested attributes which use the friendly_id gem
2269
- self._brick_nested_friendly_id if Object.const_defined?('FriendlyId') &&
2270
- assoc.klass.instance_variable_get(:@friendly_id_config)
2271
- new_attrib_text = self._brick_find_permits(assoc.klass, (new_permits = assoc.klass.columns_hash.keys.map(&:to_sym)), done_permits)
2272
- new_permits << :_destroy
2273
- current_permits << { "#{assoc.name}_attributes".to_sym => new_permits }
2274
- s << "#{assoc.name}_attributes: #{new_attrib_text}"
2275
- end
2276
- end
2277
- end
2278
- current_permits
2279
- end
2280
-
2281
2294
  def _brick_nested_friendly_id
2282
2295
  unless @_brick_nested_friendly_id
2283
2296
  ::ActiveRecord::Base.class_exec do
@@ -257,6 +257,9 @@ function linkSchemas() {
257
257
  # Has one relationships
258
258
  ::Brick.has_ones = app.config.brick.fetch(:has_ones, nil)
259
259
 
260
+ # accepts_nested_attributes_for relationships
261
+ ::Brick.nested_attributes = app.config.brick.fetch(:nested_attributes, nil)
262
+
260
263
  # Polymorphic associations
261
264
  ::Brick.polymorphics = app.config.brick.fetch(:polymorphics, nil)
262
265
  end
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 151
8
+ TINY = 152
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
@@ -477,6 +477,12 @@ module Brick
477
477
  end
478
478
  end
479
479
 
480
+ # Associations upon which to add #accepts_nested_attributes_for logic
481
+ # @api public
482
+ def nested_attributes=(anaf)
483
+ Brick.config.nested_attributes = anaf
484
+ end
485
+
480
486
  # Polymorphic associations
481
487
  def polymorphics=(polys)
482
488
  polys = polys.each_with_object({}) { |poly, s| s[poly] = nil } if polys.is_a?(Array)
@@ -256,6 +256,10 @@ if ActiveRecord::Base.respond_to?(:brick_select) && !::Brick.initializer_loaded
256
256
  # # instead of \"user_profile\", then apply that as a third parameter like this:
257
257
  # Brick.has_ones = [['User', 'user_profile', 'profile']]
258
258
 
259
+ # # Automatically establish #accepts_nested_attributes_for logic on has_many and has_one associations.
260
+ # # This video to demonstrates how this works: https://github.com/lorint/brick/assets/5301131/82ac4f6d-bc23-4a55-adab-bc754bcb0f26
261
+ # Brick.nested_attributes = { 'User' => ['profile', 'posts'] }
262
+
259
263
  # # We normally don't show the timestamp columns \"created_at\", \"updated_at\", and \"deleted_at\", and also do
260
264
  # # not consider them when finding associative tables to support an N:M association. (That is, ones that can be a
261
265
  # # part of a has_many :through association.) If you want to use different exclusion columns than our defaults
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.151
4
+ version: 1.0.152
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-06-08 00:00:00.000000000 Z
11
+ date: 2023-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord