Skip to content

Commit

Permalink
fix: bug with sub-items
Browse files Browse the repository at this point in the history
  • Loading branch information
ProGM committed Sep 27, 2023
1 parent ef0aa69 commit 3ae36a9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
8 changes: 4 additions & 4 deletions lib/paramoid/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def sanitize(params)
ensure_required_params!(params)
end

def permitted_params
scalar_params.to_params
end

protected

# @param [Symbol] name
Expand Down Expand Up @@ -82,10 +86,6 @@ def transformers
@transformers ||= {}.with_indifferent_access
end

def permitted_params
scalar_params.to_params
end

def transformed_keys
@transformed_keys ||= scalar_params
end
Expand Down
34 changes: 23 additions & 11 deletions lib/paramoid/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,18 @@ def ensure_required_params!(params, path: [])

_raise_on_missing_parameter!(params, output_key, current_path) if @required

if nested?
if params.is_a?(Array)
params.each { |param| @nested.ensure_required_params!(param[output_key], path: current_path) }
else
@nested.ensure_required_params!(params ? params[output_key] : nil, path: current_path)
return params unless nested?

ensure_required_nested_params!(params, current_path)
end

def ensure_required_nested_params!(params, current_path)
if params.is_a?(Array)
params.flatten.each do |param|
@nested.ensure_required_params!(param[output_key], path: current_path)
end
else
@nested.ensure_required_params!(params ? params[output_key] : nil, path: current_path)
end

params
Expand Down Expand Up @@ -103,17 +109,23 @@ def apply_nested_defaults!(params)
return params unless params

params ||= @nesting_type == :list ? [] : {}
if params.is_a?(Array)
params.map! { |param| @nested.apply_defaults!(param[output_key]) }
else
return params unless params[output_key]

result = @nested.apply_defaults!(params[output_key])
params[output_key] = result if result
return apply_nested_param_default_when_available!(params, output_key) unless params.is_a?(Array)

params.map! do |param|
apply_nested_param_default_when_available!(param, output_key)
end
params
end

def apply_nested_param_default_when_available!(param, key)
return param unless param[key]

result = @nested.apply_defaults!(param[key])
param[key] = result if result
param
end

def to_defaults
if nested?
nested_defaults = @nested.to_defaults
Expand Down
22 changes: 19 additions & 3 deletions spec/paramoid/complex_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
}
},
person: {},
items: [{}, {}]
items: [{}, { sub_items: [{ id: 5 }] }]
}
end
it 'has the default value for those' do
Expand All @@ -62,7 +62,10 @@
'buyer' => { 'payment_method' => { 'uuid' => 1 } },
'total' => 0,
'name' => 'some_name',
'items' => [{ 'price' => 0 }, { 'price' => 0 }],
'items' => [
{ 'price' => 0 },
{ 'price' => 0, 'sub_items' => [{ 'price' => 0, 'id' => 5 }] }
],
'person_attributes' => { 'role' => :user }
}
)
Expand Down Expand Up @@ -91,11 +94,24 @@
ActionController::Parameters.new(params_hash)
end

it 'returns permitted params' do
expect(subject.permitted_params).to eq(
[
:total, :name,
{ buyer: [{ payment_method: [:id] }],
items: [:id, :name, :price, :discount, { sub_items: [:id, :price] }],
person: %i[id full_name role] }
]
)
end

it 'has the default values correctly nested' do
expect(sanitized.to_unsafe_h).to eq(
{
'buyer' => { 'payment_method' => { 'uuid' => 1 } },
'items' => [{ 'price' => 0, 'name' => 'some_name', 'discount' => 0.1, 'id' => 5 }],
'items' => [
{ 'price' => 0, 'name' => 'some_name', 'discount' => 0.1, 'id' => 5 }
],
'total' => 100,
'name' => 'some_name',
'person_attributes' => { 'role' => :admin, 'id' => 1, 'full_name' => 'some_name' }
Expand Down
5 changes: 5 additions & 0 deletions spec/support/params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ def initialize(user = nil)

default :price, 0
param :discount, transformer: ->(data) { data&.to_f / 100 } if user.admin?

array :sub_items do
param :id
default :price, 0
end
end

default :total, 0
Expand Down

0 comments on commit 3ae36a9

Please sign in to comment.