accepts_nested_serialized_attributes 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5b1940475b622c6225080765c6e1edaf9ca7b034
4
+ data.tar.gz: a2fd850fc13cb62a6182fbee1f659431c30fb3e7
5
+ SHA512:
6
+ metadata.gz: 11dc0e8d40cdec1e7fb7b1b0a6acf159a7da3e95387bead7be58bb2c24ead729a7d81df302db0c0f29dae14ec44867e36fb8e866961ed042fd79dce98f8ba71d
7
+ data.tar.gz: cbbb3ef526be55361fb98f6ab0976bfc13d93d00d4eeed4f6b1a5c8417ed3c396f638813fabf795e3277e297c879aa4f760a38a405b9375bd53cdba4df8620ee
data/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in accepts_nested_serialized_attributes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Diego Salazar
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # AcceptsNestedSerializedAttributes
2
+
3
+ Writing an API? Serializing your objects between Rails powered apps? Want to use nested associations without writing extra (de)serialization code? Want to get away with just using `ActiveModel#as_json`? Do your models declare `accepts_nested_attributes_for`?
4
+
5
+ You might have noticed how calling `@model.as_json include: :association` will return a hash that includes the association but the name isn't suffixed with `_attributes` which `accepts_nested_attributes_for` needs in order to build the association from those nested attributes. Well, the API consumer could worry about it, but what if the API consumer is another Rails app? Why not make it as easy as possible for them and just send the association through with the correct key so they can just use `Model.new.from_json params[:model]` and have ActiveModel build the associations automatically given that they declare `accepts_nested_attributes_for :association`?
6
+
7
+ This tiny gem is the answer. It monkey patches `ActiveModel::Serialization#serializable_hash` and renames nested attributes keys if they are declared as such via `accepts_nested_attributes_for` in the given model.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'accepts_nested_serialized_attributes'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install accepts_nested_serialized_attributes
24
+
25
+ ## Usage
26
+
27
+ Adding the Gem to your Gemfile is enough to make this work. Given:
28
+
29
+ ```ruby
30
+ class Model < ActiveRecord::Base
31
+ has_many :associations
32
+ accepts_nested_attributes_for :associations
33
+ end
34
+ ```
35
+
36
+ Then:
37
+
38
+ ```ruby
39
+ @model = Model.first
40
+ @model.as_json include: :associations
41
+ ```
42
+
43
+ Will return:
44
+
45
+ ```ruby
46
+ {
47
+ id: 1
48
+ associations_attributes: {
49
+ attribute_1: 'value'
50
+ }
51
+ }
52
+ ```
53
+
54
+ If you don't declare `accepts_nested_attributes_for` then the attributes won't be renamed in the serialized hash.
55
+
56
+ Only the API producer needs this Gem, the consumer just needs to declare `accepts_nested_attributes_for` and they can just:
57
+
58
+ ```ruby
59
+ @model = Model.new.from_json params[:model]
60
+ ```
61
+
62
+ And the association will be built automatically as you'd expect.
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it ( https://github.com/[my-github-username]/accepts_nested_serialized_attributes/fork )
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'accepts_nested_serialized_attributes/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "accepts_nested_serialized_attributes"
8
+ spec.version = AcceptsNestedSerializedAttributes::VERSION
9
+ spec.authors = ["Diego Salazar"]
10
+ spec.email = ["diego@greyrobot.com"]
11
+ spec.summary = %q{Adds support for serializing nested associations that work with accepts_nested_attributes_for.}
12
+ spec.description = %q{A tiny hack for Rails to make Model#as_json(include: :association) return a hash with association keys suffixed with _attributes.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "activerecord"
24
+ spec.add_development_dependency "sqlite3"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,7 @@
1
+ class CreateModel < ActiveRecord::Migration
2
+ def change
3
+ create_table :models do |t|
4
+ t.string :status
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ class CreateAssociation < ActiveRecord::Migration
2
+ def change
3
+ create_table :associations do |t|
4
+ t.references :model
5
+ t.string :status
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module AcceptsNestedSerializedAttributes
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ require "accepts_nested_serialized_attributes/version"
2
+
3
+ module AcceptsNestedSerializedAttributes
4
+ module ActiveModel::Serialization
5
+ alias_method :original_serializable_hash, :serializable_hash
6
+
7
+ def serializable_hash(options = nil)
8
+ original_serializable_hash(options).tap do |hash|
9
+ hash.symbolize_keys!
10
+
11
+ self.class.nested_attributes_options.keys.each do |nested_attributes_key|
12
+ if (records = hash.delete nested_attributes_key)
13
+ hash["#{nested_attributes_key}_attributes".to_sym] = records
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,89 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, consider making
10
+ # a separate helper file that requires the additional dependencies and performs
11
+ # the additional setup, and require it from the spec files that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ # rspec-expectations config goes here. You can use an alternate
19
+ # assertion/expectation library such as wrong or the stdlib/minitest
20
+ # assertions if you prefer.
21
+ config.expect_with :rspec do |expectations|
22
+ # This option will default to `true` in RSpec 4. It makes the `description`
23
+ # and `failure_message` of custom matchers include text for helper methods
24
+ # defined using `chain`, e.g.:
25
+ # be_bigger_than(2).and_smaller_than(4).description
26
+ # # => "be bigger than 2 and smaller than 4"
27
+ # ...rather than:
28
+ # # => "be bigger than 2"
29
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
30
+ end
31
+
32
+ # rspec-mocks config goes here. You can use an alternate test double
33
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
34
+ config.mock_with :rspec do |mocks|
35
+ # Prevents you from mocking or stubbing a method that does not exist on
36
+ # a real object. This is generally recommended, and will default to
37
+ # `true` in RSpec 4.
38
+ mocks.verify_partial_doubles = true
39
+ end
40
+
41
+ # The settings below are suggested to provide a good initial experience
42
+ # with RSpec, but feel free to customize to your heart's content.
43
+ =begin
44
+ # These two settings work together to allow you to limit a spec run
45
+ # to individual examples or groups you care about by tagging them with
46
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
47
+ # get run.
48
+ config.filter_run :focus
49
+ config.run_all_when_everything_filtered = true
50
+
51
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
52
+ # For more details, see:
53
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
54
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
55
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
56
+ config.disable_monkey_patching!
57
+
58
+ # This setting enables warnings. It's recommended, but in some cases may
59
+ # be too noisy due to issues in dependencies.
60
+ config.warnings = true
61
+
62
+ # Many RSpec users commonly either run the entire suite or an individual
63
+ # file, and it's useful to allow more verbose output when running an
64
+ # individual spec file.
65
+ if config.files_to_run.one?
66
+ # Use the documentation formatter for detailed output,
67
+ # unless a formatter has already been configured
68
+ # (e.g. via a command-line flag).
69
+ config.default_formatter = 'doc'
70
+ end
71
+
72
+ # Print the 10 slowest examples and example groups at the
73
+ # end of the spec run, to help surface which specs are running
74
+ # particularly slow.
75
+ config.profile_examples = 10
76
+
77
+ # Run specs in random order to surface order dependencies. If you find an
78
+ # order dependency and want to debug it, you can fix the order by providing
79
+ # the seed, which is printed after each run.
80
+ # --seed 1234
81
+ config.order = :random
82
+
83
+ # Seed global randomization in this process using the `--seed` CLI option.
84
+ # Setting this allows you to use `--seed` to deterministically reproduce
85
+ # test failures related to randomization by passing the same `--seed` value
86
+ # as the one that triggered the failure.
87
+ Kernel.srand config.seed
88
+ =end
89
+ end
data/spec/the_spec.rb ADDED
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+ require 'active_record'
3
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
4
+ ActiveRecord::Migrator.up 'db/migrate'
5
+ require 'accepts_nested_serialized_attributes'
6
+
7
+ class Association < ActiveRecord::Base
8
+ belongs_to :model
9
+ end
10
+
11
+ describe AcceptsNestedSerializedAttributes do
12
+ before :each do # Allow clean redefinition of Model
13
+ Object.send :remove_const, :Model rescue nil
14
+ end
15
+
16
+ it 'renames nested attributes' do
17
+ class Model < ActiveRecord::Base
18
+ has_many :associations
19
+ accepts_nested_attributes_for :associations
20
+ end
21
+
22
+ model = Model.new
23
+ model.associations << Association.new
24
+
25
+ expect(model.as_json(include: :associations)).to eq({
26
+ id: nil, status: nil, associations_attributes: [{ id: nil, model_id: nil, status: nil }]
27
+ })
28
+ end
29
+
30
+ it "doesn't rename an attribute if it is not declared as nested" do
31
+ class Model < ActiveRecord::Base
32
+ has_many :associations
33
+ end
34
+
35
+ model = Model.new
36
+ model.associations << Association.new
37
+
38
+ expect(model.as_json(include: :associations)).to eq({
39
+ id: nil, status: nil, associations: [{ id: nil, model_id: nil, status: nil }]
40
+ })
41
+ end
42
+
43
+ it 'symbolizes keys in serialized hash' do
44
+ class Model < ActiveRecord::Base
45
+ has_many :associations
46
+ accepts_nested_attributes_for :associations
47
+ end
48
+
49
+ model = Model.new
50
+ model.associations << Association.new
51
+
52
+ all_symbols = model.as_json(include: :associations).keys.all? { |k| k.is_a? Symbol }
53
+ expect(all_symbols).to be true
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: accepts_nested_serialized_attributes
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Diego Salazar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: 'A tiny hack for Rails to make Model#as_json(include: :association) return
84
+ a hash with association keys suffixed with _attributes.'
85
+ email:
86
+ - diego@greyrobot.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".DS_Store"
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - accepts_nested_serialized_attributes.gemspec
99
+ - db/migrate/20141211145336_create_model.rb
100
+ - db/migrate/20141211145337_create_association.rb
101
+ - lib/accepts_nested_serialized_attributes.rb
102
+ - lib/accepts_nested_serialized_attributes/version.rb
103
+ - spec/spec_helper.rb
104
+ - spec/the_spec.rb
105
+ homepage: ''
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.4.3
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Adds support for serializing nested associations that work with accepts_nested_attributes_for.
129
+ test_files:
130
+ - spec/spec_helper.rb
131
+ - spec/the_spec.rb