i18n_scaffold_controller_template 1.1.1 → 2.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d5be1d0c48add774430a147307a864bb0ad4c15c
4
- data.tar.gz: 65dfa105d73484845b64901b45ad32a5f9d01b31
3
+ metadata.gz: 9f118a618d5b47e4c01fccfaf491f516234e25d5
4
+ data.tar.gz: 4dc88ed2b056b835b168128f63b500ece0e869f5
5
5
  SHA512:
6
- metadata.gz: 2d20bab09bd53cd99725a7ebb2b080d2e28530882db9f2edbcd788fdb8d51c15781a54d2285fe092184247c5ef1f0221ad8af209d14ddd8a1ab393e7cd50bb26
7
- data.tar.gz: 829800401b1aaa21e114c86bdb5479ac59839888f6b15e9af05dcae5ae8a3a048dab56e90b5ab214b656908a540d2fae574b4a7e2a00f97faa3837762bc5c70b
6
+ metadata.gz: 0f1ec0a3a8ac5ca0d8f8a8673bdc154a158e5aa8f43d2e689984eed0b95537cfb030e585aaaff0ed54185d5fc3fb1926af0d230883d905f4438dd17fcadc0091
7
+ data.tar.gz: df46539e5f336a2c1fa982a9ed4ea709d7e297101d0ec4005ded5113bc68d46f1be876303194f81e0dddbaafc4c2da2303a38887ef3d513ca901c58fe04c3f28
data/README.rdoc CHANGED
@@ -21,6 +21,12 @@ Next time you run
21
21
 
22
22
  you will get the controller with the named functionalities.
23
23
 
24
+ == New options
25
+
26
+ [with_api] adding --with_api will generate one additional controller in the namespace `Api::`
27
+
28
+ [api_version] this parameter adds an additional namespace specified after `Api::`
29
+
24
30
  == Contributing to i18n_scaffold_controller_template
25
31
 
26
32
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.1
1
+ 2.0.0
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: i18n_scaffold_controller_template 1.1.1 ruby lib
5
+ # stub: i18n_scaffold_controller_template 2.0.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "i18n_scaffold_controller_template"
9
- s.version = "1.1.1"
9
+ s.version = "2.0.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Dmitri Koulikoff"]
14
- s.date = "2015-09-25"
14
+ s.date = "2016-09-16"
15
15
  s.description = "Scaffold controller template expoiting i18n and aware of cancan and WiceGrid"
16
16
  s.email = "dima@koulikoff.ru"
17
17
  s.extra_rdoc_files = [
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  "i18n_scaffold_controller_template.gemspec",
30
30
  "lib/i18n_scaffold_controller_template.rb",
31
31
  "lib/i18n_scaffold_controller_template/generators/rails/scaffold_controller_generator.rb",
32
+ "lib/i18n_scaffold_controller_template/generators/rails/templates/api_controller.rb",
32
33
  "lib/i18n_scaffold_controller_template/generators/rails/templates/controller.rb",
33
34
  "lib/i18n_scaffold_controller_template/railtie.rb",
34
35
  "spec/i18n_scaffold_controller_template_spec.rb",
@@ -36,7 +37,7 @@ Gem::Specification.new do |s|
36
37
  ]
37
38
  s.homepage = "http://github.com/dima4p/i18n_scaffold_controller_template"
38
39
  s.licenses = ["MIT"]
39
- s.rubygems_version = "2.4.5"
40
+ s.rubygems_version = "2.5.1"
40
41
  s.summary = "Scaffold controller template expoiting i18n and aware of cancan and WiceGrid"
41
42
 
42
43
  if s.respond_to? :specification_version then
@@ -3,8 +3,22 @@ require 'rails/generators/rails/scaffold_controller/scaffold_controller_generato
3
3
 
4
4
  module Rails
5
5
  module Generators
6
- class ScaffoldControllerGenerator
6
+ class ScaffoldControllerGenerator < NamedBase
7
+ class_option :with_api, type: :boolean,
8
+ desc: "Generates also API controller in 'app/contorllers/api'"
9
+ class_option :api_version, type: :string,
10
+ desc: "Adds specified path as version to 'app/contorllers/api'"
11
+
7
12
  source_paths.unshift File.expand_path('../templates', __FILE__)
13
+
14
+ def create_api_controller_with_namespace
15
+ return unless options.with_api?
16
+ path = ['app/controllers/api']
17
+ path << options[:api_version].underscore if options[:api_version].present?
18
+ path << controller_class_path << "#{controller_file_name}_controller.rb"
19
+ template 'api_controller.rb', File.join(*path)
20
+ end
21
+
8
22
  end
9
23
  end
10
24
  end
@@ -0,0 +1,96 @@
1
+ <% if namespaced? -%>
2
+ require_dependency "<%= namespaced_path %>/application_controller"
3
+
4
+ <% end -%>
5
+ <% module_namespacing do -%>
6
+ class Api::<%= options[:api_version].camelcase + '::' if options[:api_version].present? %><%= controller_class_name %>Controller < ApiController
7
+ <% if defined? CanCan -%>
8
+ # before_action :set_<%= singular_table_name %>, only: [:show, :update, :destroy]
9
+ load_and_authorize_resource
10
+ <% else -%>
11
+ before_action :set_<%= singular_table_name %>, only: [:show, :update, :destroy]
12
+ <% end -%>
13
+
14
+ # GET <%= route_url %>
15
+ def index
16
+ <% unless defined? CanCan -%>
17
+ @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
18
+
19
+ <% end -%>
20
+ <% if defined? Kaminari -%>
21
+ @<%= plural_table_name %> = @<%= plural_table_name %>.page(params[:page]) if params[:page].present?
22
+ @<%= plural_table_name %> = @<%= plural_table_name %>.per(params[:per_page]) if params[:per_page].present?
23
+ <% end -%>
24
+ <% if options.jbuilder? -%>
25
+ render '<%= "/#{plural_table_name}/index.json" %>'
26
+ <% else -%>
27
+ render json: <%= "@#{plural_table_name}" %>
28
+ <% end -%>
29
+ end
30
+
31
+ # GET <%= route_url %>/1
32
+ def show
33
+ <% if options.jbuilder? -%>
34
+ render '<%= "/#{plural_table_name}/show.json" %>'
35
+ <% else -%>
36
+ render json: <%= "@#{singular_table_name}" %>
37
+ <% end -%>
38
+ end
39
+
40
+ # POST <%= route_url %>
41
+ def create
42
+ <% unless defined? CanCan -%>
43
+ @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
44
+
45
+ <% end -%>
46
+ if @<%= orm_instance.save %>
47
+ <% if options.jbuilder? -%>
48
+ render '<%= "/#{plural_table_name}/show.json" %>', status: :created, location: <%= "@#{singular_table_name}" %>
49
+ <% else -%>
50
+ render json: <%= "@#{singular_table_name}" %>, status: :created, location: <%= "@#{singular_table_name}" %>
51
+ <% end -%>
52
+ else
53
+ render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
54
+ end
55
+ end
56
+
57
+ # PATCH/PUT <%= route_url %>/1
58
+ def update
59
+ if @<%= orm_instance.update("#{singular_table_name}_params") %>
60
+ <% if options.jbuilder? -%>
61
+ render '<%= "/#{plural_table_name}/show.json" %>'
62
+ <% else -%>
63
+ render json: <%= "@#{singular_table_name}" %>
64
+ <% end -%>
65
+ else
66
+ render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
67
+ end
68
+ end
69
+
70
+ # DELETE <%= route_url %>/1
71
+ def destroy
72
+ @<%= orm_instance.destroy %>
73
+ end
74
+
75
+ private
76
+
77
+ <% unless defined? CanCan -%>
78
+ # Use callbacks to share common setup or constraints between actions.
79
+ def set_<%= singular_table_name %>
80
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
81
+ end
82
+
83
+ <% end -%>
84
+ # Only allow a trusted parameter "white list" through.
85
+ def <%= "#{singular_table_name}_params" %>
86
+ <%- if attributes_names.empty? -%>
87
+ params.fetch(:<%= singular_table_name %>, {})
88
+ <%- else -%>
89
+ list = [
90
+ <%= attributes_names.map { |name| ":#{name}" }.join(', ') %>
91
+ ]
92
+ params.require(:<%= singular_table_name %>).permit(*list)
93
+ <%- end -%>
94
+ end
95
+ end
96
+ <% end -%>
@@ -7,22 +7,22 @@ require_dependency "<%= namespaced_file_path %>/application_controller"
7
7
  <% module_namespacing do -%>
8
8
  class <%= controller_class_name %>Controller < ApplicationController
9
9
 
10
- <% if Rails.application.config.generators.options[:rails][:cancan] -%>
11
- # before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
10
+ <% if defined? CanCan -%>
12
11
  load_and_authorize_resource
13
12
  <% else -%>
14
13
  before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
15
14
  <% end -%>
16
15
 
17
16
  # GET <%= route_url %>
18
- # GET <%= route_url %>.json
19
17
  def index
20
- <% if Rails.application.config.generators.options[:rails][:cancan] -%>
21
- # @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
22
- <% else -%>
18
+ <% unless defined? CanCan -%>
23
19
  @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
24
20
  <% end -%>
25
- <% if defined?(Wice::WiceGrid) or defined?(Kaminari) -%>
21
+ <% if options.with_api? -%>
22
+ <% if defined? Wice::WiceGrid -%>
23
+ @grid = initialize_grid @<%= plural_table_name %>
24
+ <% end -%>
25
+ <% elsif defined?(Wice::WiceGrid) or defined?(Kaminari) -%>
26
26
  respond_to do |format|
27
27
  format.html do
28
28
  <% if defined? Wice::WiceGrid -%>
@@ -40,15 +40,12 @@ class <%= controller_class_name %>Controller < ApplicationController
40
40
  end
41
41
 
42
42
  # GET <%= route_url %>/1
43
- # GET <%= route_url %>/1.json
44
43
  def show
45
44
  end
46
45
 
47
46
  # GET <%= route_url %>/new
48
47
  def new
49
- <% if Rails.application.config.generators.options[:rails][:cancan] -%>
50
- # @<%= singular_table_name %> = <%= orm_class.build(class_name) %>
51
- <% else -%>
48
+ <% unless defined? CanCan -%>
52
49
  @<%= singular_table_name %> = <%= orm_class.build(class_name) %>
53
50
  <% end -%>
54
51
  end
@@ -58,14 +55,17 @@ class <%= controller_class_name %>Controller < ApplicationController
58
55
  end
59
56
 
60
57
  # POST <%= route_url %>
61
- # POST <%= route_url %>.json
62
58
  def create
63
- <% if Rails.application.config.generators.options[:rails][:cancan] -%>
64
- # @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
65
- <% else -%>
59
+ <% unless defined? CanCan -%>
66
60
  @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
67
- <% end -%>
68
61
 
62
+ <% end -%>
63
+ <% if options.with_api? -%>
64
+ if @<%= orm_instance.save %>
65
+ redirect_to @<%= singular_table_name %>, notice: t('<%= table_name %>.was_created')
66
+ else
67
+ render :new
68
+ <% else -%>
69
69
  respond_to do |format|
70
70
  if @<%= orm_instance.save %>
71
71
  format.html { redirect_to @<%= singular_table_name %>, notice: t('<%= table_name %>.was_created') }
@@ -74,12 +74,18 @@ class <%= controller_class_name %>Controller < ApplicationController
74
74
  format.html { render :new }
75
75
  format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
76
76
  end
77
+ <% end -%>
77
78
  end
78
79
  end
79
80
 
80
81
  # PATCH/PUT <%= route_url %>/1
81
- # PATCH/PUT <%= route_url %>/1.json
82
82
  def update
83
+ <% if options.with_api? -%>
84
+ if @<%= orm_instance.update("#{singular_table_name}_params") %>
85
+ redirect_to @<%= singular_table_name %>, notice: t('<%= table_name %>.was_updated')
86
+ else
87
+ render :edit
88
+ <% else -%>
83
89
  respond_to do |format|
84
90
  if @<%= orm_instance.update("#{singular_table_name}_params") %>
85
91
  format.html { redirect_to @<%= singular_table_name %>, notice: t('<%= table_name %>.was_updated') }
@@ -88,22 +94,26 @@ class <%= controller_class_name %>Controller < ApplicationController
88
94
  format.html { render :edit }
89
95
  format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
90
96
  end
97
+ <% end -%>
91
98
  end
92
99
  end
93
100
 
94
101
  # DELETE <%= route_url %>/1
95
- # DELETE <%= route_url %>/1.json
96
102
  def destroy
97
103
  @<%= orm_instance.destroy %>
104
+ <% if options.with_api? -%>
105
+ redirect_to <%= index_helper %>_url, notice: t('<%= table_name %>.was_deleted')
106
+ <% else -%>
98
107
  respond_to do |format|
99
108
  format.html { redirect_to <%= index_helper %>_url, notice: t('<%= table_name %>.was_deleted') }
100
109
  format.json { head :no_content }
101
110
  end
111
+ <% end -%>
102
112
  end
103
113
 
104
114
  private
105
115
 
106
- <% if Rails.application.config.generators.options[:rails][:cancan] -%>
116
+ <% if defined? CanCan -%>
107
117
  # Use callbacks to share common setup or constraints between actions.
108
118
  # def set_<%= singular_table_name %>
109
119
  # @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
@@ -117,14 +127,14 @@ class <%= controller_class_name %>Controller < ApplicationController
117
127
 
118
128
  # Never trust parameters from the scary internet, only allow the white list through.
119
129
  def <%= "#{singular_table_name}_params" %>
120
- <%- if attributes_names.empty? -%>
121
- params[<%= ":#{singular_table_name}" %>]
122
- <%- else -%>
130
+ <%- if attributes_names.empty? -%>
131
+ params.fetch(:<%= singular_table_name %>, {})
132
+ <%- else -%>
123
133
  list = [
124
134
  <%= attributes_names.map { |name| ":#{name}" }.join(', ') %>
125
135
  ]
126
- params.require(<%= ":#{singular_table_name}" %>).permit(*list)
127
- <%- end -%>
136
+ params.require(:<%= singular_table_name %>).permit(*list)
137
+ <%- end -%>
128
138
  end
129
139
  end
130
140
  <% end -%>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n_scaffold_controller_template
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitri Koulikoff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-25 00:00:00.000000000 Z
11
+ date: 2016-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -98,6 +98,7 @@ files:
98
98
  - i18n_scaffold_controller_template.gemspec
99
99
  - lib/i18n_scaffold_controller_template.rb
100
100
  - lib/i18n_scaffold_controller_template/generators/rails/scaffold_controller_generator.rb
101
+ - lib/i18n_scaffold_controller_template/generators/rails/templates/api_controller.rb
101
102
  - lib/i18n_scaffold_controller_template/generators/rails/templates/controller.rb
102
103
  - lib/i18n_scaffold_controller_template/railtie.rb
103
104
  - spec/i18n_scaffold_controller_template_spec.rb
@@ -122,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
123
  version: '0'
123
124
  requirements: []
124
125
  rubyforge_project:
125
- rubygems_version: 2.4.5
126
+ rubygems_version: 2.5.1
126
127
  signing_key:
127
128
  specification_version: 4
128
129
  summary: Scaffold controller template expoiting i18n and aware of cancan and WiceGrid