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

Mark resource as readonly #343

Open
lcpriest opened this issue May 24, 2019 · 1 comment
Open

Mark resource as readonly #343

lcpriest opened this issue May 24, 2019 · 1 comment

Comments

@lcpriest
Copy link
Contributor

I was scouting around the code base and was unable to find a readonly or immutable class method or include for defining a readonly resource.

Is this something available currently?

If not, I think a simple solution might be to override the save method and raise an ResourceImmutableError. Would appreciate if you can think of any side effects of that off the top of your head.

@senid231
Copy link
Member

hi @lcpriest
Contributions are welcome =)
I think we can add it on resource level
something like this will be enough

class JsonApiClient::Resource
  class_attribute :_immutable, instance_writer: false, default: false

  def self.immutable(flag = true)
    self._immutable = flag
  end

  def self.inherited(subclass)
    subclass._immutable = false
  end

  def self.custom_endpoint(name, options = {})
    if _immutable
      request_method = options.fetch(:request_method, :get).to_sym
      raise JsonApiClient::Errors::ResourceImmutableError if request_method != :get
    end
  end

  def save
    raise JsonApiClient::Errors::ResourceImmutableError if _immutable
    # ...
  end

  def destroy
    raise JsonApiClient::Errors::ResourceImmutableError if _immutable
    # ...
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants