Skip to content
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 Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ gem "simplecov-lcov"
gem "undercover"

gem "pry"

gem "gem-release", require: false
20 changes: 11 additions & 9 deletions lib/transmutation/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class << self
# Define an attribute to be serialized
#
# @param attribute_name [Symbol] The name of the attribute to serialize
# @param block [Proc] The block to call to get the value of the attribute
# @yield [object] The block to call to get the value of the attribute
# - The block is called in the context of the serializer instance
#
# @example
Expand All @@ -66,21 +66,23 @@ def attribute(attribute_name, &block)
# @param association_name [Symbol] The name of the association to serialize
# @param namespace [String, Symbol, Module] The namespace to lookup the association's serializer in
# @param serializer [String, Symbol, Class] The serializer to use for the association's serialization
# @yield [object] The block to call to get the value of the association
# - The block is called in the context of the serializer instance
# - The return value from the block is automatically serialized
#
# @example
# class UserSerializer < Transmutation::Serializer
# association :posts
# association :comments, namespace: "Nested", serializer: "User::CommentSerializer"
# association :archived_posts do
# object.posts.archived
# end
# end
def association(association_name, namespace: nil, serializer: nil)
def association(association_name, namespace: nil, serializer: nil, &custom_block)
block = lambda do
serialize(
object.send(association_name),
namespace:,
serializer:,
depth: @depth + 1,
max_depth: @max_depth
)
association_instance = custom_block ? instance_exec(&custom_block) : object.send(association_name)

serialize(association_instance, namespace:, serializer:, depth: @depth + 1, max_depth: @max_depth)
end

attributes_config[association_name] = { block:, association: true }
Expand Down
2 changes: 1 addition & 1 deletion lib/transmutation/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Transmutation
VERSION = "0.4.5"
VERSION = "0.5.0"
end
11 changes: 6 additions & 5 deletions spec/system/dummy/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# frozen_string_literal: true

class Post
attr_accessor :id, :title, :body, :user_id
attr_accessor :id, :title, :body, :user_id, :published_at

def initialize(id:, title:, body:, user_id: nil)
def initialize(id:, title:, body:, user_id: nil, published_at: nil)
self.id = id
self.title = title
self.body = body
self.user_id = user_id
self.published_at = published_at
end

def user
Expand All @@ -20,9 +21,9 @@ def user

def self.all
[
Post.new(id: 1, title: "First post", body: "First!", user_id: 1),
Post.new(id: 2, title: "How does this work?", body: "body", user_id: 2),
Post.new(id: 3, title: "Second post!?", body: "Nope...", user_id: 1)
Post.new(id: 1, title: "First post", body: "First!", user_id: 1, published_at: Time.now),
Post.new(id: 2, title: "How does this work?", body: "body", user_id: 2, published_at: Time.now),
Post.new(id: 3, title: "Second post!?", body: "Nope...", user_id: 1, published_at: nil)
]
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class UserSerializer < Api::V1::UserSerializer
attributes :first_name, :last_name

has_many :posts, namespace: "::Api::V1"

has_many :published_posts, namespace: "::Api::V1" do
object.posts.reject { |post| post.published_at.nil? }
end
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions spec/system/rendering_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
"posts" => [
{ "id" => 1, "title" => "First post" },
{ "id" => 3, "title" => "Second post!?" }
],
"published_posts" => [
{ "id" => 1, "title" => "First post" }
]
}
end
Expand Down