authorized_rails_scaffolds 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (22) hide show
  1. data/Rakefile +6 -0
  2. data/authorized_rails_scaffolds.gemspec +2 -0
  3. data/lib/authorized_rails_scaffolds.rb +22 -7
  4. data/lib/authorized_rails_scaffolds/helper.rb +13 -5
  5. data/lib/authorized_rails_scaffolds/rspec_scaffold_generator_helper.rb +82 -0
  6. data/lib/authorized_rails_scaffolds/{view_spec_helper.rb → rspec_scaffold_generator_view_helper.rb} +1 -3
  7. data/lib/authorized_rails_scaffolds/version.rb +1 -1
  8. data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/_form.html.erb +1 -1
  9. data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/controller.rb +3 -3
  10. data/lib/generators/authorized_rails_scaffolds/install_templates/templates/scaffold/index.html.erb +19 -9
  11. data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/controller_spec.rb +39 -36
  12. data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/edit_spec.rb +20 -11
  13. data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/index_spec.rb +53 -15
  14. data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/new_spec.rb +40 -30
  15. data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/routing_spec.rb +16 -11
  16. data/lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/show_spec.rb +22 -13
  17. data/spec/lib/authorized_rails_scaffolds/rspec_scaffold_generator_helper_spec.rb +147 -0
  18. data/spec/spec_helper.rb +18 -0
  19. data/spec/support/rspec_scaffold_generator_helper_macros.rb +15 -0
  20. metadata +39 -12
  21. data/lib/authorized_rails_scaffolds/controller_spec_helper.rb +0 -31
  22. data/lib/authorized_rails_scaffolds/routing_spec_helper.rb +0 -29
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ # If you want to make this the default task
7
+ task :default => :spec
@@ -19,6 +19,8 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency 'railties', '>= 3.1'
22
+ spec.add_development_dependency "rails", '>= 3.1'
22
23
  spec.add_development_dependency "bundler", "~> 1.3"
23
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency 'rspec'
24
26
  end
@@ -1,13 +1,28 @@
1
1
  require "authorized_rails_scaffolds/version"
2
2
 
3
3
  module AuthorizedRailsScaffolds
4
-
5
- mattr_accessor :parent_models
6
- @@parent_models = []
7
-
4
+ class Configuration
5
+ attr_accessor :parent_models
6
+
7
+ def initialize
8
+ self.parent_models = []
9
+ end
10
+ end
11
+
12
+ def self.config
13
+ @config ||= Configuration.new
14
+ end
15
+
16
+ def self.configure
17
+ yield(config) if block_given?
18
+ end
19
+
20
+ # mattr_accessor :parent_models
21
+ # @@parent_models = []
22
+
8
23
  end
9
24
 
10
25
  require "authorized_rails_scaffolds/helper"
11
- require "authorized_rails_scaffolds/controller_spec_helper"
12
- require "authorized_rails_scaffolds/routing_spec_helper"
13
- require "authorized_rails_scaffolds/view_spec_helper"
26
+ require "authorized_rails_scaffolds/rspec_scaffold_generator_helper"
27
+ require "authorized_rails_scaffolds/rspec_scaffold_generator_view_helper"
28
+
@@ -11,13 +11,13 @@ module AuthorizedRailsScaffolds
11
11
  @controller_prefix = options[:controller_prefix] || options[:class_name].split('::')[0..-2].join('::')
12
12
 
13
13
  # Determine Parent Prefix i.e. user_company
14
- parent_prefix = AuthorizedRailsScaffolds.parent_models.collect{ |x| x.underscore }.join('_')
14
+ parent_prefix = AuthorizedRailsScaffolds.config.parent_models.collect{ |x| x.underscore }.join('_')
15
15
 
16
16
  # Route Prefix i.e. awesome_user_company
17
17
  route_prefix = [@namespace_prefix, parent_prefix].reject{ |x|x.blank? }.join('_')
18
18
  @route_prefix = route_prefix.blank? ? '' : "#{route_prefix}_"
19
19
 
20
- @parent_variables = AuthorizedRailsScaffolds.parent_models.collect{ |x| "@#{x.underscore}" }.join(', ')
20
+ @parent_variables = AuthorizedRailsScaffolds.config.parent_models.collect{ |x| "@#{x.underscore}" }.join(', ')
21
21
 
22
22
  # Route Helpers
23
23
  @route_params_prefix = @parent_variables.blank? ? "" : "#{@parent_variables}, "
@@ -45,6 +45,14 @@ module AuthorizedRailsScaffolds
45
45
  @plural_var_name
46
46
  end
47
47
 
48
+ def extra_params
49
+ extra_params = ''
50
+ AuthorizedRailsScaffolds.config.parent_models.each_with_index do |model, model_index|
51
+ extra_params += ", :#{model.underscore}_id => \"#{model_index + 2}\""
52
+ end
53
+ extra_params
54
+ end
55
+
48
56
  def form_object_array(variable = nil)
49
57
  variable ||= "@#{var_name}"
50
58
  namespace_prefix = ":#{@namespace_prefix}" unless @namespace_prefix.blank?
@@ -79,8 +87,8 @@ module AuthorizedRailsScaffolds
79
87
 
80
88
  # call arguments
81
89
  def index_action_params_prefix
82
- if AuthorizedRailsScaffolds.parent_models.any?
83
- AuthorizedRailsScaffolds.parent_models.collect{|x| ":#{x.underscore}_id => @#{x.underscore}.to_param"}.join(', ')
90
+ if AuthorizedRailsScaffolds.config.parent_models.any?
91
+ AuthorizedRailsScaffolds.config.parent_models.collect{|x| ":#{x.underscore}_id => @#{x.underscore}.to_param"}.join(', ')
84
92
  else
85
93
  ''
86
94
  end
@@ -88,7 +96,7 @@ module AuthorizedRailsScaffolds
88
96
 
89
97
  def references_show_route(attribute_name, variable = nil)
90
98
  variable ||= "@#{@var_name}.#{attribute_name}"
91
- if AuthorizedRailsScaffolds.parent_models.any? && AuthorizedRailsScaffolds.parent_models.last.underscore == attribute_name
99
+ if AuthorizedRailsScaffolds.config.parent_models.any? && AuthorizedRailsScaffolds.config.parent_models.last.underscore == attribute_name
92
100
  references_path = "#{@route_prefix}path(#{variable})"
93
101
  else
94
102
  references_path = "#{attribute_name}_path(#{variable})"
@@ -0,0 +1,82 @@
1
+ class AuthorizedRailsScaffolds::RSpecScaffoldGeneratorHelper < AuthorizedRailsScaffolds::Helper
2
+
3
+ def initialize(options = {})
4
+ super options
5
+
6
+ parent_models = AuthorizedRailsScaffolds.config.parent_models
7
+
8
+ class_name_parts = (options[:class_name] || options[:local_class_name]).split("::")
9
+
10
+ example_parent_values = {}
11
+ parent_models.each_with_index do |parent_model, index|
12
+ example_parent_values[parent_model] = index + 2
13
+ end
14
+
15
+ namespace_prefix_modules = class_name_parts[0..-2] # ['Example', 'V1']
16
+ local_class_name = class_name_parts[-1]
17
+
18
+ parent_model_and_value_parts = []
19
+ parent_model_param_parts = []
20
+ parent_models.each do |parent_model|
21
+ parent_value = example_parent_values[parent_model]
22
+
23
+ # User, 2
24
+ parent_model_and_value_parts << parent_model.pluralize
25
+ parent_model_and_value_parts << parent_value
26
+
27
+ parent_model_param_parts << "#{parent_model.underscore}_id => #{parent_value}"
28
+ end
29
+
30
+ # Directory for the generated controller: i.e. awesome/foo_bars
31
+ @controller_directory = [namespace_prefix_modules + [local_class_name.pluralize]].join("/").underscore
32
+
33
+ # Example index route: i.e. /awesome/users/2/foo_bars
34
+ @example_controller_path = [namespace_prefix_modules + parent_model_and_value_parts + [local_class_name.pluralize]].join("/").underscore
35
+
36
+ @example_controller_path_extra_params = parent_model_param_parts.any? ? ", :#{parent_model_param_parts.join(', :')}" : ''
37
+
38
+ @attributes = options[:attributes]
39
+ end
40
+
41
+ def create_factory_model
42
+ extra_params = extra_model_params
43
+ "FactoryGirl.create(:#{var_name}#{extra_params})"
44
+ end
45
+
46
+ def create_parent_model(model_name)
47
+ extra_params = extra_model_params(model_name)
48
+ "FactoryGirl.create(:#{model_name}#{extra_params})"
49
+ end
50
+
51
+ # Directory for the generated controller: i.e. awesome/foo_bars
52
+ def controller_directory
53
+ @controller_directory
54
+ end
55
+
56
+ # Example index route: i.e. /awesome/users/2/foo_bars
57
+ def example_controller_path
58
+ "/#{@example_controller_path}"
59
+ end
60
+
61
+ # Extra params for an example controller path: i.e. ', :user_id => 2'
62
+ def example_controller_path_extra_params
63
+ @example_controller_path_extra_params
64
+ end
65
+
66
+ def parent_model_tables
67
+ @parent_model_tables ||= AuthorizedRailsScaffolds.config.parent_models.map { |model| model.underscore }
68
+ end
69
+
70
+ protected
71
+
72
+ def extra_model_params(model_name = nil)
73
+ argument_params = []
74
+ AuthorizedRailsScaffolds.config.parent_models.each do |parent_model|
75
+ attribute = parent_model.underscore
76
+ break if model_name == attribute
77
+ argument_params << ", :#{attribute} => @#{attribute}"
78
+ end
79
+ argument_params.join('')
80
+ end
81
+
82
+ end
@@ -1,9 +1,7 @@
1
- class AuthorizedRailsScaffolds::ViewSpecHelper < AuthorizedRailsScaffolds::Helper
1
+ class AuthorizedRailsScaffolds::RSpecScaffoldGeneratorViewHelper < AuthorizedRailsScaffolds::RSpecScaffoldGeneratorHelper
2
2
 
3
3
  def initialize(options = {})
4
4
  super options
5
-
6
- @attributes = options[:attributes]
7
5
  end
8
6
 
9
7
  def output_attributes
@@ -1,3 +1,3 @@
1
1
  module AuthorizedRailsScaffolds
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.10"
3
3
  end
@@ -23,7 +23,7 @@ plural_var_name = t_helper.plural_var_name # Pluralized non-namespaced variable
23
23
  <div class="form-actions">
24
24
  <%%= f.button :submit, :class => 'btn-primary' %>
25
25
  <%%= link_to t('.cancel', :default => t("helpers.links.cancel")),
26
- <%= t_helper.controller_index_path %>,
26
+ @<%= var_name %>.persisted? ? <%= t_helper.controller_show_route "@#{var_name}" %> : <%= t_helper.controller_index_path %>,
27
27
  :class => 'btn'
28
28
  %>
29
29
  </div>
@@ -20,10 +20,10 @@ orm_instance = Rails::Generators::ActiveModel.new var_name
20
20
  -%>
21
21
  <% module_namespacing do -%>
22
22
  class <%= controller_class_name %>Controller < <%= t_helper.ns_controller_prefix %>ApplicationController
23
- <%- AuthorizedRailsScaffolds.parent_models.each_with_index do |model, model_index| -%>
24
- load_and_authorize_resource :<%= model.underscore %><% if model_index > 0 %>, :through => :<%= AuthorizedRailsScaffolds.parent_models[model_index - 1].underscore %><% end %>
23
+ <%- AuthorizedRailsScaffolds.config.parent_models.each_with_index do |model, model_index| -%>
24
+ load_and_authorize_resource :<%= model.underscore %><% if model_index > 0 %>, :through => :<%= AuthorizedRailsScaffolds.config.parent_models[model_index - 1].underscore %><% end %>
25
25
  <%- end -%>
26
- load_and_authorize_resource :<%= var_name%><% if AuthorizedRailsScaffolds.parent_models.any? %>, :through => :<%= AuthorizedRailsScaffolds.parent_models.last.underscore %><% end %>
26
+ load_and_authorize_resource :<%= var_name%><% if AuthorizedRailsScaffolds.config.parent_models.any? %>, :through => :<%= AuthorizedRailsScaffolds.config.parent_models.last.underscore %><% end %>
27
27
 
28
28
  # GET <%= route_url %>
29
29
  # GET <%= route_url %>.json
@@ -19,9 +19,15 @@ plural_var_name = t_helper.plural_var_name # Pluralized non-namespaced variable
19
19
  <thead>
20
20
  <tr>
21
21
  <th>&nbsp;</th>
22
- <%- attributes.each do |attribute| -%>
22
+ <%- attributes.each do |attribute| -%>
23
+ <%- if attribute.type == :references -%>
24
+ <%% unless @<%= attribute.name %> %>
25
+ <th><%%= model_class.human_attribute_name(:<%= attribute.name %>) %></th>
26
+ <%% end %>
27
+ <%- else -%>
23
28
  <th><%%= model_class.human_attribute_name(:<%= attribute.name %>) %></th>
24
- <%- end -%>
29
+ <%- end -%>
30
+ <%- end -%>
25
31
  <th><%%= model_class.human_attribute_name(:created_at) %></th>
26
32
  <th><%%=t '.actions', :default => t("helpers.actions") %></th>
27
33
  </tr>
@@ -30,16 +36,20 @@ plural_var_name = t_helper.plural_var_name # Pluralized non-namespaced variable
30
36
  <%% @<%= plural_var_name %>.each do |<%= var_name %>| %>
31
37
  <tr class="<%= var_name %>_<%%= <%= var_name %>.id %>">
32
38
  <td class="id-column"><%%= <%= var_name %>.id %></td>
33
- <%- if attributes.any? -%>
39
+ <%- if attributes.any? -%>
34
40
  <td class="title-column"><%%= link_to <%= var_name %>.<%= attributes[0].name %>, <%= t_helper.controller_show_route(var_name) %> %></td>
35
- <%- attributes[1..-1].each do |attribute| -%>
36
- <%- if [:datetime, :timestamp, :time, :date].include? attribute.type -%>
41
+ <%- attributes[1..-1].each do |attribute| -%>
42
+ <%- if attribute.type == :references -%>
43
+ <%% unless @<%= attribute.name %> %>
44
+ <td><%%= <%= var_name %>.<%= attribute.name %> %></td>
45
+ <%% end %>
46
+ <%- elsif [:datetime, :timestamp, :time, :date].include? attribute.type -%>
37
47
  <td><%% unless <%= var_name %>.<%= attribute.name %>.nil? %><%%=l <%= var_name %>.<%= attribute.name %><% if attribute.type == :datetime %>, :format => :long<% end %><% if attribute.type == :time %>, :format => :short<% end %> %><%% end %></td>
38
- <%- else -%>
48
+ <%- else -%>
39
49
  <td><%%= <%= var_name %>.<%= attribute.name %> %></td>
40
- <%- end -%>
41
- <%- end -%>
42
- <%- end -%>
50
+ <%- end -%>
51
+ <%- end -%>
52
+ <%- end -%>
43
53
  <td><%%=l <%= var_name %>.created_at, :format => :long %></td>
44
54
  <td class="table-actions">
45
55
  <%%= link_to t('.edit', :default => t("helpers.links.edit")),
@@ -21,7 +21,7 @@ require 'spec_helper'
21
21
  <% module_namespacing do -%>
22
22
  <%-
23
23
 
24
- t_helper = AuthorizedRailsScaffolds::ControllerSpecHelper.new(
24
+ t_helper = AuthorizedRailsScaffolds::RSpecScaffoldGeneratorHelper.new(
25
25
  :class_name => class_name,
26
26
  :singular_table_name => singular_table_name,
27
27
  :file_name => file_name,
@@ -32,6 +32,8 @@ local_class_name = t_helper.local_class_name # Non-Namespaced class name
32
32
  var_name = t_helper.var_name # Non-namespaced variable name
33
33
  plural_var_name = t_helper.plural_var_name # Pluralized non-namespaced variable name
34
34
 
35
+ parent_model_tables = t_helper.parent_model_tables
36
+
35
37
  -%>
36
38
  describe <%= controller_class_name %>Controller do
37
39
 
@@ -47,22 +49,22 @@ describe <%= controller_class_name %>Controller do
47
49
  FactoryGirl.attributes_for(:<%= var_name %>)
48
50
  end
49
51
 
50
- <%- if AuthorizedRailsScaffolds.parent_models.any? -%>
52
+ <%- if parent_model_tables.any? -%>
51
53
  before(:each) do
52
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
53
- @<%= model.underscore %> = <%= t_helper.create_parent_model model.underscore %>
54
+ <%- parent_model_tables.each do |parent_model| -%>
55
+ @<%= parent_model %> = <%= t_helper.create_parent_model parent_model %>
54
56
  <%- end -%>
55
57
  end
56
58
 
57
59
  <%- end -%>
58
60
  <% unless options[:singleton] -%>
59
61
  describe "GET index" do
60
- context do # Within default nesting
61
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
62
- grant_ability :read, <%= model.classify %>
62
+ context <% if parent_model_tables.any? %>"within <%= parent_model_tables.join('/') %> nesting"<% end %> do<%- unless parent_model_tables.any? -%> # Within default nesting<% end %>
63
+ <%- parent_model_tables.each do |parent_model| -%>
64
+ grant_ability :read, <%= parent_model.classify %>
63
65
  <%- end -%>
64
66
 
65
- context 'without a user' do
67
+ context 'without a user session' do
66
68
  describe 'with valid request' do
67
69
  before(:each) do
68
70
  @<%= var_name %> = <%= t_helper.create_factory_model %>
@@ -103,16 +105,12 @@ describe <%= controller_class_name %>Controller do
103
105
 
104
106
  <% end -%>
105
107
  describe "GET show" do
106
- context do # Within default nesting
107
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
108
- grant_ability :read, <%= model.classify %>
108
+ context <% if parent_model_tables.any? %>"within <%= parent_model_tables.join('/') %> nesting"<% end %> do<%- unless parent_model_tables.any? -%> # Within default nesting<% end %>
109
+ <%- parent_model_tables.each do |parent_model| -%>
110
+ grant_ability :read, <%= parent_model.classify %>
109
111
  <%- end -%>
110
112
 
111
- context 'without a user' do
112
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
113
- grant_ability :read, <%= model.classify %>
114
- <%- end -%>
115
-
113
+ context 'without a user session' do
116
114
  describe 'with valid request' do
117
115
  before(:each) do
118
116
  @<%= var_name %> = <%= t_helper.create_factory_model %>
@@ -152,12 +150,12 @@ describe <%= controller_class_name %>Controller do
152
150
  end
153
151
 
154
152
  describe "GET new" do
155
- context do # Within default nesting
156
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
157
- grant_ability :read, <%= model.classify %>
153
+ context <% if parent_model_tables.any? %>"within <%= parent_model_tables.join('/') %> nesting"<% end %> do<%- unless parent_model_tables.any? -%> # Within default nesting<% end %>
154
+ <%- parent_model_tables.each do |parent_model| -%>
155
+ grant_ability :read, <%= parent_model.classify %>
158
156
  <%- end -%>
159
157
 
160
- context 'without a user' do
158
+ context 'without a user session' do
161
159
  describe 'with valid request' do
162
160
  before(:each) do
163
161
  get :new, {<%= t_helper.index_action_params_prefix %>}
@@ -194,12 +192,12 @@ describe <%= controller_class_name %>Controller do
194
192
  end
195
193
 
196
194
  describe "GET edit" do
197
- context do # Within default nesting
198
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
199
- grant_ability :read, <%= model.classify %>
195
+ context <% if parent_model_tables.any? %>"within <%= parent_model_tables.join('/') %> nesting"<% end %> do<%- unless parent_model_tables.any? -%> # Within default nesting<% end %>
196
+ <%- parent_model_tables.each do |parent_model| -%>
197
+ grant_ability :read, <%= parent_model.classify %>
200
198
  <%- end -%>
201
199
 
202
- context 'without a user' do
200
+ context 'without a user session' do
203
201
  describe 'with valid request' do
204
202
  before(:each) do
205
203
  @<%= var_name %> = <%= t_helper.create_factory_model %>
@@ -239,12 +237,12 @@ describe <%= controller_class_name %>Controller do
239
237
  end
240
238
 
241
239
  describe "POST create" do
242
- context do # Within default nesting
243
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
244
- grant_ability :read, <%= model.classify %>
240
+ context <% if parent_model_tables.any? %>"within <%= parent_model_tables.join('/') %> nesting"<% end %> do<%- unless parent_model_tables.any? -%> # Within default nesting<% end %>
241
+ <%- parent_model_tables.each do |parent_model| -%>
242
+ grant_ability :read, <%= parent_model.classify %>
245
243
  <%- end -%>
246
244
 
247
- context 'without a user' do
245
+ context 'without a user session' do
248
246
  describe 'with valid params' do
249
247
  before(:each) do
250
248
  post :create, {<%= t_helper.action_params_prefix %>:<%= var_name %> => valid_create_attributes}
@@ -280,6 +278,11 @@ describe <%= controller_class_name %>Controller do
280
278
  assigns(:<%= var_name %>).should be_a(<%= local_class_name %>)
281
279
  assigns(:<%= var_name %>).should be_persisted
282
280
  end
281
+ <% if parent_model_tables.any? -%>
282
+ it "assigns the parent <%= parent_model_tables[-1].classify %> to <%= var_name %>" do
283
+ assigns(:<%= var_name %>).<%= parent_model_tables[-1] %>.should eq(@<%= parent_model_tables[-1] %>)
284
+ end
285
+ <% end -%>
283
286
  it "redirects to the created <%= var_name %>" do
284
287
  response.should redirect_to(<%= t_helper.controller_show_route "#{local_class_name}.last" %>)
285
288
  end
@@ -301,12 +304,12 @@ describe <%= controller_class_name %>Controller do
301
304
  end
302
305
 
303
306
  describe "PUT update" do
304
- context do # Within default nesting
305
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
306
- grant_ability :read, <%= model.classify %>
307
+ context <% if parent_model_tables.any? %>"within <%= parent_model_tables.join('/') %> nesting"<% end %> do<%- unless parent_model_tables.any? -%> # Within default nesting<% end %>
308
+ <%- parent_model_tables.each do |parent_model| -%>
309
+ grant_ability :read, <%= parent_model.classify %>
307
310
  <%- end -%>
308
311
 
309
- context 'without a user' do
312
+ context 'without a user session' do
310
313
  describe 'with valid params' do
311
314
  before(:each) do
312
315
  @<%= var_name %> = <%= t_helper.create_factory_model %>
@@ -374,12 +377,12 @@ describe <%= controller_class_name %>Controller do
374
377
  end
375
378
 
376
379
  describe "DELETE destroy" do
377
- context do # Within default nesting
378
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
379
- grant_ability :read, <%= model.classify %>
380
+ context <% if parent_model_tables.any? %>"within <%= parent_model_tables.join('/') %> nesting"<% end %> do<%- unless parent_model_tables.any? -%> # Within default nesting<% end %>
381
+ <%- parent_model_tables.each do |parent_model| -%>
382
+ grant_ability :read, <%= parent_model.classify %>
380
383
  <%- end -%>
381
384
 
382
- context 'without a user' do
385
+ context 'without a user session' do
383
386
  describe 'with valid request' do
384
387
  before(:each) do
385
388
  @<%= var_name %> = <%= t_helper.create_factory_model %>
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  <%-
4
4
 
5
- t_helper = AuthorizedRailsScaffolds::ViewSpecHelper.new(
5
+ t_helper = AuthorizedRailsScaffolds::RSpecScaffoldGeneratorViewHelper.new(
6
6
  :class_name => class_name,
7
7
  :singular_table_name => singular_table_name,
8
8
  :file_name => file_name,
@@ -12,29 +12,38 @@ t_helper = AuthorizedRailsScaffolds::ViewSpecHelper.new(
12
12
  local_class_name = t_helper.local_class_name # Non-Namespaced class name
13
13
  var_name = t_helper.var_name # Non-namespaced variable name
14
14
 
15
+ controller_directory = t_helper.controller_directory
16
+ parent_model_tables = t_helper.parent_model_tables
17
+
15
18
  output_attributes = t_helper.output_attributes
16
19
  standard_attributes = t_helper.standard_attributes
17
20
  datetime_attributes = t_helper.datetime_attributes
18
21
 
19
22
  -%>
20
- describe "<%= ns_table_name %>/edit" do
23
+ describe "<%= controller_directory %>/edit" do
21
24
 
22
- before(:each) do
23
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
24
- @<%= model.underscore %> = assign(:<%= model.underscore %>, FactoryGirl.build_stubbed(:<%= model.underscore %>))
25
+ <% parent_model_tables.each_with_index do |parent_model, index| -%>
26
+ <%- if index == 0 -%>
27
+ let(:<%= parent_model %>) { FactoryGirl.build_stubbed(:<%= parent_model %>) }
28
+ <%- else -%>
29
+ let(:<%= parent_model %>) { FactoryGirl.build_stubbed(:<%= parent_model %>, :<%= parent_model_tables[index - 1] %> => <%= parent_model_tables[index - 1] %>) }
30
+ <%- end -%>
25
31
  <%- end -%>
26
- @<%= var_name %> = assign(:<%= var_name %>, FactoryGirl.build_stubbed(:<%= var_name %><%= output_attributes.empty? ? '))' : ',' %>
32
+ let(:<%= var_name %>) do
33
+ FactoryGirl.build_stubbed(:<%= var_name %><%= output_attributes.empty? ? ')' : ',' %>
27
34
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
28
- :<%= attribute.name %> => <%= t_helper.factory_attribute_value attribute.type, value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
35
+ :<%= attribute.name %> => <% if attribute.type == :references && parent_model_tables.include?(attribute.name) %><%= attribute.name %><% else %><%= t_helper.factory_attribute_value attribute.type, value_for(attribute) %><% end %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
29
36
  <% end -%>
30
- <%= output_attributes.empty? ? "" : " ))\n" -%>
37
+ <%= output_attributes.empty? ? "" : " )\n" -%>
31
38
  end
32
39
 
33
- context do # Within default nesting
40
+ context <% if parent_model_tables.any? %>"within <%= parent_model_tables.join('/') %> nesting"<% end %> do<%- unless parent_model_tables.any? -%> # Within default nesting<% end %>
34
41
  before(:each) do
35
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
36
- assign(:<%= model.underscore %>, @<%= model.underscore %>)
42
+ # Add Properties for view scope
43
+ <%- parent_model_tables.each do |parent_model| -%>
44
+ assign(:<%= parent_model %>, @<%= parent_model %> = <%= parent_model %>)
37
45
  <%- end -%>
46
+ assign(:<%= var_name %>, @<%= var_name %> = <%= var_name %>)
38
47
  end
39
48
 
40
49
  it "renders the edit <%= var_name %> form" do
@@ -2,13 +2,16 @@ require 'spec_helper'
2
2
 
3
3
  <%-
4
4
 
5
- t_helper = AuthorizedRailsScaffolds::ViewSpecHelper.new(
5
+ t_helper = AuthorizedRailsScaffolds::RSpecScaffoldGeneratorViewHelper.new(
6
6
  :class_name => class_name,
7
7
  :singular_table_name => singular_table_name,
8
8
  :file_name => file_name,
9
9
  :attributes => attributes
10
10
  )
11
11
 
12
+ controller_directory = t_helper.controller_directory
13
+ parent_model_tables = t_helper.parent_model_tables
14
+
12
15
  local_class_name = t_helper.local_class_name # Non-Namespaced class name
13
16
  var_name = t_helper.var_name # Non-namespaced variable name
14
17
  plural_var_name = t_helper.plural_var_name # Pluralized non-namespaced variable name
@@ -16,30 +19,40 @@ plural_var_name = t_helper.plural_var_name # Pluralized non-namespaced variable
16
19
  output_attributes = t_helper.output_attributes
17
20
 
18
21
  -%>
19
- describe "<%= ns_table_name %>/index" do
22
+ describe "<%= controller_directory %>/index" do
20
23
 
21
- before(:each) do
22
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
23
- @<%= model.underscore %> = FactoryGirl.build_stubbed(:<%= model.underscore %>)
24
+ <% parent_model_tables.each_with_index do |parent_model, index| -%>
25
+ <%- if index == 0 -%>
26
+ let(:<%= parent_model %>) { FactoryGirl.build_stubbed(:<%= parent_model %>) }
27
+ <%- else -%>
28
+ let(:<%= parent_model %>) { FactoryGirl.build_stubbed(:<%= parent_model %>, :<%= parent_model_tables[index - 1] %> => <%= parent_model_tables[index - 1] %>) }
29
+ <%- end -%>
24
30
  <%- end -%>
25
31
  <% [1,2].each_with_index do |id, model_index| -%>
26
- @<%= var_name %>_<%= model_index + 1 %> = FactoryGirl.build_stubbed(:<%= var_name %><%= output_attributes.empty? ? ')' : ',' %>
32
+ let(:<%= var_name %>_<%= id %>) do
33
+ FactoryGirl.build_stubbed(:<%= var_name %><%= output_attributes.empty? ? ')' : ',' %>
27
34
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
28
- :<%= attribute.name %> => <%= t_helper.factory_attribute_value attribute.type, value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
35
+ :<%= attribute.name %> => <% if attribute.type == :references && parent_model_tables.include?(attribute.name) %><%= attribute.name %><% else %><%= t_helper.factory_attribute_value attribute.type, value_for(attribute) %><% end %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
29
36
  <% end -%>
30
- <% if !output_attributes.empty? -%>
31
- )
37
+ <%= output_attributes.empty? ? "" : " )\n" -%>
38
+ end
32
39
  <% end -%>
40
+
41
+ context <% if parent_model_tables.any? %>"within <%= parent_model_tables.join('/') %> nesting"<% end %> do<%- unless parent_model_tables.any? -%> # Within default nesting<% end %>
42
+ before(:each) do
43
+ # Add Properties for view scope
44
+ <%- parent_model_tables.each do |parent_model| -%>
45
+ assign(:<%= parent_model %>, @<%= parent_model %> = <%= parent_model %>)
46
+ <%- end -%>
47
+ <% [1,2].each_with_index do |id, model_index| -%>
48
+ @<%= var_name %>_<%= id %> = <%= var_name %>_<%= id %>
33
49
  <% end -%>
34
- assign(:<%= plural_var_name %>, [
50
+ assign(:<%= plural_var_name %>, [
35
51
  <% [1,2].each_with_index do |id, model_index| -%>
36
- @<%= var_name %>_<%= id %><%= model_index == 1 ? '' : ',' %>
52
+ @<%= var_name %>_<%= id %><%= model_index == 1 ? '' : ',' %>
37
53
  <% end -%>
38
- ])
39
- end
54
+ ])
40
55
 
41
- context do # Within default nesting
42
- before(:each) do
43
56
  @ability = Object.new
44
57
  @ability.extend(CanCan::Ability)
45
58
  controller.stub(:current_ability) { @ability }
@@ -84,6 +97,7 @@ describe "<%= ns_table_name %>/index" do
84
97
  <%- end -%>
85
98
  <% end -%>
86
99
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
100
+ <%- next if attribute.type == :references -%>
87
101
  <%- if webrat? -%>
88
102
  rendered.should have_selector("tr>td", :content => <%= factory_attribute_string attribute.type, value_for(attribute) %>.to_s, :count => 2)
89
103
  <%- else -%>
@@ -91,6 +105,30 @@ describe "<%= ns_table_name %>/index" do
91
105
  <%- end -%>
92
106
  <% end -%>
93
107
  end
108
+ <% output_attributes.each_with_index do |attribute, attribute_index| -%>
109
+ <%- next unless attribute.type == :references && !parent_model_tables.include?(attribute.name.to_s) -%>
110
+
111
+ it "displays the <%= attribute.name %> belonging to <%= var_name %>" do
112
+ render
113
+ <%- if webrat? -%>
114
+ rendered.should have_selector("tr>td", :content => <%= attribute.name %>.to_s, :count => 2)
115
+ <%- else -%>
116
+ assert_select "tr>td", :text => <%= attribute.name %>.to_s, :count => 2
117
+ <%- end -%>
118
+ end
119
+ <% end -%>
120
+ <% output_attributes.each_with_index do |attribute, attribute_index| -%>
121
+ <%- next unless attribute.type == :references && parent_model_tables.include?(attribute.name.to_s) -%>
122
+
123
+ it "does not display the <%= attribute.name %> belonging to <%= var_name %>" do
124
+ render
125
+ <%- if webrat? -%>
126
+ rendered.should have_selector("tr>td", :content => <%= attribute.name %>.to_s, :count => 0)
127
+ <%- else -%>
128
+ assert_select "tr>td", :text => <%= attribute.name %>.to_s, :count => 0
129
+ <%- end -%>
130
+ end
131
+ <% end -%>
94
132
 
95
133
  describe 'show <%= var_name %> link' do
96
134
  it "renders a link to <%= ns_file_name %>_path" do
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  <%-
4
4
 
5
- t_helper = AuthorizedRailsScaffolds::ViewSpecHelper.new(
5
+ t_helper = AuthorizedRailsScaffolds::RSpecScaffoldGeneratorViewHelper.new(
6
6
  :class_name => class_name,
7
7
  :singular_table_name => singular_table_name,
8
8
  :file_name => file_name,
@@ -12,27 +12,38 @@ t_helper = AuthorizedRailsScaffolds::ViewSpecHelper.new(
12
12
  local_class_name = t_helper.local_class_name # Non-Namespaced class name
13
13
  var_name = t_helper.var_name # Non-namespaced variable name
14
14
 
15
+ controller_directory = t_helper.controller_directory
16
+ parent_model_tables = t_helper.parent_model_tables
17
+
15
18
  output_attributes = t_helper.output_attributes
16
19
  standard_attributes = t_helper.standard_attributes
17
20
  datetime_attributes = t_helper.datetime_attributes
18
21
 
19
22
  -%>
20
- describe "<%= ns_table_name %>/new" do
21
- before(:each) do
22
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
23
- @<%= model.underscore %> = FactoryGirl.build_stubbed(:<%= model.underscore %>)
23
+ describe "<%= controller_directory %>/new" do
24
+
25
+ <% parent_model_tables.each_with_index do |parent_model, index| -%>
26
+ <%- if index == 0 -%>
27
+ let(:<%= parent_model %>) { FactoryGirl.build_stubbed(:<%= parent_model %>) }
28
+ <%- else -%>
29
+ let(:<%= parent_model %>) { FactoryGirl.build_stubbed(:<%= parent_model %>, :<%= parent_model_tables[index - 1] %> => <%= parent_model_tables[index - 1] %>) }
30
+ <%- end -%>
24
31
  <%- end -%>
25
- assign(:<%= var_name %>, FactoryGirl.build(:<%= var_name %><%= output_attributes.empty? ? '))' : ',' %>
32
+ let(:<%= var_name %>) do
33
+ FactoryGirl.build(:<%= var_name %><%= output_attributes.empty? ? ')' : ',' %>
26
34
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
27
- :<%= attribute.name %> => <%= t_helper.factory_attribute_value attribute.type, value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
35
+ :<%= attribute.name %> => <% if attribute.type == :references && parent_model_tables.include?(attribute.name) %><%= attribute.name %><% else %><%= t_helper.factory_attribute_value attribute.type, value_for(attribute) %><% end %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
28
36
  <% end -%>
29
- <%= !output_attributes.empty? ? " ))\n end" : " end" %>
37
+ <%= output_attributes.empty? ? "" : " )\n" -%>
38
+ end
30
39
 
31
- context do # Within default nesting
40
+ context <% if parent_model_tables.any? %>"within <%= parent_model_tables.join('/') %> nesting"<% end %> do<%- unless parent_model_tables.any? -%> # Within default nesting<% end %>
32
41
  before(:each) do
33
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
34
- assign(:<%= model.underscore %>, @<%= model.underscore %>)
42
+ # Add Properties for view scope
43
+ <%- parent_model_tables.each do |parent_model| -%>
44
+ assign(:<%= parent_model %>, @<%= parent_model %> = <%= parent_model %>)
35
45
  <%- end -%>
46
+ assign(:<%= var_name %>, @<%= var_name %> = <%= var_name %>)
36
47
  end
37
48
 
38
49
  it "renders new <%= var_name %> form" do
@@ -61,31 +72,31 @@ describe "<%= ns_table_name %>/new" do
61
72
  end
62
73
  <% end -%>
63
74
  end
64
-
65
75
  <% if datetime_attributes.any? -%>
76
+
66
77
  it "renders all date/time form elements" do
67
78
  render
68
79
 
69
80
  <% if webrat? -%>
70
81
  rendered.should have_selector("form", :action => <%= t_helper.controller_index_path %>, :method => "post") do |form|
71
- <% for attribute in datetime_attributes -%>
72
- <%- if [:date, :datetime].include? attribute.type -%>
73
- form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
74
- form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
75
- form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
76
- <%- end -%>
77
- <%- if [:time, :datetime].include? attribute.type -%>
78
- form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
79
- form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
80
- <%- end -%>
81
- <% end -%>
82
+ <%- for attribute in datetime_attributes -%>
83
+ <%- if [:date, :datetime].include? attribute.type -%>
84
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
85
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
86
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
87
+ <%- end -%>
88
+ <%- if [:time, :datetime].include? attribute.type -%>
89
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
90
+ form.should have_selector("select#<%= var_name %>_<%= attribute.name %>", :name => "<%= var_name %>[<%= attribute.name %>]")
91
+ <%- end -%>
92
+ <% end -%>
82
93
  end
83
94
  <% else -%>
84
95
  # Run the generator again with the --webrat flag if you want to use webrat matchers
85
96
  assert_select "form[action=?][method=?]", <%= t_helper.controller_index_path %>, "post" do
86
- <% for attribute in datetime_attributes -%>
97
+ <%- for attribute in datetime_attributes -%>
87
98
  # <%= attribute.name %> values
88
- <%- if [:date, :datetime].include? attribute.type -%>
99
+ <%- if [:date, :datetime].include? attribute.type -%>
89
100
  assert_select "select#<%= var_name %>_<%= attribute.name %>_1i[name=?]", "<%= var_name %>[<%= attribute.name %>(1i)]" do
90
101
  assert_select "option[selected=selected]", :text => "<%= t_helper.date_select_year_value attribute.default %>", :count => 1
91
102
  end
@@ -95,20 +106,19 @@ describe "<%= ns_table_name %>/new" do
95
106
  assert_select "select#<%= var_name %>_<%= attribute.name %>_3i[name=?]", "<%= var_name %>[<%= attribute.name %>(3i)]" do
96
107
  assert_select "option[selected=selected]", :text => "<%= t_helper.date_select_day_value attribute.default %>", :count => 1
97
108
  end
98
- <%- end -%>
99
- <%- if [:time, :datetime].include? attribute.type -%>
109
+ <%- end -%>
110
+ <%- if [:time, :datetime].include? attribute.type -%>
100
111
  assert_select "select#<%= var_name %>_<%= attribute.name %>_4i[name=?]", "<%= var_name %>[<%= attribute.name %>(4i)]" do
101
112
  assert_select "option[selected=selected]", :text => "<%= t_helper.date_select_hour_value attribute.default %>", :count => 1
102
113
  end
103
114
  assert_select "select#<%= var_name %>_<%= attribute.name %>_5i[name=?]", "<%= var_name %>[<%= attribute.name %>(5i)]" do
104
115
  assert_select "option[selected=selected]", :text => "<%= t_helper.date_select_minute_value attribute.default %>", :count => 1
105
116
  end
117
+ <%- end -%>
106
118
  <%- end -%>
107
- <% end -%>
108
119
  end
109
120
  <% end -%>
110
121
  end
111
- end
112
-
113
122
  <% end -%>
123
+ end
114
124
  end
@@ -3,12 +3,17 @@ require "spec_helper"
3
3
  <% module_namespacing do -%>
4
4
  <%-
5
5
 
6
- t_helper = AuthorizedRailsScaffolds::RoutingSpecHelper.new(
7
- ns_table_name: ns_table_name
6
+ t_helper = AuthorizedRailsScaffolds::RSpecScaffoldGeneratorHelper.new(
7
+ :class_name => class_name,
8
+ :singular_table_name => singular_table_name,
9
+ :file_name => file_name,
10
+ :attributes => attributes
8
11
  )
9
12
 
10
- request_path = t_helper.request_path
11
- extra_params = t_helper.extra_params
13
+ # request_path = t_helper.request_path
14
+ controller_directory = t_helper.controller_directory
15
+ example_controller_path = t_helper.example_controller_path
16
+ example_controller_path_extra_params = t_helper.example_controller_path_extra_params
12
17
 
13
18
  -%>
14
19
  describe <%= controller_class_name %>Controller do
@@ -16,32 +21,32 @@ describe <%= controller_class_name %>Controller do
16
21
 
17
22
  <% unless options[:singleton] -%>
18
23
  it "routes to #index" do
19
- get("/<%= request_path %>").should route_to("<%= ns_table_name %>#index"<%= extra_params %>)
24
+ get("<%= example_controller_path %>").should route_to("<%= controller_directory %>#index"<%= example_controller_path_extra_params %>)
20
25
  end
21
26
 
22
27
  <% end -%>
23
28
  it "routes to #new" do
24
- get("/<%= request_path %>/new").should route_to("<%= ns_table_name %>#new"<%= extra_params %>)
29
+ get("<%= example_controller_path %>/new").should route_to("<%= controller_directory %>#new"<%= example_controller_path_extra_params %>)
25
30
  end
26
31
 
27
32
  it "routes to #show" do
28
- get("/<%= request_path %>/1").should route_to("<%= ns_table_name %>#show"<%= extra_params %>, :id => "1")
33
+ get("<%= example_controller_path %>/1").should route_to("<%= controller_directory %>#show"<%= example_controller_path_extra_params %>, :id => "1")
29
34
  end
30
35
 
31
36
  it "routes to #edit" do
32
- get("/<%= request_path %>/1/edit").should route_to("<%= ns_table_name %>#edit"<%= extra_params %>, :id => "1")
37
+ get("<%= example_controller_path %>/1/edit").should route_to("<%= controller_directory %>#edit"<%= example_controller_path_extra_params %>, :id => "1")
33
38
  end
34
39
 
35
40
  it "routes to #create" do
36
- post("/<%= request_path %>").should route_to("<%= ns_table_name %>#create"<%= extra_params %>)
41
+ post("<%= example_controller_path %>").should route_to("<%= controller_directory %>#create"<%= example_controller_path_extra_params %>)
37
42
  end
38
43
 
39
44
  it "routes to #update" do
40
- put("/<%= request_path %>/1").should route_to("<%= ns_table_name %>#update"<%= extra_params %>, :id => "1")
45
+ put("<%= example_controller_path %>/1").should route_to("<%= controller_directory %>#update"<%= example_controller_path_extra_params %>, :id => "1")
41
46
  end
42
47
 
43
48
  it "routes to #destroy" do
44
- delete("/<%= request_path %>/1").should route_to("<%= ns_table_name %>#destroy"<%= extra_params %>, :id => "1")
49
+ delete("<%= example_controller_path %>/1").should route_to("<%= controller_directory %>#destroy"<%= example_controller_path_extra_params %>, :id => "1")
45
50
  end
46
51
 
47
52
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  <%-
4
4
 
5
- t_helper = AuthorizedRailsScaffolds::ViewSpecHelper.new(
5
+ t_helper = AuthorizedRailsScaffolds::RSpecScaffoldGeneratorViewHelper.new(
6
6
  :class_name => class_name,
7
7
  :singular_table_name => singular_table_name,
8
8
  :file_name => file_name,
@@ -12,29 +12,38 @@ t_helper = AuthorizedRailsScaffolds::ViewSpecHelper.new(
12
12
  local_class_name = t_helper.local_class_name # Non-Namespaced class name
13
13
  var_name = t_helper.var_name # Non-namespaced variable name
14
14
 
15
+ controller_directory = t_helper.controller_directory
16
+ parent_model_tables = t_helper.parent_model_tables
17
+
15
18
  output_attributes = t_helper.output_attributes
16
19
  references_attributes = t_helper.references_attributes
17
20
 
18
21
  -%>
19
- describe "<%= ns_table_name %>/show" do
20
- before(:each) do
21
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
22
- @<%= model.underscore %> = FactoryGirl.build_stubbed(:<%= model.underscore %>)
22
+ describe "<%= controller_directory %>/show" do
23
+
24
+ <% parent_model_tables.each_with_index do |parent_model, index| -%>
25
+ <%- if index == 0 -%>
26
+ let(:<%= parent_model %>) { FactoryGirl.build_stubbed(:<%= parent_model %>) }
27
+ <%- else -%>
28
+ let(:<%= parent_model %>) { FactoryGirl.build_stubbed(:<%= parent_model %>, :<%= parent_model_tables[index - 1] %> => <%= parent_model_tables[index - 1] %>) }
29
+ <%- end -%>
23
30
  <%- end -%>
24
- @<%= var_name %> = FactoryGirl.build_stubbed(:<%= var_name %><%= output_attributes.empty? ? ')' : ',' %>
31
+ let(:<%= var_name %>) do
32
+ FactoryGirl.build_stubbed(:<%= var_name %><%= output_attributes.empty? ? ')' : ',' %>
25
33
  <% output_attributes.each_with_index do |attribute, attribute_index| -%>
26
- :<%= attribute.name %> => <%= t_helper.factory_attribute_value attribute.type, value_for(attribute) %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
27
- <% end -%>
28
- <% if !output_attributes.empty? -%>
29
- )
34
+ :<%= attribute.name %> => <% if attribute.type == :references && parent_model_tables.include?(attribute.name) %><%= attribute.name %><% else %><%= t_helper.factory_attribute_value attribute.type, value_for(attribute) %><% end %><%= attribute_index == output_attributes.length - 1 ? '' : ','%>
30
35
  <% end -%>
36
+ <%= output_attributes.empty? ? "" : " )\n" -%>
31
37
  end
32
38
 
33
- context do # Within default nesting
39
+ context <% if parent_model_tables.any? %>"within <%= parent_model_tables.join('/') %> nesting"<% end %> do<%- unless parent_model_tables.any? -%> # Within default nesting<% end %>
34
40
  before(:each) do
35
- <%- AuthorizedRailsScaffolds.parent_models.each do |model| -%>
36
- assign(:<%= model.underscore %>, @<%= model.underscore %>)
41
+ # Add Properties for view scope
42
+ <%- parent_model_tables.each do |parent_model| -%>
43
+ assign(:<%= parent_model %>, @<%= parent_model %> = <%= parent_model %>)
37
44
  <%- end -%>
45
+ assign(:<%= var_name %>, @<%= var_name %> = <%= var_name %>)
46
+
38
47
  @ability = Object.new
39
48
  @ability.extend(CanCan::Ability)
40
49
  controller.stub(:current_ability) { @ability }
@@ -0,0 +1,147 @@
1
+ require 'spec_helper'
2
+
3
+ describe AuthorizedRailsScaffolds::RSpecScaffoldGeneratorHelper do
4
+
5
+ describe '#controller_directory' do
6
+ it 'underscores the class_name value' do
7
+ subject = build_controller_spec_helper :class_name => 'FooBar'
8
+ subject.controller_directory.should eq('foo_bars')
9
+ end
10
+ it 'adds parent_models to the file path' do
11
+ subject = build_controller_spec_helper :class_name => 'Example::FooBar'
12
+ subject.controller_directory.should eq('example/foo_bars')
13
+ end
14
+ it 'adds multiple parent_models to the file path' do
15
+ subject = build_controller_spec_helper :class_name => 'Example::V1::FooBar'
16
+ subject.controller_directory.should eq('example/v1/foo_bars')
17
+ end
18
+ context 'with a parent model' do
19
+ before(:each) do
20
+ AuthorizedRailsScaffolds.configure do |config|
21
+ config.parent_models = ['Parent']
22
+ end
23
+ end
24
+ it 'ignores the parent_model value' do
25
+ subject = build_controller_spec_helper :class_name => 'FooBar'
26
+ subject.controller_directory.should eq('foo_bars')
27
+ end
28
+ end
29
+ end
30
+
31
+ describe '#example_controller_path' do
32
+ it 'underscores the class_name value' do
33
+ subject = build_controller_spec_helper :class_name => 'FooBar'
34
+ subject.example_controller_path.should eq('/foo_bars')
35
+ end
36
+ it 'adds parent_models to the file path' do
37
+ subject = build_controller_spec_helper :class_name => 'Example::FooBar'
38
+ subject.example_controller_path.should eq('/example/foo_bars')
39
+ end
40
+ it 'adds multiple parent_models to the file path' do
41
+ subject = build_controller_spec_helper :class_name => 'Example::V1::FooBar'
42
+ subject.example_controller_path.should eq('/example/v1/foo_bars')
43
+ end
44
+ context 'with a parent model' do
45
+ before(:each) do
46
+ AuthorizedRailsScaffolds.configure do |config|
47
+ config.parent_models = ['Parent']
48
+ end
49
+ end
50
+ it 'adds the parent model before the class name' do
51
+ subject = build_controller_spec_helper :class_name => 'FooBar'
52
+ subject.example_controller_path.should eq('/parents/2/foo_bars')
53
+ end
54
+ it 'adds the parent model after the parent module' do
55
+ subject = build_controller_spec_helper :class_name => 'Example::FooBar'
56
+ subject.example_controller_path.should eq('/example/parents/2/foo_bars')
57
+ end
58
+ it 'adds the parent model after multiple parent module' do
59
+ subject = build_controller_spec_helper :class_name => 'Example::V1::FooBar'
60
+ subject.example_controller_path.should eq('/example/v1/parents/2/foo_bars')
61
+ end
62
+ end
63
+ context 'with multiple parent models' do
64
+ before(:each) do
65
+ AuthorizedRailsScaffolds.configure do |config|
66
+ config.parent_models = ['Grandparent', 'Parent']
67
+ end
68
+ it 'adds the parent models before the class name' do
69
+ subject = build_controller_spec_helper :class_name => 'FooBar'
70
+ subject.example_controller_path.should eq('/grandparents/2/parents/3/foo_bars')
71
+ end
72
+ it 'adds the parent models after the parent module' do
73
+ subject = build_controller_spec_helper :class_name => 'Example::FooBar'
74
+ subject.example_controller_path.should eq('/example/grandparents/2/parents/3/foo_bars')
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ describe '#example_controller_path_extra_params' do
81
+ context 'with no parent models' do
82
+ it 'returns an empty string with no parent module' do
83
+ subject = build_controller_spec_helper :class_name => 'FooBar'
84
+ subject.example_controller_path_extra_params.should eq('')
85
+ end
86
+ it 'returns an empty string with a parent module' do
87
+ subject = build_controller_spec_helper :class_name => 'Example::FooBar'
88
+ subject.example_controller_path_extra_params.should eq('')
89
+ end
90
+ end
91
+ context 'with a parent model' do
92
+ before(:each) do
93
+ AuthorizedRailsScaffolds.configure do |config|
94
+ config.parent_models = ['Parent']
95
+ end
96
+ end
97
+ it 'adds the parent model with a value assigned to it' do
98
+ subject = build_controller_spec_helper :class_name => 'FooBar'
99
+ subject.example_controller_path_extra_params.should eq(', :parent_id => 2')
100
+ end
101
+ end
102
+ context 'with multiple parent models' do
103
+ before(:each) do
104
+ AuthorizedRailsScaffolds.configure do |config|
105
+ config.parent_models = ['Grandparent', 'Parent']
106
+ end
107
+ end
108
+ it 'adds the parent models with their values' do
109
+ subject = build_controller_spec_helper :class_name => 'FooBar'
110
+ subject.example_controller_path_extra_params.should eq(', :grandparent_id => 2, :parent_id => 3')
111
+ end
112
+ end
113
+ end
114
+
115
+ describe '#parent_model_tables' do
116
+ context 'with no parent_models' do
117
+ it 'returns an empty array' do
118
+ subject = build_controller_spec_helper
119
+ subject.parent_model_tables.should eq([])
120
+ end
121
+ end
122
+ context 'with a parent model' do
123
+ before(:each) do
124
+ AuthorizedRailsScaffolds.configure do |config|
125
+ config.parent_models = ['Parent']
126
+ end
127
+ end
128
+ it 'returns an array containing the models table name' do
129
+ subject = build_controller_spec_helper
130
+ subject.parent_model_tables.should eq(['parent'])
131
+ end
132
+ end
133
+ context 'with multiple parent models' do
134
+ before(:each) do
135
+ AuthorizedRailsScaffolds.configure do |config|
136
+ config.parent_models = ['Grandparent', 'Parent']
137
+ end
138
+ end
139
+ it 'returns an array containing all model table names' do
140
+ subject = build_controller_spec_helper
141
+ subject.parent_model_tables.should eq(['grandparent', 'parent'])
142
+ end
143
+ end
144
+ end
145
+
146
+
147
+ end
@@ -0,0 +1,18 @@
1
+ require "bundler/setup"
2
+ Bundler.require
3
+
4
+ require 'rails'
5
+
6
+ require 'authorized_rails_scaffolds'
7
+ require 'support/rspec_scaffold_generator_helper_macros'
8
+
9
+ RSpec.configure do |config|
10
+ config.include RSpecScaffoldGeneratorHelperMacros
11
+ # some (optional) config here
12
+
13
+ config.after(:each) do
14
+ AuthorizedRailsScaffolds.configure do |config|
15
+ config.parent_models = []
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+
2
+ module RSpecScaffoldGeneratorHelperMacros
3
+
4
+ def build_controller_spec_helper(args = {})
5
+ defaults = {
6
+ class_name: 'Scaffold::Example', # Model class name
7
+ singular_table_name: 'scaffold_example', # path and model name
8
+ file_name: 'example', # last part of singular_table_name
9
+ attributes: [],
10
+ }
11
+ defaults.merge! (args)
12
+ AuthorizedRailsScaffolds::RSpecScaffoldGeneratorHelper.new(defaults)
13
+ end
14
+
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authorized_rails_scaffolds
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-11 00:00:00.000000000Z
12
+ date: 2013-04-25 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
16
- requirement: &70305143799400 !ruby/object:Gem::Requirement
16
+ requirement: &70301980494120 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,21 @@ dependencies:
21
21
  version: '3.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70305143799400
24
+ version_requirements: *70301980494120
25
+ - !ruby/object:Gem::Dependency
26
+ name: rails
27
+ requirement: &70301980493620 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '3.1'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70301980493620
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: bundler
27
- requirement: &70305143798900 !ruby/object:Gem::Requirement
38
+ requirement: &70301980493160 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ~>
@@ -32,10 +43,21 @@ dependencies:
32
43
  version: '1.3'
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *70305143798900
46
+ version_requirements: *70301980493160
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: rake
38
- requirement: &70305143798520 !ruby/object:Gem::Requirement
49
+ requirement: &70301980492780 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70301980492780
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec
60
+ requirement: &70301980492320 !ruby/object:Gem::Requirement
39
61
  none: false
40
62
  requirements:
41
63
  - - ! '>='
@@ -43,7 +65,7 @@ dependencies:
43
65
  version: '0'
44
66
  type: :development
45
67
  prerelease: false
46
- version_requirements: *70305143798520
68
+ version_requirements: *70301980492320
47
69
  description: Creates scaffolds for Twitter Bootstrap with generated RSpec coverage
48
70
  email:
49
71
  - bemo56@hotmail.com
@@ -58,11 +80,10 @@ files:
58
80
  - Rakefile
59
81
  - authorized_rails_scaffolds.gemspec
60
82
  - lib/authorized_rails_scaffolds.rb
61
- - lib/authorized_rails_scaffolds/controller_spec_helper.rb
62
83
  - lib/authorized_rails_scaffolds/helper.rb
63
- - lib/authorized_rails_scaffolds/routing_spec_helper.rb
84
+ - lib/authorized_rails_scaffolds/rspec_scaffold_generator_helper.rb
85
+ - lib/authorized_rails_scaffolds/rspec_scaffold_generator_view_helper.rb
64
86
  - lib/authorized_rails_scaffolds/version.rb
65
- - lib/authorized_rails_scaffolds/view_spec_helper.rb
66
87
  - lib/generators/authorized_rails_scaffolds/install_macros/USAGE
67
88
  - lib/generators/authorized_rails_scaffolds/install_macros/install_macros_generator.rb
68
89
  - lib/generators/authorized_rails_scaffolds/install_macros/templates/devise_can_can/USAGE
@@ -82,6 +103,9 @@ files:
82
103
  - lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/new_spec.rb
83
104
  - lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/routing_spec.rb
84
105
  - lib/generators/authorized_rails_scaffolds/install_templates/templates/spec/show_spec.rb
106
+ - spec/lib/authorized_rails_scaffolds/rspec_scaffold_generator_helper_spec.rb
107
+ - spec/spec_helper.rb
108
+ - spec/support/rspec_scaffold_generator_helper_macros.rb
85
109
  homepage: https://github.com/bmorrall/authorized_rails_scaffolds
86
110
  licenses:
87
111
  - MIT
@@ -108,4 +132,7 @@ signing_key:
108
132
  specification_version: 3
109
133
  summary: Replaces Rails and RSpec's default generators with templates taking full
110
134
  advantage of Authentication (Devise), Authorization (CanCan) and Test Coverage (RSpec)
111
- test_files: []
135
+ test_files:
136
+ - spec/lib/authorized_rails_scaffolds/rspec_scaffold_generator_helper_spec.rb
137
+ - spec/spec_helper.rb
138
+ - spec/support/rspec_scaffold_generator_helper_macros.rb
@@ -1,31 +0,0 @@
1
- class AuthorizedRailsScaffolds::ControllerSpecHelper < AuthorizedRailsScaffolds::Helper
2
-
3
- def initialize(options = {})
4
- super options
5
-
6
- @attributes = options[:attributes]
7
- end
8
-
9
- def create_factory_model
10
- extra_params = extra_model_params
11
- "FactoryGirl.create(:#{var_name}#{extra_params})"
12
- end
13
-
14
- def create_parent_model(model_name)
15
- extra_params = extra_model_params(model_name)
16
- "FactoryGirl.create(:#{model_name}#{extra_params})"
17
- end
18
-
19
- protected
20
-
21
- def extra_model_params(model_name = nil)
22
- argument_params = []
23
- AuthorizedRailsScaffolds.parent_models.each do |parent_model|
24
- attribute = parent_model.underscore
25
- break if model_name == attribute
26
- argument_params << ", :#{attribute} => @#{attribute}"
27
- end
28
- argument_params.join('')
29
- end
30
-
31
- end
@@ -1,29 +0,0 @@
1
- class AuthorizedRailsScaffolds::RoutingSpecHelper
2
-
3
- def initialize(options = {})
4
- @ns_table_name = options[:ns_table_name]
5
- end
6
-
7
- def extra_params
8
- extra_params = ''
9
- AuthorizedRailsScaffolds.parent_models.each_with_index do |model, model_index|
10
- extra_params += ", :#{model.underscore}_id => \"#{model_index + 2}\""
11
- end
12
- extra_params
13
- end
14
-
15
- def request_path
16
- # Remove last part of the path
17
- parts = @ns_table_name.split('/')[0..-2] || []
18
-
19
- AuthorizedRailsScaffolds.parent_models.each_with_index do |model, model_index|
20
- parts << model.underscore.pluralize
21
- parts << model_index + 2
22
- end
23
-
24
- # Add Final Part
25
- parts << @ns_table_name.split('/')[-1]
26
- parts.join('/')
27
- end
28
-
29
- end