https://github.com/nerzh/flexserializer
serializer with groups attributes
https://github.com/nerzh/flexserializer
Last synced: 5 months ago
JSON representation
serializer with groups attributes
- Host: GitHub
- URL: https://github.com/nerzh/flexserializer
- Owner: nerzh
- License: mit
- Created: 2017-09-25T12:35:57.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-01-01T19:12:10.000Z (over 7 years ago)
- Last Synced: 2025-12-01T13:21:00.681Z (7 months ago)
- Language: Ruby
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
## Flexserializer
# Conveniently structuring your attributes into groups for ActiveModelSerializer
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'flexserializer'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install flexserializer
## Usage
```ruby
# default_attributes do
# define_attributes :attr1, :attr2
# define_attribute :attr3
# define_has_one :attr4
# define_has_many :attr5
# end
class UserSerializer < Flexserializer::Base
# Example default_attributes for all groups
default_attributes do
define_attributes :id, :name, :last_name
end
# Examples of the group definitions
# 1) group can have many different names for flexible naming
group(:with_names, :with_ages) do
define_attribute :age
end
# 2) or single name
group(:with_avatar) do
define_attribute :avatar
end
# If you want to transfer the group name to the next Serializer.
# define_options - hash with your options
def avatar
options = define_options.merge({group: :for_user})
ActiveModelSerializers::SerializableResource.new(object.avatar, options).serializable_hash
end
end
```
# Call group
for example
```ruby
class TestController < ApplicationController
def index
@hash = ActiveModelSerializers::SerializableResource.new(Model.all, {group: :group_name_1}).serializable_hash
end
end
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/flexserializer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the Flexserializer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/flexserializer/blob/master/CODE_OF_CONDUCT.md).