Skip to content

Allow passing options to attributes the same way as to attribute#266

Merged
lgebhardt merged 1 commit intoJSONAPI-Resources:masterfrom
Garage-mca:feature-attributes-options
Jun 19, 2015
Merged

Allow passing options to attributes the same way as to attribute#266
lgebhardt merged 1 commit intoJSONAPI-Resources:masterfrom
Garage-mca:feature-attributes-options

Conversation

@biomancer
Copy link
Copy Markdown
Contributor

Like this

attributes :starts_at, :ends_at, format: :timestamp

lgebhardt added a commit that referenced this pull request Jun 19, 2015
Allow passing options to attributes the same way as to attribute
@lgebhardt lgebhardt merged commit 5aaf22c into JSONAPI-Resources:master Jun 19, 2015
@lgebhardt
Copy link
Copy Markdown
Contributor

Great addition. Thanks!

@butchmarshall
Copy link
Copy Markdown
Contributor

I am passing..

{..., "status-at"=>1512650542}

with JSONAPI::Resource defining

attributes :status_at, format: :timestamp

and get:

Internal Server Error: undefined method `unformat' for nil:NilClass /home/butch/.rvm/gems/ruby-2.4.2/bundler/gems/jsonapi-resources-3a02a4d2e149/lib/jsonapi/request_parser.rb:580:in `unformat_value'
/home/butch/.rvm/gems/ruby-2.4.2/bundler/gems/jsonapi-resources-3a02a4d2e149/lib/jsonapi/request_parser.rb:513:in `block (2 levels) in parse_params'
/home/butch/.rvm/gems/ruby-2.4.2/gems/actionpack-4.2.10/lib/action_controller/metal/strong_parameters.rb:185:in `each_pair'
/home/butch/.rvm/gems/ruby-2.4.2/gems/actionpack-4.2.10/lib/action_controller/metal/strong_parameters.rb:185:in `each_pair'

@senid231
Copy link
Copy Markdown
Contributor

senid231 commented Dec 11, 2017

@butchmarshall it's because jsonapi-resources doesn't provide custom value formatters
you need to create your own
something like this:

class TimestampValueFormatter < JSONAPI::ValueFormatter
  class << self
    def format(raw_value)
      raw_value.to_i unless raw_value.nil?
    end

    def unformat(raw_value)
      Time.at(raw_value) unless raw_value.nil?
    end
  end
end

but I agree that error undefined method unformat'` is wrong in this case
we should reset to default value formatter and provide warning or raise correct error.
In any case I think it should be in a separate issue

@lgebhardt what do you think we should do in this case?

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

Successfully merging this pull request may close these issues.

4 participants