diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 000000000..fd06a9268 --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.3.4 \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index e3280a61b..db3fa4f46 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 ae9a7d6a6..9716cc93e 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 b52622d37..6979fab4b 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 000000000..8398ee168 --- /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 formated 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 cfbbb9582..5c716e6ff 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 @@ -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 formated 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