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 an immutable option to JSON API resources. #346

Merged
merged 1 commit into from
Aug 29, 2019
Merged
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
6 changes: 6 additions & 0 deletions lib/json_api_client/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def initialize(env, msg = nil)
class ClientError < ApiError
end

class ResourceImmutableError < StandardError
def initialize(msg = 'Resource immutable')
super msg
end
end

class AccessDenied < ClientError
end

Expand Down
25 changes: 25 additions & 0 deletions lib/json_api_client/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class Resource
instance_accessor: false
class_attribute :add_defaults_to_changes,
instance_writer: false

class_attribute :_immutable,
instance_writer: false,
default: false

self.primary_key = :id
self.parser = Parsers::Parser
self.paginator = Paginating::Paginator
Expand Down Expand Up @@ -94,6 +99,18 @@ def type
table_name
end

# Indicates whether this resource is mutable or immutable;
# by default, all resources are mutable.
#
# @return [Boolean]
def immutable(flag = true)
self._immutable = flag
end

def inherited(subclass)
subclass._immutable = false
end

# Specifies the relative path that should be used for this resource;
# by default, this is inferred from the resource class name.
#
Expand Down Expand Up @@ -215,6 +232,11 @@ def route_formatter
# @option [Symbol] :on One of [:collection or :member] to decide whether it's a collect or member method
# @option [Symbol] :request_method The request method (:get, :post, etc)
def custom_endpoint(name, options = {})
if _immutable
request_method = options.fetch(:request_method, :get).to_sym
raise JsonApiClient::Errors::ResourceImmutableError if request_method != :get
end

if :collection == options.delete(:on)
collection_endpoint(name, options)
else
Expand Down Expand Up @@ -440,6 +462,7 @@ def valid?(context = nil)
# @return [Boolean] Whether or not the save succeeded
def save
return false unless valid?
raise JsonApiClient::Errors::ResourceImmutableError if _immutable

self.last_result_set = if persisted?
self.class.requestor.update(self)
Expand Down Expand Up @@ -471,6 +494,8 @@ def save
#
# @return [Boolean] Whether or not the destroy succeeded
def destroy
raise JsonApiClient::Errors::ResourceImmutableError if _immutable

self.last_result_set = self.class.requestor.destroy(self)
if last_result_set.has_errors?
fill_errors
Expand Down
10 changes: 10 additions & 0 deletions test/unit/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,14 @@ def test_default_params_overrideable
assert_equal(article.type, 'Story')
end

def test_immutable
Article.immutable(true)
article = Article.new(type: 'Story')
assert_raises JsonApiClient::Errors::ResourceImmutableError do
article.save
end
ensure
Article.immutable(false)
end

end