test/fixtures/poro.rb in active_model_serializers-0.9.13 vs test/fixtures/poro.rb in active_model_serializers-0.10.0.rc1

- old
+ new

@@ -1,255 +1,206 @@ class Model - def initialize(hash = {}) + def initialize(hash={}) @attributes = hash end + def cache_key + "#{self.class.name.downcase}/#{self.id}-#{self.updated_at}" + end + + def updated_at + @attributes[:updated_at] ||= DateTime.now.to_time.to_i + end + def read_attribute_for_serialization(name) if name == :id || name == 'id' - object_id - elsif respond_to?(name) - send name + id else @attributes[name] end end -end - -### -## Models -### - -module TestNamespace2 - class Test < Model - attr_writer :sub_test - - def sub_test - @sub_test ||= TestNamespace2::SubTest.new(name: 'N1', description: 'D1') - end + def id + @attributes[:id] || @attributes['id'] || object_id end - class SubTest < Model; end -end - -class User < Model - def profile - @profile ||= Profile.new(name: 'N1', description: 'D1') + def to_param + id end -end -class UserInfo < Model - def user - @user ||= User.new(name: 'N1', email: 'E1') + def method_missing(meth, *args) + if meth.to_s =~ /^(.*)=$/ + @attributes[$1.to_sym] = args[0] + elsif @attributes.key?(meth) + @attributes[meth] + else + super + end end end class Profile < Model end -class Category < Model - def posts - @posts ||= [Post.new(title: 'T1', body: 'B1'), - Post.new(title: 'T2', body: 'B2')] - end -end +class ProfileSerializer < ActiveModel::Serializer + attributes :name, :description -class Post < Model - def comments - @comments ||= [Comment.new(content: 'C1'), - Comment.new(content: 'C2')] - end -end + urls :posts, :comments -class SpecialPost < Post - def special_comment - @special_comment ||= Comment.new(content: 'special') + def arguments_passed_in? + options[:my_options] == :accessible end end -class Type < Model -end +class ProfilePreviewSerializer < ActiveModel::Serializer + attributes :name -class SelfReferencingUser < Model - def type - @type ||= Type.new(name: 'N1') - end - def parent - @parent ||= SelfReferencingUserParent.new(name: 'N1') - end + urls :posts, :comments end -class SelfReferencingUserParent < Model - def type - @type ||= Type.new(name: 'N2') - end - def parent - end -end +Post = Class.new(Model) +Like = Class.new(Model) +Comment = Class.new(Model) +Author = Class.new(Model) +Bio = Class.new(Model) +Blog = Class.new(Model) +Role = Class.new(Model) +User = Class.new(Model) +Location = Class.new(Model) +Place = Class.new(Model) -class Comment < Model -end +module Spam; end +Spam::UnrelatedLink = Class.new(Model) -class WebLog < Model -end +PostSerializer = Class.new(ActiveModel::Serializer) do + cache key:'post', expires_in: 0.1 + attributes :id, :title, :body -class Interview < Model - def attachment - @attachment ||= Image.new(url: 'U1') + has_many :comments + belongs_to :blog + belongs_to :author + url :comments + + def blog + Blog.new(id: 999, name: "Custom blog") end -end -class Mail < Model - def attachments - @attachments ||= [Image.new(url: 'U1'), - Video.new(html: 'H1')] + def custom_options + options end end -class Image < Model -end +SpammyPostSerializer = Class.new(ActiveModel::Serializer) do + attributes :id + has_many :related -class Video < Model + def self.root_name + 'posts' + end end -### -## Serializers -### +CommentSerializer = Class.new(ActiveModel::Serializer) do + cache expires_in: 1.day + attributes :id, :body -module TestNamespace2 - class TestSerializer < ActiveModel::Serializer - attributes :name, :email + belongs_to :post + belongs_to :author - has_one :sub_test + def custom_options + options end - - class SubTestSerializer < ActiveModel::Serializer - def description - description = object.read_attribute_for_serialization(:description) - scope ? "#{description} - #{scope}" : description - end - - attributes :name, :description - end end -class UserSerializer < ActiveModel::Serializer - attributes :name, :email +AuthorSerializer = Class.new(ActiveModel::Serializer) do + cache key:'writer' + attributes :id, :name - has_one :profile + has_many :posts, embed: :ids + has_many :roles, embed: :ids + has_one :bio end -class TypeSerializer < ActiveModel::Serializer - attributes :name -end +RoleSerializer = Class.new(ActiveModel::Serializer) do + cache only: [:name] + attributes :id, :name, :description, :slug -class SelfReferencingUserParentSerializer < ActiveModel::Serializer - attributes :name - has_one :type, serializer: TypeSerializer, embed: :ids, embed_in_root: true -end - -class SelfReferencingUserSerializer < ActiveModel::Serializer - attributes :name - - has_one :type, serializer: TypeSerializer, embed: :ids, embed_in_root: true - has_one :parent, serializer: SelfReferencingUserSerializer, embed: :ids, embed_in_root: true -end - -class UserInfoSerializer < ActiveModel::Serializer - has_one :user, serializer: UserSerializer -end - -class ProfileSerializer < ActiveModel::Serializer - def description - description = object.read_attribute_for_serialization(:description) - scope ? "#{description} - #{scope}" : description + def slug + "#{name}-#{id}" end - attributes :name, :description + belongs_to :author end -class CategorySerializer < ActiveModel::Serializer - attributes :name +LikeSerializer = Class.new(ActiveModel::Serializer) do + attributes :id, :time - has_many :posts + belongs_to :post end -class PostSerializer < ActiveModel::Serializer - attributes :title, :body +LocationSerializer = Class.new(ActiveModel::Serializer) do + cache only: [:place] + attributes :id, :lat, :lng - alias_method :title, :title # silence method redefinition warning - def title - keyword = serialization_options[:highlight_keyword] - title = object.read_attribute_for_serialization(:title) - title = title.gsub(keyword,"'#{keyword}'") if keyword - title - end + belongs_to :place - has_many :comments + def place + 'Nowhere' + end end -class SpecialPostSerializer < ActiveModel::Serializer - attributes :title, :body - has_many :comments, root: :comments, embed_in_root: true, embed: :ids - has_one :special_comment, root: :comments, embed_in_root: true, embed: :ids -end +PlaceSerializer = Class.new(ActiveModel::Serializer) do + attributes :id, :name -class CommentSerializer < ActiveModel::Serializer - attributes :content + has_many :locations end -class WebLogSerializer < ActiveModel::Serializer - attributes :name, :display_name -end +BioSerializer = Class.new(ActiveModel::Serializer) do + cache except: [:content] + attributes :id, :content, :rating -class WebLogLowerCamelSerializer < WebLogSerializer - format_keys :lower_camel + belongs_to :author end -class InterviewSerializer < ActiveModel::Serializer - attributes :text +BlogSerializer = Class.new(ActiveModel::Serializer) do + attributes :id, :name - has_one :attachment, polymorphic: true + belongs_to :writer + has_many :articles end -class MailSerializer < ActiveModel::Serializer - attributes :body - - has_many :attachments, polymorphic: true +PaginatedSerializer = Class.new(ActiveModel::Serializer::ArraySerializer) do + def json_key + 'paginated' + end end -class ImageSerializer < ActiveModel::Serializer - attributes :url +AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do + attribute :id + attribute :name, key: :title end -class VideoSerializer < ActiveModel::Serializer - attributes :html -end +CommentPreviewSerializer = Class.new(ActiveModel::Serializer) do + attributes :id -class ShortProfileSerializer < ::ProfileSerializer; end - -module TestNamespace - class ProfileSerializer < ::ProfileSerializer; end - class UserSerializer < ::UserSerializer; end + belongs_to :post end -ActiveModel::Serializer.setup do |config| - config.default_key_type = :name -end +AuthorPreviewSerializer = Class.new(ActiveModel::Serializer) do + attributes :id -class NameKeyUserSerializer < ActiveModel::Serializer - attributes :name, :email - - has_one :profile + has_many :posts end -class NameKeyPostSerializer < ActiveModel::Serializer - attributes :title, :body +PostPreviewSerializer = Class.new(ActiveModel::Serializer) do + def self.root_name + 'posts' + end - has_many :comments -end + attributes :title, :body, :id -ActiveModel::Serializer.setup do |config| - config.default_key_type = nil + has_many :comments, serializer: CommentPreviewSerializer + belongs_to :author, serializer: AuthorPreviewSerializer end - +Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do + attributes :id +end