Skip to content

Commit 93bbc59

Browse files
authored
Merge pull request #1982 from bf4/better_ams_model_interface
Better AMS Model attributes interface
2 parents cd09e89 + 772b799 commit 93bbc59

File tree

12 files changed

+21
-15
lines changed

12 files changed

+21
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Breaking changes:
66

77
Features:
88

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

1113
Misc:

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class SomeResource < ActiveRecord::Base
110110
end
111111
# or
112112
class SomeResource < ActiveModelSerializers::Model
113-
attr_accessor :title, :body
113+
attributes :title, :body
114114
end
115115
```
116116

docs/ARCHITECTURE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ ActiveModelSerializers::Model may be used either as a template, or in production
105105

106106
```ruby
107107
class MyModel < ActiveModelSerializers::Model
108-
attr_accessor :id, :name, :level
108+
attributes :id, :name, :level
109109
end
110110
```
111111

docs/howto/serialize_poro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Fortunately, ActiveModelSerializers provides a [`ActiveModelSerializers::Model`]
2525
```ruby
2626
# my_model.rb
2727
class MyModel < ActiveModelSerializers::Model
28-
attr_accessor :id, :name, :level
28+
attributes :id, :name, :level
2929
end
3030
```
3131

lib/active_model_serializers/model.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ class Model
66
include ActiveModel::Model
77
include ActiveModel::Serializers::JSON
88

9+
def self.attributes(*names)
10+
attr_accessor(*names)
11+
end
12+
913
attr_reader :attributes, :errors
1014

1115
def initialize(attributes = {})

test/action_controller/serialization_scope_name_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
module SerializationScopeTesting
44
class User < ActiveModelSerializers::Model
5-
attr_accessor :id, :name, :admin
5+
attributes :id, :name, :admin
66
def admin?
77
admin
88
end
99
end
1010
class Comment < ActiveModelSerializers::Model
11-
attr_accessor :id, :body
11+
attributes :id, :body
1212
end
1313
class Post < ActiveModelSerializers::Model
14-
attr_accessor :id, :title, :body, :comments
14+
attributes :id, :title, :body, :comments
1515
end
1616
class PostSerializer < ActiveModel::Serializer
1717
attributes :id, :title, :body, :comments

test/active_model_serializers/model_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def setup
1010

1111
def test_initialization_with_string_keys
1212
klass = Class.new(ActiveModelSerializers::Model) do
13-
attr_accessor :key
13+
attributes :key
1414
end
1515
value = 'value'
1616

test/adapter/json_api/relationship_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def build_serializer_and_serialize_relationship(model, relationship_name, &block
384384

385385
def new_model(model_attributes)
386386
Class.new(ActiveModelSerializers::Model) do
387-
attr_accessor(*model_attributes.keys)
387+
attributes(*model_attributes.keys)
388388

389389
def self.name
390390
'TestModel'

test/cache_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def test_uses_adapter_in_cache_key
244244
# rubocop:disable Metrics/AbcSize
245245
def test_a_serializer_rendered_by_two_adapter_returns_differently_fetch_attributes
246246
Object.const_set(:Alert, Class.new(ActiveModelSerializers::Model) do
247-
attr_accessor :id, :status, :resource, :started_at, :ended_at, :updated_at, :created_at
247+
attributes :id, :status, :resource, :started_at, :ended_at, :updated_at, :created_at
248248
end)
249249
Object.const_set(:UncachedAlertSerializer, Class.new(ActiveModel::Serializer) do
250250
attributes :id, :status, :resource, :started_at, :ended_at, :updated_at, :created_at

test/fixtures/poro.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def respond_to_missing?(method_name, _include_private = false)
3131
# model.validate! # => ["cannot be nil"]
3232
# model.errors.full_messages # => ["name cannot be nil"]
3333
class ModelWithErrors < ::ActiveModelSerializers::Model
34-
attr_accessor :name
34+
attributes :name
3535
end
3636

3737
class Profile < Model

0 commit comments

Comments
 (0)