leonardo 1.6.1 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (21) hide show
  1. data/CHANGELOG +8 -0
  2. data/lib/generators/erb/leosca/leosca_generator.rb +32 -18
  3. data/lib/generators/leolay/leolay_generator.rb +11 -8
  4. data/lib/generators/leolay/templates/spec/factories.rb +6 -6
  5. data/lib/generators/{base.rb → leonardo.rb} +49 -25
  6. data/lib/generators/leosca/install_generator.rb +1 -1
  7. data/lib/generators/rails/leosca/leosca_generator.rb +12 -28
  8. data/lib/generators/rails/leosca_controller/leosca_controller_generator.rb +9 -85
  9. data/lib/generators/rails/leosca_controller/templates/controller.rb +2 -10
  10. data/lib/generators/rspec/leointegration/leointegration_generator.rb +29 -0
  11. data/lib/generators/rspec/leointegration/templates/request_spec.rb +52 -0
  12. data/lib/generators/rspec/leosca/leosca_generator.rb +45 -0
  13. data/lib/generators/{leolay/templates/lib/templates/rspec/scaffold → rspec/leosca/templates}/controller_spec.rb +1 -1
  14. data/lib/generators/{leolay/templates/lib/templates/rspec/scaffold → rspec/leosca/templates}/edit_spec.rb +1 -1
  15. data/lib/generators/{leolay/templates/lib/templates/rspec/scaffold → rspec/leosca/templates}/index_spec.rb +7 -1
  16. data/lib/generators/{leolay/templates/lib/templates/rspec/scaffold → rspec/leosca/templates}/new_spec.rb +1 -1
  17. data/lib/generators/rspec/leosca/templates/routing_spec.rb +37 -0
  18. data/lib/generators/{leolay/templates/lib/templates/rspec/scaffold → rspec/leosca/templates}/show_spec.rb +1 -1
  19. metadata +15 -13
  20. data/lib/generators/leolay/templates/lib/generators/rspec/scaffold/scaffold_generator.rb +0 -173
  21. data/lib/generators/leolay/templates/lib/templates/rspec/scaffold/routing_spec.rb +0 -37
data/CHANGELOG CHANGED
@@ -1,8 +1,16 @@
1
+ 1.7.0 (October 1th, 2011) Marco Mastrodonato
2
+ * New feature to add a resource management within a namescape. Unlike the original way (rails g leosca admin/resource ...which remains available), the resource is not under the namespace but only its management so you could create one for several kind of users. Try it adding the new option like this example: --leospace=admin
3
+ * Huge code improvements
4
+
5
+
6
+
1
7
  1.6.1 (September 20th, 2011) Marco Mastrodonato
2
8
  * Improved ajax show title
3
9
  * List resource: the link to parent show resource is now ajax (if remote = true)
4
10
  * Minor bug fix
5
11
 
12
+
13
+
6
14
  1.6.0 (September 16th, 2011) Marco Mastrodonato
7
15
  * Nested resource: use new option rails g leosca product --under=category field:type
8
16
  * Improved views data format
@@ -1,26 +1,22 @@
1
- require 'rails/generators/erb'
2
- require 'rails/generators/resource_helpers'
3
- require File.join(File.dirname(__FILE__), '../../base')
1
+ require 'rails/generators/erb/scaffold/scaffold_generator'
2
+ require File.join(File.dirname(__FILE__), '../../leonardo')
4
3
 
5
4
  module Erb
6
5
  module Generators
7
- class LeoscaGenerator < Base
8
- include Rails::Generators::ResourceHelpers
6
+ class LeoscaGenerator < ::Erb::Generators::ScaffoldGenerator
9
7
  include ::Leonardo::Leosca
10
8
  include ::Leonardo::Nested
11
9
  #puts 'erb:leosca'
12
10
 
13
11
  source_root File.expand_path('../templates', __FILE__)
14
- argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
15
12
  class_option :authorization, :type => :boolean, :default => true, :description => "Add code to manage authorization with cancan"
16
13
  class_option :remote, :type => :boolean, :default => true, :description => "Enable ajax. You can also do later set remote to true into index view."
17
14
  class_option :formtastic, :type => :boolean, :default => true, :description => "Create forms to manage with formtastic gem"
18
- class_option :under, :type => :string, :default => "", :banner => "brand/category"
19
-
20
- def create_root_folder
21
- empty_directory File.join("app/views", controller_file_path)
22
- end
15
+ class_option :under, :type => :string, :default => "", :banner => "brand/category", :desc => "To nest a resource under another(s)"
16
+ class_option :leospace, :type => :string, :default => "", :banner => ":admin", :desc => "To nest a resource under namespace(s)"
17
+ class_option :auth_class, :type => :boolean, :default => 'user', :desc => "Set the authentication class name"
23
18
 
19
+ #override
24
20
  def copy_view_files
25
21
  available_views.each do |view|
26
22
  filenames = filenames_all_formats(view, source_paths)
@@ -36,7 +32,7 @@ module Erb
36
32
  end
37
33
  filename_source = filename unless search_into_subfolder
38
34
 
39
- template filename_source, File.join("app/views", controller_file_path, filename)
35
+ template filename_source, File.join("app/views", base_namespaces, controller_file_path, filename)
40
36
  end
41
37
  end
42
38
  end
@@ -44,20 +40,37 @@ module Erb
44
40
  def update_layout_html
45
41
  file = "app/views/layouts/_#{CONFIG[:default_style]}.html.erb"
46
42
  if nested?
47
- inject_into_file file, :after => "<!-- Insert below here other #{last_parent} elements -->#{CRLF}" do
48
- <<-FILE.gsub(/^ /, '')
43
+ inject_into_file file, :before => " <!-- Insert above here other #{last_parent} elements -->" do
44
+ <<-FILE.gsub(/^ /, '')
49
45
  #{"<% if can?(:read, #{class_name}) && controller.controller_path == '#{controller_name}' -%>" if authorization?}
50
46
  <li class="active"><%= t('models.#{plural_table_name}') %></li>
51
- <!-- Insert below here other #{singular_table_name} elements -->
47
+ <!-- Insert above here other #{singular_table_name} elements -->
52
48
  #{"<% end -%>" if authorization?}
53
49
  FILE
54
50
  end if File.exists?(file)
51
+ #elsif leospaced?
52
+ # inject_into_file file, :before => " <!-- Insert above other elements -->" do
53
+ # <<-FILE.gsub(/^ /, '')
54
+ # #{"<% if #{options.auth_class}_signed_in? && current_#{options.auth_class}.role?(#{last_namespace.inspect}) -%>" if authorization?}
55
+ # <li class="<%= controller.controller_path == '#{last_namespace}' ? 'active' : '' %>"><a href="<%= #{last_namespace}_index_path %>">#{last_namespace.capitalize}</a></li>
56
+ # <!-- Insert above here other #{last_namespace} elements -->
57
+ # #{"<% end -%>" if authorization?}
58
+ # FILE
59
+ # end if File.exists?(file)
60
+ # inject_into_file file, :before => " <!-- Insert above other elements -->" do
61
+ # <<-FILE.gsub(/^ /, '')
62
+ # #{"<% if can?(:read, #{class_name}) -%>" if authorization?}
63
+ # <li class="<%= controller.controller_path == '#{formatted_namespace_path}#{controller_name}' ? 'active' : '' %>"><a href="<%= #{list_resources_path} %>"><%= t('models.#{plural_table_name}') %></a></li>
64
+ # <!-- Insert above here other #{singular_table_name} elements -->
65
+ # #{"<% end -%>" if authorization?}
66
+ # FILE
67
+ # end if File.exists?(file)
55
68
  else
56
- inject_into_file file, :after => "<!-- Insert below other elements -->#{CRLF}" do
69
+ inject_into_file file, :before => " <!-- Insert above other elements -->" do
57
70
  <<-FILE.gsub(/^ /, '')
58
71
  #{"<% if can?(:read, #{class_name}) -%>" if authorization?}
59
- <li class="<%= controller.controller_path == '#{controller_name}' ? 'active' : '' %>"><a href="<%= #{plural_table_name}_path %>"><%= t('models.#{plural_table_name}') %></a></li>
60
- <!-- Insert below here other #{singular_table_name} elements -->
72
+ <li class="<%= controller.controller_path == '#{formatted_namespace_path}#{controller_name}' ? 'active' : '' %>"><a href="<%= #{list_resources_path} %>"><%= t('models.#{plural_table_name}') %></a></li>
73
+ <!-- Insert above here other #{singular_table_name} elements -->
61
74
  #{"<% end -%>" if authorization?}
62
75
  FILE
63
76
  end if File.exists?(file)
@@ -77,6 +90,7 @@ module Erb
77
90
 
78
91
  protected
79
92
 
93
+ #Override
80
94
  def available_views
81
95
  %w(index edit show new _form _list destroy _show)
82
96
  end
@@ -6,7 +6,7 @@ class LeolayGenerator < Rails::Generators::Base
6
6
  class_option :second_color, :type => :string, :default => nil, :desc => "Force a secondary color for the stylesheet"
7
7
  class_option :authentication, :type => :boolean, :default => true, :desc => "Add code to manage authentication with devise"
8
8
  class_option :authorization, :type => :boolean, :default => true, :desc => "Add code to manage authorization with cancan"
9
- class_option :user_class, :type => :boolean, :default => 'user', :desc => "Set the user class name"
9
+ class_option :auth_class, :type => :boolean, :default => 'user', :desc => "Set the authentication class name"
10
10
  class_option :formtastic, :type => :boolean, :default => true, :desc => "Copy formtastic files into leosca custom folder (inside project)"
11
11
  class_option :jquery_ui, :type => :boolean, :default => true, :desc => "To use jQuery ui improvement"
12
12
  class_option :rspec, :type => :boolean, :default => true, :desc => "Include custom rspec generator and custom templates"
@@ -112,7 +112,7 @@ class LeolayGenerator < Rails::Generators::Base
112
112
  end
113
113
 
114
114
  def setup_authentication
115
- file = "app/models/#{options.user_class}.rb"
115
+ file = "app/models/#{options.auth_class}.rb"
116
116
  #puts "File #{file} #{File.exists?(file) ? "" : "does not"} exists!"
117
117
  return unless options.authentication? and File.exists?(file)
118
118
 
@@ -133,6 +133,9 @@ class LeolayGenerator < Rails::Generators::Base
133
133
  def role?(role)
134
134
  roles.include? role.to_s
135
135
  end
136
+ def admin?
137
+ self.role? 'admin'
138
+ end
136
139
  FILE
137
140
  end
138
141
 
@@ -333,12 +336,12 @@ class LeolayGenerator < Rails::Generators::Base
333
336
  copy_file file, file
334
337
  file = "spec/helpers/application_helpers_spec.rb"
335
338
  copy_file file, file
336
- file = "lib/templates/rspec/scaffold"
337
- directory file, file
338
- file = "lib/templates/rspec/scaffold"
339
- directory file, file
340
- file = "lib/generators/rspec/scaffold"
341
- directory file, file
339
+ #file = "lib/templates/rspec/scaffold"
340
+ #directory file, file
341
+ #file = "lib/templates/rspec/scaffold"
342
+ #directory file, file
343
+ #file = "lib/generators/rspec/scaffold"
344
+ #directory file, file
342
345
 
343
346
  end
344
347
 
@@ -1,17 +1,17 @@
1
1
  FactoryGirl.define do
2
2
  factory :user do
3
- email 'test@leonardo.com'
4
- password 'abcd1234'
5
- roles ['user']
3
+ sequence(:email) {|n| "test#{n}@leonardo.com" }
4
+ password 'abcd1234'
5
+ roles ['user']
6
6
 
7
7
  trait :guest do
8
- roles ['guest']
8
+ roles ['guest']
9
9
  end
10
10
  trait :manager do
11
- roles ['manager']
11
+ roles ['manager']
12
12
  end
13
13
  trait :admin do
14
- roles ['admin']
14
+ roles ['admin']
15
15
  end
16
16
 
17
17
  factory :user_guest, :traits => [:guest]
@@ -58,18 +58,18 @@ module Leonardo
58
58
  end
59
59
  def attribute_to_requests(attribute)
60
60
  case attribute.type
61
- when :boolean then " check '#{singular_table_name}_#{attribute.name}' if #{singular_table_name}.#{attribute.name}"
62
- when :references, :belongs_to then " select #{singular_table_name}.#{attribute.name}.name, :from => '#{singular_table_name}_#{attribute.name}_id'"
61
+ when :boolean then "check '#{singular_table_name}_#{attribute.name}' if #{singular_table_name}.#{attribute.name}"
62
+ when :references, :belongs_to then "select #{singular_table_name}.#{attribute.name}.name, :from => '#{singular_table_name}_#{attribute.name}_id'"
63
63
  when :datetime, :time, :timestamp
64
64
  then ""
65
- when :date then " fill_in '#{singular_table_name}_#{attribute.name}', :with => #{singular_table_name}.#{attribute.name}.strftime('%d-%m-%Y')"
66
- else " fill_in '#{singular_table_name}_#{attribute.name}', :with => #{singular_table_name}.#{attribute.name}"
65
+ when :date then "fill_in '#{singular_table_name}_#{attribute.name}', :with => #{singular_table_name}.#{attribute.name}.strftime('%d-%m-%Y')"
66
+ else "fill_in '#{singular_table_name}_#{attribute.name}', :with => #{singular_table_name}.#{attribute.name}"
67
67
  end
68
68
  end
69
69
  def attribute_to_erb(attribute, object)
70
70
  case attribute.type
71
71
  when :boolean then "<%= #{object}.#{attribute.name} ? style_image_tag(\"ico_v.png\", :class => \"ico_true\") : style_image_tag(\"ico_x.png\", :class => \"ico_false\") %>"
72
- when :references, :belongs_to then "<%= link_to((#{object}.#{attribute.name}.try(:name) || \"\#{t('models.#{attribute.name}')} \#{#{object}.#{attribute.name}.try(:id)}\"), #{object}.#{attribute.name}, :remote => remote) %>"
72
+ when :references, :belongs_to then "<%= link_to((#{object}.#{attribute.name}.try(:name) || \"\#{t('models.#{attribute.name}')} \#{#{object}.#{attribute.name}.try(:id)}\"), #{object}.#{attribute.name}, :remote => @remote) %>"
73
73
  when :integer then "<%= number_with_delimiter #{object}.#{attribute.name} %>"
74
74
  when :decimal then "<%= number_to_currency #{object}.#{attribute.name} %>"
75
75
  when :float then "<%= number_with_precision #{object}.#{attribute.name} %>"
@@ -92,28 +92,15 @@ module Leonardo
92
92
  return "have_xpath('//table/tbody/tr')", "have_no_xpath('//table/tbody/tr')"
93
93
  end
94
94
  end
95
- def plural_path_file_name
96
- (class_path + [plural_table_name]).join('/')
97
- end
98
95
  end
99
96
 
100
97
  module Nested
101
98
  protected
102
99
 
103
- #product under brand/category => "brand_category_product"
104
- def underscore_resource_path(names=:all_singular)
105
- case names
106
- when :all_singular
107
- resource_path.join('_')
108
- when :all_plural
109
- #who needs?
110
- when :parent_singular_resource_plural
111
- resource_path.join('_').pluralize
112
- else
113
- "#{names.to_s}_not_supported"
114
- end
115
-
116
- end
100
+ #Add leonardo namespace to class_path
101
+ #def class_path
102
+ # super + base_namespaces
103
+ #end
117
104
 
118
105
  #product => products_path
119
106
  #product under category => category_products_path(@category)
@@ -124,6 +111,7 @@ module Leonardo
124
111
 
125
112
  #product under category => category_products_path(category)
126
113
  #product under brand/category => brand_category_products_path(@brand, category)
114
+ #TODO: figure out how to build links for a particular resource in the path
127
115
  def list_resources_path_back
128
116
  return unless nested?
129
117
  "#{underscore_resource_path(:parent_singular_resource_plural)}_path(#{formatted_parent_resources("@").reverse.sub(/@/, "").reverse})"
@@ -180,18 +168,34 @@ module Leonardo
180
168
  resources.size > 1 ? "#{delimiter[0..0]}#{str_resources}#{delimiter[1..1]}" : str_resources
181
169
  end
182
170
 
171
+ #product under brand/category => "brand_category_product"
172
+ def underscore_resource_path(names=:all_singular)
173
+ case names
174
+ when :all_singular
175
+ resource_path.join('_')
176
+ #when :all_plural
177
+ #who needs?
178
+ when :parent_singular_resource_plural
179
+ resource_path.join('_').pluralize
180
+ else
181
+ "#{names.to_s}_not_supported"
182
+ end
183
+ end
184
+
183
185
  #product under brand/category => ["brand", "category", "product"] or ["@brand", "@category", "@product"]
184
- def resource_path(prefix_parent="", prefix_resource="", resource=nil)
186
+ def resource_path(prefix_parent="", prefix_resource="", resource=nil, prefix_namespace="")
185
187
  if resource
186
188
  prefix_parent = "#{resource}."
187
189
  else
188
190
  resource = singular_table_name
189
191
  end
190
192
 
193
+ prefix_namespace = ":" if prefix_namespace.empty? && prefix_parent.any?
194
+
191
195
  if nested?
192
- parent_resources(prefix_parent) << "#{prefix_resource}#{resource}"
196
+ (base_namespaces(prefix_namespace) + parent_resources(prefix_parent)) << "#{prefix_resource}#{resource}"
193
197
  else
194
- ["#{prefix_resource}#{resource}"]
198
+ base_namespaces(prefix_namespace) << "#{prefix_resource}#{resource}"
195
199
  end
196
200
  end
197
201
 
@@ -217,6 +221,7 @@ module Leonardo
217
221
 
218
222
  #product under brand/category => ["brand", "category"]
219
223
  def base_parent_resources
224
+ return [] unless options[:under].present?
220
225
  options[:under].split('/').map{|m| m.underscore}
221
226
  end
222
227
 
@@ -224,6 +229,25 @@ module Leonardo
224
229
  options[:under].present?
225
230
  end
226
231
 
232
+ ### NAMESPACE ###
233
+ def leospaced?
234
+ options[:leospace].present?
235
+ end
236
+
237
+ def base_namespaces(prefix="")
238
+ return [] unless options[:leospace].present?
239
+ options[:leospace].split('/').map{|m| "#{prefix}#{m.underscore}"}
240
+ end
241
+
242
+ def last_namespace(prefix="")
243
+ base_namespaces(prefix).last
244
+ end
245
+
246
+ def formatted_namespace_path(separator='/')
247
+ return "" unless leospaced?
248
+ "#{base_namespaces.join(separator)}#{separator}"
249
+ end
250
+
227
251
  module Test
228
252
  protected
229
253
  #Add parent(s) param(s) to request
@@ -8,7 +8,7 @@ module Leosca
8
8
  directory "erb/leosca", "lib/generators/erb/leosca" if options.erb?
9
9
  directory "rails/leosca", "lib/generators/rails/leosca"
10
10
  directory "rails/leosca_controller", "lib/generators/rails/leosca_controller"
11
- copy_file "base.rb", "lib/generators/base.rb"
11
+ copy_file "leonardo.rb", "lib/generators/leonardo.rb"
12
12
  end
13
13
  end
14
14
  end
@@ -1,44 +1,28 @@
1
- require 'rails/generators/rails/resource/resource_generator'
2
- require File.join(File.dirname(__FILE__), '../../base')
1
+ require 'rails/generators/rails/scaffold/scaffold_generator'
2
+ require File.join(File.dirname(__FILE__), '../../leonardo')
3
3
 
4
4
  module Rails
5
5
  module Generators
6
- class LeoscaGenerator < ResourceGenerator #metagenerator
6
+ class LeoscaGenerator < ::Rails::Generators::ScaffoldGenerator
7
7
  include ::Leonardo::Nested
8
8
  #puts 'rails:leosca'
9
9
 
10
- remove_hook_for :resource_controller
11
- remove_class_option :actions
12
-
13
- class_option :stylesheets, :type => :boolean, :default => false, :desc => "Generate Stylesheets"
14
- class_option :stylesheet_engine, :desc => "Engine for Stylesheets"
15
- class_option :under, :type => :string, :default => "", :banner => "brand/category", :desc => "Choose namespace(s) if resource must be nested"
10
+ class_option :under, :type => :string, :default => "", :banner => "brand/category", :desc => "To nest a resource under another(s)"
11
+ class_option :leospace, :type => :string, :default => "", :banner => ":admin", :desc => "To nest a resource under namespace(s)"
16
12
 
13
+ remove_hook_for :scaffold_controller
17
14
  hook_for :leosca_controller, :required => true
18
15
 
19
- hook_for :assets do |assets|
20
- invoke assets, [controller_name]
21
- end
22
-
23
- hook_for :stylesheet_engine do |stylesheet_engine|
24
- invoke stylesheet_engine, [controller_name] if options[:stylesheets] && behavior == :invoke
25
- end
26
-
27
- #Override resource def
16
+ #Override
28
17
  def add_resource_route
29
18
  return if options[:actions].present?
30
19
 
31
- if options[:under].present?
32
- route_resources = plural_parent_resources
33
- route_map = "resources"
34
- else
35
- route_resources = regular_class_path
36
- route_map = "namespace"
37
- end
38
-
39
- route_config = route_resources.collect{|m| "#{route_map} :#{m} do " }.join(" ")
20
+ route_config = ""
21
+ route_config << plural_parent_resources.map{|m| "resources :#{m} do " }.join(" ") if nested?
22
+ route_config << base_namespaces.map{|m| "namespace :#{m} do " }.join(" ") if leospaced?
23
+ route_config << regular_class_path.map{|m| "namespace :#{m} do " }.join(" ")
40
24
  route_config << "resources :#{file_name.pluralize}"
41
- route_config << " end" * route_resources.size
25
+ route_config << " end" * (regular_class_path.size + plural_parent_resources.size + base_namespaces.size)
42
26
  route route_config
43
27
  end
44
28
 
@@ -1,13 +1,12 @@
1
- require 'rails/generators/resource_helpers'
2
- require File.join(File.dirname(__FILE__), '../../base')
1
+ require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
2
+ require File.join(File.dirname(__FILE__), '../../leonardo')
3
3
 
4
4
  WINDOWS = (RUBY_PLATFORM =~ /dos|win32|cygwin/i) || (RUBY_PLATFORM =~ /(:?mswin|mingw)/)
5
5
  CRLF = WINDOWS ? "\r\n" : "\n"
6
6
 
7
7
  module Rails
8
8
  module Generators
9
- class LeoscaControllerGenerator < NamedBase
10
- include ResourceHelpers
9
+ class LeoscaControllerGenerator < ::Rails::Generators::ScaffoldControllerGenerator
11
10
  include ::Leonardo::Leosca
12
11
  include ::Leonardo::Nested
13
12
  include ::Leonardo::Nested::Test
@@ -18,25 +17,16 @@ module Rails
18
17
  class_option :seeds, :type => :boolean, :default => true, :desc => "Create seeds to run with rake db:seed"
19
18
  class_option :seeds_elements, :type => :string, :default => "30", :desc => "Choose seeds elements", :banner => "NUMBER"
20
19
  class_option :remote, :type => :boolean, :default => true, :desc => "Enable ajax. You can also do later set remote to true into index view."
21
- class_option :under, :type => :string, :default => "", :banner => "brand/category", :desc => "Nested resources"
22
-
23
-
24
- check_class_collision :suffix => "Controller"
25
-
26
- class_option :orm, :banner => "NAME", :type => :string, :required => true,
27
- :desc => "ORM to generate the controller for"
20
+ class_option :under, :type => :string, :default => "", :banner => "brand/category", :desc => "To nest a resource under another(s)"
21
+ class_option :leospace, :type => :string, :default => "", :banner => ":admin", :desc => "To nest a resource under namespace(s)"
28
22
 
23
+ #Override
29
24
  def create_controller_files
30
- template 'controller.rb', File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb")
25
+ template 'controller.rb', File.join('app/controllers', class_path, base_namespaces, "#{controller_file_name}_controller.rb")
31
26
  end
32
27
 
33
- hook_for :template_engine, :as => :leosca
34
- hook_for :test_framework, :as => :scaffold
35
-
36
- # Invoke the helper using the controller name (pluralized)
37
- hook_for :helper, :as => :scaffold do |invoked|
38
- invoke invoked, [ controller_name ]
39
- end
28
+ #Override
29
+ hook_for :template_engine, :test_framework, :as => :leosca
40
30
 
41
31
  def update_yaml_locales
42
32
  #Inject model and attributes name into yaml files for i18n
@@ -142,72 +132,6 @@ module Rails
142
132
  file = "spec/spec_helper.rb"
143
133
  return unless File.exists? file
144
134
 
145
- check_attr_to_have, check_attr_to_not_have = get_attr_to_match
146
-
147
- file = "spec/requests/#{plural_path_file_name}_spec.rb"
148
- remove = <<-FILE.gsub(/^ /, '')
149
- it "works! (now write some real specs)" do
150
- # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
151
- get #{plural_table_name}_path
152
- response.status.should be(200)
153
- end
154
- FILE
155
- gsub_file file, remove, ""
156
-
157
- inject_into_file file, :after => "describe \"GET /#{plural_table_name}\" do" do
158
- <<-FILE.gsub(/^ /, '')
159
-
160
- it "displays #{plural_table_name}" do
161
- #{singular_table_name} = Factory(:#{singular_table_name})
162
- visit #{list_resources_path_test}
163
- #{"login_view_as(:user_guest)" if authentication?}
164
- #save_and_open_page #uncomment to debug
165
- page.should #{check_attr_to_have}
166
- assert page.find("#tr\#{#{singular_table_name}.id}").visible?
167
- end
168
- FILE
169
- end
170
-
171
- inject_into_file file, :before => /^end/ do
172
- items = []
173
- attributes.each do |attribute|
174
- items << attribute_to_requests(attribute)
175
- end
176
- <<-FILE.gsub(/^ /, '')
177
-
178
- describe "POST /#{plural_table_name}" do
179
- it "creates a new #{singular_table_name}" do
180
- #{singular_table_name} = Factory.build(:#{singular_table_name})
181
- visit #{new_resource_path_test}
182
- #{"login_view_as(:user_admin)" if authentication?}
183
- #{items.join(CRLF)}
184
- click_button "Create \#{I18n.t('models.#{singular_table_name}')}"
185
- #save_and_open_page #uncomment to debug
186
- page.should have_content(I18n.t(:created, :model => I18n.t('models.#{singular_table_name}')))
187
- page.should #{check_attr_to_have}
188
- end
189
- end
190
-
191
- describe "Check ajax /#{plural_table_name}" do
192
- it "checks links on list page", :js => true do
193
- #{singular_table_name} = Factory(:#{singular_table_name})
194
- visit #{list_resources_path_test}
195
- #{"login_view_as(:user_manager)" if authentication?} #authentication
196
- page.find("div#list").should #{check_attr_to_have}
197
- click_link I18n.t(:show)
198
- page.find("div.ui-dialog").should #{check_attr_to_have} #checks if dialog is appeared
199
- click_link "close" #close dialog
200
- !page.find("div.ui-dialog").visible? #checks if dialog has been closed
201
- click_link I18n.t(:destroy) #check ajax destroy
202
- page.driver.browser.switch_to.alert.accept #confirms destroy
203
- #save_and_open_page #uncomment to debug
204
- page.find("div#list").should #{check_attr_to_not_have} #checks if content has been removed
205
- !page.find("#tr\#{#{singular_table_name}.id}").visible? #checks if row has been hidden
206
- end
207
- end
208
- FILE
209
- end
210
-
211
135
  file = "spec/factories.rb"
212
136
  inject_into_file file, :before => " ### Insert below here other your factories ###" do
213
137
  items = []
@@ -1,24 +1,16 @@
1
1
  <% module_namespacing do -%>
2
- class <%= controller_class_name %>Controller < ApplicationController
2
+ class <%= leospaced? ? (base_namespaces << controller_class_name).join('/').camelize : controller_class_name %>Controller < ApplicationController
3
3
  <%= "before_filter :authenticate_user!#{CRLF}" if authentication? -%>
4
4
  <%= "load_and_authorize_resource#{CRLF}" if authorization? -%>
5
5
  <%#= "before_filter :load_parents#{CRLF}" if nested? -%>
6
- before_filter :load_before
6
+ before_filter :load_before
7
7
 
8
8
  def load_before
9
9
  @remote = <%= options.remote? %>
10
- <% base_parent_resources.each do |parent| -%>
11
- @<%= parent %> = <%= parent.classify %>.find params[:<%= parent %>_id]
12
- <% end -%>
13
- end
14
-
15
- <% if nested? -%>
16
- def load_parents
17
10
  <% base_parent_resources.each do |parent| -%>
18
11
  @<%= parent %> = <%= parent.classify %>.find params[:<%= parent %>_id]
19
12
  <% end -%>
20
13
  end
21
- <% end -%>
22
14
 
23
15
  # GET <%= route_url %>
24
16
  # GET <%= route_url %>.json
@@ -0,0 +1,29 @@
1
+ require 'generators/rspec/integration/integration_generator'
2
+ require 'generators/leonardo' #leonardo base
3
+
4
+ module Rspec
5
+ module Generators
6
+ class LeointegrationGenerator < ::Rspec::Generators::IntegrationGenerator
7
+ include ::Leonardo::Leosca
8
+ include ::Leonardo::Nested
9
+ include ::Leonardo::Nested::Test
10
+
11
+ #puts 'Rspec:Generators:LeointegrationGenerator'
12
+ source_paths << File.expand_path('../templates', __FILE__)
13
+
14
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
15
+
16
+ class_option :remote, :type => :boolean, :default => true, :desc => "It checks ajax sections"
17
+ class_option :under, :type => :string, :default => "", :banner => "brand/category", :desc => "Nested resources"
18
+ class_option :leospace, :type => :string, :default => "", :banner => ":admin", :desc => "To nest a resource under namespace(s)"
19
+
20
+ def generate_request_spec
21
+ return unless options[:request_specs]
22
+
23
+ template 'request_spec.rb',
24
+ File.join('spec/requests', class_path, base_namespaces, "#{table_name}_spec.rb")
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+ <%-
3
+ items = []
4
+ attributes.each do |attribute|
5
+ items << " #{attribute_to_requests(attribute)}"
6
+ end
7
+ check_attr_to_have, check_attr_to_not_have = get_attr_to_match
8
+ -%>
9
+
10
+ describe "<%= class_name.pluralize %>" do
11
+ describe "GET /<%= table_name %>" do
12
+ it "displays <%= plural_table_name %>" do
13
+ <%= singular_table_name %> = Factory(:<%= singular_table_name %>)
14
+ visit <%= list_resources_path_test %>
15
+ <%= "login_view_as(:user_guest)" if authentication? -%>
16
+ #save_and_open_page #uncomment to debug
17
+ page.should <%= check_attr_to_have %>
18
+ assert page.find("#tr#{<%= singular_table_name %>.id}").visible?
19
+ end
20
+
21
+ describe "POST /<%= plural_table_name %>" do
22
+ it "creates a new <%= singular_table_name %>" do
23
+ <%= singular_table_name %> = Factory.build(:<%= singular_table_name %>)
24
+ visit <%= new_resource_path_test %>
25
+ <%= "login_view_as(:user_admin)" if authentication? %>
26
+ <%= items.join(CRLF) %>
27
+ click_button "Create #{I18n.t('models.<%= singular_table_name %>')}"
28
+ #save_and_open_page #uncomment to debug
29
+ page.should have_content(I18n.t(:created, :model => I18n.t('models.<%= singular_table_name %>')))
30
+ page.should <%= check_attr_to_have %>
31
+ end
32
+ end
33
+
34
+ describe "Check ajax /<%= plural_table_name %>" do
35
+ it "checks links on list page", :js => true do
36
+ <%= singular_table_name %> = Factory(:<%= singular_table_name %>)
37
+ visit <%= list_resources_path_test %>
38
+ <%= "login_view_as(:user_manager)" if authentication? %> #authentication
39
+ page.find("div#list").should <%= check_attr_to_have %>
40
+ click_link I18n.t(:show)
41
+ page.find("div.ui-dialog").should <%= check_attr_to_have %> #checks if dialog is appeared
42
+ click_link "close" #close dialog
43
+ !page.find("div.ui-dialog").visible? #checks if dialog has been closed
44
+ click_link I18n.t(:destroy) #check ajax destroy
45
+ page.driver.browser.switch_to.alert.accept #confirms destroy
46
+ #save_and_open_page #uncomment to debug
47
+ page.find("div#list").should <%= check_attr_to_not_have %> #checks if content has been removed
48
+ !page.find("#tr#{<%= singular_table_name %>.id}").visible? #checks if row has been hidden
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,45 @@
1
+ require 'generators/rspec/scaffold/scaffold_generator'
2
+ require 'generators/leonardo' #leonardo base
3
+
4
+ module Rspec
5
+ module Generators
6
+ class LeoscaGenerator < ::Rspec::Generators::ScaffoldGenerator
7
+ include ::Leonardo::Leosca
8
+ include ::Leonardo::Nested
9
+ include ::Leonardo::Nested::Test
10
+ #puts 'Rspec:Generators:LeoscaGenerator'
11
+ source_paths << File.expand_path('../templates', __FILE__)
12
+
13
+ #Leonardo options
14
+ class_option :remote, :type => :boolean, :default => true, :desc => "It checks ajax sections"
15
+ class_option :under, :type => :string, :default => "", :banner => "brand/category", :desc => "Nested resources"
16
+ class_option :leospace, :type => :string, :default => "", :banner => ":admin", :desc => "To nest a resource under namespace(s)"
17
+
18
+ #Override
19
+ def generate_controller_spec
20
+ return unless options[:controller_specs]
21
+
22
+ template 'controller_spec.rb',
23
+ File.join('spec/controllers', base_namespaces, controller_class_path, "#{controller_file_name}_controller_spec.rb")
24
+ end
25
+
26
+ #Override
27
+ def generate_routing_spec
28
+ return unless options[:routing_specs]
29
+
30
+ template 'routing_spec.rb',
31
+ File.join('spec/routing', base_namespaces, controller_class_path, "#{controller_file_name}_routing_spec.rb")
32
+ end
33
+
34
+ hook_for :integration_tool, :as => :leointegration
35
+
36
+ protected
37
+
38
+ #Override
39
+ def copy_view(view)
40
+ template "#{view}_spec.rb",
41
+ File.join("spec/views", base_namespaces, controller_file_path, "#{view}.html.#{options[:template_engine]}_spec.rb")
42
+ end
43
+ end
44
+ end
45
+ end
@@ -18,7 +18,7 @@ require 'spec_helper'
18
18
  # Message expectations are only used when there is no simpler way to specify
19
19
  # that an instance is receiving a specific message.
20
20
 
21
- describe <%= controller_class_name %>Controller do
21
+ describe <%= leospaced? ? (base_namespaces << controller_class_name).join('/').camelize : controller_class_name %>Controller do
22
22
 
23
23
  <% unless options[:singleton] -%>
24
24
  describe "GET index" do
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- describe "<%= ns_table_name %>/edit.html.<%= options[:template_engine] %>" do
4
+ describe "<%= formatted_namespace_path %><%= ns_table_name %>/edit.html.<%= options[:template_engine] %>" do
5
5
  before(:each) do
6
6
  @<%= ns_file_name %> = assign(:<%= ns_file_name %>, Factory(:<%= ns_file_name %>) )
7
7
  <%- base_parent_resources.each do |parent| -%>
@@ -15,6 +15,12 @@ def value_for_check(attribute)
15
15
  "#{name} => (r.#{attribute.name}.try(:name) || r.#{attribute.name}.try(:id))"
16
16
  when :boolean
17
17
  nil
18
+ when :decimal
19
+ "#{name} => number_to_currency(r.#{attribute.name})"
20
+ when :integer
21
+ "#{name} => number_with_delimiter(r.#{attribute.name})"
22
+ when :float
23
+ "#{name} => number_with_precision(r.#{attribute.name})"
18
24
  else
19
25
  "#{name} => r.#{attribute.name}"
20
26
  end
@@ -23,7 +29,7 @@ end
23
29
  %>
24
30
 
25
31
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
26
- describe "<%= ns_table_name %>/index.html.<%= options[:template_engine] %>" do
32
+ describe "<%= formatted_namespace_path %><%= ns_table_name %>/index.html.<%= options[:template_engine] %>" do
27
33
  before(:each) do
28
34
  <%- parents = []; str_parents_create = str_parents_where = "" -%>
29
35
  <%- base_parent_resources.each do |parent| -%>
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
4
- describe "<%= ns_table_name %>/new.html.<%= options[:template_engine] %>" do
4
+ describe "<%= formatted_namespace_path %><%= ns_table_name %>/new.html.<%= options[:template_engine] %>" do
5
5
  before(:each) do
6
6
  <%= "#{ns_file_name} = " if nested? %>assign(:<%= ns_file_name %>, Factory.build(:<%= ns_file_name %>) )
7
7
  <%- base_parent_resources.each do |parent| -%>
@@ -0,0 +1,37 @@
1
+ require "spec_helper"
2
+
3
+ describe <%= leospaced? ? (base_namespaces << controller_class_name).join('/').camelize : controller_class_name %>Controller do
4
+ describe "routing" do
5
+
6
+ <% unless options[:singleton] -%>
7
+ it "routes to #index" do
8
+ get("/<%= formatted_namespace_path %><%= plural_parent_resources.join('/1/') %><%= "/1/" if nested? %><%= ns_table_name %>").should route_to("<%= formatted_namespace_path %><%= ns_table_name %>#index"<%= nested_params_http_request("1") %>)
9
+ end
10
+
11
+ <% end -%>
12
+ it "routes to #new" do
13
+ get("/<%= formatted_namespace_path %><%= plural_parent_resources.join('/1/') %><%= "/1/" if nested? %><%= ns_table_name %>/new").should route_to("<%= formatted_namespace_path %><%= ns_table_name %>#new"<%= nested_params_http_request("1") %>)
14
+ end
15
+
16
+ it "routes to #show" do
17
+ get("/<%= formatted_namespace_path %><%= plural_parent_resources.join('/1/') %><%= "/1/" if nested? %><%= ns_table_name %>/1").should route_to("<%= formatted_namespace_path %><%= ns_table_name %>#show", :id => "1"<%= nested_params_http_request("1") %>)
18
+ end
19
+
20
+ it "routes to #edit" do
21
+ get("/<%= formatted_namespace_path %><%= plural_parent_resources.join('/1/') %><%= "/1/" if nested? %><%= ns_table_name %>/1/edit").should route_to("<%= formatted_namespace_path %><%= ns_table_name %>#edit", :id => "1"<%= nested_params_http_request("1") %>)
22
+ end
23
+
24
+ it "routes to #create" do
25
+ post("/<%= formatted_namespace_path %><%= plural_parent_resources.join('/1/') %><%= "/1/" if nested? %><%= ns_table_name %>").should route_to("<%= formatted_namespace_path %><%= ns_table_name %>#create"<%= nested_params_http_request("1") %>)
26
+ end
27
+
28
+ it "routes to #update" do
29
+ put("/<%= formatted_namespace_path %><%= plural_parent_resources.join('/1/') %><%= "/1/" if nested? %><%= ns_table_name %>/1").should route_to("<%= formatted_namespace_path %><%= ns_table_name %>#update", :id => "1"<%= nested_params_http_request("1") %>)
30
+ end
31
+
32
+ it "routes to #destroy" do
33
+ delete("/<%= formatted_namespace_path %><%= plural_parent_resources.join('/1/') %><%= "/1/" if nested? %><%= ns_table_name %>/1").should route_to("<%= formatted_namespace_path %><%= ns_table_name %>#destroy", :id => "1"<%= nested_params_http_request("1") %>)
34
+ end
35
+
36
+ end
37
+ end
@@ -14,7 +14,7 @@ end
14
14
  %>
15
15
 
16
16
  <% output_attributes = attributes.reject{|attribute| [:datetime, :timestamp, :time, :date].index(attribute.type) } -%>
17
- describe "<%= ns_table_name %>/show.html.<%= options[:template_engine] %>" do
17
+ describe "<%= formatted_namespace_path %><%= ns_table_name %>/show.html.<%= options[:template_engine] %>" do
18
18
  before(:each) do
19
19
  @<%= ns_file_name %> = assign(:<%= ns_file_name %>, Factory(:<%= ns_file_name %>) )
20
20
  <%- base_parent_resources.each do |parent| -%>
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leonardo
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 6
9
- - 1
10
- version: 1.6.1
8
+ - 7
9
+ - 0
10
+ version: 1.7.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Marco Mastrodonato
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-20 00:00:00 Z
18
+ date: 2011-09-28 00:00:00 Z
19
19
  dependencies: []
20
20
 
21
21
  description: A generator for creating Rails 3.1 applications ready to go. It generates the layout, the style, the internationalization and manage external gems for authentication, authorization and other. It also provides a customized scaffold to generates cool sites ajax ready in few minutes. If you find a bug please report to m.mastrodonato@gmail.com
@@ -27,7 +27,6 @@ extensions: []
27
27
  extra_rdoc_files: []
28
28
 
29
29
  files:
30
- - lib/generators/base.rb
31
30
  - lib/generators/erb/leosca/leosca_generator.rb
32
31
  - lib/generators/erb/leosca/templates/destroy.js.erb
33
32
  - lib/generators/erb/leosca/templates/edit.html.erb
@@ -62,13 +61,6 @@ files:
62
61
  - lib/generators/leolay/templates/config/locales/kaminari.it.yml
63
62
  - lib/generators/leolay/templates/config.rb
64
63
  - lib/generators/leolay/templates/layout_helper.rb
65
- - lib/generators/leolay/templates/lib/generators/rspec/scaffold/scaffold_generator.rb
66
- - lib/generators/leolay/templates/lib/templates/rspec/scaffold/controller_spec.rb
67
- - lib/generators/leolay/templates/lib/templates/rspec/scaffold/edit_spec.rb
68
- - lib/generators/leolay/templates/lib/templates/rspec/scaffold/index_spec.rb
69
- - lib/generators/leolay/templates/lib/templates/rspec/scaffold/new_spec.rb
70
- - lib/generators/leolay/templates/lib/templates/rspec/scaffold/routing_spec.rb
71
- - lib/generators/leolay/templates/lib/templates/rspec/scaffold/show_spec.rb
72
64
  - lib/generators/leolay/templates/lib/utility.rb
73
65
  - lib/generators/leolay/templates/spec/factories.rb
74
66
  - lib/generators/leolay/templates/spec/helpers/application_helpers_spec.rb
@@ -119,6 +111,7 @@ files:
119
111
  - lib/generators/leolay/templates/vendor/assets/stylesheets/formtastic/formtastic_changes.css
120
112
  - lib/generators/leolay/templates/vendor/assets/stylesheets/vendor.css
121
113
  - lib/generators/leolay/USAGE
114
+ - lib/generators/leonardo.rb
122
115
  - lib/generators/leosca/install_generator.rb
123
116
  - lib/generators/leosca/USAGE
124
117
  - lib/generators/rails/leosca/leosca_generator.rb
@@ -127,6 +120,15 @@ files:
127
120
  - lib/generators/rails/leosca_controller/leosca_controller_generator.rb
128
121
  - lib/generators/rails/leosca_controller/templates/controller.rb
129
122
  - lib/generators/rails/leosca_controller/USAGE
123
+ - lib/generators/rspec/leointegration/leointegration_generator.rb
124
+ - lib/generators/rspec/leointegration/templates/request_spec.rb
125
+ - lib/generators/rspec/leosca/leosca_generator.rb
126
+ - lib/generators/rspec/leosca/templates/controller_spec.rb
127
+ - lib/generators/rspec/leosca/templates/edit_spec.rb
128
+ - lib/generators/rspec/leosca/templates/index_spec.rb
129
+ - lib/generators/rspec/leosca/templates/new_spec.rb
130
+ - lib/generators/rspec/leosca/templates/routing_spec.rb
131
+ - lib/generators/rspec/leosca/templates/show_spec.rb
130
132
  - LICENSE
131
133
  - README.rdoc
132
134
  - CHANGELOG
@@ -1,173 +0,0 @@
1
- require 'generators/rspec'
2
- require 'rails/generators/resource_helpers'
3
- require 'generators/base' #leonardo base
4
-
5
- puts "*" * 22
6
- puts "Custom rspec generator"
7
- puts "*" * 22
8
-
9
- module Rspec
10
- module Generators
11
- class ScaffoldGenerator < Base
12
- include Rails::Generators::ResourceHelpers
13
- include ::Leonardo::Leosca
14
- include ::Leonardo::Nested
15
- include ::Leonardo::Nested::Test
16
- source_paths << File.expand_path("../../helper/templates", __FILE__)
17
- argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
18
-
19
- class_option :orm, :desc => "ORM used to generate the controller"
20
- class_option :template_engine, :desc => "Template engine to generate view files"
21
- class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller"
22
-
23
- class_option :controller_specs, :type => :boolean, :default => true, :desc => "Generate controller specs"
24
- class_option :view_specs, :type => :boolean, :default => true, :desc => "Generate view specs"
25
- #Leonardo includes custom views to support capybara
26
- #class_option :webrat, :type => :boolean, :default => false, :desc => "Use webrat methods/matchers"
27
- #class_option :webrat_matchers, :type => :boolean, :default => false, :desc => "Use webrat methods/matchers (deprecated - use --webrat)"
28
- class_option :helper_specs, :type => :boolean, :default => true, :desc => "Generate helper specs"
29
- class_option :routing_specs, :type => :boolean, :default => true, :desc => "Generate routing specs"
30
-
31
- #Leonardo options
32
- class_option :remote, :type => :boolean, :default => true, :desc => "It checks ajax sections"
33
- class_option :under, :type => :string, :default => "", :banner => "brand/category", :desc => "Nested resources"
34
-
35
- def generate_controller_spec
36
- return unless options[:controller_specs]
37
-
38
- template 'controller_spec.rb',
39
- File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
40
- end
41
-
42
- def generate_view_specs
43
- return unless options[:view_specs]
44
-
45
- copy_view :edit
46
- copy_view :index unless options[:singleton]
47
- copy_view :new
48
- copy_view :show
49
- end
50
-
51
- # Invoke the helper using the controller name (pluralized)
52
- hook_for :helper, :as => :scaffold do |invoked|
53
- invoke invoked, [ controller_name ]
54
- end
55
-
56
- def generate_routing_spec
57
- return unless options[:routing_specs]
58
-
59
- template 'routing_spec.rb',
60
- File.join('spec/routing', controller_class_path, "#{controller_file_name}_routing_spec.rb")
61
- end
62
-
63
- hook_for :integration_tool, :as => :integration
64
-
65
- protected
66
-
67
- def webrat?
68
- RSpec.deprecate("--webrat-matchers", "--webrat") if options[:webrat_matchers]
69
- options[:webrat] || options[:webrat_matchers]
70
- end
71
-
72
- def copy_view(view)
73
- template "#{view}_spec.rb",
74
- File.join("spec/views", controller_file_path, "#{view}.html.#{options[:template_engine]}_spec.rb")
75
- end
76
-
77
- def params
78
- "{'these' => 'params'}"
79
- end
80
-
81
- # support for namespaced-resources
82
- def ns_file_name
83
- if ARGV[0].match(/(\w+)\/(\w+)/)
84
- "#{$1.underscore}_#{$2.singularize.underscore}"
85
- else
86
- file_name
87
- end
88
- end
89
-
90
- # support for namespaced-resources
91
- def ns_table_name
92
- if ARGV[0].match(/(\w+)\/(\w+)/)
93
- "#{$1.underscore}/#{$2.tableize}"
94
- else
95
- table_name
96
- end
97
- end
98
-
99
- # Returns the name of the mock. For example, if the file name is user,
100
- # it returns mock_user.
101
- #
102
- # If a hash is given, it uses the hash key as the ORM method and the
103
- # value as response. So, for ActiveRecord and file name "User":
104
- #
105
- # mock_file_name(:save => true)
106
- # #=> mock_user(:save => true)
107
- #
108
- # If another ORM is being used and another method instead of save is
109
- # called, it will be the one used.
110
- #
111
- def mock_file_name(hash=nil)
112
- if hash
113
- method, and_return = hash.to_a.first
114
- method = orm_instance.send(method).split('.').last.gsub(/\(.*?\)/, '')
115
- "mock_#{ns_file_name}(:#{method} => #{and_return})"
116
- else
117
- "mock_#{ns_file_name}"
118
- end
119
- end
120
-
121
- # Receives the ORM chain and convert to expects. For ActiveRecord:
122
- #
123
- # should! orm_class.find(User, "37")
124
- # #=> User.should_receive(:find).with(37)
125
- #
126
- # For Datamapper:
127
- #
128
- # should! orm_class.find(User, "37")
129
- # #=> User.should_receive(:get).with(37)
130
- #
131
- def should_receive(chain)
132
- stub_or_should_chain(:should_receive, chain)
133
- end
134
-
135
- # Receives the ORM chain and convert to stub. For ActiveRecord:
136
- #
137
- # stub orm_class.find(User, "37")
138
- # #=> User.stub(:find).with(37)
139
- #
140
- # For Datamapper:
141
- #
142
- # stub orm_class.find(User, "37")
143
- # #=> User.stub(:get).with(37)
144
- #
145
- def stub(chain)
146
- stub_or_should_chain(:stub, chain)
147
- end
148
-
149
- def stub_or_should_chain(mode, chain)
150
- receiver, method = chain.split(".")
151
- method.gsub!(/\((.*?)\)/, '')
152
-
153
- response = "#{receiver}.#{mode}(:#{method})"
154
- response << ".with(#{$1})" unless $1.blank?
155
- response
156
- end
157
-
158
- def value_for(attribute)
159
- case attribute.type
160
- when :string
161
- "#{attribute.name.titleize}".inspect
162
- else
163
- attribute.default.inspect
164
- end
165
- end
166
-
167
- def banner
168
- self.class.banner
169
- end
170
-
171
- end
172
- end
173
- end
@@ -1,37 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe <%= controller_class_name %>Controller do
4
- describe "routing" do
5
-
6
- <% unless options[:singleton] -%>
7
- it "routes to #index" do
8
- get("/<%= plural_parent_resources.join('/1/') %><%= "/1/" if nested? %><%= ns_table_name %>").should route_to("<%= ns_table_name %>#index"<%= nested_params_http_request("1") %>)
9
- end
10
-
11
- <% end -%>
12
- it "routes to #new" do
13
- get("/<%= plural_parent_resources.join('/1/') %><%= "/1/" if nested? %><%= ns_table_name %>/new").should route_to("<%= ns_table_name %>#new"<%= nested_params_http_request("1") %>)
14
- end
15
-
16
- it "routes to #show" do
17
- get("/<%= plural_parent_resources.join('/1/') %><%= "/1/" if nested? %><%= ns_table_name %>/1").should route_to("<%= ns_table_name %>#show", :id => "1"<%= nested_params_http_request("1") %>)
18
- end
19
-
20
- it "routes to #edit" do
21
- get("/<%= plural_parent_resources.join('/1/') %><%= "/1/" if nested? %><%= ns_table_name %>/1/edit").should route_to("<%= ns_table_name %>#edit", :id => "1"<%= nested_params_http_request("1") %>)
22
- end
23
-
24
- it "routes to #create" do
25
- post("/<%= plural_parent_resources.join('/1/') %><%= "/1/" if nested? %><%= ns_table_name %>").should route_to("<%= ns_table_name %>#create"<%= nested_params_http_request("1") %>)
26
- end
27
-
28
- it "routes to #update" do
29
- put("/<%= plural_parent_resources.join('/1/') %><%= "/1/" if nested? %><%= ns_table_name %>/1").should route_to("<%= ns_table_name %>#update", :id => "1"<%= nested_params_http_request("1") %>)
30
- end
31
-
32
- it "routes to #destroy" do
33
- delete("/<%= plural_parent_resources.join('/1/') %><%= "/1/" if nested? %><%= ns_table_name %>/1").should route_to("<%= ns_table_name %>#destroy", :id => "1"<%= nested_params_http_request("1") %>)
34
- end
35
-
36
- end
37
- end