Skip to content

Better AMS Model attributes interface #1982

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

Merged
merged 1 commit into from
Nov 21, 2016
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Breaking changes:

Features:

- [#1982](https://github.com/rails-api/active_model_serializers/pull/1982) Add ActiveModelSerializers::Model.attributes to configure PORO attributes (@bf4).

Fixes:

Misc:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class SomeResource < ActiveRecord::Base
end
# or
class SomeResource < ActiveModelSerializers::Model
attr_accessor :title, :body
attributes :title, :body
end
```

Expand Down
2 changes: 1 addition & 1 deletion docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ ActiveModelSerializers::Model may be used either as a template, or in production

```ruby
class MyModel < ActiveModelSerializers::Model
attr_accessor :id, :name, :level
attributes :id, :name, :level
end
```

Expand Down
2 changes: 1 addition & 1 deletion docs/howto/serialize_poro.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Fortunately, ActiveModelSerializers provides a [`ActiveModelSerializers::Model`]
```ruby
# my_model.rb
class MyModel < ActiveModelSerializers::Model
attr_accessor :id, :name, :level
attributes :id, :name, :level
end
```

Expand Down
4 changes: 4 additions & 0 deletions lib/active_model_serializers/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class Model
include ActiveModel::Model
include ActiveModel::Serializers::JSON

def self.attributes(*names)
attr_accessor(*names)
end

attr_reader :attributes, :errors

def initialize(attributes = {})
Expand Down
6 changes: 3 additions & 3 deletions test/action_controller/serialization_scope_name_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

module SerializationScopeTesting
class User < ActiveModelSerializers::Model
attr_accessor :id, :name, :admin
attributes :id, :name, :admin
def admin?
admin
end
end
class Comment < ActiveModelSerializers::Model
attr_accessor :id, :body
attributes :id, :body
end
class Post < ActiveModelSerializers::Model
attr_accessor :id, :title, :body, :comments
attributes :id, :title, :body, :comments
end
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body, :comments
Expand Down
2 changes: 1 addition & 1 deletion test/active_model_serializers/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def setup

def test_initialization_with_string_keys
klass = Class.new(ActiveModelSerializers::Model) do
attr_accessor :key
attributes :key
end
value = 'value'

Expand Down
2 changes: 1 addition & 1 deletion test/adapter/json_api/relationship_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ def build_serializer_and_serialize_relationship(model, relationship_name, &block

def new_model(model_attributes)
Class.new(ActiveModelSerializers::Model) do
attr_accessor(*model_attributes.keys)
attributes(*model_attributes.keys)

def self.name
'TestModel'
Expand Down
2 changes: 1 addition & 1 deletion test/cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def test_uses_adapter_in_cache_key
# rubocop:disable Metrics/AbcSize
def test_a_serializer_rendered_by_two_adapter_returns_differently_fetch_attributes
Object.const_set(:Alert, Class.new(ActiveModelSerializers::Model) do
attr_accessor :id, :status, :resource, :started_at, :ended_at, :updated_at, :created_at
attributes :id, :status, :resource, :started_at, :ended_at, :updated_at, :created_at
end)
Object.const_set(:UncachedAlertSerializer, Class.new(ActiveModel::Serializer) do
attributes :id, :status, :resource, :started_at, :ended_at, :updated_at, :created_at
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def respond_to_missing?(method_name, _include_private = false)
# model.validate! # => ["cannot be nil"]
# model.errors.full_messages # => ["name cannot be nil"]
class ModelWithErrors < ::ActiveModelSerializers::Model
attr_accessor :name
attributes :name
end

class Profile < Model
Expand Down
6 changes: 3 additions & 3 deletions test/serializers/read_attribute_for_serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class Serializer
class ReadAttributeForSerializationTest < ActiveSupport::TestCase
# https://github.com/rails-api/active_model_serializers/issues/1653
class Parent < ActiveModelSerializers::Model
attr_accessor :id
attributes :id
end
class Child < Parent
attr_accessor :name
attributes :name
end
class ParentSerializer < ActiveModel::Serializer
attributes :$id
Expand All @@ -30,7 +30,7 @@ def test_child_serializer_calls_dynamic_method_in_parent_serializer

# https://github.com/rails-api/active_model_serializers/issues/1658
class ErrorResponse < ActiveModelSerializers::Model
attr_accessor :error
attributes :error
end
class ApplicationSerializer < ActiveModel::Serializer
attributes :status
Expand Down
4 changes: 2 additions & 2 deletions test/serializers/serialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ module ActiveModel
class Serializer
class SerializationTest < ActiveSupport::TestCase
class Blog < ActiveModelSerializers::Model
attr_accessor :id, :name, :authors
attributes :id, :name, :authors
end
class Author < ActiveModelSerializers::Model
attr_accessor :id, :name
attributes :id, :name
end
class BlogSerializer < ActiveModel::Serializer
attributes :id
Expand Down