authorized_rails_scaffolds 0.0.14 → 0.0.15
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 +4 -4
- data/README.md +3 -0
- data/lib/authorized_rails_scaffolds.rb +23 -6
- data/lib/authorized_rails_scaffolds/helper.rb +13 -54
- data/lib/authorized_rails_scaffolds/{controller_macros.rb → macros/controller_macros.rb} +1 -1
- data/lib/authorized_rails_scaffolds/{factory_macros.rb → macros/factory_macros.rb} +5 -5
- data/lib/authorized_rails_scaffolds/macros/parent_macros.rb +56 -0
- data/lib/authorized_rails_scaffolds/macros/path_macros.rb +70 -0
- data/lib/authorized_rails_scaffolds/macros/resource_macros.rb +53 -0
- data/lib/authorized_rails_scaffolds/macros/route_example_macros.rb +65 -0
- data/lib/authorized_rails_scaffolds/{test_var_macros.rb → macros/test_var_macros.rb} +9 -9
- data/lib/authorized_rails_scaffolds/rails_erb_scaffold_helper.rb +0 -25
- data/lib/authorized_rails_scaffolds/rails_helper_helper.rb +41 -0
- data/lib/authorized_rails_scaffolds/rails_scaffold_controller_helper.rb +27 -2
- data/lib/authorized_rails_scaffolds/rspec_integration_helper.rb +9 -0
- data/lib/authorized_rails_scaffolds/rspec_scaffold_controller_helper.rb +8 -4
- data/lib/authorized_rails_scaffolds/rspec_scaffold_helper.rb +5 -4
- data/lib/authorized_rails_scaffolds/rspec_scaffold_routing_helper.rb +2 -2
- data/lib/authorized_rails_scaffolds/rspec_scaffold_view_helper.rb +1 -1
- data/lib/authorized_rails_scaffolds/version.rb +1 -1
- data/lib/generators/authorized_rails_scaffolds/install_macros/install_macros_generator.rb +2 -1
- data/lib/generators/authorized_rails_scaffolds/install_macros/templates/devise_can_can/USAGE +1 -0
- data/lib/generators/authorized_rails_scaffolds/install_macros/templates/devise_can_can/request_macros.rb +8 -0
- data/lib/generators/authorized_rails_scaffolds/install_templates/install_templates_generator.rb +14 -0
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/_form.html.erb +2 -1
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/controller.rb +68 -35
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/helper.rb +25 -0
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/index.html.erb +15 -15
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/show.html.erb +1 -1
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/controller_spec.rb +90 -84
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/edit_spec.rb +37 -30
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/index_spec.rb +56 -51
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/new_spec.rb +33 -26
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/request_spec.rb +125 -0
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/routing_spec.rb +12 -9
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/show_spec.rb +20 -17
- data/spec/lib/authorized_rails_scaffolds/rails_erb_scaffold_helper_spec.rb +24 -23
- data/spec/lib/authorized_rails_scaffolds/rails_helper_helper_spec.rb +66 -0
- data/spec/lib/authorized_rails_scaffolds/rails_scaffold_controller_helper_spec.rb +42 -0
- data/spec/lib/authorized_rails_scaffolds/rspec_scaffold_controller_helper_spec.rb +7 -7
- data/spec/lib/authorized_rails_scaffolds/rspec_scaffold_routing_helper_spec.rb +14 -14
- data/spec/lib/authorized_rails_scaffolds/rspec_scaffold_view_helper_spec.rb +4 -4
- metadata +16 -7
- data/lib/authorized_rails_scaffolds/resource_macros.rb +0 -81
- data/lib/authorized_rails_scaffolds/route_example_macros.rb +0 -43
@@ -1,16 +1,16 @@
|
|
1
|
-
module TestVarMacros
|
1
|
+
module AuthorizedRailsScaffolds::Macros::TestVarMacros
|
2
2
|
|
3
3
|
# Variable to hold stubs of parent models
|
4
4
|
def references_test_var(references_table)
|
5
|
-
"@#{
|
5
|
+
"@#{references_test_name(references_table)}"
|
6
6
|
end
|
7
7
|
|
8
8
|
def references_test_sym(references_table)
|
9
|
-
":#{
|
9
|
+
":#{references_test_name(references_table)}"
|
10
10
|
end
|
11
11
|
|
12
|
-
def
|
13
|
-
if
|
12
|
+
def references_test_name(references_table, var_number = nil)
|
13
|
+
if parent_model_names.include? references_table
|
14
14
|
parent_property = "parent_#{references_table}"
|
15
15
|
else
|
16
16
|
parent_property = "stub_#{references_table}"
|
@@ -21,17 +21,17 @@ module TestVarMacros
|
|
21
21
|
|
22
22
|
# Variable name stub resource is assigned to (i.e. @foo_bar)
|
23
23
|
def resource_test_var(var_number = nil)
|
24
|
-
"@#{
|
24
|
+
"@#{resource_test_name(var_number)}"
|
25
25
|
end
|
26
26
|
|
27
27
|
# Symbol used to assign test resources (i.e. :foo_bar)
|
28
28
|
def resource_test_sym(var_number = nil)
|
29
|
-
":#{
|
29
|
+
":#{resource_test_name(var_number)}"
|
30
30
|
end
|
31
31
|
|
32
32
|
# Generator for properties used for testing
|
33
|
-
def
|
34
|
-
resource_property = "test_#{
|
33
|
+
def resource_test_name(var_number = nil)
|
34
|
+
resource_property = "test_#{resource_name}"
|
35
35
|
resource_property = "#{resource_property}_#{var_number}" unless var_number.nil?
|
36
36
|
resource_property
|
37
37
|
end
|
@@ -6,31 +6,6 @@ class AuthorizedRailsScaffolds::RailsErbScaffoldHelper < AuthorizedRailsScaffold
|
|
6
6
|
@modular_class_name = options[:class_name] || options[:local_class_name]
|
7
7
|
end
|
8
8
|
|
9
|
-
# returns values that should be parsed by a form in order for post and put actions to work
|
10
|
-
def scoped_values_for_form(variable = nil)
|
11
|
-
variable ||= resource_var
|
12
|
-
|
13
|
-
form_argument_values = []
|
14
|
-
|
15
|
-
# Add the modules
|
16
|
-
parent_module_groups.each do |parent_module|
|
17
|
-
form_argument_values << ":#{parent_module}"
|
18
|
-
end
|
19
|
-
|
20
|
-
# Add the models
|
21
|
-
parent_model_tables.each do |parent_model|
|
22
|
-
form_argument_values << parent_variable(parent_model)
|
23
|
-
end
|
24
|
-
|
25
|
-
form_argument_values << variable
|
26
|
-
|
27
|
-
if form_argument_values.count == 1
|
28
|
-
return variable
|
29
|
-
else
|
30
|
-
"[#{form_argument_values.join(', ')}]"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
9
|
# Class name with parent modules included (i.e. Example::FooBar)
|
35
10
|
# Name of class assumed by default generators, used as a base for determining modules and class
|
36
11
|
def modular_class_name
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class AuthorizedRailsScaffolds::RailsHelperHelper < AuthorizedRailsScaffolds::Helper
|
2
|
+
|
3
|
+
def initialize(options = {})
|
4
|
+
super options
|
5
|
+
|
6
|
+
@modular_class_name = options[:class_name] || options[:local_class_name]
|
7
|
+
end
|
8
|
+
|
9
|
+
# Class name with parent modules included (i.e. Example::FooBar)
|
10
|
+
# Name of class assumed by default generators, used as a base for determining modules and class
|
11
|
+
def modular_class_name
|
12
|
+
@modular_class_name.singularize
|
13
|
+
end
|
14
|
+
|
15
|
+
# returns values that should be parsed by a form in order for post and put actions to work
|
16
|
+
def scoped_values_for_form(variable, use_shallow_route=false)
|
17
|
+
variable ||= resource_var
|
18
|
+
|
19
|
+
form_argument_values = []
|
20
|
+
|
21
|
+
# Add the modules
|
22
|
+
parent_module_groups.each do |parent_module|
|
23
|
+
form_argument_values << ":#{parent_module}"
|
24
|
+
end
|
25
|
+
|
26
|
+
# Add the models
|
27
|
+
parent_models = use_shallow_route ? parent_model_names[0..-2] : parent_model_names
|
28
|
+
parent_models.each do |parent_model|
|
29
|
+
form_argument_values << parent_variable(parent_model)
|
30
|
+
end
|
31
|
+
|
32
|
+
form_argument_values << variable
|
33
|
+
|
34
|
+
if form_argument_values.count == 1
|
35
|
+
return variable
|
36
|
+
else
|
37
|
+
"[#{form_argument_values.join(', ')}]"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
class AuthorizedRailsScaffolds::RailsScaffoldControllerHelper < AuthorizedRailsScaffolds::Helper
|
2
|
-
include ControllerMacros
|
3
|
-
include RouteExampleMacros
|
2
|
+
include AuthorizedRailsScaffolds::Macros::ControllerMacros
|
3
|
+
include AuthorizedRailsScaffolds::Macros::RouteExampleMacros
|
4
4
|
|
5
5
|
def initialize(options = {})
|
6
6
|
super options
|
@@ -17,4 +17,29 @@ class AuthorizedRailsScaffolds::RailsScaffoldControllerHelper < AuthorizedRailsS
|
|
17
17
|
@modular_class_name
|
18
18
|
end
|
19
19
|
|
20
|
+
def load_and_authorize_parent(model_name)
|
21
|
+
load_resource_code = "load_and_authorize_resource #{parent_sym(model_name)}"
|
22
|
+
|
23
|
+
parent_model = model_parent_name(model_name)
|
24
|
+
unless parent_model.nil?
|
25
|
+
load_resource_code += ", :through => #{parent_sym(parent_model)}"
|
26
|
+
end
|
27
|
+
|
28
|
+
load_resource_code
|
29
|
+
end
|
30
|
+
|
31
|
+
def load_resource
|
32
|
+
load_resource_code = "load_resource #{resource_symbol}"
|
33
|
+
|
34
|
+
if parent_models.any?
|
35
|
+
load_resource_code += ", :through => #{parent_sym(parent_model_names.last)}"
|
36
|
+
end
|
37
|
+
|
38
|
+
if shallow_routes?
|
39
|
+
load_resource_code += ", :shallow => true"
|
40
|
+
end
|
41
|
+
|
42
|
+
load_resource_code
|
43
|
+
end
|
44
|
+
|
20
45
|
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class AuthorizedRailsScaffolds::RSpecIntegrationHelper < AuthorizedRailsScaffolds::RSpecScaffoldHelper
|
2
|
+
include AuthorizedRailsScaffolds::Macros::FactoryMacros
|
3
|
+
include AuthorizedRailsScaffolds::Macros::RouteExampleMacros
|
4
|
+
|
5
|
+
def extra_comments
|
6
|
+
parent_models.any? ? " belonging to a #{parent_models[-1]}" : ''
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
class AuthorizedRailsScaffolds::RSpecScaffoldControllerHelper < AuthorizedRailsScaffolds::RSpecScaffoldHelper
|
2
|
-
include ControllerMacros
|
3
|
-
include FactoryMacros
|
4
|
-
include RouteExampleMacros
|
2
|
+
include AuthorizedRailsScaffolds::Macros::ControllerMacros
|
3
|
+
include AuthorizedRailsScaffolds::Macros::FactoryMacros
|
4
|
+
include AuthorizedRailsScaffolds::Macros::RouteExampleMacros
|
5
5
|
|
6
6
|
def initialize(options = {})
|
7
7
|
super options
|
@@ -10,8 +10,12 @@ class AuthorizedRailsScaffolds::RSpecScaffoldControllerHelper < AuthorizedRailsS
|
|
10
10
|
@controller_class_name = options[:controller_class_name]
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
13
|
+
def build_example_index_params(*extra_params)
|
14
14
|
[example_route_extra_params + extra_params].join(", ")
|
15
15
|
end
|
16
16
|
|
17
|
+
def build_example_show_params(*extra_params)
|
18
|
+
[example_route_extra_params(shallow_routes?) + extra_params].join(", ")
|
19
|
+
end
|
20
|
+
|
17
21
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
class AuthorizedRailsScaffolds::RSpecScaffoldHelper < AuthorizedRailsScaffolds::Helper
|
2
|
-
include TestVarMacros
|
2
|
+
include AuthorizedRailsScaffolds::Macros::TestVarMacros
|
3
3
|
|
4
4
|
def initialize(options = {})
|
5
5
|
super options
|
@@ -14,12 +14,13 @@ class AuthorizedRailsScaffolds::RSpecScaffoldHelper < AuthorizedRailsScaffolds::
|
|
14
14
|
@modular_class_name
|
15
15
|
end
|
16
16
|
|
17
|
-
def parent_variables
|
18
|
-
@parent_variables ||=
|
17
|
+
def parent_variables(use_shallow_route=false)
|
18
|
+
@parent_variables ||= parent_model_names.collect{ |parent_table| references_test_name(parent_table) }
|
19
|
+
use_shallow_route ? @parent_variables[0..-2] : @parent_variables
|
19
20
|
end
|
20
21
|
|
21
22
|
def references_show_route(attribute_name, variable = nil)
|
22
|
-
variable ||= "#{
|
23
|
+
variable ||= "#{resource_test_name}.#{attribute_name}"
|
23
24
|
super attribute_name, variable
|
24
25
|
end
|
25
26
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
class AuthorizedRailsScaffolds::RSpecScaffoldRoutingHelper < AuthorizedRailsScaffolds::RSpecScaffoldHelper
|
2
|
-
include ControllerMacros
|
3
|
-
include RouteExampleMacros
|
2
|
+
include AuthorizedRailsScaffolds::Macros::ControllerMacros
|
3
|
+
include AuthorizedRailsScaffolds::Macros::RouteExampleMacros
|
4
4
|
|
5
5
|
def initialize(options = {})
|
6
6
|
super options
|
@@ -9,8 +9,9 @@ class AuthorizedRailsScaffolds::InstallMacrosGenerator < Rails::Generators::Base
|
|
9
9
|
parts << options[:authentication].underscore unless options[:authentication].nil?
|
10
10
|
parts << options[:authorization].underscore unless options[:authorization].nil?
|
11
11
|
template_file = parts.any? ? parts.join('_') : nil
|
12
|
-
|
12
|
+
|
13
13
|
copy_file [template_file, 'controller_macros.rb'].join('/'), "spec/support/#{template_file}_controller_macros.rb"
|
14
|
+
copy_file [template_file, 'request_macros.rb'].join('/'), "spec/support/#{template_file}_request_macros.rb"
|
14
15
|
readme [template_file, 'USAGE'].join('/')
|
15
16
|
end
|
16
17
|
|
data/lib/generators/authorized_rails_scaffolds/install_templates/install_templates_generator.rb
CHANGED
@@ -20,6 +20,12 @@ class AuthorizedRailsScaffolds::InstallTemplatesGenerator < Rails::Generators::B
|
|
20
20
|
# Model Spec Templates
|
21
21
|
copy_rspec_model_template 'model_spec.rb'
|
22
22
|
|
23
|
+
# Helper Spec Template
|
24
|
+
copy_rails_helper_controller 'helper.rb'
|
25
|
+
|
26
|
+
# Request Spec Templates
|
27
|
+
copy_rspec_integration_template 'request_spec.rb'
|
28
|
+
|
23
29
|
# Routing Spec Templates
|
24
30
|
copy_rspec_scaffold_template 'routing_spec.rb'
|
25
31
|
|
@@ -32,6 +38,10 @@ class AuthorizedRailsScaffolds::InstallTemplatesGenerator < Rails::Generators::B
|
|
32
38
|
|
33
39
|
protected
|
34
40
|
|
41
|
+
def copy_rails_helper_controller(template_name)
|
42
|
+
copy_file "scaffold/#{template_name}", "lib/templates/rails/helper/#{template_name}"
|
43
|
+
end
|
44
|
+
|
35
45
|
def copy_erb_scaffold_template(template_name)
|
36
46
|
copy_file "scaffold/#{template_name}", "lib/templates/erb/scaffold/#{template_name}"
|
37
47
|
end
|
@@ -40,6 +50,10 @@ class AuthorizedRailsScaffolds::InstallTemplatesGenerator < Rails::Generators::B
|
|
40
50
|
copy_file "scaffold/#{template_name}", "lib/templates/rails/scaffold_controller/#{template_name}"
|
41
51
|
end
|
42
52
|
|
53
|
+
def copy_rspec_integration_template(template_name)
|
54
|
+
copy_file "spec/#{template_name}", "lib/templates/rspec/integration/#{template_name}"
|
55
|
+
end
|
56
|
+
|
43
57
|
def copy_rspec_model_template(template_name)
|
44
58
|
copy_file "spec/#{template_name}", "lib/templates/rspec/model/#{template_name}"
|
45
59
|
end
|
data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/_form.html.erb
CHANGED
@@ -6,10 +6,11 @@ t_helper = AuthorizedRailsScaffolds::RailsErbScaffoldHelper.new(
|
|
6
6
|
:file_name => file_name
|
7
7
|
)
|
8
8
|
|
9
|
+
resource_name = t_helper.resource_name
|
9
10
|
resource_var = t_helper.resource_var
|
10
11
|
|
11
12
|
-%>
|
12
|
-
<%%= simple_form_for(<%=
|
13
|
+
<%%= simple_form_for(<%= resource_name %>_form_values(<%= resource_var %>), :html => { class: 'form-horizontal' }) do |f| %>
|
13
14
|
<%%= f.error_notification %>
|
14
15
|
|
15
16
|
<div class="form-inputs">
|
data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/controller.rb
CHANGED
@@ -11,126 +11,159 @@ require_dependency "<%= namespaced_file_path %>/application_controller"
|
|
11
11
|
# controller_class_name
|
12
12
|
# singular_table_name
|
13
13
|
# file_name
|
14
|
+
# human_name
|
14
15
|
# orm_instance
|
15
16
|
# route_url
|
16
17
|
#
|
17
18
|
|
18
19
|
t_helper = AuthorizedRailsScaffolds::RailsScaffoldControllerHelper.new(
|
19
20
|
:class_name => class_name,
|
21
|
+
:human_name => human_name,
|
20
22
|
:controller_class_name => controller_class_name,
|
21
23
|
:singular_table_name => singular_table_name,
|
22
24
|
:file_name => file_name
|
23
25
|
)
|
24
26
|
|
25
27
|
resource_class = t_helper.resource_class # Non-Namespaced class name
|
28
|
+
resource_human_name = t_helper.resource_human_name
|
26
29
|
resource_symbol = t_helper.resource_symbol
|
27
|
-
|
30
|
+
resource_name = t_helper.resource_name
|
31
|
+
resource_array_name = t_helper.resource_array_name
|
28
32
|
resource_var = t_helper.resource_var
|
29
|
-
|
33
|
+
resource_array_var = t_helper.resource_array_var # Pluralized non-namespaced variable name
|
30
34
|
|
31
|
-
|
35
|
+
example_index_path = t_helper.example_index_path
|
36
|
+
example_show_path = t_helper.example_show_path
|
32
37
|
|
33
38
|
# Override default orm instance
|
34
|
-
orm_instance = Rails::Generators::ActiveModel.new
|
39
|
+
orm_instance = Rails::Generators::ActiveModel.new resource_name
|
35
40
|
|
36
41
|
-%>
|
37
42
|
<% module_namespacing do -%>
|
38
43
|
class <%= t_helper.controller_class_name %> < <%= t_helper.application_controller_class %>
|
39
|
-
<%-
|
40
|
-
|
44
|
+
<%- t_helper.parent_model_names.each_with_index do |model_name, model_index| -%>
|
45
|
+
<%= t_helper.load_and_authorize_parent model_name %>
|
41
46
|
<%- end -%>
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
47
|
+
<%= t_helper.load_resource %>
|
48
|
+
<%- if t_helper.shallow_routes? -%>
|
49
|
+
before_filter :load_shallow_resources
|
50
|
+
<%- end -%>
|
51
|
+
authorize_resource <%= resource_symbol %>
|
52
|
+
|
53
|
+
# GET <%= example_index_path %>
|
54
|
+
# GET <%= example_index_path %>.json
|
46
55
|
def index
|
47
|
-
# <%=
|
56
|
+
# <%= resource_array_var %> = <%= orm_class.all(resource_class) %>
|
57
|
+
# <%= resource_array_var %> = <%= resource_array_var %>.page(params[:page] || 1)
|
48
58
|
|
49
59
|
respond_to do |format|
|
50
60
|
format.html # index.html.erb
|
51
|
-
format.json { render <%= key_value :json,
|
61
|
+
format.json { render <%= key_value :json, "{ #{key_value(resource_array_name, resource_array_var)} }" %> }
|
52
62
|
end
|
53
63
|
end
|
54
64
|
|
55
|
-
# GET <%=
|
56
|
-
# GET <%=
|
65
|
+
# GET <%= example_show_path %>
|
66
|
+
# GET <%= example_show_path %>.json
|
57
67
|
def show
|
58
68
|
# <%= resource_var %> = <%= orm_class.find(resource_class, "params[:id]") %>
|
59
69
|
|
60
70
|
respond_to do |format|
|
61
71
|
format.html # show.html.erb
|
62
|
-
format.json { render <%= key_value :json, resource_var %> }
|
72
|
+
format.json { render <%= key_value :json, "{ #{key_value(resource_name, resource_var)} }" %> }
|
63
73
|
end
|
64
74
|
end
|
65
75
|
|
66
|
-
# GET <%=
|
67
|
-
# GET <%=
|
76
|
+
# GET <%= example_index_path %>/new
|
77
|
+
# GET <%= example_index_path %>/new.json
|
68
78
|
def new
|
69
79
|
# <%= resource_var %> = <%= orm_class.build(resource_class) %>
|
70
80
|
|
71
81
|
respond_to do |format|
|
72
82
|
format.html # new.html.erb
|
73
|
-
format.json { render <%= key_value :json,
|
83
|
+
format.json { render <%= key_value :json, "{ #{key_value(resource_name, resource_var)} }" %> }
|
74
84
|
end
|
75
85
|
end
|
76
86
|
|
77
|
-
# GET <%=
|
87
|
+
# GET <%= example_show_path %>/edit
|
78
88
|
def edit
|
79
89
|
# <%= resource_var %> = <%= orm_class.find(resource_class, "params[:id]") %>
|
80
90
|
end
|
81
91
|
|
82
|
-
# POST <%=
|
83
|
-
# POST <%=
|
92
|
+
# POST <%= example_index_path %>
|
93
|
+
# POST <%= example_index_path %>.json
|
84
94
|
def create
|
85
95
|
# <%= resource_var %> = <%= orm_class.build(resource_class, "params[#{resource_symbol}]") %>
|
86
96
|
|
87
97
|
respond_to do |format|
|
88
98
|
if @<%= orm_instance.save %>
|
89
|
-
format.html { redirect_to <%= t_helper.controller_show_route(resource_var) %>, <%= key_value :notice, "'#{
|
90
|
-
format.json { render <%= key_value :json, resource_var %>, <%= key_value :status, ':created' %>, <%= key_value :location, t_helper.controller_show_route(resource_var) %> }
|
99
|
+
format.html { redirect_to <%= t_helper.controller_show_route(resource_var) %>, <%= key_value :notice, "'#{resource_human_name} was successfully created.'" %> }
|
100
|
+
format.json { render <%= key_value :json, "{ #{key_value(resource_name, resource_var)} }" %>, <%= key_value :status, ':created' %>, <%= key_value :location, t_helper.controller_show_route(resource_var) %> }
|
91
101
|
else
|
92
102
|
format.html { render <%= key_value :action, '"new"' %> }
|
93
|
-
format.json { render <%= key_value :json, "@#{orm_instance.errors}" %>, <%= key_value :status, ':unprocessable_entity' %> }
|
103
|
+
format.json { render <%= key_value :json, "{ " + key_value('errors', "@#{orm_instance.errors}") + " }" %>, <%= key_value :status, ':unprocessable_entity' %> }
|
94
104
|
end
|
95
105
|
end
|
96
106
|
end
|
97
107
|
|
98
|
-
# PUT <%=
|
99
|
-
# PUT <%=
|
108
|
+
# PUT <%= example_show_path %>
|
109
|
+
# PUT <%= example_show_path %>.json
|
100
110
|
def update
|
101
111
|
# <%= resource_var %> = <%= orm_class.find(resource_class, "params[:id]") %>
|
102
112
|
|
103
113
|
respond_to do |format|
|
104
114
|
if @<%= orm_instance.update_attributes("params[#{resource_symbol}]") %>
|
105
|
-
format.html { redirect_to <%= t_helper.controller_show_route resource_var %>, <%= key_value :notice, "'#{
|
115
|
+
format.html { redirect_to <%= t_helper.controller_show_route resource_var %>, <%= key_value :notice, "'#{resource_human_name} was successfully updated.'" %> }
|
106
116
|
format.json { head :no_content }
|
107
117
|
else
|
108
118
|
format.html { render <%= key_value :action, '"edit"' %> }
|
109
|
-
format.json { render <%= key_value :json, "@#{orm_instance.errors}" %>, <%= key_value :status, ':unprocessable_entity' %> }
|
119
|
+
format.json { render <%= key_value :json, "{ " + key_value('errors', "@#{orm_instance.errors}") + " }" %>, <%= key_value :status, ':unprocessable_entity' %> }
|
110
120
|
end
|
111
121
|
end
|
112
122
|
end
|
113
123
|
|
114
|
-
# DELETE <%=
|
115
|
-
# DELETE <%=
|
124
|
+
# DELETE <%= example_show_path %>
|
125
|
+
# DELETE <%= example_show_path %>.json
|
116
126
|
def destroy
|
117
127
|
# <%= resource_var %> = <%= orm_class.find(resource_class, "params[:id]") %>
|
118
128
|
@<%= orm_instance.destroy %>
|
119
129
|
|
120
130
|
respond_to do |format|
|
121
|
-
format.html { redirect_to <%= t_helper.controller_index_route %> }
|
131
|
+
format.html { redirect_to <%= t_helper.controller_index_route %>, <%= key_value :notice, "'#{resource_human_name} was successfully deleted.'" %> }
|
122
132
|
format.json { head :no_content }
|
123
133
|
end
|
124
134
|
end
|
125
135
|
|
126
136
|
protected
|
137
|
+
<%- if t_helper.shallow_routes? -%>
|
138
|
+
|
139
|
+
# Loads parent resources from <%= resource_var %> if not included in route
|
140
|
+
def load_shallow_resources
|
141
|
+
<%- reverse_parent_models = t_helper.parent_model_names.reverse -%>
|
142
|
+
<%- reverse_parent_models.each_with_index do |parent_model, parent_index| -%>
|
143
|
+
<%- if parent_index == 0 -%>
|
144
|
+
if <%= resource_var %> && <%= resource_var %>.persisted?
|
145
|
+
<%= t_helper.parent_variable(parent_model) %> = <%= resource_var %>.<%= parent_model %> if <%= t_helper.parent_variable(parent_model) %>.nil?
|
146
|
+
end
|
147
|
+
<%- else -%>
|
148
|
+
if <%= t_helper.parent_variable(reverse_parent_models[parent_index-1]) %> && <%= t_helper.parent_variable(parent_model) %>.nil?
|
149
|
+
<%= t_helper.parent_variable(parent_model) %> = <%= t_helper.parent_variable(reverse_parent_models[parent_index-1]) %>.<%= parent_model %>
|
150
|
+
end
|
151
|
+
<%- end -%>
|
152
|
+
<%- end -%>
|
153
|
+
end
|
154
|
+
<%- end -%>
|
127
155
|
|
128
156
|
# Capture any access violations, ensure User isn't unnessisarily redirected to root
|
129
157
|
rescue_from CanCan::AccessDenied do |exception|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
158
|
+
respond_to do |format|
|
159
|
+
format.html do
|
160
|
+
if params[:action] == 'index'<%= t_helper.parent_model_names.collect { |parent_model| " or #{t_helper.parent_variable(parent_model)}.nil?" }.join('') %>
|
161
|
+
redirect_to root_url, :alert => exception.message
|
162
|
+
else
|
163
|
+
redirect_to <%= t_helper.controller_index_route %>, :alert => exception.message
|
164
|
+
end
|
165
|
+
end
|
166
|
+
format.json { head :no_content, :status => :forbidden }
|
134
167
|
end
|
135
168
|
end
|
136
169
|
|