Sha256: bd786c1d1340fa636478b2013eea2302bb838d4a728e4664e763c36247cf6b1f

Contents?: true

Size: 1.46 KB

Versions: 19

Compression:

Stored size: 1.46 KB

Contents

require 'active_record'

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
    t.string :title
    t.text :body
    t.references :author
    t.timestamps null: false
  end
  create_table :authors, force: true do |t|
    t.string :name
    t.string :code
    t.timestamps null: false
  end
  create_table :comments, force: true do |t|
    t.text :contents
    t.references :author
    t.references :post
    t.timestamp null: false
  end
end

module ARModels
  class Post < ActiveRecord::Base
    has_many :comments
    belongs_to :author
  end

  class Comment < ActiveRecord::Base
    belongs_to :post
    belongs_to :author
  end

  class Author < ActiveRecord::Base
    has_many :posts
  end

  class PostDeserializer < FunWithJsonApi::Deserializer
    resource_class Post

    attribute :title
    attribute :body

    has_many :comments, -> { CommentDeserializer }
    belongs_to :author, -> { AuthorDeserializer }
  end

  class CommentDeserializer < FunWithJsonApi::Deserializer
    resource_class Comment

    attribute :contents

    belongs_to :author, -> { AuthorDeserializer }
  end

  class AuthorSerializer < ActiveModel::Serializer
    type 'person'
    attribute :name
    has_many :posts
  end

  class AuthorDeserializer < FunWithJsonApi::Deserializer
    type 'person'
    resource_class Author

    attribute :name

    has_many :posts, -> { PostDeserializer }
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
fun_with_json_api-0.0.11.1 spec/fixtures/active_record.rb
fun_with_json_api-0.0.11 spec/fixtures/active_record.rb
fun_with_json_api-0.0.10.4 spec/fixtures/active_record.rb
fun_with_json_api-0.0.10.3 spec/fixtures/active_record.rb
fun_with_json_api-0.0.10.2 spec/fixtures/active_record.rb
fun_with_json_api-0.0.10.1 spec/fixtures/active_record.rb
fun_with_json_api-0.0.10 spec/fixtures/active_record.rb
fun_with_json_api-0.0.9 spec/fixtures/active_record.rb
fun_with_json_api-0.0.8.2 spec/fixtures/active_record.rb
fun_with_json_api-0.0.8.1 spec/fixtures/active_record.rb
fun_with_json_api-0.0.8 spec/fixtures/active_record.rb
fun_with_json_api-0.0.7 spec/fixtures/active_record.rb
fun_with_json_api-0.0.6.pre.alpha.2 spec/fixtures/active_record.rb
fun_with_json_api-0.0.6.pre.alpha.1 spec/fixtures/active_record.rb
fun_with_json_api-0.0.6.1 spec/fixtures/active_record.rb
fun_with_json_api-0.0.6 spec/fixtures/active_record.rb
fun_with_json_api-0.0.5 spec/fixtures/active_record.rb
fun_with_json_api-0.0.4 spec/fixtures/active_record.rb
fun_with_json_api-0.0.3 spec/fixtures/active_record.rb