collection_json_rails 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: 240dd9d091a8eb659286c677b94d636751e337aa
4
+ data.tar.gz: 147e1d601de30be39add7146439d904f16d690e8
5
+ SHA512:
6
+ metadata.gz: 8a726a16a1b3090e749d53bb2e35b21de897c98615aa1124a6193db1141c88e0fa0f2048a6983b76584af30e891e5f06ce1481119b89dc2cebe060478d6e0c8b
7
+ data.tar.gz: c51c2ceca97f4348e4404a029d78cd914471121553c78e22c22f9090f6145231fe0e09a6a2f5afbc6bd7a438378f120706f2c94ae8f36b9f2cee2081b47107e0
data/.gitignore ADDED
@@ -0,0 +1,14 @@
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
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in collection_json_rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Carles Jove i Buxeda
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,87 @@
1
+ CollectionJson Rails
2
+ ====================
3
+
4
+ This gem will add Rails specific features to [CollectionJson
5
+ Serializer](https://github.com/carlesjove/collection_json_serializer).
6
+
7
+ ## Features
8
+
9
+ #### Parsing of incoming templates:
10
+
11
+ It integrates with strong parameters. `accept_template!` accepts one argument:
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.
16
+
17
+ ```ruby
18
+ class PostsController < ApplicationController
19
+ include CollectionJson::Rails::AcceptTemplate
20
+
21
+ private
22
+
23
+ def post_params
24
+ accept_template!(:post).permit(:title)
25
+ end
26
+ ```
27
+
28
+ ## TO-DO Features
29
+
30
+ #### One step serialization:
31
+
32
+ ```ruby
33
+ class PostsController < ApplicationController
34
+ include CollectionJson::Rails::Render
35
+
36
+ def index
37
+ @posts = Post.all
38
+
39
+ render json: @posts, status: :ok
40
+ end
41
+ ```
42
+
43
+ #### Generators:
44
+
45
+ ```bash
46
+ $ rails g cj:serializer post
47
+ # => app/serializers/post_serializer.rb
48
+ ```
49
+
50
+ Wonder: should be invocated by scaffold, models, etc. generators?
51
+
52
+ #### URLs helpers
53
+
54
+ No more fighting with routes generation:
55
+
56
+ ```ruby
57
+ class PostSerializer < CollectionJson::Serializer
58
+ href :posts_url
59
+
60
+ items do
61
+ href :post_url, :self
62
+ ```
63
+
64
+ ## Installation
65
+
66
+ Add this line to your application's Gemfile:
67
+
68
+ ```ruby
69
+ gem 'collection_json_rails'
70
+ ```
71
+
72
+ And then execute:
73
+
74
+ $ bundle
75
+
76
+ Or install it yourself as:
77
+
78
+ $ gem install collection_json_rails
79
+
80
+
81
+ ## Contributing
82
+
83
+ 1. Fork it ( https://github.com/[my-github-username]/collection_json_rails/fork )
84
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
85
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
86
+ 4. Push to the branch (`git push origin my-new-feature`)
87
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ end
8
+
9
+ task :default => :test
10
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'collection_json_rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "collection_json_rails"
8
+ spec.version = CollectionJson::Rails::VERSION
9
+ spec.authors = ["Carles Jove i Buxeda"]
10
+ spec.email = ["hola@carlus.cat"]
11
+ spec.summary = %q{Collection+JSON Serializers with the Rails touch}
12
+ spec.description = %q{Adds Rails specific funcionalitaties for an enjoyable
13
+ experience}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "actionpack", "~> 4.0"
23
+ spec.add_dependency "railties", "~> 4.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest"
28
+ end
@@ -0,0 +1,4 @@
1
+ require "collection_json_rails/version"
2
+
3
+ require "collection_json_rails/action_controller/accept_template"
4
+
@@ -0,0 +1,35 @@
1
+ module CollectionJson::Rails
2
+ module AcceptTemplate
3
+ def accept_template!(key)
4
+ return params.require(key) unless template_is_valid?
5
+ parse_template_as_params!(params, key).require(key)
6
+ end
7
+
8
+ private
9
+
10
+ def template_is_valid?
11
+ params.key?(:template) && params[:template].key?(:data)
12
+ end
13
+
14
+ # Parses a Collection+JSON template as the params Rails expects
15
+ #
16
+ # "template": {
17
+ # "data": [
18
+ # { "name": "title", "value": "Best title ever" }
19
+ # ]
20
+ # }
21
+ #
22
+ # "key_name": {
23
+ # "title": "Best title ever"
24
+ # }
25
+ def parse_template_as_params!(params, key)
26
+ params[:template][:data].each do |d|
27
+ params[key] = Hash.new unless params.key?(key)
28
+ params[key][d[:name]] = d[:value]
29
+ end
30
+
31
+ params
32
+ end
33
+ end
34
+ end
35
+
@@ -0,0 +1,5 @@
1
+ module CollectionJson
2
+ module Rails
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ require 'minitest_helper'
2
+
3
+ class PostsController < ActionController::Base
4
+ include CollectionJson::Rails::AcceptTemplate
5
+
6
+ def create
7
+ accept_template!(:post).permit(:title, :body)
8
+ head :ok
9
+ end
10
+ end
11
+
12
+ class TestAcceptTemplate < ActionController::TestCase
13
+ tests PostsController
14
+
15
+ def setup
16
+ @template = {
17
+ template: {
18
+ data: [
19
+ { name: "title", value: "My Post Title" },
20
+ { name: "body", value: "This is my first Post" }
21
+ ]
22
+ }
23
+ }
24
+ end
25
+
26
+ def test_that_incoming_templates_are_accepted
27
+ post :create, @template
28
+
29
+ assert_response :ok
30
+ end
31
+ end
32
+
@@ -0,0 +1,27 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'rails'
4
+ require 'action_controller'
5
+
6
+ require 'collection_json_rails'
7
+
8
+ require 'minitest/autorun'
9
+
10
+ module ActionController
11
+ SharedTestRoutes = ActionDispatch::Routing::RouteSet.new
12
+ SharedTestRoutes.draw do
13
+ post ':controller(/:action)'
14
+ end
15
+
16
+ class Base
17
+ include ActionController::Testing
18
+ include SharedTestRoutes.url_helpers
19
+ end
20
+
21
+ class ActionController::TestCase
22
+ setup do
23
+ @routes = SharedTestRoutes
24
+ end
25
+ end
26
+ end
27
+
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: collection_json_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Carles Jove i Buxeda
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
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: |-
84
+ Adds Rails specific funcionalitaties for an enjoyable
85
+ experience
86
+ email:
87
+ - hola@carlus.cat
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - collection_json_rails.gemspec
99
+ - lib/collection_json_rails.rb
100
+ - lib/collection_json_rails/action_controller/accept_template.rb
101
+ - lib/collection_json_rails/version.rb
102
+ - test/controller/accept_template_test.rb
103
+ - test/minitest_helper.rb
104
+ homepage: ''
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Collection+JSON Serializers with the Rails touch
128
+ test_files:
129
+ - test/controller/accept_template_test.rb
130
+ - test/minitest_helper.rb
131
+ has_rdoc: