brick 1.0.149 → 1.0.150

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: 91e6b3d87c423a90e6e6cf62a07235e185373d9bb60990579a13ccea83c6a645
4
- data.tar.gz: 8670e080c676eb7c5be3bb62b7ce4e7a310e8d30c98c3d189d513bd9e06a2af3
3
+ metadata.gz: 7914cc56d8cd8919835028d5ad9b05a50a2b17baf32c9699d1f78bbaf9417e0d
4
+ data.tar.gz: a7f945e136fe8401c54e8fd24a4d92a5cef24b22bb68d2caceac28d809ff8f07
5
5
  SHA512:
6
- metadata.gz: 58ff4070f313c577b027f2c83babb99c2aeecaf0cfd05edbed89ed6e5a32485a9bdc8849e303d9eb16fa6f5824beb66e4dbf282843bc4e2b1a00c7ec13e0616a
7
- data.tar.gz: 20cd0a6b43f267a653923a86ded57b041cbe49adbd1e239236faac18415eab0e8515490e0828dcc67f4d7e4f809efae9ff7249a1ab5d9a9626a8a24dced8d2d9
6
+ metadata.gz: fa3ed344e3d4beed83cf9a6fd7059d78032497f8a6c51c825fd50d163e61bd8029afe3dd82e055631d8258b0c9cf54f7d176e064c47fe9fb5a70cc25cdb67faf
7
+ data.tar.gz: e43ca92a155c805d4dc5be0a2548d9612bc0b1a92cb2e65722ca4c55c81dbef9b476493d6764eece75489e9caf60e2a1046ca960d9a58cafe060aeae11db954a
@@ -2238,12 +2238,7 @@ class Object
2238
2238
 
2239
2239
  if is_need_params
2240
2240
  code << " def #{params_name}\n"
2241
- permits = model.columns_hash.keys.map(&:to_sym)
2242
- permits_txt = permits.map(&:inspect) +
2243
- model.reflect_on_all_associations.select { |assoc| assoc.macro == :has_many && assoc.options[:through] }.map do |assoc|
2244
- permits << { "#{assoc.name.to_s.singularize}_ids".to_sym => [] }
2245
- "#{assoc.name.to_s.singularize}_ids: []"
2246
- end
2241
+ permits_txt = self._brick_find_permits(model, (permits = model.columns_hash.keys.map(&:to_sym)))
2247
2242
  code << " params.require(:#{require_name = model.name.underscore.tr('/', '_')
2248
2243
  }).permit(#{permits_txt.join(', ')})\n"
2249
2244
  code << " end\n"
@@ -2259,6 +2254,98 @@ class Object
2259
2254
  [built_controller, code]
2260
2255
  end
2261
2256
 
2257
+ def _brick_find_permits(model, current_permits)
2258
+ model.reflect_on_all_associations.select { |assoc| assoc.macro == :has_many }.each_with_object([]) do |assoc, s|
2259
+ if assoc.options[:through]
2260
+ current_permits << { "#{assoc.name.to_s.singularize}_ids".to_sym => [] }
2261
+ s << "#{assoc.name.to_s.singularize}_ids: []"
2262
+ elsif assoc.active_record.instance_methods.include?(:"#{assoc.name}_attributes=")
2263
+ # Support nested attributes which use the friendly_id gem
2264
+ self._brick_nested_friendly_id if Object.const_defined?('FriendlyId') &&
2265
+ assoc.klass.instance_variable_get(:@friendly_id_config)
2266
+ new_attrib_text = self._brick_find_permits(assoc.klass, (new_permits = assoc.klass.columns_hash.keys.map(&:to_sym)))
2267
+ new_permits << :_destroy
2268
+ current_permits << { "#{assoc.name}_attributes".to_sym => new_permits }
2269
+ s << "#{assoc.name}_attributes: #{new_attrib_text}"
2270
+ end
2271
+ end
2272
+ current_permits
2273
+ end
2274
+
2275
+ def _brick_nested_friendly_id
2276
+ unless @_brick_nested_friendly_id
2277
+ ::ActiveRecord::Base.class_exec do
2278
+ if private_instance_methods.include?(:assign_nested_attributes_for_collection_association)
2279
+ alias _brick_anafca assign_nested_attributes_for_collection_association
2280
+ def assign_nested_attributes_for_collection_association(association_name, attributes_collection)
2281
+ association = association(association_name)
2282
+ slug_column = association.klass.instance_variable_get(:@friendly_id_config)&.slug_column
2283
+ return _brick_anafca unless slug_column
2284
+
2285
+ # Here is the FriendlyId version of #assign_nested_attributes_for_collection_association
2286
+ options = nested_attributes_options[association_name]
2287
+ attributes_collection = attributes_collection.to_h if attributes_collection.respond_to?(:permitted?)
2288
+
2289
+ unless attributes_collection.is_a?(Hash) || attributes_collection.is_a?(Array)
2290
+ raise ArgumentError, "Hash or Array expected for attribute `#{association_name}`, got #{attributes_collection.class.name} (#{attributes_collection.inspect})"
2291
+ end
2292
+
2293
+ check_record_limit!(options[:limit], attributes_collection)
2294
+
2295
+ if attributes_collection.is_a? Hash
2296
+ keys = attributes_collection.keys
2297
+ attributes_collection = if keys.include?("id") || keys.include?(:id)
2298
+ [attributes_collection]
2299
+ else
2300
+ attributes_collection.values
2301
+ end
2302
+ end
2303
+ existing_records = if association.loaded?
2304
+ association.target
2305
+ else
2306
+ attribute_ids = attributes_collection.filter_map { |a| a["id"] || a[:id] }
2307
+ if attribute_ids.empty?
2308
+ []
2309
+ else # Implement the same logic as "friendly" scope
2310
+ association.scope.where(association.klass.primary_key => attribute_ids)
2311
+ .or(association.scope.where(slug_column => attribute_ids))
2312
+ end
2313
+ end
2314
+ attributes_collection.each do |attributes|
2315
+ attributes = attributes.to_h if attributes.respond_to?(:permitted?)
2316
+ attributes = attributes.with_indifferent_access
2317
+ if attributes["id"].blank? && attributes[slug_column].blank?
2318
+ unless reject_new_record?(association_name, attributes)
2319
+ association.reader.build(attributes.except(*::ActiveRecord::Base::UNASSIGNABLE_KEYS))
2320
+ end
2321
+ elsif (attr_id_str = attributes["id"].to_s) &&
2322
+ (existing_record = existing_records.detect { |record| record.id.to_s == attr_id_str ||
2323
+ record.send(slug_column).to_s == attr_id_str })
2324
+ unless call_reject_if(association_name, attributes)
2325
+ # Make sure we are operating on the actual object which is in the association's
2326
+ # proxy_target array (either by finding it, or adding it if not found)
2327
+ # Take into account that the proxy_target may have changed due to callbacks
2328
+ target_record = association.target.detect { |record| record.id.to_s == attr_id_str ||
2329
+ record.send(slug_column).to_s == attr_id_str }
2330
+ if target_record
2331
+ existing_record = target_record
2332
+ else
2333
+ association.add_to_target(existing_record, skip_callbacks: true)
2334
+ end
2335
+
2336
+ assign_to_or_mark_for_destruction(existing_record, attributes, options[:allow_destroy])
2337
+ end
2338
+ else
2339
+ raise_nested_attributes_record_not_found!(association_name, attributes["id"])
2340
+ end
2341
+ end
2342
+ end # anafca
2343
+ end
2344
+ end if ::ActiveRecord::QueryMethods.instance_methods.include?(:or)
2345
+ @_brick_nested_friendly_id = true
2346
+ end
2347
+ end
2348
+
2262
2349
  def _brick_get_hm_assoc_name(relation, hm_assoc, source = nil)
2263
2350
  assoc_name, needs_class = if (relation[:hm_counts][hm_assoc[:inverse_table]]&.> 1) &&
2264
2351
  hm_assoc[:alternate_name] != (source || name.underscore)
@@ -5,7 +5,7 @@ module Brick
5
5
  module VERSION
6
6
  MAJOR = 1
7
7
  MINOR = 0
8
- TINY = 149
8
+ TINY = 150
9
9
 
10
10
  # PRE is nil unless it's a pre-release (beta, RC, etc.)
11
11
  PRE = 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.149
4
+ version: 1.0.150
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-01 00:00:00.000000000 Z
11
+ date: 2023-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord