serialize_with 0.1.2
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.
- data/.gitignore +18 -0
- data/Gemfile +5 -0
- data/LICENSE +22 -0
- data/README.md +113 -0
- data/Rakefile +2 -0
- data/lib/mongoid/serialize_with.rb +9 -0
- data/lib/serialize_with/railtie.rb +11 -0
- data/lib/serialize_with/version.rb +3 -0
- data/lib/serialize_with.rb +83 -0
- data/serialize_with.gemspec +24 -0
- data/spec/active_record_serialize_with_spec.rb +136 -0
- data/spec/mongoid.yml +6 -0
- data/spec/mongoid_serialize_with_spec.rb +145 -0
- data/spec/spec_helper.rb +51 -0
- metadata +147 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2012 JC Grubbs
|
|
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,113 @@
|
|
|
1
|
+
# SerializeWith
|
|
2
|
+
|
|
3
|
+
SerializeWith is a small add-on to ActiveRecord and Mongoid which allows
|
|
4
|
+
for storing serialization options within model classes.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
Add this line to your application's Gemfile:
|
|
9
|
+
|
|
10
|
+
gem "serialize_with"
|
|
11
|
+
|
|
12
|
+
And then execute:
|
|
13
|
+
|
|
14
|
+
$ bundle
|
|
15
|
+
|
|
16
|
+
Or install it yourself as:
|
|
17
|
+
|
|
18
|
+
$ gem install serialize_with
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### Basics
|
|
23
|
+
|
|
24
|
+
SerializeWith works with the normal serialization options that can be
|
|
25
|
+
passed to `as_json`, `to_json`, `to_xml`, or `serializable_hash`.
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
class Order < ActiveRecord::Base
|
|
29
|
+
serialize_with include: [:customer, :shipment], except: :auth_token
|
|
30
|
+
end
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Supported options are:
|
|
34
|
+
|
|
35
|
+
* include
|
|
36
|
+
* methods
|
|
37
|
+
* only
|
|
38
|
+
* except
|
|
39
|
+
|
|
40
|
+
Now you can execute any serialization method and the `serialize_with`
|
|
41
|
+
options will be applied.
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
order = Order.first(customer_id: 1, shipment_id: 2, auth_token: "12345")
|
|
45
|
+
order.as_json #=> { customer: {...}, shipment: {...} }
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Contexts
|
|
49
|
+
|
|
50
|
+
When you need to apply different serialization rules depending on a
|
|
51
|
+
context you can name the serialization options and apply them
|
|
52
|
+
conditionally.
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
class Order < ActiveRecord::Base
|
|
56
|
+
serialize_with include: [:customer, :shipment], except: :auth_token
|
|
57
|
+
serialize_with :public, include: [:shipment], only: [:ship_date, :delivery_date]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
order = Order.first
|
|
61
|
+
order.to_json(:public)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Local/Global Configuration
|
|
65
|
+
|
|
66
|
+
You can still use locally applied configuration in addition to the
|
|
67
|
+
configuration specified in the model.
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
class Order < ActiveRecord::Base
|
|
71
|
+
serialize_with include: [:customer, :shipment], except: :auth_token
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
order = Order.first
|
|
75
|
+
order.as_json(include: [:product], methods: :order_total)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
In the case of `include` and `methods` options, local configuration will
|
|
79
|
+
be merged with global configuration. In the above example `customer`,
|
|
80
|
+
`shipment`, and `product` associations will be included.
|
|
81
|
+
|
|
82
|
+
Locally applied `only` or `except` options will override the global
|
|
83
|
+
options.
|
|
84
|
+
|
|
85
|
+
### Active Record
|
|
86
|
+
|
|
87
|
+
The SerializeWith library requires no additional setup to work with
|
|
88
|
+
ActiveRecord. Just include the gem and call `serialize_with` from
|
|
89
|
+
within any model class.
|
|
90
|
+
|
|
91
|
+
### Mongoid
|
|
92
|
+
|
|
93
|
+
To use SerializeWith in Mongoid models you'll need to include the
|
|
94
|
+
`Mongoid::SerializeWith` module.
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
class Order
|
|
98
|
+
include Mongoid::Document
|
|
99
|
+
include Mongoid::SerializeWith
|
|
100
|
+
field :ship_date, type: Date
|
|
101
|
+
embeds_many :order_items
|
|
102
|
+
belongs_to :customer
|
|
103
|
+
serialize_with include: :customer
|
|
104
|
+
end
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Contributing
|
|
108
|
+
|
|
109
|
+
1. Fork it
|
|
110
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
111
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
112
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
113
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
require "serialize_with/version"
|
|
2
|
+
require "serialize_with/railtie" if defined?(Rails)
|
|
3
|
+
|
|
4
|
+
module SerializeWith
|
|
5
|
+
|
|
6
|
+
def serialize_with(context, options = {})
|
|
7
|
+
options, context = context, :default unless context.is_a?(Symbol)
|
|
8
|
+
__setup_serialize_with unless self.respond_to?(:__serialization_options)
|
|
9
|
+
self.__serialization_options[context] = options
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
private
|
|
13
|
+
|
|
14
|
+
def __setup_serialize_with
|
|
15
|
+
self.class_attribute :__serialization_options
|
|
16
|
+
self.__serialization_options = {}
|
|
17
|
+
include InstanceMethods
|
|
18
|
+
include MongoidSerializationOverrides if defined?(Mongoid)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
module InstanceMethods
|
|
22
|
+
|
|
23
|
+
def as_json(context = nil, options = {})
|
|
24
|
+
super(__prepare_options_arguments(context, options))
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def to_xml(context = nil, options = {})
|
|
28
|
+
super(__prepare_options_arguments(context, options))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def serializable_hash(local_options = nil)
|
|
32
|
+
local_options ||= {}
|
|
33
|
+
context = local_options[:context] || :default
|
|
34
|
+
options = self.class.__serialization_options
|
|
35
|
+
merged_options = __merge_serialization_options(context, local_options, options)
|
|
36
|
+
HashWithIndifferentAccess.new(super(merged_options))
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def __merge_serialization_options(context, local_options, options)
|
|
42
|
+
[:include, :methods].each do |key|
|
|
43
|
+
next unless options[context] && options[context][key]
|
|
44
|
+
local_options[key] ||= []
|
|
45
|
+
local_options[key] += Array.wrap(options[context][key])
|
|
46
|
+
end
|
|
47
|
+
[:only, :except].each do |key|
|
|
48
|
+
next unless options[context] && options[context][key]
|
|
49
|
+
local_options[key] = Array.wrap(options[context][key]) unless local_options[key]
|
|
50
|
+
end
|
|
51
|
+
local_options
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def __prepare_options_arguments(context, options)
|
|
55
|
+
options[:context] = context if context.is_a?(Symbol)
|
|
56
|
+
options = context if context.is_a?(Hash)
|
|
57
|
+
options
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# This overrides the serialize_relations method in Mongoid::Serialization to
|
|
63
|
+
# prevent it from passing serialization options through to relations.
|
|
64
|
+
module MongoidSerializationOverrides
|
|
65
|
+
|
|
66
|
+
def serialize_relations(attributes = {}, options = {})
|
|
67
|
+
inclusions = options[:include]
|
|
68
|
+
relation_names(inclusions).each do |name|
|
|
69
|
+
metadata = relations[name.to_s]
|
|
70
|
+
if metadata && relation = send(metadata.name)
|
|
71
|
+
attributes[metadata.name.to_s] =
|
|
72
|
+
relation.serializable_hash #(relation_options(inclusions, options, name))
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def relation_names(inclusions)
|
|
78
|
+
inclusions.is_a?(Hash) ? inclusions.keys : Array.wrap(inclusions)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
require File.expand_path("../lib/serialize_with/version", __FILE__)
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |gem|
|
|
5
|
+
gem.authors = ["JC Grubbs", "Kori Roys"]
|
|
6
|
+
gem.email = ["jc@devmynd.com", "kori@devmynd.com"]
|
|
7
|
+
gem.description = %q{An add-on for ActiveRecord and Mongoid which allows serialization options to be stored on a model.}
|
|
8
|
+
gem.summary = %q{An add-on for ActiveRecord and Mongoid which allows serialization options to be stored on a model.}
|
|
9
|
+
gem.homepage = "https://github.com/thegrubbsian/serialize_with"
|
|
10
|
+
|
|
11
|
+
gem.files = `git ls-files`.split($\)
|
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
14
|
+
gem.name = "serialize_with"
|
|
15
|
+
gem.require_paths = ["lib"]
|
|
16
|
+
gem.version = SerializeWith::VERSION
|
|
17
|
+
|
|
18
|
+
gem.add_dependency "activesupport", "~>3.0"
|
|
19
|
+
|
|
20
|
+
gem.add_development_dependency "activerecord", "~>3.0"
|
|
21
|
+
gem.add_development_dependency "mongoid", "~>3.0"
|
|
22
|
+
gem.add_development_dependency "rspec"
|
|
23
|
+
gem.add_development_dependency "sqlite3"
|
|
24
|
+
end
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
module ActiveRecordSpec
|
|
4
|
+
|
|
5
|
+
class Order < ActiveRecord::Base
|
|
6
|
+
serialize_with include: :order_items
|
|
7
|
+
serialize_with :private, include: [:order_items, :customer]
|
|
8
|
+
has_many :order_items
|
|
9
|
+
belongs_to :customer
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class OrderItem < ActiveRecord::Base
|
|
13
|
+
serialize_with methods: [:tax_amount], include: [:product]
|
|
14
|
+
belongs_to :order
|
|
15
|
+
belongs_to :product
|
|
16
|
+
def tax_amount; price * 0.09 end
|
|
17
|
+
def apple_tax_amount; price * 999 end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
class Product < ActiveRecord::Base
|
|
21
|
+
serialize_with only: [:name, :price]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class Customer < ActiveRecord::Base
|
|
25
|
+
serialize_with except: :last_name
|
|
26
|
+
has_many :orders
|
|
27
|
+
has_many :order_items, through: :orders
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
describe "SerializeWith ActiveRecord" do
|
|
33
|
+
|
|
34
|
+
before do
|
|
35
|
+
ActiveRecordSpec::Customer.delete_all
|
|
36
|
+
ActiveRecordSpec::Order.delete_all
|
|
37
|
+
ActiveRecordSpec::OrderItem.delete_all
|
|
38
|
+
|
|
39
|
+
@customer = ActiveRecordSpec::Customer.create!(last_name: "Smith", first_name: "Carol", address: "123 Address Street")
|
|
40
|
+
@order = ActiveRecordSpec::Order.create!(customer_id: @customer.id, order_total: 400)
|
|
41
|
+
@product = ActiveRecordSpec::Product.create!(name: "Banana", price: 140.00, sku: "sdfh3j234k")
|
|
42
|
+
@order_item = ActiveRecordSpec::OrderItem.create!(order_id: @order.id, quantity: 7000, product_id: @product.id,
|
|
43
|
+
product_sku: "skdjfhkjwehr", price: 50.00)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
describe "default context" do
|
|
47
|
+
|
|
48
|
+
context "when serialize_with is given an include option it" do
|
|
49
|
+
|
|
50
|
+
it "returns the association" do
|
|
51
|
+
@order.as_json[:order_items].should == [@order_item.as_json]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it "allows default and local configuration" do
|
|
55
|
+
json = @order.as_json(include: [:customer])
|
|
56
|
+
json[:customer].should == @customer.as_json
|
|
57
|
+
json[:order_items].should == [@order_item.as_json]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
context "when serialize_with is given a methods option it" do
|
|
63
|
+
|
|
64
|
+
it "returns the method" do
|
|
65
|
+
@order_item.as_json[:tax_amount].should == @order_item.tax_amount
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "allows default and local configuration" do
|
|
69
|
+
json = @order_item.as_json(methods: [:apple_tax_amount])
|
|
70
|
+
json[:tax_amount].should == @order_item.tax_amount
|
|
71
|
+
json[:apple_tax_amount].should == @order_item.apple_tax_amount
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
context "when serialize_with is given an except option it" do
|
|
77
|
+
|
|
78
|
+
it "does not return the specified property" do
|
|
79
|
+
@customer.as_json["last_name"].should be_nil
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
it "allows local configuration to override the default" do
|
|
83
|
+
json = @customer.as_json(except: [:first_name])
|
|
84
|
+
json["first_name"].should be_nil
|
|
85
|
+
json["last_name"].should == @customer.last_name
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
context "when serialize_with is given an only option" do
|
|
91
|
+
|
|
92
|
+
it "returns only the specified properties" do
|
|
93
|
+
@product.as_json.keys.sort.should == ["name", "price"]
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
it "allows local configuration to override the default" do
|
|
97
|
+
json = @product.as_json(only: [:price, :sku])
|
|
98
|
+
json.keys.sort.should == ["price", "sku"]
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
describe "alternate contexts" do
|
|
106
|
+
|
|
107
|
+
it "apply the correct serialization rules" do
|
|
108
|
+
@order.as_json.should_not include(:customer)
|
|
109
|
+
@order.as_json(:private)[:customer].should == @customer.as_json
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
it "respects the context and local rules" do
|
|
113
|
+
json = @order.as_json(:private, except: [:order_total])
|
|
114
|
+
json[:customer].should == @customer.as_json
|
|
115
|
+
json["order_total"].should be_nil
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# This is not standard as the built-in serialization logic returns
|
|
121
|
+
# method, includes, except, and only properties with different key
|
|
122
|
+
# types.
|
|
123
|
+
it "returns a hash that can be accessed with strings or symbol keys" do
|
|
124
|
+
json = @order.as_json(:private)
|
|
125
|
+
json["customer"].should == @customer.as_json
|
|
126
|
+
json[:customer].should == @customer.as_json
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
it "all works together as expected :)" do
|
|
130
|
+
json = @customer.as_json(include: [:order_items])
|
|
131
|
+
json[:last_name].should be_nil
|
|
132
|
+
json[:order_items].should == [@order_item.as_json]
|
|
133
|
+
json[:order_items][0][:product].should == @product.as_json
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
end
|
data/spec/mongoid.yml
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
module MongoidSpec
|
|
4
|
+
|
|
5
|
+
class Order
|
|
6
|
+
include Mongoid::Document
|
|
7
|
+
include Mongoid::SerializeWith
|
|
8
|
+
serialize_with :private, include: [:customer]
|
|
9
|
+
embeds_many :order_items
|
|
10
|
+
belongs_to :customer
|
|
11
|
+
field :order_total
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class OrderItem
|
|
15
|
+
include Mongoid::Document
|
|
16
|
+
include Mongoid::SerializeWith
|
|
17
|
+
serialize_with methods: [:tax_amount], include: [:product]
|
|
18
|
+
embedded_in :order
|
|
19
|
+
belongs_to :product
|
|
20
|
+
field :product_sku
|
|
21
|
+
field :quantity, type: Integer
|
|
22
|
+
field :price, type: Float
|
|
23
|
+
def tax_amount; price * 0.09 end
|
|
24
|
+
def apple_tax_amount; price * 999 end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
class Product
|
|
28
|
+
include Mongoid::Document
|
|
29
|
+
include Mongoid::SerializeWith
|
|
30
|
+
serialize_with only: [:name, :price]
|
|
31
|
+
field :name
|
|
32
|
+
field :sku
|
|
33
|
+
field :manufacturer
|
|
34
|
+
field :price, type: Float
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
class Customer
|
|
38
|
+
include Mongoid::Document
|
|
39
|
+
include Mongoid::SerializeWith
|
|
40
|
+
serialize_with except: [:last_name]
|
|
41
|
+
has_many :orders
|
|
42
|
+
field :first_name
|
|
43
|
+
field :last_name
|
|
44
|
+
field :address
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
describe "SerializeWith Mongoid" do
|
|
50
|
+
|
|
51
|
+
before do
|
|
52
|
+
MongoidSpec::Customer.delete_all
|
|
53
|
+
MongoidSpec::Order.delete_all
|
|
54
|
+
MongoidSpec::OrderItem.delete_all
|
|
55
|
+
|
|
56
|
+
@customer = MongoidSpec::Customer.create!(last_name: "Smith", first_name: "Carol", address: "123 Address Street")
|
|
57
|
+
@product = MongoidSpec::Product.create!(name: "Banana", price: 140.00, sku: "sdfh3j234k")
|
|
58
|
+
@order_item = MongoidSpec::OrderItem.new(quantity: 7000, product_id: @product.id, product_sku: "skdjfhkjwehr", price: 50.00)
|
|
59
|
+
@order = MongoidSpec::Order.create!(customer_id: @customer.id, order_total: 400, order_items: [@order_item])
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
describe "default context" do
|
|
63
|
+
|
|
64
|
+
context "when serialize_with is given an include option it" do
|
|
65
|
+
|
|
66
|
+
it "returns the association" do
|
|
67
|
+
@order.as_json[:order_items].should == [@order_item.as_json]
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
it "allows default and local configuration" do
|
|
71
|
+
json = @order.as_json(include: [:customer])
|
|
72
|
+
json[:customer].should == @customer.as_json
|
|
73
|
+
json[:order_items].should == [@order_item.as_json]
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
context "when serialize_with is given a methods option it" do
|
|
79
|
+
|
|
80
|
+
it "returns the method" do
|
|
81
|
+
@order_item.as_json[:tax_amount].should == @order_item.tax_amount
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it "allows default and local configuration" do
|
|
85
|
+
json = @order_item.as_json(methods: [:apple_tax_amount])
|
|
86
|
+
json[:tax_amount].should == @order_item.tax_amount
|
|
87
|
+
json[:apple_tax_amount].should == @order_item.apple_tax_amount
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
context "when serialize_with is given an except option it" do
|
|
93
|
+
|
|
94
|
+
it "does not return the specified property" do
|
|
95
|
+
@customer.as_json["last_name"].should be_nil
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it "allows local configuration to override the default" do
|
|
99
|
+
json = @customer.as_json(except: [:first_name])
|
|
100
|
+
json["first_name"].should be_nil
|
|
101
|
+
json["last_name"].should == @customer.last_name
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
context "when serialize_with is given an only option" do
|
|
107
|
+
|
|
108
|
+
it "returns only the specified properties" do
|
|
109
|
+
@product.as_json.keys.sort.should == ["name", "price"]
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
it "allows local configuration to override the default" do
|
|
113
|
+
json = @product.as_json(only: [:price, :sku])
|
|
114
|
+
json.keys.sort.should == ["price", "sku"]
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
describe "alternate contexts" do
|
|
122
|
+
|
|
123
|
+
it "apply the correct serialization rules" do
|
|
124
|
+
@order.as_json.should_not include(:customer)
|
|
125
|
+
@order.as_json(:private)[:customer].should == @customer.as_json
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
it "respects the context and local rules" do
|
|
129
|
+
json = @order.as_json(:private, except: [:order_total])
|
|
130
|
+
json[:customer].should == @customer.as_json
|
|
131
|
+
json["order_total"].should be_nil
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# This is not standard as the built-in serialization logic returns
|
|
137
|
+
# method, includes, except, and only properties with different key
|
|
138
|
+
# types.
|
|
139
|
+
it "returns a hash that can be accessed with strings or symbol keys" do
|
|
140
|
+
json = @order.as_json(:private)
|
|
141
|
+
json["customer"].should == @customer.as_json
|
|
142
|
+
json[:customer].should == @customer.as_json
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
ENV["RAILS_ENV"] = "development"
|
|
2
|
+
ENV["MONGOID_ENV"] = "development"
|
|
3
|
+
|
|
4
|
+
require "rubygems"
|
|
5
|
+
require "serialize_with"
|
|
6
|
+
require "active_record"
|
|
7
|
+
require "mongoid"
|
|
8
|
+
require "mongoid/serialize_with"
|
|
9
|
+
|
|
10
|
+
Mongoid.load!("#{Dir.pwd}/spec/mongoid.yml")
|
|
11
|
+
|
|
12
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
|
13
|
+
ActiveRecord::Migration.verbose = false
|
|
14
|
+
ActiveRecord::Base.include_root_in_json = false
|
|
15
|
+
|
|
16
|
+
migration = Class.new(ActiveRecord::Migration) do
|
|
17
|
+
def change
|
|
18
|
+
create_table :customers, :force => true do |t|
|
|
19
|
+
t.string :first_name
|
|
20
|
+
t.string :last_name
|
|
21
|
+
t.string :address
|
|
22
|
+
end
|
|
23
|
+
create_table :products, :force => true do |t|
|
|
24
|
+
t.string :name
|
|
25
|
+
t.string :sku
|
|
26
|
+
t.string :manufacturer
|
|
27
|
+
t.decimal :price
|
|
28
|
+
end
|
|
29
|
+
create_table :orders, :force => true do |t|
|
|
30
|
+
t.integer :customer_id
|
|
31
|
+
t.decimal :order_total
|
|
32
|
+
end
|
|
33
|
+
create_table :order_items, :force => true do |t|
|
|
34
|
+
t.integer :order_id
|
|
35
|
+
t.string :product_sku
|
|
36
|
+
t.integer :quantity
|
|
37
|
+
t.integer :product_id
|
|
38
|
+
t.decimal :price
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
migration.new.migrate(:up)
|
|
44
|
+
|
|
45
|
+
ActiveSupport.on_load(:active_record) do
|
|
46
|
+
self.extend SerializeWith
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
RSpec.configure do |config|
|
|
50
|
+
config.mock_with :rspec
|
|
51
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: serialize_with
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.2
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- JC Grubbs
|
|
9
|
+
- Kori Roys
|
|
10
|
+
autorequire:
|
|
11
|
+
bindir: bin
|
|
12
|
+
cert_chain: []
|
|
13
|
+
date: 2012-07-15 00:00:00.000000000 Z
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: activesupport
|
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
|
18
|
+
none: false
|
|
19
|
+
requirements:
|
|
20
|
+
- - ~>
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: '3.0'
|
|
23
|
+
type: :runtime
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
none: false
|
|
27
|
+
requirements:
|
|
28
|
+
- - ~>
|
|
29
|
+
- !ruby/object:Gem::Version
|
|
30
|
+
version: '3.0'
|
|
31
|
+
- !ruby/object:Gem::Dependency
|
|
32
|
+
name: activerecord
|
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
|
34
|
+
none: false
|
|
35
|
+
requirements:
|
|
36
|
+
- - ~>
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '3.0'
|
|
39
|
+
type: :development
|
|
40
|
+
prerelease: false
|
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
42
|
+
none: false
|
|
43
|
+
requirements:
|
|
44
|
+
- - ~>
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '3.0'
|
|
47
|
+
- !ruby/object:Gem::Dependency
|
|
48
|
+
name: mongoid
|
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
|
50
|
+
none: false
|
|
51
|
+
requirements:
|
|
52
|
+
- - ~>
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
type: :development
|
|
56
|
+
prerelease: false
|
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
58
|
+
none: false
|
|
59
|
+
requirements:
|
|
60
|
+
- - ~>
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: '3.0'
|
|
63
|
+
- !ruby/object:Gem::Dependency
|
|
64
|
+
name: rspec
|
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
|
66
|
+
none: false
|
|
67
|
+
requirements:
|
|
68
|
+
- - ! '>='
|
|
69
|
+
- !ruby/object:Gem::Version
|
|
70
|
+
version: '0'
|
|
71
|
+
type: :development
|
|
72
|
+
prerelease: false
|
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
74
|
+
none: false
|
|
75
|
+
requirements:
|
|
76
|
+
- - ! '>='
|
|
77
|
+
- !ruby/object:Gem::Version
|
|
78
|
+
version: '0'
|
|
79
|
+
- !ruby/object:Gem::Dependency
|
|
80
|
+
name: sqlite3
|
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
|
82
|
+
none: false
|
|
83
|
+
requirements:
|
|
84
|
+
- - ! '>='
|
|
85
|
+
- !ruby/object:Gem::Version
|
|
86
|
+
version: '0'
|
|
87
|
+
type: :development
|
|
88
|
+
prerelease: false
|
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
90
|
+
none: false
|
|
91
|
+
requirements:
|
|
92
|
+
- - ! '>='
|
|
93
|
+
- !ruby/object:Gem::Version
|
|
94
|
+
version: '0'
|
|
95
|
+
description: An add-on for ActiveRecord and Mongoid which allows serialization options
|
|
96
|
+
to be stored on a model.
|
|
97
|
+
email:
|
|
98
|
+
- jc@devmynd.com
|
|
99
|
+
- kori@devmynd.com
|
|
100
|
+
executables: []
|
|
101
|
+
extensions: []
|
|
102
|
+
extra_rdoc_files: []
|
|
103
|
+
files:
|
|
104
|
+
- .gitignore
|
|
105
|
+
- Gemfile
|
|
106
|
+
- LICENSE
|
|
107
|
+
- README.md
|
|
108
|
+
- Rakefile
|
|
109
|
+
- lib/mongoid/serialize_with.rb
|
|
110
|
+
- lib/serialize_with.rb
|
|
111
|
+
- lib/serialize_with/railtie.rb
|
|
112
|
+
- lib/serialize_with/version.rb
|
|
113
|
+
- serialize_with.gemspec
|
|
114
|
+
- spec/active_record_serialize_with_spec.rb
|
|
115
|
+
- spec/mongoid.yml
|
|
116
|
+
- spec/mongoid_serialize_with_spec.rb
|
|
117
|
+
- spec/spec_helper.rb
|
|
118
|
+
homepage: https://github.com/thegrubbsian/serialize_with
|
|
119
|
+
licenses: []
|
|
120
|
+
post_install_message:
|
|
121
|
+
rdoc_options: []
|
|
122
|
+
require_paths:
|
|
123
|
+
- lib
|
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
125
|
+
none: false
|
|
126
|
+
requirements:
|
|
127
|
+
- - ! '>='
|
|
128
|
+
- !ruby/object:Gem::Version
|
|
129
|
+
version: '0'
|
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
|
+
none: false
|
|
132
|
+
requirements:
|
|
133
|
+
- - ! '>='
|
|
134
|
+
- !ruby/object:Gem::Version
|
|
135
|
+
version: '0'
|
|
136
|
+
requirements: []
|
|
137
|
+
rubyforge_project:
|
|
138
|
+
rubygems_version: 1.8.24
|
|
139
|
+
signing_key:
|
|
140
|
+
specification_version: 3
|
|
141
|
+
summary: An add-on for ActiveRecord and Mongoid which allows serialization options
|
|
142
|
+
to be stored on a model.
|
|
143
|
+
test_files:
|
|
144
|
+
- spec/active_record_serialize_with_spec.rb
|
|
145
|
+
- spec/mongoid.yml
|
|
146
|
+
- spec/mongoid_serialize_with_spec.rb
|
|
147
|
+
- spec/spec_helper.rb
|