Skip to content

Commit 134225a

Browse files
committed
Merge pull request #245 from adomokos/renaming-createable-and-updateable
Renaming createable and updateable to 'creatable' and 'updatable'
2 parents 019fa02 + 47e0f00 commit 134225a

7 files changed

Lines changed: 76 additions & 24 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ end
119119
Context flows through from the controller and can be used to control the attributes based on the current user (or other
120120
value).
121121

122-
##### Createable and Updateable Attributes
122+
##### Creatable and Updatable Attributes
123123

124-
By default all attributes are assumed to be updateable and createable. To prevent some attributes from being accepted by
125-
the `update` or `create` methods, override the `self.updateable_fields` and `self.createable_fields` methods on a resource.
124+
By default all attributes are assumed to be updatable and creatable. To prevent some attributes from being accepted by
125+
the `update` or `create` methods, override the `self.updatable_fields` and `self.creatable_fields` methods on a resource.
126126

127127
This example prevents `full_name` from being set:
128128

@@ -136,18 +136,18 @@ class ContactResource < JSONAPI::Resource
136136
"#{@model.name_first}, #{@model.name_last}"
137137
end
138138

139-
def self.updateable_fields(context)
139+
def self.updatable_fields(context)
140140
super - [:full_name]
141141
end
142142

143-
def self.createable_fields(context)
143+
def self.creatable_fields(context)
144144
super - [:full_name]
145145
end
146146
end
147147
```
148148

149149
The `context` is not by default used by the `ResourceController`, but may be used if you override the controller methods.
150-
By using the context you have the option to determine the createable and updateable fields based on the user.
150+
By using the context you have the option to determine the creatable and updatable fields based on the user.
151151

152152
##### Sortable Attributes
153153

jsonapi-resources.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ Gem::Specification.new do |spec|
2525
spec.add_development_dependency 'minitest-spec-rails'
2626
spec.add_development_dependency 'minitest-reporters'
2727
spec.add_development_dependency 'simplecov'
28+
spec.add_development_dependency 'pry'
2829
spec.add_dependency 'rails', '>= 4.0'
2930
end

lib/jsonapi/request.rb

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,22 @@ def add_show_related_resources_operation(association_type)
316316
)
317317
end
318318

319+
# TODO: Please remove after `createable_fields` is removed
320+
# :nocov:
321+
def creatable_fields
322+
if @resource_klass.respond_to?(:createable_fields)
323+
creatable_fields = @resource_klass.createable_fields(@context)
324+
else
325+
creatable_fields = @resource_klass.creatable_fields(@context)
326+
end
327+
end
328+
# :nocov:
329+
319330
def parse_add_operation(data)
320331
Array.wrap(data).each do |params|
321332
verify_type(params[:type])
322333

323-
data = parse_params(params, @resource_klass.createable_fields(@context))
334+
data = parse_params(params, creatable_fields)
324335
@operations.push JSONAPI::CreateResourceOperation.new(
325336
@resource_klass,
326337
{
@@ -486,12 +497,23 @@ def verify_permitted_params(params, allowed_fields)
486497
raise JSONAPI::Exceptions::ParametersNotAllowed.new(params_not_allowed) if params_not_allowed.length > 0
487498
end
488499

500+
# TODO: Please remove after `updateable_fields` is removed
501+
# :nocov:
502+
def updatable_fields
503+
if @resource_klass.respond_to?(:updateable_fields)
504+
@resource_klass.updateable_fields(@context)
505+
else
506+
@resource_klass.updatable_fields(@context)
507+
end
508+
end
509+
# :nocov:
510+
489511
def parse_add_association_operation(data, association_type, parent_key)
490512
association = resource_klass._association(association_type)
491513

492514
if association.is_a?(JSONAPI::Association::HasMany)
493515
object_params = {relationships: {format_key(association.name) => {data: data}}}
494-
verified_param_set = parse_params(object_params, @resource_klass.updateable_fields(@context))
516+
verified_param_set = parse_params(object_params, updatable_fields)
495517

496518
@operations.push JSONAPI::CreateHasManyAssociationOperation.new(
497519
resource_klass,
@@ -509,8 +531,7 @@ def parse_update_association_operation(data, association_type, parent_key)
509531

510532
if association.is_a?(JSONAPI::Association::HasOne)
511533
object_params = {relationships: {format_key(association.name) => {data: data}}}
512-
513-
verified_param_set = parse_params(object_params, @resource_klass.updateable_fields(@context))
534+
verified_param_set = parse_params(object_params, updatable_fields)
514535

515536
@operations.push JSONAPI::ReplaceHasOneAssociationOperation.new(
516537
resource_klass,
@@ -526,7 +547,7 @@ def parse_update_association_operation(data, association_type, parent_key)
526547
end
527548

528549
object_params = {relationships: {format_key(association.name) => {data: data}}}
529-
verified_param_set = parse_params(object_params, @resource_klass.updateable_fields(@context))
550+
verified_param_set = parse_params(object_params, updatable_fields)
530551

531552
@operations.push JSONAPI::ReplaceHasManyAssociationOperation.new(
532553
resource_klass,
@@ -564,7 +585,7 @@ def parse_single_replace_operation(data, keys)
564585
@resource_klass,
565586
{
566587
resource_id: key,
567-
data: parse_params(data, @resource_klass.updateable_fields(@context))
588+
data: parse_params(data, updatable_fields)
568589
}
569590
)
570591
end

lib/jsonapi/resource.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,29 @@ def primary_key(key)
287287
@_primary_key = key.to_sym
288288
end
289289

290-
# Override in your resource to filter the updateable keys
291-
def updateable_fields(context = nil)
292-
_updateable_associations | _attributes.keys - [:id]
290+
# TODO: remove this after the createable_fields and updateable_fields are phased out
291+
# :nocov:
292+
def method_missing(method, *args)
293+
if method.to_s.match /createable_fields/
294+
ActiveSupport::Deprecation.warn("`createable_fields` is deprecated, please use `creatable_fields` instead")
295+
self.creatable_fields(*args)
296+
elsif method.to_s.match /updateable_fields/
297+
ActiveSupport::Deprecation.warn("`updateable_fields` is deprecated, please use `updatable_fields` instead")
298+
self.updatable_fields(*args)
299+
else
300+
super
301+
end
293302
end
303+
# :nocov:
294304

295-
# Override in your resource to filter the createable keys
296-
def createable_fields(context = nil)
297-
_updateable_associations | _attributes.keys
305+
# Override in your resource to filter the updatable keys
306+
def updatable_fields(context = nil)
307+
_updatable_associations | _attributes.keys - [:id]
308+
end
309+
310+
# Override in your resource to filter the creatable keys
311+
def creatable_fields(context = nil)
312+
_updatable_associations | _attributes.keys
298313
end
299314

300315
# Override in your resource to filter the sortable keys
@@ -464,7 +479,7 @@ def _attribute_options(attr)
464479
default_attribute_options.merge(@_attributes[attr])
465480
end
466481

467-
def _updateable_associations
482+
def _updatable_associations
468483
@_associations.map { |key, association| key }
469484
end
470485

@@ -617,5 +632,6 @@ def _associate(klass, *attrs)
617632
end
618633
end
619634
end
635+
620636
end
621637
end

test/controllers/controller_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2321,4 +2321,4 @@ def test_index_default_filter_override
23212321
assert json_response['data'].is_a?(Array)
23222322
assert_equal 4, json_response['data'].size
23232323
end
2324-
end
2324+
end

test/fixtures/active_record.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,11 +613,11 @@ def subject
613613
filters :title, :author, :tags, :comments
614614
filters :id, :ids
615615

616-
def self.updateable_fields(context)
616+
def self.updatable_fields(context)
617617
super(context) - [:author, :subject]
618618
end
619619

620-
def self.createable_fields(context)
620+
def self.creatable_fields(context)
621621
super(context) - [:subject]
622622
end
623623

test/unit/resource/resource_test.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,22 @@ def test_find_by_key_with_customized_base_records
132132
end
133133
end
134134

135-
def test_updateable_fields_does_not_include_id
136-
assert(!CatResource.updateable_fields.include?(:id))
135+
def test_updatable_fields_does_not_include_id
136+
assert(!CatResource.updatable_fields.include?(:id))
137+
end
138+
139+
# TODO: Please remove after `updateable_fields` is removed
140+
def test_updateable_fields_delegates_to_updatable_fields_with_deprecation
141+
ActiveSupport::Deprecation.silence do
142+
assert_empty(CatResource.updateable_fields(nil) - [:mother, :father, :name, :breed])
143+
end
144+
end
145+
146+
# TODO: Please remove after `createable_fields` is removed
147+
def test_createable_fields_delegates_to_creatable_fields_with_deprecation
148+
ActiveSupport::Deprecation.silence do
149+
assert_empty(CatResource.createable_fields(nil) - [:mother, :father, :name, :breed, :id])
150+
end
137151
end
138152

139153
def test_has_many_association_filters

0 commit comments

Comments
 (0)