collection_json_rails 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +56 -40
- data/lib/collection_json_rails/generators/serializer/serializer_generator.rb +25 -0
- data/lib/collection_json_rails/generators/serializer/templates/serializer.rb +8 -0
- data/lib/collection_json_rails/version.rb +1 -1
- data/lib/collection_json_rails.rb +2 -0
- data/test/generators/serializer_generator_test.rb +33 -0
- data/test/generators/tmp/app/serializers/user_serializer.rb +6 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 151057d40027eadc3ffb330c66b6be7b0f19f01b
|
4
|
+
data.tar.gz: e66f5b0774f6a3282b27f26ac27e38e10541f7b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dcb421e998e2710e24104ee5ca1c7caffe59b5eb8be631151ae6353910291d4868cd8d56956c95632a6f92ef3585bc3a64b4b127830dc6ddbb585dbeceb907aa
|
7
|
+
data.tar.gz: e545172d45e37f9d45f9e66d5ed6b1e608090bc931fb533ae61c8e89d11bc69bc9e73b81ec5bf1f47833a1daeab1c6d97242b616a0eec6015d94f69ec3fac1dc
|
data/README.md
CHANGED
@@ -1,42 +1,47 @@
|
|
1
|
-
|
1
|
+
Collection+Json Rails
|
2
2
|
====================
|
3
3
|
|
4
|
-
|
4
|
+
Add Rails specific features to [CollectionJson
|
5
5
|
Serializer](https://github.com/carlesjove/collection_json_serializer).
|
6
6
|
|
7
|
-
##
|
8
|
-
|
9
|
-
#### Parsing of incoming templates:
|
7
|
+
## Installation
|
10
8
|
|
11
|
-
|
12
|
-
the name of the model. That is what you would usually pass at the `require()`
|
13
|
-
method of strong parameters. This will parse the incoming template as params,
|
14
|
-
and return the result of `params.require(:model)`. Then, you can just proceed as
|
15
|
-
usual.
|
9
|
+
Add this line to your application's Gemfile:
|
16
10
|
|
17
11
|
```ruby
|
18
|
-
|
19
|
-
|
12
|
+
gem 'collection_json_rails'
|
13
|
+
```
|
20
14
|
|
21
|
-
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install collection_json_rails
|
22
22
|
|
23
|
-
def post_params
|
24
|
-
accept_template!(:post).permit(:title)
|
25
|
-
end
|
26
|
-
```
|
27
23
|
|
24
|
+
That's all you need. CollectionJson::Serializer is included ;-)
|
28
25
|
|
29
|
-
|
26
|
+
## Usage
|
30
27
|
|
31
|
-
You
|
28
|
+
You just need to create serializers for your models. Here's an example:
|
32
29
|
|
33
30
|
```ruby
|
34
31
|
class PostSerializer < CollectionJson::Serializer
|
35
32
|
items do
|
36
33
|
attributes :title, :body
|
37
34
|
end
|
35
|
+
|
36
|
+
template :title, :body
|
38
37
|
end
|
38
|
+
```
|
39
39
|
|
40
|
+
#### Responding with Collection+JSON
|
41
|
+
|
42
|
+
Now you can respond with Collection+JSON using the regular `render`:
|
43
|
+
|
44
|
+
```ruby
|
40
45
|
class PostsController < ApplicationController
|
41
46
|
include CollectionJson::Rails::Render
|
42
47
|
|
@@ -48,45 +53,56 @@ class PostsController < ApplicationController
|
|
48
53
|
end
|
49
54
|
```
|
50
55
|
|
51
|
-
## TO-DO Features
|
52
|
-
|
53
|
-
#### Generators:
|
54
56
|
|
55
|
-
|
56
|
-
$ rails g cj:serializer post
|
57
|
-
# => app/serializers/post_serializer.rb
|
58
|
-
```
|
57
|
+
#### Accepting templates:
|
59
58
|
|
60
|
-
|
59
|
+
Collection+JSON supports write templates (YEAH!). You can accept them easily by using `accept_template!` in a similar fashion as you'd use strong parameters.
|
61
60
|
|
62
|
-
|
61
|
+
`accept_template!` takes one argument: the name of the model (that's what you'd usually pass to `require` when using strong parameters). Then, you can proceed as usual and whitelist attributes with `permit`.
|
63
62
|
|
64
|
-
|
63
|
+
Note: Noticed the exclamation mark? This means that `accept_template!` rewrites
|
64
|
+
`params`. Should `params[:model]` be present, it'd be rewritten with template's
|
65
|
+
attributes. All other attributes of `params` are untouched.
|
65
66
|
|
66
67
|
```ruby
|
67
|
-
class
|
68
|
-
|
68
|
+
class PostsController < ApplicationController
|
69
|
+
include CollectionJson::Rails::AcceptTemplate
|
69
70
|
|
70
|
-
|
71
|
-
|
71
|
+
private
|
72
|
+
|
73
|
+
def post_params
|
74
|
+
accept_template!(:post).permit(:title, :body)
|
75
|
+
end
|
72
76
|
```
|
73
77
|
|
74
|
-
|
78
|
+
#### Generators:
|
75
79
|
|
76
|
-
|
80
|
+
`$ rails g cj serializer post title body`
|
77
81
|
|
78
82
|
```ruby
|
79
|
-
|
83
|
+
# app/serializers/post_serializer.rb
|
84
|
+
|
85
|
+
class PostSerializer < CollectionJson::Serializer
|
86
|
+
items do
|
87
|
+
attributes :title, :body
|
88
|
+
end
|
89
|
+
end
|
80
90
|
```
|
81
91
|
|
82
|
-
And then execute:
|
83
92
|
|
84
|
-
|
93
|
+
## TO-DO Features
|
85
94
|
|
86
|
-
|
95
|
+
#### URLs helpers
|
87
96
|
|
88
|
-
|
97
|
+
No more fighting with routes generation:
|
89
98
|
|
99
|
+
```ruby
|
100
|
+
class PostSerializer < CollectionJson::Serializer
|
101
|
+
href :posts_url
|
102
|
+
|
103
|
+
items do
|
104
|
+
href :post_url, :self
|
105
|
+
```
|
90
106
|
|
91
107
|
## Contributing
|
92
108
|
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Generators
|
5
|
+
class SerializerGenerator < NamedBase
|
6
|
+
source_root File.expand_path("../templates", __FILE__)
|
7
|
+
namespace "cj"
|
8
|
+
|
9
|
+
argument :attributes, type: :array, default: [], banner: "field"
|
10
|
+
|
11
|
+
def create_serializer
|
12
|
+
template "serializer.rb",
|
13
|
+
File.join("app/serializers", class_path,
|
14
|
+
"#{file_name}_serializer.rb")
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def item_attributes
|
20
|
+
attributes.map { |a| ":#{a.name}" }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
require 'rails/generators'
|
3
|
+
|
4
|
+
class SerializerGeneratorTest < Rails::Generators::TestCase
|
5
|
+
destination File.expand_path("../tmp", __FILE__)
|
6
|
+
setup :prepare_destination
|
7
|
+
tests Rails::Generators::SerializerGenerator
|
8
|
+
|
9
|
+
def test_that_a_serializer_is_generated
|
10
|
+
run_generator %w(user)
|
11
|
+
|
12
|
+
assert_file "app/serializers/user_serializer.rb",
|
13
|
+
/class UserSerializer < CollectionJson::Serializer/
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_that_attributes_can_be_passed
|
17
|
+
run_generator %w(user first_name email)
|
18
|
+
|
19
|
+
assert_file "app/serializers/user_serializer.rb" do |serializer|
|
20
|
+
assert_match /items do/, serializer
|
21
|
+
assert_match /attributes :first_name, :email/, serializer
|
22
|
+
assert_match /end/, serializer
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_that_a_serializer_can_be_generated_in_a_namespace
|
27
|
+
run_generator %w(admin/user)
|
28
|
+
|
29
|
+
assert_file "app/serializers/admin/user_serializer.rb",
|
30
|
+
/class Admin::UserSerializer < CollectionJson::Serializer/
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: collection_json_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carles Jove i Buxeda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: collection_json_serializer
|
@@ -113,9 +113,13 @@ files:
|
|
113
113
|
- lib/collection_json_rails.rb
|
114
114
|
- lib/collection_json_rails/action_controller/accept_template.rb
|
115
115
|
- lib/collection_json_rails/action_controller/render.rb
|
116
|
+
- lib/collection_json_rails/generators/serializer/serializer_generator.rb
|
117
|
+
- lib/collection_json_rails/generators/serializer/templates/serializer.rb
|
116
118
|
- lib/collection_json_rails/version.rb
|
117
119
|
- test/controller/accept_template_test.rb
|
118
120
|
- test/controller/render_test.rb
|
121
|
+
- test/generators/serializer_generator_test.rb
|
122
|
+
- test/generators/tmp/app/serializers/user_serializer.rb
|
119
123
|
- test/minitest_helper.rb
|
120
124
|
homepage: https://github.com/carlesjove/collection_json_rails
|
121
125
|
licenses:
|
@@ -144,5 +148,7 @@ summary: Collection+JSON Serializers with the Rails touch
|
|
144
148
|
test_files:
|
145
149
|
- test/controller/accept_template_test.rb
|
146
150
|
- test/controller/render_test.rb
|
151
|
+
- test/generators/serializer_generator_test.rb
|
152
|
+
- test/generators/tmp/app/serializers/user_serializer.rb
|
147
153
|
- test/minitest_helper.rb
|
148
154
|
has_rdoc:
|