Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for mass assignment in Rails 4 and 5 #316

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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