Skip to content

Commit

Permalink
Release v0.6.1 (bugfix release)
Browse files Browse the repository at this point in the history
  • Loading branch information
MoskitoHero committed Jun 14, 2024
1 parent 964be2f commit 6142df5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 27 deletions.
19 changes: 12 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
# Changelog

## v0.6.1 (2024-06-14)

`v0.6.1` is a patch release that fixes a bug in the `scope` argument of the `many` method.

## v0.6.0 (2024-06-14)

### ✨New features
- Added the `context` argument to the serializer's `initialize` method. This allows you to pass a context hash to the serializer, which can be used to pass arguments to the serializer, that can be used with a `context` object within the serializer definition.
```ruby
class UserSerializer < Barley::Serializer
attributes :id, :name

def name
if context[:upcase]
object.name.upcase
Expand All @@ -15,7 +20,7 @@
end
end
end

Serializer.new(User.last, context: {upcase: true}).serializable_hash
# => { id: 1, name: "JOHN DOE" }
```
Expand All @@ -27,7 +32,7 @@
many :posts, scope: :published do
attributes :id, :title
end
many :posts, key: :popular, scope: -> { where("views > 10_000").order(views: :desc).limit(5) } do
attributes :id, :title
end
Expand All @@ -47,7 +52,7 @@
```ruby
class UserSerializer < Barley::Serializer
attributes :id, :name
def name
if context[:upcase]
object.name.upcase
Expand All @@ -56,7 +61,7 @@
end
end
end
Serializer.new(User.last).with_context(upcase: true).serializable_hash
# => { id: 1, name: "JOHN DOE" }
```
Expand Down Expand Up @@ -110,10 +115,10 @@ User.last.as_json(root: true)
```ruby
many :posts do
attributes :id, :title, :body
one :author do
attributes :name, :email
one :profile, key_name: :author_profile do
attributes :id, :bio
end
Expand Down
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Barley is a fast and efficient ActiveModel serializer.

Cerealize your ActiveModel objects into flat hashes with a dead simple, yet versatile DSL, and caching and type-checking baked in. Our daily bread is to make your API faster.
Cerealize your ActiveModel objects into flat hashes with a dead simple, yet versatile DSL, and caching and type-checking baked in. Our daily bread is to make your API faster.

You don't believe us? Check out the [benchmarks](#benchmarks). 😎

Expand All @@ -28,30 +28,30 @@ Then define your attributes and associations in a serializer class.
```ruby
# /app/serializers/user_serializer.rb
class UserSerializer < Barley::Serializer

attributes id: Types::Strict::Integer, :name

attribute :email
attribute :value, type: Types::Coercible::Integer

many :posts

many :posts, key_name: :featured, scope: :featured

many :posts, key_name: :popular, scope: -> { where("views > 10_000").limit(3) }

one :group, serializer: CustomGroupSerializer

many :related_users, key: :friends, cache: true

one :profile, cache: { expires_in: 1.day } do
attributes :avatar, :social_url

attribute :badges do
object.badges.map(&:display_name)
end
end

end
```

Expand Down Expand Up @@ -89,7 +89,7 @@ You can also define the serializer class with the `serializer` macro.
# /app/models/user.rb
class User < ApplicationRecord
include Barley::Serializable

serializer UserSerializer
end
```
Expand Down Expand Up @@ -143,12 +143,12 @@ You can define a custom serializer for the association with the `serializer` opt

You can of course define serializers with inner classes for simple needs.

```ruby
```ruby
class UserSerializer < Barley::Serializer
attributes :id, :name, :email, :created_at, :updated_at

one :group, serializer: LocalGroupSerializer

class LocalGroupSerializer < Barley::Serializer
attributes :id, :name
end
Expand Down Expand Up @@ -208,7 +208,7 @@ Feel like using a block to define your associations? You can do that too.
```ruby
many :posts do
attributes :id, :title, :body

one :author do
attributes :name, :email
end
Expand Down Expand Up @@ -240,7 +240,7 @@ class PostSerializer < Barley::Serializer
attribute :is_owner do
object.user == context.current_user
end

many :comments do
many :likes do
attribute :is_owner do
Expand All @@ -264,7 +264,7 @@ serializer = PostSerializer.new(Post.last, context: my_context)
You have two generators available. One to generate the serializer class:

```shell
rails generate barley:serializer User
rails generate barley:serializer User
# or
rails generate barley:serializer User --name=CustomUserSerializer
```
Expand Down Expand Up @@ -318,7 +318,7 @@ end
```

## Type checking
Barley can check the type of the object you are serializing with the [dry-types](https://dry-rb.org/gems/dry-types/main/) gem.
Barley can check the type of the object you are serializing with the [dry-types](https://dry-rb.org/gems/dry-types/main/) gem.

It will raise an error if the object is not of the expected type, or coerce it to the correct type and perform constraints checks.

Expand All @@ -345,7 +345,7 @@ You will soon be able to replace all occurrences of `Serializer` with `Cerealize
# /app/models/user.rb
class User < ApplicationRecord
include Barley::Cerealizable

cerealizer UserCerealizer
end

Expand Down
7 changes: 6 additions & 1 deletion lib/barley/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def one(key, key_name: nil, serializer: nil, cache: false, &block)
# @param key_name [Symbol] the key name in the hash
# @param serializer [Class] the serializer to use
# @param cache [Boolean, Hash<Symbol, ActiveSupport::Duration>] whether to cache the result, or a hash with options for the cache
# @param scope [Symbol] the scope to use to fetch the elements
# @param scope [Symbol, Proc] the scope to use to fetch the elements
# @param block [Proc] a block to use to define the serializer inline
def many(key, key_name: nil, serializer: nil, cache: false, scope: nil, &block)
key_name ||= key
Expand All @@ -183,6 +183,11 @@ def many(key, key_name: nil, serializer: nil, cache: false, scope: nil, &block)
end
define_method(key_name) do
elements = object.send(key)
if scope.is_a?(Symbol)
elements = elements.send(scope)
elsif scope.is_a?(Proc)
elements = elements.instance_exec(&scope)
end
return [] if elements.empty?

el_serializer = serializer || elements.first.serializer.class
Expand Down
2 changes: 1 addition & 1 deletion lib/barley/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Barley
VERSION = "0.6"
VERSION = "0.6.1"
end

0 comments on commit 6142df5

Please sign in to comment.