Skip to content

Commit

Permalink
Merge pull request #147 from dchandekstark/issue-146
Browse files Browse the repository at this point in the history
Updates HydraEditor::Form.model_attributes.
  • Loading branch information
jrgriffiniii authored Mar 21, 2018
2 parents 78ea076 + f1cba1d commit 6cf7299
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 10 additions & 4 deletions app/forms/hydra_editor/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ def multiple?(field)
# ImageForm.model_attributes(params[:image])
# # => { title: 'My new image' }
def model_attributes(form_params)
clean_params = sanitize_params(form_params)
terms.each do |key|
clean_params[key].delete('') if clean_params[key]
sanitize_params(form_params).tap do |clean_params|
terms.each do |key|
if clean_params[key]
if multiple?(key)
clean_params[key].delete('')
elsif clean_params[key] == ''
clean_params[key] = nil
end
end
end
end
clean_params
end

def sanitize_params(form_params)
Expand Down
5 changes: 5 additions & 0 deletions spec/forms/hydra_editor_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class TestForm
subject { TestForm.model_attributes(params) }

it { is_expected.to eq('creator' => 'bob', 'title' => []) }

describe "setting non-multiple attribute to nil when value is empty string" do
let(:params) { ActionController::Parameters.new(title: [''], creator: '') }
it { is_expected.to eq('creator' => nil, 'title' => []) }
end
end
end

Expand Down

0 comments on commit 6cf7299

Please sign in to comment.