collection_json_rails 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 151057d40027eadc3ffb330c66b6be7b0f19f01b
4
- data.tar.gz: e66f5b0774f6a3282b27f26ac27e38e10541f7b7
3
+ metadata.gz: e0acab955df5e8a66e5509f4d16de0509290c11c
4
+ data.tar.gz: 01a5073c1d15aa36e3d7da87457d56a1bdce86c3
5
5
  SHA512:
6
- metadata.gz: dcb421e998e2710e24104ee5ca1c7caffe59b5eb8be631151ae6353910291d4868cd8d56956c95632a6f92ef3585bc3a64b4b127830dc6ddbb585dbeceb907aa
7
- data.tar.gz: e545172d45e37f9d45f9e66d5ed6b1e608090bc931fb533ae61c8e89d11bc69bc9e73b81ec5bf1f47833a1daeab1c6d97242b616a0eec6015d94f69ec3fac1dc
6
+ metadata.gz: 7e43aa15b8063738c814b8ea27ef129485d504a0ca79e426f6a976f3af872d7571fefd00750c33d53c2eec6a5e27c83f3b66e62b0147ee2f8e36a13c861a5ac1
7
+ data.tar.gz: c8fb74497b2dbbab51e57942e6e2d49ba79daa9d30130c3681d00e3af62c050896833456b1cd3d6347c9abd423d6493dc9f39d80af469caa237bed210fb56125
data/README.md CHANGED
@@ -89,19 +89,18 @@ class PostSerializer < CollectionJson::Serializer
89
89
  end
90
90
  ```
91
91
 
92
+ #### URL helpers
92
93
 
93
- ## TO-DO Features
94
-
95
- #### URLs helpers
96
-
97
- No more fighting with routes generation:
94
+ Getting routes in your serializers is easy as shit. Simply pass the method name as a symbol. No need to reference the object.
98
95
 
99
96
  ```ruby
100
97
  class PostSerializer < CollectionJson::Serializer
101
98
  href :posts_url
99
+ links dashboard: { href: :dashboard_url }
102
100
 
103
101
  items do
104
- href :post_url, :self
102
+ href :post_url
103
+ link comments: { href: :comments_url }
105
104
  ```
106
105
 
107
106
  ## Contributing
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
20
  spec.require_paths = ["lib"]
21
21
 
22
- spec.add_runtime_dependency "collection_json_serializer", "~> 0.3"
22
+ spec.add_runtime_dependency "collection_json_serializer", "~> 0.3.4"
23
23
 
24
24
  spec.add_dependency "actionpack", "~> 4.0"
25
25
  spec.add_dependency "railties", "~> 4.0"
@@ -1,9 +1,14 @@
1
1
  require "collection_json_serializer"
2
2
 
3
3
  require "collection_json_rails/version"
4
+ require "collection_json_rails/support"
4
5
 
5
6
  require "collection_json_rails/action_controller/accept_template"
6
7
  require "collection_json_rails/action_controller/render"
7
8
 
9
+ require "collection_json_rails/serializer/serializer"
10
+ require "collection_json_rails/serializer/items"
11
+ require "collection_json_rails/serializer/objects/item"
12
+
8
13
  require "collection_json_rails/generators/serializer/serializer_generator"
9
14
 
@@ -0,0 +1,13 @@
1
+ module CollectionJson
2
+ class Serializer
3
+ class Items
4
+ include CollectionJson::Rails::Support
5
+
6
+ def link(hash)
7
+ @links = Array.new unless @links.is_a?(Array)
8
+ @links << rewrite_href_of(hash)
9
+ end
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,8 @@
1
+ class CollectionJson::Serializer::Objects::Item
2
+ private
3
+
4
+ def set_href
5
+ Rails.application.routes.url_helpers.send(@serializer.items.href, @resource)
6
+ end
7
+ end
8
+
@@ -0,0 +1,13 @@
1
+ class CollectionJson::Serializer
2
+ include CollectionJson::Rails::Support
3
+
4
+ def href
5
+ method = self.class.href.first
6
+ route(method)
7
+ end
8
+
9
+ def links
10
+ self.class.links.map {|link| rewrite_href_of(link) }
11
+ end
12
+ end
13
+
@@ -0,0 +1,19 @@
1
+ module CollectionJson::Rails
2
+ module Support
3
+ def route(href)
4
+ if href.is_a?(Symbol)
5
+ ::Rails.application.routes.url_helpers.send(href)
6
+ else
7
+ href
8
+ end
9
+ end
10
+
11
+ def rewrite_href_of(hash)
12
+ params = hash.extract_params
13
+ href = route(params[:properties][:href])
14
+ hash[params[:name]][:href] = href
15
+ hash
16
+ end
17
+ end
18
+ end
19
+
@@ -1,5 +1,5 @@
1
1
  module CollectionJson
2
2
  module Rails
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -1,26 +1,6 @@
1
1
  require 'minitest_helper'
2
2
 
3
- class Post
4
- attr_accessor :title, :body
5
- end
6
-
7
- class PostSerializer < CollectionJson::Serializer
8
- items do
9
- attributes :title, :body
10
- end
11
- end
12
-
13
3
  class PostsController < ActionController::Base
14
- include CollectionJson::Rails::Render
15
-
16
- def index
17
- post = Post.new
18
- post.title = "My title"
19
- post.body = "My first post"
20
-
21
- render json: post, status: :ok
22
- end
23
-
24
4
  def without_serializer
25
5
  render json: { my_hash: "should be jsonified too" }
26
6
  end
@@ -33,26 +13,6 @@ end
33
13
  class TestRender < ActionController::TestCase
34
14
  tests PostsController
35
15
 
36
- def test_that_cj_responses_are_rendered
37
- expected = {
38
- collection: {
39
- version: "1.0",
40
- items: [
41
- {
42
- data: [
43
- { name: "title", value: "My title" },
44
- { name: "body", value: "My first post" }
45
- ]
46
- }
47
- ]
48
- }
49
- }
50
-
51
- get :index, {}
52
-
53
- assert_equal expected.to_json, response.body
54
- end
55
-
56
16
  def test_that_objects_without_serializer_are_rendered_as_plain_json
57
17
  get :without_serializer
58
18
 
@@ -7,6 +7,8 @@ require 'collection_json_rails'
7
7
 
8
8
  require 'minitest/autorun'
9
9
 
10
+ require 'support/test_app'
11
+
10
12
  module ActionController
11
13
  SharedTestRoutes = ActionDispatch::Routing::RouteSet.new
12
14
  SharedTestRoutes.draw do
@@ -0,0 +1,36 @@
1
+ require 'minitest_helper'
2
+
3
+ class TestSerializer < ActionController::TestCase
4
+ tests PostsController
5
+
6
+ def test_that_cj_responses_are_as_expected
7
+ expected = {
8
+ collection: {
9
+ version: "1.0",
10
+ href: "http://test.com/posts",
11
+ items: [
12
+ {
13
+ href: "http://test.com/posts/my-pretty-url",
14
+ data: [
15
+ { name: "title", value: "My title" },
16
+ { name: "body", value: "My first post" }
17
+ ],
18
+ links: [
19
+ { rel: "posts", href: "http://test.com/posts", name: "posts" },
20
+ { rel: "external", href: "http://example.com", name: "external" }
21
+ ]
22
+ }
23
+ ],
24
+ links: [
25
+ { rel: "posts", href: "http://test.com/posts", name: "posts" },
26
+ { rel: "external", href: "http://example.com", name: "external" }
27
+ ]
28
+ }
29
+ }
30
+
31
+ get :index, {}
32
+
33
+ assert_equal expected.to_json, response.body
34
+ end
35
+ end
36
+
@@ -0,0 +1,46 @@
1
+ class TestApp < Rails::Application
2
+ config.secret_key_base = "milo_goes_to_college"
3
+ end
4
+
5
+ Rails.application = TestApp
6
+
7
+ Rails.application.routes.default_url_options[:host] = "http://test.com"
8
+
9
+ Rails.application.routes.draw do
10
+ resources :posts
11
+ end
12
+
13
+ class Post
14
+ attr_accessor :title, :body
15
+
16
+ def to_param
17
+ "my-pretty-url"
18
+ end
19
+ end
20
+
21
+ class PostSerializer < CollectionJson::Serializer
22
+ items do
23
+ href :post_url
24
+ attributes :title, :body
25
+ link posts: { href: :posts_url }
26
+ link external: { href: "http://example.com" }
27
+ end
28
+
29
+ href :posts_url
30
+ links posts: { href: :posts_url }
31
+ links external: { href: "http://example.com" }
32
+ end
33
+
34
+ class PostsController < ActionController::Base
35
+ include CollectionJson::Rails::Render
36
+
37
+ def index
38
+ post = Post.new
39
+ post.title = "My title"
40
+ post.body = "My first post"
41
+
42
+ render json: post, status: :ok
43
+ end
44
+ end
45
+
46
+
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.2.0
4
+ version: 0.3.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-05 00:00:00.000000000 Z
11
+ date: 2015-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: collection_json_serializer
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.3'
19
+ version: 0.3.4
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.3'
26
+ version: 0.3.4
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: actionpack
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -115,12 +115,18 @@ files:
115
115
  - lib/collection_json_rails/action_controller/render.rb
116
116
  - lib/collection_json_rails/generators/serializer/serializer_generator.rb
117
117
  - lib/collection_json_rails/generators/serializer/templates/serializer.rb
118
+ - lib/collection_json_rails/serializer/items.rb
119
+ - lib/collection_json_rails/serializer/objects/item.rb
120
+ - lib/collection_json_rails/serializer/serializer.rb
121
+ - lib/collection_json_rails/support.rb
118
122
  - lib/collection_json_rails/version.rb
119
123
  - test/controller/accept_template_test.rb
120
124
  - test/controller/render_test.rb
121
125
  - test/generators/serializer_generator_test.rb
122
126
  - test/generators/tmp/app/serializers/user_serializer.rb
123
127
  - test/minitest_helper.rb
128
+ - test/serializer_test.rb
129
+ - test/support/test_app.rb
124
130
  homepage: https://github.com/carlesjove/collection_json_rails
125
131
  licenses:
126
132
  - MIT
@@ -151,4 +157,6 @@ test_files:
151
157
  - test/generators/serializer_generator_test.rb
152
158
  - test/generators/tmp/app/serializers/user_serializer.rb
153
159
  - test/minitest_helper.rb
160
+ - test/serializer_test.rb
161
+ - test/support/test_app.rb
154
162
  has_rdoc: