Skip to content

Commit

Permalink
Update mass assignment methods for Rails 4 and 5
Browse files Browse the repository at this point in the history
  • Loading branch information
rayzeller committed May 24, 2019
1 parent ff72556 commit 63a1e74
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/services/forest_liana/resource_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/services/forest_liana/resource_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions test/services/forest_liana/resource_creator_test.rb
Original file line number Diff line number Diff line change
@@ -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
25 changes: 24 additions & 1 deletion test/services/forest_liana/resource_updater_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'minitest/mock'

module ForestLiana
class ResourceUpdaterTest < ActiveSupport::TestCase

Expand Down Expand Up @@ -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: {
Expand All @@ -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

0 comments on commit 63a1e74

Please sign in to comment.