authorized_rails_scaffolds 0.0.10 → 0.0.11
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 +7 -0
- data/README.md +13 -26
- data/lib/authorized_rails_scaffolds.rb +11 -2
- data/lib/authorized_rails_scaffolds/controller_macros.rb +22 -0
- data/lib/authorized_rails_scaffolds/factory_macros.rb +65 -0
- data/lib/authorized_rails_scaffolds/helper.rb +38 -115
- data/lib/authorized_rails_scaffolds/rails_erb_scaffold_helper.rb +40 -0
- data/lib/authorized_rails_scaffolds/rails_scaffold_controller_helper.rb +20 -0
- data/lib/authorized_rails_scaffolds/resource_macros.rb +81 -0
- data/lib/authorized_rails_scaffolds/route_example_macros.rb +43 -0
- data/lib/authorized_rails_scaffolds/rspec_scaffold_controller_helper.rb +17 -0
- data/lib/authorized_rails_scaffolds/rspec_scaffold_helper.rb +26 -0
- data/lib/authorized_rails_scaffolds/rspec_scaffold_routing_helper.rb +12 -0
- data/lib/authorized_rails_scaffolds/{rspec_scaffold_generator_view_helper.rb → rspec_scaffold_view_helper.rb} +3 -2
- data/lib/authorized_rails_scaffolds/test_var_macros.rb +40 -0
- data/lib/authorized_rails_scaffolds/version.rb +1 -1
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/_form.html.erb +4 -6
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/controller.rb +50 -34
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/edit.html.erb +2 -4
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/index.html.erb +17 -18
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/new.html.erb +2 -4
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/show.html.erb +15 -17
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/controller_spec.rb +126 -101
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/edit_spec.rb +44 -39
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/index_spec.rb +73 -65
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/model_spec.rb +9 -8
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/new_spec.rb +38 -35
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/routing_spec.rb +21 -10
- data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/show_spec.rb +37 -20
- data/spec/lib/authorized_rails_scaffolds/rails_erb_scaffold_helper_spec.rb +59 -0
- data/spec/lib/authorized_rails_scaffolds/rails_scaffold_controller_helper_spec.rb +89 -0
- data/spec/lib/authorized_rails_scaffolds/rspec_scaffold_controller_helper_spec.rb +155 -0
- data/spec/lib/authorized_rails_scaffolds/{rspec_scaffold_generator_helper_spec.rb → rspec_scaffold_routing_helper_spec.rb} +57 -50
- data/spec/lib/authorized_rails_scaffolds/rspec_scaffold_view_helper_spec.rb +86 -0
- data/spec/spec_helper.rb +5 -2
- data/spec/support/rails_erb_scaffold_helper_macros.rb +15 -0
- data/spec/support/rails_scaffold_controller_helper_macros.rb +15 -0
- data/spec/support/{rspec_scaffold_generator_helper_macros.rb → rspec_scaffold_controller_helper_macros.rb} +2 -2
- data/spec/support/rspec_scaffold_routing_helper_macros.rb +15 -0
- data/spec/support/rspec_scaffold_view_helper_macros.rb +15 -0
- metadata +66 -28
- data/lib/authorized_rails_scaffolds/rspec_scaffold_generator_helper.rb +0 -82
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d09c935f8d34bf09cb594ada693383b9eca38a74
|
4
|
+
data.tar.gz: d7b570dcd10a6a4755aef6bb727a5744dfcc1d90
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8fadb2e8a6f331e11e2efe73abfd4db41ae33ce182bd2d8f1bba7223b84fa31ac628a497a8b358a749414efc7483d8a867e9044a0762da290254be6fcf19add6
|
7
|
+
data.tar.gz: d82853f18b54c24777df0d27e4fa1715c635914f75ca8a69fb62d54ff38ab006f063dad1f3b4f5e3e6628b73c7593c8749fd739db334bc0f0276efa32947258b
|
data/README.md
CHANGED
@@ -22,32 +22,31 @@ Add this line to your application's Gemfile:
|
|
22
22
|
|
23
23
|
gem 'authorized_rails_scaffolds', :group => :development
|
24
24
|
|
25
|
-
|
25
|
+
Create a initializer at `config/nitializers/authorized_rails_scaffolds.rb` containing:
|
26
26
|
|
27
|
-
|
27
|
+
if Rails.env.development?
|
28
|
+
|
29
|
+
AuthorizedRailsScaffolds.configure do |config|
|
30
|
+
# i.e. ['Category', 'User'] for Awesome/FooBar => awesome_category_user_foo_bars_path
|
31
|
+
config.parent_models = []
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
28
35
|
|
29
|
-
|
36
|
+
And then execute:
|
30
37
|
|
31
|
-
$
|
38
|
+
$ bundle
|
39
|
+
$ rails g authorized_rails_scaffolds:install_templates
|
40
|
+
$ rails g authorized_rails_scaffolds:install_macros
|
32
41
|
|
33
42
|
## Usage
|
34
43
|
|
35
|
-
`authorized_rails_scaffolds` is a development only gem that adds templates for quickly creating new controllers and views to be displayed primarily viewed with Twitter Bootstrap.
|
36
|
-
|
37
|
-
Once you have run all the generators you want, you can remove the gem with no side effects.
|
38
|
-
|
39
|
-
### Install Scaffold Templates
|
40
|
-
|
41
44
|
`authorized_rails_scaffolds` provides a series of rails generators that replace the default scaffold templates generated by Rails.
|
42
45
|
|
43
46
|
The generated Controllers require CanCan to be installed on your system, however it is possible change this behavour by uncommenting out the first line of each generated action manually.
|
44
47
|
|
45
48
|
The generated Controller only handles Authorization and assumes Authentication is managed in the Application Controller or is added manually.
|
46
49
|
|
47
|
-
You can install the generators by running:
|
48
|
-
|
49
|
-
$ rails generate authorized_rails_scaffolds:install_scaffold
|
50
|
-
|
51
50
|
This gem doesn't override the default scaffold.css generated by `rails g scaffold`, however it works best with `rails g scaffold_controller`.
|
52
51
|
|
53
52
|
For example:
|
@@ -55,18 +54,6 @@ For example:
|
|
55
54
|
rails g model FooBar name:string age:integer date_of_birth:date lunch_time:time programmer:boolean joined_at:datetime
|
56
55
|
rails g scaffold_controller Awesome/FooBar name:string age:integer date_of_birth:date lunch_time:time programmer:boolean joined_at:datetime
|
57
56
|
|
58
|
-
### Install Spec Templates
|
59
|
-
|
60
|
-
`authorized_rails_scaffolds` provides a series of rspec generators that replace the default controller and view spec templates generated by RSpec.
|
61
|
-
|
62
|
-
You can install the generators by running:
|
63
|
-
|
64
|
-
$ rails generate authorized_rails_scaffolds:install_spec
|
65
|
-
|
66
|
-
You will need to implement `login_unauthorized_user` and `login_user_with_ability` into your Controller specs.
|
67
|
-
|
68
|
-
Installing Scaffold Controllers in Rails (`rails g scaffold_controller`) generates view and controller specs if RSpec is installed as a gem.
|
69
|
-
|
70
57
|
## Contributing
|
71
58
|
|
72
59
|
1. Fork it
|
@@ -22,7 +22,16 @@ module AuthorizedRailsScaffolds
|
|
22
22
|
|
23
23
|
end
|
24
24
|
|
25
|
+
require "authorized_rails_scaffolds/controller_macros"
|
26
|
+
require "authorized_rails_scaffolds/factory_macros"
|
27
|
+
require "authorized_rails_scaffolds/resource_macros"
|
28
|
+
require "authorized_rails_scaffolds/route_example_macros"
|
29
|
+
require "authorized_rails_scaffolds/test_var_macros"
|
25
30
|
require "authorized_rails_scaffolds/helper"
|
26
|
-
require "authorized_rails_scaffolds/
|
27
|
-
require "authorized_rails_scaffolds/
|
31
|
+
require "authorized_rails_scaffolds/rails_erb_scaffold_helper"
|
32
|
+
require "authorized_rails_scaffolds/rails_scaffold_controller_helper"
|
33
|
+
require "authorized_rails_scaffolds/rspec_scaffold_helper"
|
34
|
+
require "authorized_rails_scaffolds/rspec_scaffold_controller_helper"
|
35
|
+
require "authorized_rails_scaffolds/rspec_scaffold_routing_helper"
|
36
|
+
require "authorized_rails_scaffolds/rspec_scaffold_view_helper"
|
28
37
|
|
@@ -0,0 +1,22 @@
|
|
1
|
+
#
|
2
|
+
# requires modular_class_name to be defined
|
3
|
+
#
|
4
|
+
# also makes use of @controller_class_name value
|
5
|
+
#
|
6
|
+
module ControllerMacros
|
7
|
+
|
8
|
+
# The namespaced class the Controller inherits from (i.e. Example::ApplicationController)
|
9
|
+
def application_controller_class
|
10
|
+
@application_controller_class = 'ApplicationController'
|
11
|
+
if parent_modules.any?
|
12
|
+
@application_controller_class = "#{parent_modules.join('::')}::#{@application_controller_class}"
|
13
|
+
end
|
14
|
+
@application_controller_class
|
15
|
+
end
|
16
|
+
|
17
|
+
def controller_class_name
|
18
|
+
controller_class_prefix = @controller_class_name || modular_class_name.pluralize
|
19
|
+
"#{controller_class_prefix}Controller"
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module FactoryMacros
|
2
|
+
|
3
|
+
# Returns code that will generate attribute_value as an attribute_type
|
4
|
+
def factory_attribute_value(attribute_type, attribute_value)
|
5
|
+
case attribute_type
|
6
|
+
when :datetime
|
7
|
+
"DateTime.parse(#{attribute_value})"
|
8
|
+
when :time
|
9
|
+
value_as_time = attribute_value.to_time.strftime('%T')
|
10
|
+
"Time.parse(#{value_as_time.dump})"
|
11
|
+
when :date
|
12
|
+
value_as_date = attribute_value.to_time.strftime('%Y-%m-%d')
|
13
|
+
"Date.parse(#{value_as_date.dump})"
|
14
|
+
else
|
15
|
+
attribute_value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns the expected output string of attribute_value if it is an attribute_type
|
20
|
+
def factory_attribute_string(attribute_type, attribute_value)
|
21
|
+
case attribute_type
|
22
|
+
when :datetime
|
23
|
+
attribute_value_as_date = DateTime.parse(attribute_value)
|
24
|
+
I18n.l(attribute_value_as_date, :format => :long).dump
|
25
|
+
when :time
|
26
|
+
attribute_value_as_time = Time.parse(attribute_value)
|
27
|
+
I18n.l(attribute_value_as_time, :format => :short).dump
|
28
|
+
when :date
|
29
|
+
attribute_value_as_date = Date.parse(attribute_value)
|
30
|
+
I18n.l(attribute_value_as_date).dump
|
31
|
+
else
|
32
|
+
attribute_value
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def create_resource_from_factory
|
37
|
+
extra_factory_params = build_extra_factory_params
|
38
|
+
"FactoryGirl.create(#{resource_symbol}#{extra_factory_params})"
|
39
|
+
end
|
40
|
+
|
41
|
+
def create_parent_resource_from_factory(parent_table_name)
|
42
|
+
extra_factory_params = build_extra_factory_params(parent_table_name)
|
43
|
+
"FactoryGirl.create(:#{parent_table_name}#{extra_factory_params})"
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
|
48
|
+
def build_extra_factory_params(parent_table_name = nil)
|
49
|
+
if parent_table_name.nil?
|
50
|
+
attribute = parent_model_tables.last
|
51
|
+
else
|
52
|
+
parent_index = parent_model_tables.index(parent_table_name.to_s)
|
53
|
+
unless parent_index.nil? || parent_index == 0
|
54
|
+
attribute = parent_model_tables[parent_index - 1]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
if attribute.nil?
|
59
|
+
return ''
|
60
|
+
else
|
61
|
+
return ", :#{attribute} => #{references_test_property(attribute)}"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -1,38 +1,18 @@
|
|
1
1
|
module AuthorizedRailsScaffolds
|
2
|
-
|
3
2
|
class Helper
|
3
|
+
include ResourceMacros
|
4
4
|
|
5
5
|
def initialize(options = {})
|
6
|
-
@local_class_name = options[:local_class_name]
|
6
|
+
# @local_class_name = options[:local_class_name]
|
7
7
|
@var_name = options[:var_name] || options[:file_name] # Non-namespaced variable name
|
8
|
-
@plural_var_name = options[:plural_var_name] || @var_name.pluralize # Pluralized non-namespaced variable name
|
9
|
-
# Determine namespace prefix i.e awesome
|
10
|
-
@namespace_prefix = options[:namespace_prefix] || options[:singular_table_name][0..-(@var_name.length + 2)]
|
11
|
-
@controller_prefix = options[:controller_prefix] || options[:class_name].split('::')[0..-2].join('::')
|
12
|
-
|
13
|
-
# Determine Parent Prefix i.e. user_company
|
14
|
-
parent_prefix = AuthorizedRailsScaffolds.config.parent_models.collect{ |x| x.underscore }.join('_')
|
15
|
-
|
16
|
-
# Route Prefix i.e. awesome_user_company
|
17
|
-
route_prefix = [@namespace_prefix, parent_prefix].reject{ |x|x.blank? }.join('_')
|
18
|
-
@route_prefix = route_prefix.blank? ? '' : "#{route_prefix}_"
|
19
|
-
|
20
|
-
@parent_variables = AuthorizedRailsScaffolds.config.parent_models.collect{ |x| "@#{x.underscore}" }.join(', ')
|
21
8
|
|
22
|
-
#
|
23
|
-
@
|
24
|
-
@index_path_prefix = "#{@route_prefix}#{@plural_var_name}"
|
25
|
-
@single_path_prefix = "#{@route_prefix}#{var_name}"
|
26
|
-
end
|
9
|
+
# Pluralized non-namespaced variable name
|
10
|
+
@plural_var_name ||= options[:plural_var_name] || @var_name.pluralize
|
27
11
|
|
28
|
-
|
29
|
-
|
30
|
-
"#{@controller_prefix}::" unless @controller_prefix.blank?
|
31
|
-
end
|
12
|
+
# Determine namespace prefix i.e awesome
|
13
|
+
# options[:namespace_prefix]
|
32
14
|
|
33
|
-
|
34
|
-
def local_class_name
|
35
|
-
@local_class_name
|
15
|
+
@singular_table_name = options[:singular_table_name]
|
36
16
|
end
|
37
17
|
|
38
18
|
# Non-namespaced variable name (i.e. foo_bar)
|
@@ -40,113 +20,56 @@ module AuthorizedRailsScaffolds
|
|
40
20
|
@var_name
|
41
21
|
end
|
42
22
|
|
43
|
-
# Pluralized non-namespaced variable name (i.e. foo_bars)
|
44
23
|
def plural_var_name
|
45
24
|
@plural_var_name
|
46
25
|
end
|
47
26
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
extra_params += ", :#{model.underscore}_id => \"#{model_index + 2}\""
|
52
|
-
end
|
53
|
-
extra_params
|
27
|
+
# Route Prefix parts i.e. ['awesome', 'user', 'company']
|
28
|
+
def route_prefix_values
|
29
|
+
@route_prefix_values ||= [parent_module_groups + parent_model_tables]
|
54
30
|
end
|
55
31
|
|
56
|
-
def
|
57
|
-
|
58
|
-
namespace_prefix = ":#{@namespace_prefix}" unless @namespace_prefix.blank?
|
59
|
-
array = [namespace_prefix, @parent_variables, variable].reject{ |x| x.blank? || x == '_' }.join(', ')
|
60
|
-
"[#{array}]"
|
32
|
+
def collection_route_prefix
|
33
|
+
@collection_route_prefix ||= [route_prefix_values + [resource_table_name.pluralize]].join('_')
|
61
34
|
end
|
62
35
|
|
63
|
-
def
|
64
|
-
|
65
|
-
if variable.blank?
|
66
|
-
"#{@single_path_prefix}_path(#{@parent_variables})"
|
67
|
-
else
|
68
|
-
"#{@single_path_prefix}_path(#{@route_params_prefix}#{variable})"
|
69
|
-
end
|
36
|
+
def member_route_prefix
|
37
|
+
@member_route_prefix ||= [route_prefix_values + [resource_table_name]].join('_')
|
70
38
|
end
|
71
39
|
|
72
|
-
def
|
73
|
-
|
74
|
-
"#{@index_path_prefix}_path"
|
75
|
-
else
|
76
|
-
"#{@index_path_prefix}_path(#{@parent_variables})"
|
77
|
-
end
|
40
|
+
def parent_variables
|
41
|
+
@parent_variables ||= parent_model_tables.collect{ |parent_table| parent_variable(parent_table) }
|
78
42
|
end
|
79
43
|
|
80
|
-
def
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
# call arguments
|
89
|
-
def index_action_params_prefix
|
90
|
-
if AuthorizedRailsScaffolds.config.parent_models.any?
|
91
|
-
AuthorizedRailsScaffolds.config.parent_models.collect{|x| ":#{x.underscore}_id => @#{x.underscore}.to_param"}.join(', ')
|
92
|
-
else
|
93
|
-
''
|
94
|
-
end
|
44
|
+
def controller_show_route(variable = nil)
|
45
|
+
variables = [] + parent_variables
|
46
|
+
variables += [variable] unless variable.nil?
|
47
|
+
controller_routes = "#{member_route_prefix}_path"
|
48
|
+
controller_routes += "(#{variables.join(', ')})" if variables.any?
|
49
|
+
controller_routes
|
95
50
|
end
|
96
51
|
|
97
|
-
def
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
references_path = "#{attribute_name}_path(#{variable})"
|
103
|
-
unless @namespace_prefix.blank?
|
104
|
-
references_path = "#{@namespace_prefix}_#{references_path}"
|
105
|
-
end
|
106
|
-
end
|
107
|
-
references_path
|
52
|
+
def controller_index_path
|
53
|
+
variables = parent_variables
|
54
|
+
controller_index_path = "#{collection_route_prefix}_path"
|
55
|
+
controller_index_path += "(#{variables.join(', ')})" if variables.any?
|
56
|
+
controller_index_path
|
108
57
|
end
|
109
58
|
|
110
|
-
def
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
"#{index_action_params}, "
|
116
|
-
end
|
59
|
+
def controller_index_route
|
60
|
+
variables = parent_variables
|
61
|
+
controller_index_route = "#{collection_route_prefix}_url"
|
62
|
+
controller_index_route += "(#{variables.join(', ')})" if variables.any?
|
63
|
+
controller_index_route
|
117
64
|
end
|
118
65
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
when :datetime
|
123
|
-
"DateTime.parse(#{attribute_value})"
|
124
|
-
when :time
|
125
|
-
value_as_time = attribute_value.to_time.strftime('%T')
|
126
|
-
"Time.parse(#{value_as_time.dump})"
|
127
|
-
when :date
|
128
|
-
value_as_date = attribute_value.to_time.strftime('%Y-%m-%d')
|
129
|
-
"Date.parse(#{value_as_date.dump})"
|
130
|
-
else
|
131
|
-
attribute_value
|
132
|
-
end
|
133
|
-
end
|
66
|
+
def references_show_route(attribute_name, variable = nil)
|
67
|
+
variable ||= "#{resource_var}.#{attribute_name}"
|
68
|
+
path_variables = [parent_module_groups + [attribute_name]]
|
134
69
|
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
when :datetime
|
139
|
-
attribute_value_as_date = DateTime.parse(attribute_value)
|
140
|
-
I18n.l(attribute_value_as_date, :format => :long).dump
|
141
|
-
when :time
|
142
|
-
attribute_value_as_time = Time.parse(attribute_value)
|
143
|
-
I18n.l(attribute_value_as_time, :format => :short).dump
|
144
|
-
when :date
|
145
|
-
attribute_value_as_date = Date.parse(attribute_value)
|
146
|
-
I18n.l(attribute_value_as_date).dump
|
147
|
-
else
|
148
|
-
attribute_value
|
149
|
-
end
|
70
|
+
references_show_route = path_variables.join('_')
|
71
|
+
references_show_route += "_path(#{variable})"
|
72
|
+
references_show_route
|
150
73
|
end
|
151
74
|
|
152
75
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class AuthorizedRailsScaffolds::RailsErbScaffoldHelper < 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
|
+
# 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_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
|
+
# Class name with parent modules included (i.e. Example::FooBar)
|
35
|
+
# Name of class assumed by default generators, used as a base for determining modules and class
|
36
|
+
def modular_class_name
|
37
|
+
@modular_class_name
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class AuthorizedRailsScaffolds::RailsScaffoldControllerHelper < AuthorizedRailsScaffolds::Helper
|
2
|
+
include ControllerMacros
|
3
|
+
include RouteExampleMacros
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
super options
|
7
|
+
|
8
|
+
# Modularized class name generated by spec generator
|
9
|
+
@controller_class_name = options[:controller_class_name]
|
10
|
+
|
11
|
+
@modular_class_name = options[:class_name] || options[:local_class_name]
|
12
|
+
end
|
13
|
+
|
14
|
+
# Class name with parent modules included (i.e. Example::FooBar)
|
15
|
+
# Name of class assumed by default generators, used as a base for determining modules and class
|
16
|
+
def modular_class_name
|
17
|
+
@modular_class_name
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
#
|
2
|
+
# requires modular_class_name to be defined
|
3
|
+
#
|
4
|
+
module ResourceMacros
|
5
|
+
|
6
|
+
### Base Resource ###
|
7
|
+
|
8
|
+
# Class name of the resource being tested (i.e. 'FooBar')
|
9
|
+
def resource_class
|
10
|
+
# @local_class_name
|
11
|
+
@resource_class ||= modular_class_name.split('::')[-1]
|
12
|
+
end
|
13
|
+
|
14
|
+
# Table name of the Resource being tested (i.e. foo_bar)
|
15
|
+
def resource_table_name
|
16
|
+
@resource_table_name ||= resource_class.underscore
|
17
|
+
end
|
18
|
+
|
19
|
+
# Symbol used to represent resource (i.e. :foo_bar)
|
20
|
+
def resource_symbol
|
21
|
+
":#{resource_table_name}"
|
22
|
+
end
|
23
|
+
|
24
|
+
# Name for plural of a resource
|
25
|
+
def resource_plural_name
|
26
|
+
@resource_plural_name ||= plural_var_name || (var_name || resource_table_name).pluralize
|
27
|
+
end
|
28
|
+
|
29
|
+
def resource_plural_sym
|
30
|
+
":#{resource_plural_name}"
|
31
|
+
end
|
32
|
+
|
33
|
+
# Variable resource is assigned to in a singular context (i.e. @foo_bar)
|
34
|
+
def resource_var
|
35
|
+
@resource_var_name ||= "@#{var_name || resource_table_name}"
|
36
|
+
end
|
37
|
+
|
38
|
+
# Variable resource is assigned to in a plural context (i.e. @foo_bars)
|
39
|
+
def resources_var
|
40
|
+
@resource_var_name ||= "@#{resource_plural_name}"
|
41
|
+
end
|
42
|
+
|
43
|
+
# Directory of the current resource: i.e. awesome/foo_bars
|
44
|
+
def resource_directory
|
45
|
+
@resource_directory = [parent_module_groups + [resource_table_name.pluralize]].join("/")
|
46
|
+
end
|
47
|
+
|
48
|
+
### Parent Models ###
|
49
|
+
|
50
|
+
# An array of parent model classes (i.e. ['User', 'Category])
|
51
|
+
def parent_models
|
52
|
+
@parent_models ||= AuthorizedRailsScaffolds.config.parent_models
|
53
|
+
end
|
54
|
+
|
55
|
+
# Table names of parent models (i.e. ['user', 'category'])
|
56
|
+
def parent_model_tables
|
57
|
+
@parent_model_tables ||= parent_models.map { |model| model.underscore }
|
58
|
+
end
|
59
|
+
|
60
|
+
# Variable name used to save a parent_table_name
|
61
|
+
def parent_variable(parent_table_name)
|
62
|
+
"@#{parent_table_name}"
|
63
|
+
end
|
64
|
+
|
65
|
+
def parent_sym(parent_table_name)
|
66
|
+
":#{parent_table_name}"
|
67
|
+
end
|
68
|
+
|
69
|
+
### Parent Modules ###
|
70
|
+
|
71
|
+
# The parent modules of a controller (i.e. ['Api', 'V1'])
|
72
|
+
def parent_modules
|
73
|
+
@parent_modules ||= modular_class_name.split('::')[0..-2]
|
74
|
+
end
|
75
|
+
|
76
|
+
# Array of symbols used for links to parents (i.e. ['api', 'v1'])
|
77
|
+
def parent_module_groups
|
78
|
+
@parent_module_groups ||= parent_modules.map { |parent_module| parent_module.underscore }
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|