rest_controller_generators 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Integrum
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ rest_controller_generators
2
+ ==========================
3
+
4
+ Generators for creating RESTfull controllers in Rails
@@ -0,0 +1,11 @@
1
+ Description:
2
+ Create a Rails RESTful controller.
3
+ Expects an ApiController to exist in the same namespace
4
+
5
+ Example:
6
+ rails generate rest_controller Api::V2::Things name description
7
+
8
+ This will create:
9
+ app/controllers/api/v2/things_controller.rb
10
+
11
+ with :name and :description as whitelisted params
@@ -0,0 +1,47 @@
1
+ class RestControllerGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path('../templates', __FILE__)
3
+
4
+ def copy_controller_file
5
+ template 'controller.rb', File.join("app/controllers", class_path_dir, "#{plural_file_name}_controller.rb")
6
+ end
7
+
8
+ protected
9
+ def class_path_dir
10
+ File.join parse_namespace.map(&:underscore)
11
+ end
12
+
13
+ def class_path_namespace
14
+ parse_namespace.map(&:titleize).map{|fragment| "#{fragment}::"}.join
15
+ end
16
+
17
+ def parse_namespace
18
+ class_path = name.include?('/') ? name.split('/') : name.split('::')
19
+ class_path.pop
20
+ class_path
21
+ end
22
+
23
+ def class_name
24
+ file_name.classify
25
+ end
26
+
27
+ def plural_class_name
28
+ @plural_class_name ||= class_name.pluralize
29
+ end
30
+
31
+ def singular_class_name
32
+ @singular_class_name ||= class_name.singularize
33
+ end
34
+
35
+ def singular_file_name
36
+ @singular_file_name ||= file_name.singularize
37
+ end
38
+
39
+ def singular_route
40
+ class_path = name.include?('/') ? name.split('/') : name.split('::')
41
+ "#{class_path.map(&:underscore).join '_'}_path"
42
+ end
43
+
44
+ def whitelisted_attributes
45
+ args
46
+ end
47
+ end
@@ -0,0 +1,38 @@
1
+ class <%= class_path_namespace %><%= plural_class_name %>Controller < <%= class_path_namespace %>ApiController
2
+ before_filter :find_<%= singular_file_name %>, :except => [:index, :create]
3
+
4
+ def index
5
+ respond_with <%= singular_class_name %>.all
6
+ end
7
+
8
+ def create
9
+ respond_with <%= singular_class_name %>.create <%= singular_file_name %>_params
10
+ end
11
+
12
+ def show
13
+ respond_with @<%= singular_file_name %>
14
+ end
15
+
16
+ def update
17
+ respond_with @<%= singular_file_name %>.update_attributes <%= singular_file_name %>_params
18
+ end
19
+
20
+ def destroy
21
+ respond_with @<%= singular_file_name %>.destroy
22
+ end
23
+
24
+ protected
25
+ def find_<%= singular_file_name %>
26
+ @<%= singular_file_name %> = <%= singular_class_name %>.find params[:id]
27
+ end
28
+
29
+ def <%= singular_file_name %>_params
30
+ {<% whitelisted_attributes.each do |attribute| %>
31
+ :<%= attribute %> => params[:<%= attribute %>],<% end %>
32
+ }
33
+ end
34
+
35
+ def <%= singular_file_name %>_url(<%= singular_file_name %>)
36
+ <%= singular_route %> <%= singular_file_name %>
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module RestControllerGenerators
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rest_controller_generators'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rest_controller_generators"
8
+ spec.version = RestControllerGenerators::VERSION
9
+ spec.authors = ["Jade Meskill", "Roy van de Water"]
10
+ spec.email = ["gems@integrumtech.com"]
11
+ spec.description = "Rest Controller Generators"
12
+ spec.summary = "Generators for creating RESTfull controllers in Rails"
13
+ spec.homepage = "https://github.com/integrum/rest_controller_generators"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rest_controller_generators
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jade Meskill
9
+ - Roy van de Water
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-12-25 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '1.3'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: '1.3'
31
+ description: Rest Controller Generators
32
+ email:
33
+ - gems@integrumtech.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - LICENSE
40
+ - README.md
41
+ - lib/generators/rest_controller/USAGE
42
+ - lib/generators/rest_controller/rest_controller_generator.rb
43
+ - lib/generators/rest_controller/templates/controller.rb
44
+ - lib/rest_controller_generators.rb
45
+ - rest_controller_generators.gemspec
46
+ homepage: https://github.com/integrum/rest_controller_generators
47
+ licenses:
48
+ - MIT
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.23
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Generators for creating RESTfull controllers in Rails
71
+ test_files: []
72
+ has_rdoc: