nested_attributes_serializer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.jrubyrc +1 -0
- data/.rspec +3 -0
- data/.rubocop.yml +22 -0
- data/.travis.yml +12 -0
- data/Gemfile +23 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/nested_attributes_serializer.rb +35 -0
- data/lib/nested_attributes_serializer/version.rb +3 -0
- data/nested_attributes_serializer.gemspec +19 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 79ce523f7782beec5f14b496862227f28ba22bfc
|
4
|
+
data.tar.gz: bda14744d95a5e516bd95c142b33c4d0b56ec18b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e435bf8c30db9f8d346150704e4f5ec5e4e61876bfba979c820e3809ee6aa16364baffe8e779c61197b0df691ecb9dbb530c0b5f722cc07e27d984a3462ed630
|
7
|
+
data.tar.gz: ca271b438c4f43eb6ff24972df7ffe3fa33b35911554c6220a4c4a91ec72afc601515bfb0ad9f523bb2ffe73d21059b199a4eaaa29f23d78d14709891fd06ab4
|
data/.gitignore
ADDED
data/.jrubyrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
debug.fullTrace=true
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Metrics/LineLength:
|
2
|
+
Max: 140
|
3
|
+
|
4
|
+
Style/Documentation:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
Style/EmptyLineBetweenDefs:
|
8
|
+
AllowAdjacentOneLineDefs: true
|
9
|
+
|
10
|
+
Style/HashSyntax:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Style/RaiseArgs:
|
14
|
+
EnforcedStyle: compact
|
15
|
+
|
16
|
+
Style/StringLiterals:
|
17
|
+
EnforcedStyle: double_quotes
|
18
|
+
SupportedStyles:
|
19
|
+
- double_quotes
|
20
|
+
|
21
|
+
Style/UnlessElse:
|
22
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
group :development do
|
6
|
+
gem "activerecord"
|
7
|
+
gem "bundler"
|
8
|
+
gem "rake"
|
9
|
+
gem "rubocop", "~> 0.41.1", require: false
|
10
|
+
gem "rspec", "~> 3.0"
|
11
|
+
gem "simplecov"
|
12
|
+
gem "sqlite3"
|
13
|
+
|
14
|
+
if !ENV["CI"] && RUBY_ENGINE == "ruby"
|
15
|
+
gem "pry"
|
16
|
+
|
17
|
+
if RUBY_VERSION < "2.0.0"
|
18
|
+
gem "pry-nav"
|
19
|
+
else
|
20
|
+
gem "pry-byebug"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Michael Sievers
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# NestedAttributesSerializer
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'nested_attributes_serializer'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install nested_attributes_serializer
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class Search < ApplicationRecord
|
23
|
+
include NestedAttributesSerializer
|
24
|
+
|
25
|
+
has_many :queries
|
26
|
+
|
27
|
+
accepts_nested_attributes_for :queries
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class Query < ApplicationRecord
|
33
|
+
belongs_to :search
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
search = Search.new(queries_attributes: [{query: "foo"}, {query: "bar"}])
|
39
|
+
search.queries
|
40
|
+
# [
|
41
|
+
# #<Query:0x000000043e02e8 id: nil, query: "foo", created_at: nil, updated_at: nil, search_id: nil>,
|
42
|
+
# #<Query:0x000000043cdd00 id: nil, query: "bar", created_at: nil, updated_at: nil, search_id: nil>
|
43
|
+
# ]
|
44
|
+
|
45
|
+
serialized_search = search.to_nested_attributes(include: :queries)
|
46
|
+
# {
|
47
|
+
# "id"=>nil, "created_at"=>nil, "updated_at"=>nil,
|
48
|
+
# "queries_attributes"=>[
|
49
|
+
# {"id"=>nil, "query"=>"foo", "created_at"=>nil, "updated_at"=>nil, "search_id"=>nil},
|
50
|
+
# {"id"=>nil, "query"=>"bar", "created_at"=>nil, "updated_at"=>nil, "search_id"=>nil}
|
51
|
+
# ]
|
52
|
+
# }
|
53
|
+
|
54
|
+
deserialized_search = Search.new(serialized_search)
|
55
|
+
deserialized_search.queries
|
56
|
+
|
57
|
+
# [
|
58
|
+
# #<Query:0x00000003c59ee8 id: nil, query: "foo", created_at: nil, updated_at: nil, search_id: nil>,
|
59
|
+
# #<Query:0x00000003c3f5e8 id: nil, query: "bar", created_at: nil, updated_at: nil, search_id: nil>
|
60
|
+
# ]
|
61
|
+
```
|
62
|
+
|
63
|
+
## Development
|
64
|
+
|
65
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
66
|
+
|
67
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
68
|
+
|
69
|
+
## Contributing
|
70
|
+
|
71
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/msievers/nested_attributes_serializer.
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "nested_attributes_serializer"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "nested_attributes_serializer/version"
|
2
|
+
|
3
|
+
module NestedAttributesSerializer
|
4
|
+
# Returns a hash representing the model. It accepts the some options
|
5
|
+
# as serializable_hash.
|
6
|
+
def as_nested_attributes(options = {})
|
7
|
+
hash = serializable_hash(options)
|
8
|
+
NestedAttributesSerializer.rename_includes(hash, options)
|
9
|
+
hash
|
10
|
+
end
|
11
|
+
|
12
|
+
# see #as_nested_attributes
|
13
|
+
def to_nested_attributes(*args)
|
14
|
+
as_nested_attributes(*args)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Renames the keys of associations to conform to rails nested attributes
|
18
|
+
# naming convention. This is a implemented as a module method in order
|
19
|
+
# to not pollute the method namespace of the including class/module.
|
20
|
+
def self.rename_includes(hash, options)
|
21
|
+
# inspired by activemodel/lib/active_model/serialization.rb
|
22
|
+
return unless includes = options[:include]
|
23
|
+
|
24
|
+
unless includes.is_a?(Hash)
|
25
|
+
includes = Hash[Array(includes).map { |n| n.is_a?(Hash) ? n.to_a.first : [n, {}] }]
|
26
|
+
end
|
27
|
+
|
28
|
+
includes.each do |association, opts|
|
29
|
+
old_key = association.to_s
|
30
|
+
new_key = "#{old_key}_attributes"
|
31
|
+
hash[new_key] = hash.delete(old_key)
|
32
|
+
rename_includes(hash[new_key], opts)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "nested_attributes_serializer/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nested_attributes_serializer"
|
8
|
+
spec.version = NestedAttributesSerializer::VERSION
|
9
|
+
spec.authors = ["Michael Sievers"]
|
10
|
+
|
11
|
+
spec.summary = %q{ActiveModel serializer which returns hashes according to Rails nested attributes naming convention.}
|
12
|
+
spec.homepage = "https://github.com/msievers/nested_attributes_serializer"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nested_attributes_serializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Sievers
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-07 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".gitignore"
|
20
|
+
- ".jrubyrc"
|
21
|
+
- ".rspec"
|
22
|
+
- ".rubocop.yml"
|
23
|
+
- ".travis.yml"
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- bin/console
|
29
|
+
- bin/setup
|
30
|
+
- lib/nested_attributes_serializer.rb
|
31
|
+
- lib/nested_attributes_serializer/version.rb
|
32
|
+
- nested_attributes_serializer.gemspec
|
33
|
+
homepage: https://github.com/msievers/nested_attributes_serializer
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
metadata: {}
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 2.5.1
|
54
|
+
signing_key:
|
55
|
+
specification_version: 4
|
56
|
+
summary: ActiveModel serializer which returns hashes according to Rails nested attributes
|
57
|
+
naming convention.
|
58
|
+
test_files: []
|