-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
scaffold_generator.rb
112 lines (94 loc) · 3.49 KB
/
scaffold_generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require 'generators/rspec'
require 'generators/resource_helpers'
module Rspec
module Generators
class ScaffoldGenerator < Base
include Rails::Generators::ResourceHelpers
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
class_option :orm, :desc => "ORM used to generate the controller"
class_option :template_engine, :desc => "Template engine to generate view files"
class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller"
class_option :views, :type => :boolean, :default => true
class_option :routes, :type => :boolean, :default => true
def copy_controller_files
template 'controller_spec.rb',
File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
end
def copy_view_files
return unless options[:views]
copy_view :edit
copy_view :index unless options[:singleton]
copy_view :new
copy_view :show
end
def copy_routing_files
return unless options[:routes]
template 'routing_spec.rb',
File.join('spec/routing', controller_class_path, "#{controller_file_name}_routing_spec.rb")
end
hook_for :integration_tool, :as => :integration
protected
def copy_view(view)
template "#{view}_spec.rb",
File.join("spec/views", controller_file_path, "#{view}.html.#{options[:template_engine]}_spec.rb")
end
def params
"{'these' => 'params'}"
end
# Returns the name of the mock. For example, if the file name is user,
# it returns mock_user.
#
# If a hash is given, it uses the hash key as the ORM method and the
# value as response. So, for ActiveRecord and file name "User":
#
# mock_file_name(:save => true)
# #=> mock_user(:save => true)
#
# If another ORM is being used and another method instead of save is
# called, it will be the one used.
#
def mock_file_name(hash=nil)
if hash
method, and_return = hash.to_a.first
method = orm_instance.send(method).split('.').last.gsub(/\(.*?\)/, '')
"mock_#{file_name}(:#{method} => #{and_return})"
else
"mock_#{file_name}"
end
end
# Receives the ORM chain and convert to expects. For ActiveRecord:
#
# should! orm_class.find(User, "37")
# #=> User.should_receive(:find).with(37)
#
# For Datamapper:
#
# should! orm_class.find(User, "37")
# #=> User.should_receive(:get).with(37)
#
def should!(chain)
stub_or_should_chain(:should_receive, chain)
end
# Receives the ORM chain and convert to stub. For ActiveRecord:
#
# stub! orm_class.find(User, "37")
# #=> User.stub!(:find).with(37)
#
# For Datamapper:
#
# stub! orm_class.find(User, "37")
# #=> User.stub!(:get).with(37)
#
def stub!(chain)
stub_or_should_chain(:stub!, chain)
end
def stub_or_should_chain(mode, chain)
receiver, method = chain.split(".")
method.gsub!(/\((.*?)\)/, '')
response = "#{receiver}.#{mode}(:#{method})"
response << ".with(#{$1})" unless $1.blank?
response
end
end
end
end