serialization_scopes 1.3.0 → 1.4.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.
- data/.gitignore +1 -0
- data/.project +18 -0
- data/.rspec +1 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/Gemfile.3.0.x +4 -0
- data/Gemfile.3.0.x.lock +49 -0
- data/Gemfile.3.1.x +4 -0
- data/Gemfile.3.1.x.lock +51 -0
- data/Gemfile.3.2.x +4 -0
- data/Gemfile.3.2.x.lock +51 -0
- data/Gemfile.lock +79 -0
- data/README.md +103 -0
- data/Rakefile +11 -0
- data/lib/serialization_scopes.rb +2 -1
- data/lib/serialization_scopes/responder.rb +2 -2
- data/serialization_scopes.gemspec +27 -0
- data/spec/helper_models.rb +66 -0
- data/spec/serialization_scopes/responder_spec.rb +31 -0
- data/spec/serialization_scopes_spec.rb +125 -0
- data/spec/spec_helper.rb +15 -0
- metadata +72 -19
- data/VERSION +0 -1
data/.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
*.gem
|
data/.project
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<projectDescription>
|
|
3
|
+
<name>serialization_scopes</name>
|
|
4
|
+
<comment></comment>
|
|
5
|
+
<projects>
|
|
6
|
+
</projects>
|
|
7
|
+
<buildSpec>
|
|
8
|
+
<buildCommand>
|
|
9
|
+
<name>com.aptana.ide.core.unifiedBuilder</name>
|
|
10
|
+
<arguments>
|
|
11
|
+
</arguments>
|
|
12
|
+
</buildCommand>
|
|
13
|
+
</buildSpec>
|
|
14
|
+
<natures>
|
|
15
|
+
<nature>com.aptana.ruby.core.rubynature</nature>
|
|
16
|
+
<nature>com.aptana.projects.webnature</nature>
|
|
17
|
+
</natures>
|
|
18
|
+
</projectDescription>
|
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--colour
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.3.0.x
ADDED
data/Gemfile.3.0.x.lock
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
serialization_scopes (1.4.0)
|
|
5
|
+
activemodel (>= 3.0.0)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: http://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
activemodel (3.0.20)
|
|
11
|
+
activesupport (= 3.0.20)
|
|
12
|
+
builder (~> 2.1.2)
|
|
13
|
+
i18n (~> 0.5.0)
|
|
14
|
+
activerecord (3.0.20)
|
|
15
|
+
activemodel (= 3.0.20)
|
|
16
|
+
activesupport (= 3.0.20)
|
|
17
|
+
arel (~> 2.0.10)
|
|
18
|
+
tzinfo (~> 0.3.23)
|
|
19
|
+
activeresource (3.0.20)
|
|
20
|
+
activemodel (= 3.0.20)
|
|
21
|
+
activesupport (= 3.0.20)
|
|
22
|
+
activesupport (3.0.20)
|
|
23
|
+
arel (2.0.10)
|
|
24
|
+
builder (2.1.2)
|
|
25
|
+
diff-lcs (1.2.4)
|
|
26
|
+
i18n (0.5.0)
|
|
27
|
+
rake (10.1.0)
|
|
28
|
+
rspec (2.13.0)
|
|
29
|
+
rspec-core (~> 2.13.0)
|
|
30
|
+
rspec-expectations (~> 2.13.0)
|
|
31
|
+
rspec-mocks (~> 2.13.0)
|
|
32
|
+
rspec-core (2.13.1)
|
|
33
|
+
rspec-expectations (2.13.0)
|
|
34
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
|
35
|
+
rspec-mocks (2.13.1)
|
|
36
|
+
sqlite3 (1.3.7)
|
|
37
|
+
tzinfo (0.3.37)
|
|
38
|
+
|
|
39
|
+
PLATFORMS
|
|
40
|
+
ruby
|
|
41
|
+
|
|
42
|
+
DEPENDENCIES
|
|
43
|
+
activemodel (~> 3.0.0)
|
|
44
|
+
activerecord (>= 3.0.0)
|
|
45
|
+
activeresource (>= 3.0.0)
|
|
46
|
+
rake
|
|
47
|
+
rspec
|
|
48
|
+
serialization_scopes!
|
|
49
|
+
sqlite3
|
data/Gemfile.3.1.x
ADDED
data/Gemfile.3.1.x.lock
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
serialization_scopes (1.4.0)
|
|
5
|
+
activemodel (>= 3.0.0)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: http://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
activemodel (3.1.12)
|
|
11
|
+
activesupport (= 3.1.12)
|
|
12
|
+
builder (~> 3.0.0)
|
|
13
|
+
i18n (~> 0.6)
|
|
14
|
+
activerecord (3.1.12)
|
|
15
|
+
activemodel (= 3.1.12)
|
|
16
|
+
activesupport (= 3.1.12)
|
|
17
|
+
arel (~> 2.2.3)
|
|
18
|
+
tzinfo (~> 0.3.29)
|
|
19
|
+
activeresource (3.1.12)
|
|
20
|
+
activemodel (= 3.1.12)
|
|
21
|
+
activesupport (= 3.1.12)
|
|
22
|
+
activesupport (3.1.12)
|
|
23
|
+
multi_json (~> 1.0)
|
|
24
|
+
arel (2.2.3)
|
|
25
|
+
builder (3.0.4)
|
|
26
|
+
diff-lcs (1.2.4)
|
|
27
|
+
i18n (0.6.4)
|
|
28
|
+
multi_json (1.7.7)
|
|
29
|
+
rake (10.1.0)
|
|
30
|
+
rspec (2.13.0)
|
|
31
|
+
rspec-core (~> 2.13.0)
|
|
32
|
+
rspec-expectations (~> 2.13.0)
|
|
33
|
+
rspec-mocks (~> 2.13.0)
|
|
34
|
+
rspec-core (2.13.1)
|
|
35
|
+
rspec-expectations (2.13.0)
|
|
36
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
|
37
|
+
rspec-mocks (2.13.1)
|
|
38
|
+
sqlite3 (1.3.7)
|
|
39
|
+
tzinfo (0.3.37)
|
|
40
|
+
|
|
41
|
+
PLATFORMS
|
|
42
|
+
ruby
|
|
43
|
+
|
|
44
|
+
DEPENDENCIES
|
|
45
|
+
activemodel (~> 3.1.0)
|
|
46
|
+
activerecord (>= 3.0.0)
|
|
47
|
+
activeresource (>= 3.0.0)
|
|
48
|
+
rake
|
|
49
|
+
rspec
|
|
50
|
+
serialization_scopes!
|
|
51
|
+
sqlite3
|
data/Gemfile.3.2.x
ADDED
data/Gemfile.3.2.x.lock
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
serialization_scopes (1.4.0)
|
|
5
|
+
activemodel (>= 3.0.0)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: http://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
activemodel (3.2.13)
|
|
11
|
+
activesupport (= 3.2.13)
|
|
12
|
+
builder (~> 3.0.0)
|
|
13
|
+
activerecord (3.2.13)
|
|
14
|
+
activemodel (= 3.2.13)
|
|
15
|
+
activesupport (= 3.2.13)
|
|
16
|
+
arel (~> 3.0.2)
|
|
17
|
+
tzinfo (~> 0.3.29)
|
|
18
|
+
activeresource (3.2.13)
|
|
19
|
+
activemodel (= 3.2.13)
|
|
20
|
+
activesupport (= 3.2.13)
|
|
21
|
+
activesupport (3.2.13)
|
|
22
|
+
i18n (= 0.6.1)
|
|
23
|
+
multi_json (~> 1.0)
|
|
24
|
+
arel (3.0.2)
|
|
25
|
+
builder (3.0.4)
|
|
26
|
+
diff-lcs (1.2.4)
|
|
27
|
+
i18n (0.6.1)
|
|
28
|
+
multi_json (1.7.7)
|
|
29
|
+
rake (10.1.0)
|
|
30
|
+
rspec (2.13.0)
|
|
31
|
+
rspec-core (~> 2.13.0)
|
|
32
|
+
rspec-expectations (~> 2.13.0)
|
|
33
|
+
rspec-mocks (~> 2.13.0)
|
|
34
|
+
rspec-core (2.13.1)
|
|
35
|
+
rspec-expectations (2.13.0)
|
|
36
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
|
37
|
+
rspec-mocks (2.13.1)
|
|
38
|
+
sqlite3 (1.3.7)
|
|
39
|
+
tzinfo (0.3.37)
|
|
40
|
+
|
|
41
|
+
PLATFORMS
|
|
42
|
+
ruby
|
|
43
|
+
|
|
44
|
+
DEPENDENCIES
|
|
45
|
+
activemodel (~> 3.2.0)
|
|
46
|
+
activerecord (>= 3.0.0)
|
|
47
|
+
activeresource (>= 3.0.0)
|
|
48
|
+
rake
|
|
49
|
+
rspec
|
|
50
|
+
serialization_scopes!
|
|
51
|
+
sqlite3
|
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
serialization_scopes (1.4.0)
|
|
5
|
+
activemodel (>= 3.0.0)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: http://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
actionpack (4.0.0)
|
|
11
|
+
activesupport (= 4.0.0)
|
|
12
|
+
builder (~> 3.1.0)
|
|
13
|
+
erubis (~> 2.7.0)
|
|
14
|
+
rack (~> 1.5.2)
|
|
15
|
+
rack-test (~> 0.6.2)
|
|
16
|
+
activemodel (4.0.0)
|
|
17
|
+
activesupport (= 4.0.0)
|
|
18
|
+
builder (~> 3.1.0)
|
|
19
|
+
activerecord (4.0.0)
|
|
20
|
+
activemodel (= 4.0.0)
|
|
21
|
+
activerecord-deprecated_finders (~> 1.0.2)
|
|
22
|
+
activesupport (= 4.0.0)
|
|
23
|
+
arel (~> 4.0.0)
|
|
24
|
+
activerecord-deprecated_finders (1.0.3)
|
|
25
|
+
activeresource (4.0.0)
|
|
26
|
+
activemodel (~> 4.0)
|
|
27
|
+
activesupport (~> 4.0)
|
|
28
|
+
rails-observers (~> 0.1.1)
|
|
29
|
+
activesupport (4.0.0)
|
|
30
|
+
i18n (~> 0.6, >= 0.6.4)
|
|
31
|
+
minitest (~> 4.2)
|
|
32
|
+
multi_json (~> 1.3)
|
|
33
|
+
thread_safe (~> 0.1)
|
|
34
|
+
tzinfo (~> 0.3.37)
|
|
35
|
+
arel (4.0.0)
|
|
36
|
+
atomic (1.1.10)
|
|
37
|
+
atomic (1.1.10-java)
|
|
38
|
+
builder (3.1.4)
|
|
39
|
+
diff-lcs (1.2.4)
|
|
40
|
+
erubis (2.7.0)
|
|
41
|
+
i18n (0.6.4)
|
|
42
|
+
minitest (4.7.5)
|
|
43
|
+
multi_json (1.7.7)
|
|
44
|
+
rack (1.5.2)
|
|
45
|
+
rack-test (0.6.2)
|
|
46
|
+
rack (>= 1.0)
|
|
47
|
+
rails-observers (0.1.1)
|
|
48
|
+
railties (~> 4.0.0.beta)
|
|
49
|
+
railties (4.0.0)
|
|
50
|
+
actionpack (= 4.0.0)
|
|
51
|
+
activesupport (= 4.0.0)
|
|
52
|
+
rake (>= 0.8.7)
|
|
53
|
+
thor (>= 0.18.1, < 2.0)
|
|
54
|
+
rake (10.1.0)
|
|
55
|
+
rspec (2.13.0)
|
|
56
|
+
rspec-core (~> 2.13.0)
|
|
57
|
+
rspec-expectations (~> 2.13.0)
|
|
58
|
+
rspec-mocks (~> 2.13.0)
|
|
59
|
+
rspec-core (2.13.1)
|
|
60
|
+
rspec-expectations (2.13.0)
|
|
61
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
|
62
|
+
rspec-mocks (2.13.1)
|
|
63
|
+
sqlite3 (1.3.7)
|
|
64
|
+
thor (0.18.1)
|
|
65
|
+
thread_safe (0.1.0)
|
|
66
|
+
atomic
|
|
67
|
+
tzinfo (0.3.37)
|
|
68
|
+
|
|
69
|
+
PLATFORMS
|
|
70
|
+
java
|
|
71
|
+
ruby
|
|
72
|
+
|
|
73
|
+
DEPENDENCIES
|
|
74
|
+
activerecord (>= 3.0.0)
|
|
75
|
+
activeresource (>= 3.0.0)
|
|
76
|
+
rake
|
|
77
|
+
rspec
|
|
78
|
+
serialization_scopes!
|
|
79
|
+
sqlite3
|
data/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
Serialization Scopes
|
|
2
|
+
====================
|
|
3
|
+
|
|
4
|
+
[](https://travis-ci.org/bsm/serialization_scopes)
|
|
5
|
+
Define scopes for XML/JSON serialization of your ActiveRecord models.
|
|
6
|
+
|
|
7
|
+
### In your models
|
|
8
|
+
|
|
9
|
+
Serialization Scopes extends ActiveRecord and provides a `serialization_scope`
|
|
10
|
+
class method for defining custom scopes. Example:
|
|
11
|
+
|
|
12
|
+
# Column names: id, title, body, date, personal_notes, comments_count, author_id
|
|
13
|
+
class Article < ActiveRecord::Base
|
|
14
|
+
has_many :comments
|
|
15
|
+
|
|
16
|
+
serialization_scope :author, :except => [:comments_count]
|
|
17
|
+
serialization_scope :reader, :only => [:title, :body, :date], :include => [:comments]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
### Scoping serialization output
|
|
21
|
+
|
|
22
|
+
Scopes can be applied as options to the *usual* `#to_xml` and `#to_json`
|
|
23
|
+
methods. Examples:
|
|
24
|
+
|
|
25
|
+
Apply the default scope. This can be overridden in models via
|
|
26
|
+
`serialization_scope :default, ...`:
|
|
27
|
+
|
|
28
|
+
Article.first.to_json
|
|
29
|
+
# => {"id":1,"title":"Hello","body":"World","date":"2010-01-01","personal_notes":"Author's notes","comments_count":3,"author_id":1}
|
|
30
|
+
|
|
31
|
+
Apply a custom scope:
|
|
32
|
+
|
|
33
|
+
Article.first.to_json(:scope => :author)
|
|
34
|
+
# => {"id":1,"title":"Hello","body":"World","date":"2010-01-01","personal_notes":"Author's notes","author_id":1}
|
|
35
|
+
|
|
36
|
+
Custom scopes can still be extended/overridden:
|
|
37
|
+
|
|
38
|
+
Article.first.to_json(:scope => :author, :only => [:title])
|
|
39
|
+
# => {"title":"Hello"}
|
|
40
|
+
|
|
41
|
+
### In your controllers
|
|
42
|
+
|
|
43
|
+
Serialization Scopes comes with a responder which you can include to your own.
|
|
44
|
+
Create your own responder:
|
|
45
|
+
|
|
46
|
+
# lib/my_responder.rb
|
|
47
|
+
class MyResponder < ActionController::Responder
|
|
48
|
+
include SerializationScopes::Responder
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
And then you need to configure your application to use it:
|
|
52
|
+
|
|
53
|
+
# app/controllers/application_controller.rb
|
|
54
|
+
require "my_responder"
|
|
55
|
+
|
|
56
|
+
class ApplicationController < ActionController::Base
|
|
57
|
+
self.responder = MyResponder
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
In your controllers, then simply define a protected `serialization_scope`
|
|
61
|
+
method. It will be automatically applied as part of `respond_with`. Example:
|
|
62
|
+
|
|
63
|
+
# app/controllers/articles_controller.rb
|
|
64
|
+
class ArticlesController < ApplicationController
|
|
65
|
+
respond_to :html, :xml, :json
|
|
66
|
+
|
|
67
|
+
def show
|
|
68
|
+
@article = Article.find params[:id]
|
|
69
|
+
respond_with @article
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
protected
|
|
73
|
+
|
|
74
|
+
def serialization_scope
|
|
75
|
+
@article && @article.author == current_user ? :author : :reader
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
## License
|
|
81
|
+
|
|
82
|
+
(The MIT License)
|
|
83
|
+
|
|
84
|
+
Copyright (c) 2011 Dimitrij Denissenko
|
|
85
|
+
|
|
86
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
87
|
+
a copy of this software and associated documentation files (the
|
|
88
|
+
"Software"), to deal in the Software without restriction, including
|
|
89
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
90
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
91
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
92
|
+
the following conditions:
|
|
93
|
+
|
|
94
|
+
The above copyright notice and this permission notice shall be
|
|
95
|
+
included in all copies or substantial portions of the Software.
|
|
96
|
+
|
|
97
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
98
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
99
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
100
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
101
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
102
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
103
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/lib/serialization_scopes.rb
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
require 'serialization_scopes/resolver'
|
|
2
2
|
require 'serialization_scopes/responder'
|
|
3
|
+
require 'active_support/concern'
|
|
3
4
|
|
|
4
5
|
module SerializationScopes
|
|
5
6
|
extend ActiveSupport::Concern
|
|
@@ -25,7 +26,7 @@ module SerializationScopes
|
|
|
25
26
|
protected
|
|
26
27
|
|
|
27
28
|
def serialization_scopes
|
|
28
|
-
@_serialization_scopes ||= superclass.respond_to?(:serialization_scopes) ? superclass.send(:serialization_scopes).clone : {}
|
|
29
|
+
@_serialization_scopes ||= superclass.respond_to?(:serialization_scopes, true) ? superclass.send(:serialization_scopes).clone : {}
|
|
29
30
|
end
|
|
30
31
|
|
|
31
32
|
end
|
|
@@ -2,8 +2,8 @@ module SerializationScopes
|
|
|
2
2
|
module Responder
|
|
3
3
|
|
|
4
4
|
def display(resource, given_options={})
|
|
5
|
-
if controller.respond_to?(:serialization_scope)
|
|
6
|
-
given_options.reverse_merge! :
|
|
5
|
+
if controller.respond_to?(:serialization_scope, true)
|
|
6
|
+
given_options.reverse_merge! scope: controller.send(:serialization_scope)
|
|
7
7
|
end
|
|
8
8
|
super
|
|
9
9
|
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.platform = Gem::Platform::RUBY
|
|
5
|
+
s.required_ruby_version = '>= 1.9.1'
|
|
6
|
+
s.required_rubygems_version = ">= 1.3.6"
|
|
7
|
+
|
|
8
|
+
s.name = "serialization_scopes"
|
|
9
|
+
s.summary = "Named scopes for ActiveRecord/ActiveResource serialization methods (to_xml, to_json)"
|
|
10
|
+
s.description = "Adds named scopes for ActiveRecord/ActiveResource serialization methods (to_xml, to_json)"
|
|
11
|
+
s.version = "1.4.0"
|
|
12
|
+
|
|
13
|
+
s.authors = ["Dimitrij Denissenko", "Evgeniy Dolzhenko"]
|
|
14
|
+
s.email = "info@blacksquaremedia.com"
|
|
15
|
+
s.homepage = "http://github.com/bsm/serialization_scopes"
|
|
16
|
+
|
|
17
|
+
s.require_path = 'lib'
|
|
18
|
+
s.files = `git ls-files`.split("\n")
|
|
19
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
|
20
|
+
|
|
21
|
+
s.add_dependency 'activemodel', '>= 3.0.0'
|
|
22
|
+
s.add_development_dependency 'activerecord', '>= 3.0.0'
|
|
23
|
+
s.add_development_dependency 'activeresource', '>= 3.0.0'
|
|
24
|
+
s.add_development_dependency 'rspec'
|
|
25
|
+
s.add_development_dependency 'rake'
|
|
26
|
+
s.add_development_dependency 'sqlite3'
|
|
27
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
ActiveRecord::Base.configurations["test"] = { 'adapter' => 'sqlite3', 'database' => ":memory:" }
|
|
2
|
+
|
|
3
|
+
RSpec.configure do |c|
|
|
4
|
+
c.before(:each) do
|
|
5
|
+
base = ActiveRecord::Base
|
|
6
|
+
base.establish_connection(:test)
|
|
7
|
+
base.connection.create_table :some_models do |t|
|
|
8
|
+
t.string :name
|
|
9
|
+
t.string :type
|
|
10
|
+
t.string :secret
|
|
11
|
+
end
|
|
12
|
+
base.connection.create_table :other_models do |t|
|
|
13
|
+
t.integer :some_id
|
|
14
|
+
t.string :description
|
|
15
|
+
t.integer :rank
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
class SomeModel < ActiveRecord::Base
|
|
21
|
+
has_many :others, :class_name => 'OtherModel', :foreign_key => :some_id
|
|
22
|
+
|
|
23
|
+
serialization_scope :default, :only => [:id, :name], :methods => :currency
|
|
24
|
+
serialization_scope :admin, :only => [:id, :secret]
|
|
25
|
+
serialization_scope :excepted, :except => [:secret]
|
|
26
|
+
serialization_scope :wow_root, :root => 'wow-root'
|
|
27
|
+
serialization_scope :nested, :only => [:id, :name], :methods => :another
|
|
28
|
+
|
|
29
|
+
def currency
|
|
30
|
+
'USD'
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def discount
|
|
34
|
+
20
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def another
|
|
38
|
+
@another ||= others.create! :description => "Random"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
class SubModel < SomeModel
|
|
43
|
+
serialization_scope :admin, :only => [:id, :secret, :token]
|
|
44
|
+
|
|
45
|
+
def token
|
|
46
|
+
"ABCD"
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
class OtherModel < ActiveRecord::Base
|
|
51
|
+
belongs_to :some, :class_name => 'SomeModel', :foreign_key => :some_id
|
|
52
|
+
|
|
53
|
+
serialization_scope :default, :only => :description
|
|
54
|
+
serialization_scope :admin, :only => [:description, :rank]
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class SomeResource < ActiveResource::Base
|
|
58
|
+
self.site = 'http://example.com'
|
|
59
|
+
|
|
60
|
+
schema do
|
|
61
|
+
integer 'id'
|
|
62
|
+
string 'name', 'secret'
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
serialization_scope :default, :only => [:id, :name]
|
|
66
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe SerializationScopes::Responder do
|
|
4
|
+
|
|
5
|
+
class FakeResponder
|
|
6
|
+
|
|
7
|
+
def display(*args)
|
|
8
|
+
args.extract_options!
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def controller
|
|
12
|
+
@controller ||= FakeController.new
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class ActualResponder < FakeResponder
|
|
17
|
+
include SerializationScopes::Responder
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
class FakeController
|
|
21
|
+
def serialization_scope
|
|
22
|
+
:author
|
|
23
|
+
end
|
|
24
|
+
protected :serialization_scope
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it 'should apply scopes by default' do
|
|
28
|
+
ActualResponder.new.display(nil, {}).should == { :scope => :author }
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
|
+
|
|
3
|
+
describe SerializationScopes do
|
|
4
|
+
|
|
5
|
+
let :some_record do
|
|
6
|
+
SomeModel.create! :name => 'Any', :secret => "key"
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
let :sub_record do
|
|
10
|
+
SubModel.create! :name => 'Any', :secret => "key"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
let :other_record do
|
|
14
|
+
OtherModel.create! :description => 'Other', :some_id => some_record.id
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
let :some_resource do
|
|
18
|
+
SomeResource.new :id => 1, :name => 'a name', :secret => 'some secret'
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def via_xml(record, options = {})
|
|
22
|
+
Hash.from_xml record.to_xml(options)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def via_json(record, options = {})
|
|
26
|
+
ActiveSupport::JSON.decode record.to_json(options)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def options_for(custom_options)
|
|
30
|
+
SomeModel.send(:scoped_serialization_options, custom_options)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it 'should scope to_xml' do
|
|
34
|
+
via_xml(some_record).should == { "some_model" => { "name" => "Any", "currency" => "USD", "id" => some_record.id }}
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'should scope to_json' do
|
|
38
|
+
via_json(some_record).should == { "name" => "Any", "currency" => "USD", "id" => some_record.id }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it 'should apply default scope' do
|
|
42
|
+
via_json(other_record).should == { "description" => "Other" }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it 'should allow to bypass default scope' do
|
|
46
|
+
via_json(some_record, :scope => false).should == { "name" => "Any", "id" => some_record.id, "secret" => "key" }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it 'should allow to apply additional restrictions' do
|
|
50
|
+
via_json(some_record, :only => :name).should == { "name" => "Any", "currency" => "USD" }
|
|
51
|
+
via_json(some_record, :methods => []).should == { "name" => "Any", "id" => some_record.id }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it 'should not allow loosening scopes' do
|
|
55
|
+
via_json(some_record, :only => [:id, :secret]).keys.should_not include("secret")
|
|
56
|
+
via_json(some_record, :methods => [:discount, :currency]).keys.should_not include("discount")
|
|
57
|
+
via_json(some_record, :scope => :excepted, :except => :id).keys.should_not include("secret")
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it 'should have separate behaviours for different scopes' do
|
|
61
|
+
via_json(some_record, :scope => :admin).should == { "id" => some_record.id, "secret" => "key" }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it 'should fallback to default scope if invalid scope is given' do
|
|
65
|
+
via_json(some_record, :scope => :invalid).should == { "name" => "Any", "currency" => "USD", "id" => some_record.id }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "should pass through options it doesn't know about" do
|
|
69
|
+
some_record.to_xml(:root => "wow-root").should include('<wow-root>')
|
|
70
|
+
some_record.to_xml(:scope => :wow_root).should include('<wow-root>')
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it 'should use default serialization scope when serialized as part of another object' do
|
|
74
|
+
via_json(:k => some_record).should == { 'k' => { "name" => "Any", "currency" => "USD", "id" => some_record.id } }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it 'should pass the scope to the nested object so that they can use own settings' do
|
|
78
|
+
via_json(some_record, :scope => :nested)['another'].should == { 'description' => 'Random' }
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it 'should correctly apply scopes to nested includes' do
|
|
82
|
+
some_record.others.create :description => "New", :rank => 100
|
|
83
|
+
via_json(some_record, :include => :others).should == { "name"=>"Any", "id"=>1, "currency"=>"USD", "others"=>[{ "description" => "New" }] }
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
it 'should propagate scopes to nested includes' do
|
|
87
|
+
some_record.others.create :description => "New", :rank => 100
|
|
88
|
+
via_json(some_record, :include => :others, :scope => :admin).
|
|
89
|
+
should == { "id"=>1, "secret"=>"key", "others"=>[{ "description" => "New", "rank" => 100 }] }
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it 'should inherit serialization options correctly' do
|
|
93
|
+
SomeModel.send(:serialization_scopes)[:default].should == {:only=>[:id, :name], :methods=>:currency}
|
|
94
|
+
SubModel.send(:serialization_scopes)[:default].should == {:only=>[:id, :name], :methods=>:currency}
|
|
95
|
+
|
|
96
|
+
SomeModel.send(:serialization_scopes)[:admin].should == {:only=>[:id, :secret]}
|
|
97
|
+
SubModel.send(:serialization_scopes)[:admin].should == {:only=>[:id, :secret, :token]}
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
it 'should correctly inherit serialization' do
|
|
101
|
+
via_json(sub_record).should == { "name"=>"Any", "id"=>1, "currency"=>"USD" }
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
it 'should be enabled on ActiveResource models' do
|
|
105
|
+
via_json(some_resource).should == { 'some_resource' => { 'id' => 1, 'name' => 'a name' } }
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
describe "additional option arguments" do
|
|
109
|
+
|
|
110
|
+
it 'should not fail when passed nil options' do
|
|
111
|
+
some_record.to_json(nil)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
it 'should not tamper options' do
|
|
115
|
+
original = {}
|
|
116
|
+
lambda { some_record.to_json(original) }.should_not change { original }
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
it 'should not tamper nested options' do
|
|
120
|
+
original = { :only => :id, :key => { :other => :value } }
|
|
121
|
+
lambda { some_record.to_json(original) }.should_not change { original }
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
end
|
|
125
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ENV["RAILS_ENV"] ||= 'test'
|
|
2
|
+
$:.unshift File.dirname(__FILE__)
|
|
3
|
+
$:.unshift File.expand_path('../../lib', __FILE__)
|
|
4
|
+
|
|
5
|
+
require 'rubygems'
|
|
6
|
+
require 'bundler/setup'
|
|
7
|
+
|
|
8
|
+
require 'active_record'
|
|
9
|
+
require 'active_resource'
|
|
10
|
+
require 'serialization_scopes'
|
|
11
|
+
require 'rspec'
|
|
12
|
+
require File.expand_path('../helper_models', __FILE__)
|
|
13
|
+
|
|
14
|
+
ActiveRecord::Base.include_root_in_json = false
|
|
15
|
+
ActiveResource::Base.include_root_in_json = true
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: serialization_scopes
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.4.0
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -10,11 +10,11 @@ authors:
|
|
|
10
10
|
autorequire:
|
|
11
11
|
bindir: bin
|
|
12
12
|
cert_chain: []
|
|
13
|
-
date:
|
|
13
|
+
date: 2013-06-27 00:00:00.000000000 Z
|
|
14
14
|
dependencies:
|
|
15
15
|
- !ruby/object:Gem::Dependency
|
|
16
16
|
name: activemodel
|
|
17
|
-
requirement:
|
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
|
18
18
|
none: false
|
|
19
19
|
requirements:
|
|
20
20
|
- - ! '>='
|
|
@@ -22,10 +22,15 @@ dependencies:
|
|
|
22
22
|
version: 3.0.0
|
|
23
23
|
type: :runtime
|
|
24
24
|
prerelease: false
|
|
25
|
-
version_requirements:
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
none: false
|
|
27
|
+
requirements:
|
|
28
|
+
- - ! '>='
|
|
29
|
+
- !ruby/object:Gem::Version
|
|
30
|
+
version: 3.0.0
|
|
26
31
|
- !ruby/object:Gem::Dependency
|
|
27
32
|
name: activerecord
|
|
28
|
-
requirement:
|
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
34
|
none: false
|
|
30
35
|
requirements:
|
|
31
36
|
- - ! '>='
|
|
@@ -33,10 +38,15 @@ dependencies:
|
|
|
33
38
|
version: 3.0.0
|
|
34
39
|
type: :development
|
|
35
40
|
prerelease: false
|
|
36
|
-
version_requirements:
|
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
42
|
+
none: false
|
|
43
|
+
requirements:
|
|
44
|
+
- - ! '>='
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: 3.0.0
|
|
37
47
|
- !ruby/object:Gem::Dependency
|
|
38
48
|
name: activeresource
|
|
39
|
-
requirement:
|
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
|
40
50
|
none: false
|
|
41
51
|
requirements:
|
|
42
52
|
- - ! '>='
|
|
@@ -44,10 +54,15 @@ dependencies:
|
|
|
44
54
|
version: 3.0.0
|
|
45
55
|
type: :development
|
|
46
56
|
prerelease: false
|
|
47
|
-
version_requirements:
|
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
58
|
+
none: false
|
|
59
|
+
requirements:
|
|
60
|
+
- - ! '>='
|
|
61
|
+
- !ruby/object:Gem::Version
|
|
62
|
+
version: 3.0.0
|
|
48
63
|
- !ruby/object:Gem::Dependency
|
|
49
64
|
name: rspec
|
|
50
|
-
requirement:
|
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
|
51
66
|
none: false
|
|
52
67
|
requirements:
|
|
53
68
|
- - ! '>='
|
|
@@ -55,10 +70,15 @@ dependencies:
|
|
|
55
70
|
version: '0'
|
|
56
71
|
type: :development
|
|
57
72
|
prerelease: false
|
|
58
|
-
version_requirements:
|
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
74
|
+
none: false
|
|
75
|
+
requirements:
|
|
76
|
+
- - ! '>='
|
|
77
|
+
- !ruby/object:Gem::Version
|
|
78
|
+
version: '0'
|
|
59
79
|
- !ruby/object:Gem::Dependency
|
|
60
80
|
name: rake
|
|
61
|
-
requirement:
|
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
|
62
82
|
none: false
|
|
63
83
|
requirements:
|
|
64
84
|
- - ! '>='
|
|
@@ -66,10 +86,15 @@ dependencies:
|
|
|
66
86
|
version: '0'
|
|
67
87
|
type: :development
|
|
68
88
|
prerelease: false
|
|
69
|
-
version_requirements:
|
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
90
|
+
none: false
|
|
91
|
+
requirements:
|
|
92
|
+
- - ! '>='
|
|
93
|
+
- !ruby/object:Gem::Version
|
|
94
|
+
version: '0'
|
|
70
95
|
- !ruby/object:Gem::Dependency
|
|
71
|
-
name: sqlite3
|
|
72
|
-
requirement:
|
|
96
|
+
name: sqlite3
|
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
|
73
98
|
none: false
|
|
74
99
|
requirements:
|
|
75
100
|
- - ! '>='
|
|
@@ -77,7 +102,12 @@ dependencies:
|
|
|
77
102
|
version: '0'
|
|
78
103
|
type: :development
|
|
79
104
|
prerelease: false
|
|
80
|
-
version_requirements:
|
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
106
|
+
none: false
|
|
107
|
+
requirements:
|
|
108
|
+
- - ! '>='
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '0'
|
|
81
111
|
description: Adds named scopes for ActiveRecord/ActiveResource serialization methods
|
|
82
112
|
(to_xml, to_json)
|
|
83
113
|
email: info@blacksquaremedia.com
|
|
@@ -85,13 +115,31 @@ executables: []
|
|
|
85
115
|
extensions: []
|
|
86
116
|
extra_rdoc_files: []
|
|
87
117
|
files:
|
|
88
|
-
-
|
|
118
|
+
- .gitignore
|
|
119
|
+
- .project
|
|
120
|
+
- .rspec
|
|
121
|
+
- .travis.yml
|
|
122
|
+
- Gemfile
|
|
123
|
+
- Gemfile.3.0.x
|
|
124
|
+
- Gemfile.3.0.x.lock
|
|
125
|
+
- Gemfile.3.1.x
|
|
126
|
+
- Gemfile.3.1.x.lock
|
|
127
|
+
- Gemfile.3.2.x
|
|
128
|
+
- Gemfile.3.2.x.lock
|
|
129
|
+
- Gemfile.lock
|
|
130
|
+
- README.md
|
|
131
|
+
- Rakefile
|
|
89
132
|
- lib/serialization_scopes.rb
|
|
90
133
|
- lib/serialization_scopes/matchers.rb
|
|
91
|
-
- lib/serialization_scopes/responder.rb
|
|
92
134
|
- lib/serialization_scopes/matchers/serialize_matcher.rb
|
|
93
135
|
- lib/serialization_scopes/resolver.rb
|
|
136
|
+
- lib/serialization_scopes/responder.rb
|
|
94
137
|
- rails/init.rb
|
|
138
|
+
- serialization_scopes.gemspec
|
|
139
|
+
- spec/helper_models.rb
|
|
140
|
+
- spec/serialization_scopes/responder_spec.rb
|
|
141
|
+
- spec/serialization_scopes_spec.rb
|
|
142
|
+
- spec/spec_helper.rb
|
|
95
143
|
homepage: http://github.com/bsm/serialization_scopes
|
|
96
144
|
licenses: []
|
|
97
145
|
post_install_message:
|
|
@@ -112,9 +160,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
112
160
|
version: 1.3.6
|
|
113
161
|
requirements: []
|
|
114
162
|
rubyforge_project:
|
|
115
|
-
rubygems_version: 1.8.
|
|
163
|
+
rubygems_version: 1.8.25
|
|
116
164
|
signing_key:
|
|
117
165
|
specification_version: 3
|
|
118
166
|
summary: Named scopes for ActiveRecord/ActiveResource serialization methods (to_xml,
|
|
119
167
|
to_json)
|
|
120
|
-
test_files:
|
|
168
|
+
test_files:
|
|
169
|
+
- spec/helper_models.rb
|
|
170
|
+
- spec/serialization_scopes/responder_spec.rb
|
|
171
|
+
- spec/serialization_scopes_spec.rb
|
|
172
|
+
- spec/spec_helper.rb
|
|
173
|
+
has_rdoc:
|
data/VERSION
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
1.3.0
|