test/fixtures/poro.rb in active_model_serializers-0.10.0.rc3 vs test/fixtures/poro.rb in active_model_serializers-0.10.0.rc4
- old
+ new
@@ -1,68 +1,43 @@
verbose = $VERBOSE
$VERBOSE = nil
-class Model
+class Model < ActiveModelSerializers::Model
FILE_DIGEST = Digest::MD5.hexdigest(File.open(__FILE__).read)
- def self.model_name
- @_model_name ||= ActiveModel::Name.new(self)
- end
-
- def initialize(hash = {})
- @attributes = hash
- end
-
- def cache_key
- "#{self.class.name.downcase}/#{self.id}-#{self.updated_at.strftime("%Y%m%d%H%M%S%9N")}"
- end
-
- def serializable_hash(options = nil)
- @attributes
- end
-
- def read_attribute_for_serialization(name)
- if name == :id || name == 'id'
- id
- else
- @attributes[name]
- end
- end
-
- def id
- @attributes[:id] || @attributes['id'] || object_id
- end
-
### Helper methods, not required to be serializable
- #
- # Convenience for adding @attributes readers and writers
+
+ # Convenience when not adding @attributes readers and writers
def method_missing(meth, *args)
if meth.to_s =~ /^(.*)=$/
- @attributes[$1.to_sym] = args[0]
- elsif @attributes.key?(meth)
- @attributes[meth]
+ attributes[$1.to_sym] = args[0]
+ elsif attributes.key?(meth)
+ attributes[meth]
else
super
end
end
+ # required for ActiveModel::AttributeAssignment#_assign_attribute
+ # in Rails 5
+ def respond_to_missing?(method_name, _include_private = false)
+ attributes.key?(method_name.to_s.tr('=', '').to_sym) || super
+ end
+
def cache_key_with_digest
"#{cache_key}/#{FILE_DIGEST}"
end
-
- def updated_at
- @attributes[:updated_at] ||= DateTime.now.to_time
- end
end
class Profile < Model
end
class ProfileSerializer < ActiveModel::Serializer
attributes :name, :description
+ # TODO: is this used anywhere?
def arguments_passed_in?
- options[:my_options] == :accessible
+ instance_options[:my_options] == :accessible
end
end
class ProfilePreviewSerializer < ActiveModel::Serializer
attributes :name
@@ -99,33 +74,30 @@
def blog
Blog.new(id: 999, name: 'Custom blog')
end
+ # TODO: is this used anywhere?
def custom_options
- options
+ instance_options
end
end
SpammyPostSerializer = Class.new(ActiveModel::Serializer) do
attributes :id
has_many :related
-
- def self.root_name
- 'posts'
- end
end
CommentSerializer = Class.new(ActiveModel::Serializer) do
cache expires_in: 1.day, skip_digest: true
attributes :id, :body
belongs_to :post
belongs_to :author
def custom_options
- options
+ instance_options
end
end
AuthorSerializer = Class.new(ActiveModel::Serializer) do
cache key: 'writer', skip_digest: true
@@ -140,11 +112,11 @@
RoleSerializer = Class.new(ActiveModel::Serializer) do
cache only: [:name], skip_digest: true
attributes :id, :name, :description, :slug
def slug
- "#{name}-#{id}"
+ "#{object.name}-#{object.id}"
end
belongs_to :author
end
@@ -184,11 +156,11 @@
belongs_to :writer
has_many :articles
end
-PaginatedSerializer = Class.new(ActiveModel::Serializer::ArraySerializer) do
+PaginatedSerializer = Class.new(ActiveModel::Serializer::CollectionSerializer) do
def json_key
'paginated'
end
end
@@ -215,13 +187,9 @@
has_many :posts
end
PostPreviewSerializer = Class.new(ActiveModel::Serializer) do
- def self.root_name
- 'posts'
- end
-
attributes :title, :body, :id
has_many :comments, serializer: CommentPreviewSerializer
belongs_to :author, serializer: AuthorPreviewSerializer
end