From 63a1e74fca2149179a0646bed80c7b7d1a21b61c Mon Sep 17 00:00:00 2001 From: Ray Zeller Date: Thu, 23 May 2019 11:23:14 -0600 Subject: [PATCH] Update mass assignment methods for Rails 4 and 5 --- CHANGELOG.md | 2 ++ app/services/forest_liana/resource_creator.rb | 4 +++ app/services/forest_liana/resource_updater.rb | 4 +++ .../forest_liana/resource_creator_test.rb | 36 +++++++++++++++++++ .../forest_liana/resource_updater_test.rb | 25 ++++++++++++- 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 test/services/forest_liana/resource_creator_test.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index e3280a61..db3fa4f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Change Log ## [Unreleased] +### Fixed +- Record Update/Creation - Add support for Rails 4 and 5 when request does not have strong params ## RELEASE 3.0.4 - 2019-05-21 ### Fixed diff --git a/app/services/forest_liana/resource_creator.rb b/app/services/forest_liana/resource_creator.rb index ae9a7d6a..9716cc93 100644 --- a/app/services/forest_liana/resource_creator.rb +++ b/app/services/forest_liana/resource_creator.rb @@ -13,6 +13,10 @@ def perform begin if has_strong_parameter @record = @resource.create(resource_params) + elsif Rails::VERSION::MAJOR >= 5 + @record = @resource.create(resource_params.to_unsafe_hash) + elsif Rails::VERSION::MAJOR == 4 + @record = @resource.create(resource_params.to_hash) else @record = @resource.create(resource_params, without_protection: true) end diff --git a/app/services/forest_liana/resource_updater.rb b/app/services/forest_liana/resource_updater.rb index b52622d3..6979fab4 100644 --- a/app/services/forest_liana/resource_updater.rb +++ b/app/services/forest_liana/resource_updater.rb @@ -15,6 +15,10 @@ def perform if has_strong_parameter @record.update_attributes(resource_params) + elsif Rails::VERSION::MAJOR >= 5 + @record.update_attributes(resource_params.to_unsafe_hash) + elsif Rails::VERSION::MAJOR == 4 + @record.update_attributes(resource_params.to_hash) else @record.update_attributes(resource_params, without_protection: true) end diff --git a/test/services/forest_liana/resource_creator_test.rb b/test/services/forest_liana/resource_creator_test.rb new file mode 100644 index 00000000..5dc41b65 --- /dev/null +++ b/test/services/forest_liana/resource_creator_test.rb @@ -0,0 +1,36 @@ +require 'minitest/mock' + +module ForestLiana + class ResourceCreatorTest < ActiveSupport::TestCase + + collection = ForestLiana::Model::Collection.new({ + name: 'SerializeField', + fields: [{ + type: 'String', + field: 'field' + }] + }) + + ForestLiana.apimap << collection + ForestLiana.models << SerializeField + + test 'Create a record on a "serialize" attribute with a well formatted value without strong params' do + params = ActionController::Parameters.new( + data: { + type: "SerializeField", + attributes: { + field: "[\"test\", \"test\"]" + } + } + ) + + creator = ResourceCreator.new(SerializeField, params) + creator.stub :has_strong_parameter, false do + creator.perform + + assert creator.record.valid? + assert creator.record.field == ["test", "test"] + end + end + end +end diff --git a/test/services/forest_liana/resource_updater_test.rb b/test/services/forest_liana/resource_updater_test.rb index cfbbb958..f3cef938 100644 --- a/test/services/forest_liana/resource_updater_test.rb +++ b/test/services/forest_liana/resource_updater_test.rb @@ -1,3 +1,5 @@ +require 'minitest/mock' + module ForestLiana class ResourceUpdaterTest < ActiveSupport::TestCase @@ -65,7 +67,7 @@ class ResourceUpdaterTest < ActiveSupport::TestCase assert updater.errors[0][:detail] == "Bad format for 'field' attribute value." end - test 'Update a record on a "serialize" attribute with a well formated value' do + test 'Update a record on a "serialize" attribute with a well formatted value' do params = ActionController::Parameters.new( id: 1, data: { @@ -82,5 +84,26 @@ class ResourceUpdaterTest < ActiveSupport::TestCase assert updater.record.valid? assert updater.record.field == ["test", "test"] end + + test 'Update a record on a "serialize" attribute with a well formatted value without strong params' do + params = ActionController::Parameters.new( + id: 1, + data: { + id: 1, + type: "SerializeField", + attributes: { + field: "[\"test\", \"test\"]" + } + } + ) + + updater = ResourceUpdater.new(SerializeField, params) + updater.stub :has_strong_parameter, false do + updater.perform + + assert updater.record.valid? + assert updater.record.field == ["test", "test"] + end + end end end