Skip to content

Commit 25f2989

Browse files
committed
Update mass assignment methods for Rails 4 and 5
1 parent ff72556 commit 25f2989

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Change Log
22

33
## [Unreleased]
4+
### Fixed
5+
- Record Update/Creation - Add support for Rails 4 and 5 when request does not have strong params
46

57
## RELEASE 3.0.4 - 2019-05-21
68
### Fixed

app/services/forest_liana/resource_creator.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ def perform
1313
begin
1414
if has_strong_parameter
1515
@record = @resource.create(resource_params)
16+
elsif Rails::VERSION::MAJOR >= 5
17+
@record = @resource.create(resource_params.to_unsafe_hash)
18+
elsif Rails::VERSION::MAJOR == 4
19+
@record = @resource.create(resource_params.to_hash)
1620
else
1721
@record = @resource.create(resource_params, without_protection: true)
1822
end

app/services/forest_liana/resource_updater.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ def perform
1515

1616
if has_strong_parameter
1717
@record.update_attributes(resource_params)
18+
elsif Rails::VERSION::MAJOR >= 5
19+
@record.update_attributes(resource_params.to_unsafe_hash)
20+
elsif Rails::VERSION::MAJOR == 4
21+
@record.update_attributes(resource_params.to_hash)
1822
else
1923
@record.update_attributes(resource_params, without_protection: true)
2024
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require 'minitest/mock'
2+
3+
module ForestLiana
4+
class ResourceCreatorTest < ActiveSupport::TestCase
5+
6+
collection = ForestLiana::Model::Collection.new({
7+
name: 'SerializeField',
8+
fields: [{
9+
type: 'String',
10+
field: 'field'
11+
}]
12+
})
13+
14+
ForestLiana.apimap << collection
15+
ForestLiana.models << SerializeField
16+
17+
test 'Create a record on a "serialize" attribute with a well formated value without strong params' do
18+
params = ActionController::Parameters.new(
19+
data: {
20+
type: "SerializeField",
21+
attributes: {
22+
field: "[\"test\", \"test\"]"
23+
}
24+
}
25+
)
26+
27+
creator = ResourceCreator.new(SerializeField, params)
28+
creator.stub :has_strong_parameter, false do
29+
creator.perform
30+
31+
assert creator.record.valid?
32+
assert creator.record.field == ["test", "test"]
33+
end
34+
end
35+
end
36+
end

test/services/forest_liana/resource_updater_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'minitest/mock'
2+
13
module ForestLiana
24
class ResourceUpdaterTest < ActiveSupport::TestCase
35

@@ -82,5 +84,26 @@ class ResourceUpdaterTest < ActiveSupport::TestCase
8284
assert updater.record.valid?
8385
assert updater.record.field == ["test", "test"]
8486
end
87+
88+
test 'Update a record on a "serialize" attribute with a well formated value without strong params' do
89+
params = ActionController::Parameters.new(
90+
id: 1,
91+
data: {
92+
id: 1,
93+
type: "SerializeField",
94+
attributes: {
95+
field: "[\"test\", \"test\"]"
96+
}
97+
}
98+
)
99+
100+
updater = ResourceUpdater.new(SerializeField, params)
101+
updater.stub :has_strong_parameter, false do
102+
updater.perform
103+
104+
assert updater.record.valid?
105+
assert updater.record.field == ["test", "test"]
106+
end
107+
end
85108
end
86109
end

0 commit comments

Comments
 (0)