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

ActiveModelSerializers::Model is broken or documentation is out of date #2001

Closed
joelpresence opened this issue Dec 14, 2016 · 5 comments
Closed

Comments

@joelpresence
Copy link

Expected behavior vs actual behavior

Thanks for your hard work on AMS! :-)

Expected: If I follow the official AMS documentation to create my own serializable resource that inherits from ActiveModelSerializers::Model, it should work and not fail with a syntax error.

Actual: fails with error:

rails console> class SomeResource < ActiveModelSerializers::Model; attributes :title, :body; end
NoMethodError: undefined method `attributes' for SomeResource:Class
Did you mean?  attribute_method?
	from (irb):17:in `<class:SomeResource>'

Steps to reproduce

(e.g., detailed walkthrough, runnable script, example application)

Follow the instructions to create your own serializable resource (quoting instructions):

# either
class SomeResource < ActiveRecord::Base
  # columns: title, body
end
# or
class SomeResource < ActiveModelSerializers::Model
  attributes :title, :body
end

The method attributes does NOT exist on the class ActiveModelSerializers::Model as you can see:

rails console> ActiveModelSerializers::Model.methods.sort.find_all { |m| m =~ /attributes/ }
=> []

We can actually inspect the class itself at /Users/joel/.rvm/gems/ruby-2.3.3/gems/active_model_serializers-0.10.3/lib/active_model_serializers/model.rb:

# ActiveModelSerializers::Model is a convenient
# serializable class to inherit from when making
# serializable non-activerecord objects.
module ActiveModelSerializers
  class Model
    include ActiveModel::Model
    include ActiveModel::Serializers::JSON

    attr_reader :attributes, :errors

    def initialize(attributes = {})
      @attributes = attributes && attributes.symbolize_keys
      @errors = ActiveModel::Errors.new(self)
      super
    end

    # Defaults to the downcased model name.
    def id
      attributes.fetch(:id) { self.class.name.downcase }
    end

    # Defaults to the downcased model name and updated_at
    def cache_key
      attributes.fetch(:cache_key) { "#{self.class.name.downcase}/#{id}-#{updated_at.strftime('%Y%m%d%H%M%S%9N')}" }
    end

    # Defaults to the time the serializer file was modified.
    def updated_at
      attributes.fetch(:updated_at) { File.mtime(__FILE__) }
    end

    def read_attribute_for_serialization(key)
      if key == :id || key == 'id'
        attributes.fetch(key) { id }
      else
        attributes[key]
      end
    end

    # The following methods are needed to be minimally implemented for ActiveModel::Errors
    # :nocov:
    def self.human_attribute_name(attr, _options = {})
      attr
    end

    def self.lookup_ancestors
      [self]
    end
    # :nocov:
  end
end

No attributes method that I can see.

The only way I could get this to work is as follows:

class SomeResource <ActiveModelSerializers::Model
  attr_accessor :title, :body
end

That works just fine and serializes as expected.

So either the documentation is wrong (and needs to mention attr_accessor) or the code is wrong in ActiveModelSerializers::Model (and needs to add the attributes class method) or both?

Any ideas how to fix this or if I missed something obvious?

This is on a rails app generated using rails new my_app --database=postgresql --api in case that matters.

I verified that I can reproduce this repeatedly on a brand new rails app (with active_model_serializers 0.10.3 installed) so this is definitely not a problem with my rails app or my setup: it is perfectly repeatable on a clean new rails app.

Environment

ActiveModelSerializers Version (commit ref if not on tag):

0.10.3

Output of ruby -e "puts RUBY_DESCRIPTION":

ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin16]

OS Type & Version:

OS X 10.12.1

Integrated application and version (e.g., Rails, Grape, etc):

rails 5.0.0.1
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin16]
OS X 10.12.1

Backtrace

(e.g., provide any applicable backtraces from your application)

Additonal helpful information

(e.g., Gemfile.lock, configurations, PR containing a failing test, git bisect results)

@bf4
Copy link
Member

bf4 commented Dec 15, 2016

Thanks for the report. Closed by #2000

@bf4 bf4 closed this as completed Dec 15, 2016
@bf4
Copy link
Member

bf4 commented Dec 15, 2016

@joelpresence We're not quite ready to release that API. Am working on it.

@joelpresence
Copy link
Author

@bf4 thanks for responding, but how do I see the correct documentation for 0.10.3 that shows how to use ActiveModelSerializers::Model since the README on master shows examples that cause syntax errors ... Is there a link to documentation for 0.10.3 as released?

Also, what do you mean by "that API"?

Thanks again and take care!

@joelpresence
Copy link
Author

Oh, https://github.com/rails-api/active_model_serializers/tree/v0.10.3 , I see, thanks! I see that it mentions attr_accessor.

@mltsy
Copy link

mltsy commented Dec 30, 2016

Ooooh! Thanks 😄 Same problem. I really have to get better at identifying the correct version of documentation for the gems I'm using!

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

3 participants