README.md in alba-3.2.0 vs README.md in alba-3.3.0
- old
+ new
@@ -96,11 +96,11 @@
### Other reasons
- Dependency free, no need to install `oj` or `activesupport` while Alba works well with them
- Well tested, the test coverage is 99%
-- Well maintained, gettings frequent update and new releases (see [version history](https://rubygems.org/gems/alba/versions))
+- Well maintained, getting frequent update and new releases (see [version history](https://rubygems.org/gems/alba/versions))
## Installation
Add this line to your application's Gemfile:
@@ -315,11 +315,11 @@
end
FooResource.new(Foo.new).serialize
```
-By default, Alba create the JSON as `'{"bar":"This is FooResource"}'`. This means Alba calls a method on a Resource class and doesn't call a method on a target object. This rule is applied to methods that are explicitly defined on Resource class, so methods that Resource class inherits from `Object` class such as `format` are ignored.
+By default, Alba creates the JSON as `'{"bar":"This is FooResource"}'`. This means Alba calls a method on a Resource class and doesn't call a method on a target object. This rule is applied to methods that are explicitly defined on Resource class, so methods that Resource class inherits from `Object` class such as `format` are ignored.
```ruby
class Foo
def format
'This is Foo'
@@ -1021,10 +1021,40 @@
user = User.new(1, nil, nil)
UserResource.new(user).serialize # => '{"id":1}'
```
+#### Caution for the second parameter in `if` proc
+
+`if` proc takes two parameters. The first one is the target object, `user` in the example above. The second one is `attribute` representing each attribute `if` option affects. Note that it actually calls attribute methods, so you cannot use it to prevent attribute methods called. This means if the target object is an `ActiveRecord::Base` object and using `association` with `if` option, you might want to skip the second parameter so that the SQL query won't be issued.
+
+Example:
+
+```ruby
+class User < ApplicationRecord
+ has_many :posts
+end
+
+class Post < ApplicationRecord
+ belongs_to :user
+end
+
+class UserResource
+ include Alba::Resource
+
+ # Since `_posts` parameter exists, `user.posts` are loaded
+ many :posts, if: proc { |user, _posts| user.admin? }
+end
+
+class UserResource2
+ include Alba::Resource
+
+ # Since `_posts` parameter doesn't exist, `user.posts` are NOT loaded
+ many :posts, if: proc { |user| user.admin? && params[:include_post] }
+end
+```
+
### Default
Alba doesn't support default value for attributes, but it's easy to set a default value.
```ruby
@@ -1253,11 +1283,11 @@
UserResourceWithDifferentMetaKey.new([user]).serialize
# => '{"users":[{"id":1,"name":"Masafumi OKURA"}],"my_meta":{"foo":"bar"}}'
UserResourceWithDifferentMetaKey.new([user]).serialize(meta: {extra: 42})
-# => '{"users":[{"id":1,"name":"Masafumi OKURA"}],"meta":{"size":1,"extra":42}}'
+# => '{"users":[{"id":1,"name":"Masafumi OKURA"}],"my_meta":{"foo":"bar","extra":42}}'
class UserResourceChangingMetaKeyOnly
include Alba::Resource
root_key :user, :users
@@ -1376,9 +1406,15 @@
attributes :id, created_at: :iso8601
end
```
You now get `created_at` attribute with `iso8601` format!
+
+#### Generating TypeScript types with typelizer gem
+
+We often want TypeScript types corresponding to serializers. That's possible with [typelizer](https://github.com/skryukov/typelizer) gem.
+
+For more information, please read its README.
### Collection serialization into Hash
Sometimes we want to serialize a collection into a Hash, not an Array. It's possible with Alba.