lazy-head-gen 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/Gemfile +49 -0
- data/Gemfile.lock +139 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lazy-head-gen.gemspec +138 -0
- data/lib/lazy-head-gen.rb +6 -0
- data/lib/lazy-head-gen/admin_controller_tests.rb +58 -0
- data/lib/lazy-head-gen/scaffold.rb +155 -0
- data/lib/lazy-head-gen/templates/admin_controller_test.rb.tt +197 -0
- data/lib/lazy-head-gen/templates/blueprints.rb.tt +32 -0
- data/lib/lazy-head-gen/templates/controller.rb.tt +33 -0
- data/lib/lazy-head-gen/templates/controller_test.rb.tt +95 -0
- data/lib/lazy-head-gen/templates/helper.rb.tt +7 -0
- data/lib/lazy-head-gen/templates/model.rb.tt +9 -0
- data/lib/lazy-head-gen/templates/model_test.rb.tt +15 -0
- data/lib/lazy-head-gen/templates/view.erb.tt +1 -0
- data/test/helper.rb +92 -0
- data/test/lazy-head-gen/test_admin_controller_tests.rb +47 -0
- data/test/lazy-head-gen/test_scaffold.rb +235 -0
- data/test/load_paths.rb +6 -0
- data/test/test_lazy-head-gen.rb +15 -0
- metadata +489 -0
@@ -0,0 +1,155 @@
|
|
1
|
+
module Padrino
|
2
|
+
module Generators
|
3
|
+
|
4
|
+
class Scaffold < Thor::Group
|
5
|
+
# register with Padrino
|
6
|
+
Padrino::Generators.add_generator(:scaffold, self)
|
7
|
+
|
8
|
+
# Define the source template root
|
9
|
+
def self.source_root; File.expand_path(File.dirname(__FILE__)); end
|
10
|
+
# Defines the banner for this CLI generator
|
11
|
+
def self.banner; "padrino-gen scaffold [name]"; end
|
12
|
+
|
13
|
+
# Include related modules
|
14
|
+
include Thor::Actions
|
15
|
+
include Padrino::Generators
|
16
|
+
include Padrino::Generators::Actions
|
17
|
+
include Padrino::Generators::Runner
|
18
|
+
include Padrino::Generators::Components::Actions
|
19
|
+
|
20
|
+
desc "Description:\n\n\tlazy-head-gen scaffold generates a new Padrino scaffold"
|
21
|
+
|
22
|
+
argument :name, :desc => "The name of your scaffold resource"
|
23
|
+
|
24
|
+
argument :properties, :desc => "The properties of the scaffold's model", :type => :array, :default => []
|
25
|
+
|
26
|
+
class_option :root, :aliases => '-r', :default => ".", :type => :string,
|
27
|
+
:desc => "The root destination"
|
28
|
+
|
29
|
+
class_option :app_path, :aliases => '-a', :default => "/app", :type => :string,
|
30
|
+
:desc => "The application destination path"
|
31
|
+
|
32
|
+
class_option :model_path, :aliases => '-m', :default => ".", :type => :string,
|
33
|
+
:desc => "The model destination path"
|
34
|
+
|
35
|
+
class_option :create_full_actions, :aliases => '-c', :default => false, :type => :boolean,
|
36
|
+
:desc => "Specify whether to generate basic (index and show) or full (index, show, new, create, edit, update and delete) actions"
|
37
|
+
|
38
|
+
class_option :skip_migration, :aliases => "-s", :default => false, :type => :boolean,
|
39
|
+
:desc => "Specify whether to skip generating a migration"
|
40
|
+
|
41
|
+
require_arguments!
|
42
|
+
|
43
|
+
def create_scaffold
|
44
|
+
self.destination_root = options[:root]
|
45
|
+
|
46
|
+
if in_app_root?
|
47
|
+
app = options[:app_path]
|
48
|
+
check_app_existence(app)
|
49
|
+
@app_name = fetch_app_name(app)
|
50
|
+
|
51
|
+
say "Generating a Padrino scaffold for '#{name}' in app '#{@app_name}'", :green
|
52
|
+
|
53
|
+
# Set variables
|
54
|
+
@pluralized = name.to_s.underscore.pluralize
|
55
|
+
@singular = name.to_s.underscore.singularize
|
56
|
+
@controller = @pluralized.camelize
|
57
|
+
@model = @singular.camelize
|
58
|
+
@properties = properties
|
59
|
+
@create_full = options[:create_full_actions]
|
60
|
+
|
61
|
+
# Create model
|
62
|
+
if invalids = invalid_fields(@properties)
|
63
|
+
say "Invalid property name:", :red
|
64
|
+
say " #{invalids.join(", ")}"
|
65
|
+
raise SystemExit
|
66
|
+
end
|
67
|
+
|
68
|
+
unless include_component_module_for(:orm)
|
69
|
+
say "You need an ORM adapter to create this scaffold's model", :red
|
70
|
+
return
|
71
|
+
end
|
72
|
+
|
73
|
+
# TODO: if model path does not equal "." should we check app exists?
|
74
|
+
# model_path = options[:model_path]
|
75
|
+
model_path = "."
|
76
|
+
|
77
|
+
template "templates/model.rb.tt", destination_root(model_path, "models", "#{@singular}.rb")
|
78
|
+
template "templates/model_test.rb.tt", destination_root("test", model_path, "models", "#{@singular}_test.rb")
|
79
|
+
|
80
|
+
# Create controller
|
81
|
+
template "templates/controller.rb.tt", destination_root(app, "controllers", "#{@pluralized}.rb")
|
82
|
+
template "templates/controller_test.rb.tt", destination_root("test", app, "controllers", "#{@pluralized}_controller_test.rb")
|
83
|
+
template "templates/helper.rb.tt", destination_root(app, "helpers", "#{@pluralized}_helper.rb")
|
84
|
+
|
85
|
+
# Create views
|
86
|
+
views = ["index", "show"]
|
87
|
+
if @create_full
|
88
|
+
views << "new" << "edit"
|
89
|
+
end
|
90
|
+
|
91
|
+
views.each do |view|
|
92
|
+
@view = view
|
93
|
+
template "templates/view.erb.tt", destination_root(app, "views", "#{@pluralized}", "#{@view}.erb")
|
94
|
+
end
|
95
|
+
|
96
|
+
# Create a migration unless skipped
|
97
|
+
create_model_migration("create_#{@pluralized}", name, properties) unless options[:skip_migration]
|
98
|
+
|
99
|
+
# Check if blueprints file exists and copy into place if not
|
100
|
+
if !File.exist?(destination_root("test", "blueprints.rb"))
|
101
|
+
template "templates/blueprints.rb.tt", destination_root("test", "blueprints.rb")
|
102
|
+
end
|
103
|
+
|
104
|
+
# Insert into text_config.rb require blueprints.rb
|
105
|
+
require_string = "\nrequire File.expand_path(File.dirname(__FILE__) + \"/blueprints.rb\")"
|
106
|
+
test_config_contents = File.read(destination_root("test", "test_config.rb"))
|
107
|
+
insert_into_file destination_root("test", "test_config.rb"), require_string, :after => "require File.expand_path('../../config/boot', __FILE__)"
|
108
|
+
|
109
|
+
# Check if a blueprint for the model has already been created as
|
110
|
+
# we don't want to wipe out an existing one
|
111
|
+
blueprints_contents = File.read(destination_root("test", "blueprints.rb"))
|
112
|
+
|
113
|
+
if !blueprints_contents.match("#{@model}.blueprint do")
|
114
|
+
# build a string of properties to insert into the blueprint entry
|
115
|
+
props = ""
|
116
|
+
@properties.each do |property|
|
117
|
+
values = property.split(":")
|
118
|
+
faker = ""
|
119
|
+
case values.last
|
120
|
+
when "boolean"
|
121
|
+
faker += "true"
|
122
|
+
when "datetime"
|
123
|
+
faker += "DateTime.now"
|
124
|
+
when "integer"
|
125
|
+
faker += "1 + rand(100)"
|
126
|
+
else
|
127
|
+
faker += "Faker::Lorem.sentence"
|
128
|
+
end
|
129
|
+
props += "\t#{values.first} { #{faker} }"
|
130
|
+
props += "\n" unless property == @properties.last
|
131
|
+
end
|
132
|
+
|
133
|
+
# Format the blueprint entry
|
134
|
+
model_blueprint = <<-MODEL
|
135
|
+
#{@model}.blueprint do
|
136
|
+
#{props}
|
137
|
+
end
|
138
|
+
|
139
|
+
MODEL
|
140
|
+
|
141
|
+
# Insert into blueprints.rb
|
142
|
+
insert_into_file destination_root("test", "blueprints.rb"), model_blueprint, :before => "# END blueprints"
|
143
|
+
else
|
144
|
+
say "Blueprints entry already exists for '#{@singular}'", :yellow
|
145
|
+
end
|
146
|
+
|
147
|
+
say "Scaffold generation for '#{name}' in app '#{app}' completed", :green
|
148
|
+
else
|
149
|
+
say "You are not at the root of a Padrino application! (config/boot.rb not found)", :red
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
end # Scaffold
|
154
|
+
end # Generators
|
155
|
+
end # Padrino
|
@@ -0,0 +1,197 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../test_config.rb')
|
2
|
+
|
3
|
+
describe "Admin::<%= @controller if @controller %>Controller" do
|
4
|
+
|
5
|
+
# START NOT LOGGED IN
|
6
|
+
describe "when not logged in" do
|
7
|
+
|
8
|
+
# START GET index
|
9
|
+
describe "GET index" do
|
10
|
+
before do
|
11
|
+
get "/admin/<%= @pluralized %>"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should not display the <%= @pluralized %> index page" do
|
15
|
+
assert_not_logged_in
|
16
|
+
end
|
17
|
+
end
|
18
|
+
# END GET index
|
19
|
+
|
20
|
+
# START GET new
|
21
|
+
describe "GET new" do
|
22
|
+
before do
|
23
|
+
get "/admin/<%= @pluralized %>/new"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should not display the <%= @pluralized %> new page" do
|
27
|
+
assert_not_logged_in
|
28
|
+
end
|
29
|
+
end
|
30
|
+
# END GET new
|
31
|
+
|
32
|
+
# START POST create
|
33
|
+
describe "Post create" do
|
34
|
+
before do
|
35
|
+
post "/admin/<%= @pluralized %>/create", :<%= @singular %> => <%= @model %>.plan
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should not return the created text" do
|
39
|
+
assert_not_logged_in
|
40
|
+
end
|
41
|
+
end
|
42
|
+
# END POST create
|
43
|
+
|
44
|
+
# START GET edit
|
45
|
+
describe "GET edit" do
|
46
|
+
before do
|
47
|
+
@<%= @singular %> = <%= @model %>.make
|
48
|
+
get "/admin/<%= @pluralized %>/edit/#{@<%= @singular %>.to_param}"
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should not display the <%= @pluralized %> edit page" do
|
52
|
+
assert_not_logged_in
|
53
|
+
end
|
54
|
+
end
|
55
|
+
# END GET edit
|
56
|
+
|
57
|
+
# START PUT update
|
58
|
+
describe "PUT update" do
|
59
|
+
before do
|
60
|
+
@<%= @singular %> = <%= @model %>.make
|
61
|
+
put "/admin/<%= @pluralized %>/update/#{@<%= @singular %>.to_param}", :<%= @singular %> => <%= @model %>.plan
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should not return the updated text" do
|
65
|
+
assert_not_logged_in
|
66
|
+
end
|
67
|
+
end
|
68
|
+
# END PUT update
|
69
|
+
|
70
|
+
# START DELETE destroy
|
71
|
+
describe "on DELETE destroy" do
|
72
|
+
before do
|
73
|
+
@<%= @singular %> = <%= @model %>.make
|
74
|
+
get "/admin/<%= @pluralized %>/destroy/#{@<%= @singular %>.to_param}"
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should not return the destroyed text" do
|
78
|
+
assert_destroy_not_logged_in
|
79
|
+
end
|
80
|
+
end
|
81
|
+
# END DELETE destroy
|
82
|
+
|
83
|
+
end
|
84
|
+
# END NOT LOGGED IN
|
85
|
+
|
86
|
+
# START LOGGED IN
|
87
|
+
describe "when logged in" do
|
88
|
+
|
89
|
+
before do
|
90
|
+
@account = Factory.make_admin
|
91
|
+
login_as_admin(@account)
|
92
|
+
end
|
93
|
+
|
94
|
+
# START GET index
|
95
|
+
describe "GET index" do
|
96
|
+
before do
|
97
|
+
get "/admin/<%= @pluralized %>"
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should display the <%= @pluralized %> index page" do
|
101
|
+
assert_logged_in
|
102
|
+
assert_equal "/admin/<%= @pluralized %>", path
|
103
|
+
end
|
104
|
+
end
|
105
|
+
# END GET index
|
106
|
+
|
107
|
+
# START GET new
|
108
|
+
describe "GET new" do
|
109
|
+
before do
|
110
|
+
get "/admin/<%= @pluralized %>/new"
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should display the <%= @pluralized %> new page" do
|
114
|
+
assert_logged_in
|
115
|
+
assert_equal "/admin/<%= @pluralized %>/new", path
|
116
|
+
end
|
117
|
+
end
|
118
|
+
# END GET new
|
119
|
+
|
120
|
+
# START POST create
|
121
|
+
describe "POST create" do
|
122
|
+
before do
|
123
|
+
@<%= @singular %>_count = <%= @model %>.count
|
124
|
+
post "/admin/<%= @pluralized %>/create", :<%= @singular %> => <%= @model %>.plan
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should create a <%= @singular %>" do
|
128
|
+
assert_equal 302, status
|
129
|
+
assert location.include?("/admin/<%= @pluralized %>/edit/")
|
130
|
+
assert_equal @<%= @singular %>_count + 1, <%= @model %>.count
|
131
|
+
end
|
132
|
+
end
|
133
|
+
# END POST create
|
134
|
+
|
135
|
+
# START GET edit
|
136
|
+
describe "GET edit" do
|
137
|
+
before do
|
138
|
+
@<%= @singular %> = <%= @model %>.make
|
139
|
+
get "/admin/<%= @pluralized %>/edit/#{@<%= @singular %>.to_param}"
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should display the <%= @pluralized %> edit page" do
|
143
|
+
assert_logged_in
|
144
|
+
assert_equal "/admin/<%= @pluralized %>/edit/#{@<%= @singular %>.to_param}", path
|
145
|
+
end
|
146
|
+
end
|
147
|
+
# END GET edit
|
148
|
+
|
149
|
+
# START PUT update
|
150
|
+
describe "PUT update" do
|
151
|
+
before do
|
152
|
+
@<%= @singular %> = <%= @model %>.make
|
153
|
+
@<%= @singular %>_count = <%= @model %>.count
|
154
|
+
@<%= @singular %>_hash = <%= @model %>.plan
|
155
|
+
|
156
|
+
key_and_value = get_key_and_value(@product)
|
157
|
+
if key_and_value[0] and key_and_value[1]
|
158
|
+
@key = key_and_value[0]
|
159
|
+
@value = key_and_value[1]
|
160
|
+
@<%= @singular %>_hash = <%= @model %>.plan(@key => @value)
|
161
|
+
end
|
162
|
+
|
163
|
+
put "/admin/<%= @pluralized %>/update/#{@<%= @singular %>.to_param}", :<%= @singular %> => @<%= @singular %>_hash
|
164
|
+
|
165
|
+
@updated_<%= @singular %> = <%= @model %>.find(@<%= @singular %>.to_param)
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should update a <%= @singular %>" do
|
169
|
+
assert_equal status, 302
|
170
|
+
assert location.include?("/admin/<%= @pluralized %>/edit/#{@<%= @singular %>.to_param}")
|
171
|
+
assert_equal @updated_<%= @singular %>.id, @<%= @singular %>.id
|
172
|
+
assert_equal @<%= @singular %>_count, <%= @model %>.count
|
173
|
+
# Only assert if the <%= @singular %> has a key other than id or timestamps
|
174
|
+
assert_equal @updated_<%= @singular %>.attributes[@key], @value if @key and @value
|
175
|
+
end
|
176
|
+
end
|
177
|
+
# END PUT update
|
178
|
+
|
179
|
+
# START DELETE destroy
|
180
|
+
describe "DELETE destroy" do
|
181
|
+
before do
|
182
|
+
@<%= @singular %> = <%= @model %>.make
|
183
|
+
@<%= @singular %>_count = <%= @model %>.count
|
184
|
+
delete "/admin/<%= @pluralized %>/destroy/#{@<%= @singular %>.to_param}"
|
185
|
+
end
|
186
|
+
|
187
|
+
it "should destroy a <%= @singular %>" do
|
188
|
+
assert_equal 302, status
|
189
|
+
assert_equal @<%= @singular %>_count - 1, <%= @model %>.count
|
190
|
+
end
|
191
|
+
end
|
192
|
+
# END DELETE destroy
|
193
|
+
|
194
|
+
end
|
195
|
+
# END LOGGED IN
|
196
|
+
|
197
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'machinist/active_record'
|
2
|
+
require 'faker'
|
3
|
+
|
4
|
+
# START blueprints
|
5
|
+
|
6
|
+
Account.blueprint do
|
7
|
+
name { Faker::Name.first_name }
|
8
|
+
surname { Faker::Name.last_name }
|
9
|
+
email { Faker::Internet.email }
|
10
|
+
password { "password" }
|
11
|
+
password_confirmation { "password" }
|
12
|
+
role { "admin" }
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
# END blueprints
|
17
|
+
|
18
|
+
# A factory which we can use to build objects which are a bit more complex or
|
19
|
+
# which require special setup which can't be done by Machinist without a bit of
|
20
|
+
# help.
|
21
|
+
#
|
22
|
+
module Factory
|
23
|
+
class << self
|
24
|
+
|
25
|
+
def make_admin
|
26
|
+
account = Account.make(:admin)
|
27
|
+
account.save!
|
28
|
+
account
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
<%= @app_name %>.controllers <%= ":#{@pluralized}" if @pluralized %> do
|
2
|
+
|
3
|
+
get :index do
|
4
|
+
render "<%= @pluralized if @pluralized %>/index"
|
5
|
+
end
|
6
|
+
|
7
|
+
get :show, :with => :id do
|
8
|
+
render "<%= @pluralized if @pluralized %>/show"
|
9
|
+
end
|
10
|
+
|
11
|
+
<% if @create_full -%>
|
12
|
+
get :new do
|
13
|
+
render "<%= @pluralized if @pluralized %>/new"
|
14
|
+
end
|
15
|
+
|
16
|
+
post :create do
|
17
|
+
"This is the create action"
|
18
|
+
end
|
19
|
+
|
20
|
+
get :edit, :with => :id do
|
21
|
+
render "<%= @pluralized if @pluralized %>/edit"
|
22
|
+
end
|
23
|
+
|
24
|
+
put :update, :with => :id do
|
25
|
+
"This is the update action"
|
26
|
+
end
|
27
|
+
|
28
|
+
delete :destroy, :with => :id do
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
<% end -%>
|
33
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../test_config.rb')
|
2
|
+
|
3
|
+
describe "<%= @controller if @controller %>Controller" do
|
4
|
+
|
5
|
+
# START GET index
|
6
|
+
describe "GET index" do
|
7
|
+
before do
|
8
|
+
get "/<%= @pluralized %>"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should display the <%= @pluralized %> index page" do
|
12
|
+
assert_match "This is the index page", body
|
13
|
+
end
|
14
|
+
end
|
15
|
+
# END GET index
|
16
|
+
|
17
|
+
# START GET show
|
18
|
+
describe "GET show" do
|
19
|
+
before do
|
20
|
+
@<%= @singular %> = <%= @model %>.make
|
21
|
+
get "/<%= @pluralized %>/show/#{@<%= @singular %>.to_param}"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should display the <%= @pluralized %> show page" do
|
25
|
+
assert_match "This is the show page", body
|
26
|
+
end
|
27
|
+
end
|
28
|
+
# END GET show
|
29
|
+
|
30
|
+
<% if @create_full -%>
|
31
|
+
# START GET new
|
32
|
+
describe "GET new" do
|
33
|
+
before do
|
34
|
+
get "/<%= @pluralized %>/new"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should display the <%= @pluralized %> new page" do
|
38
|
+
assert_match "This is the new page", body
|
39
|
+
end
|
40
|
+
end
|
41
|
+
# END GET new
|
42
|
+
|
43
|
+
# START POST create
|
44
|
+
describe "Post create" do
|
45
|
+
before do
|
46
|
+
post "/<%= @pluralized %>/create", {}
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should return the create text" do
|
50
|
+
assert_match "This is the create action", last_response.body
|
51
|
+
end
|
52
|
+
end
|
53
|
+
# END POST create
|
54
|
+
|
55
|
+
# START GET edit
|
56
|
+
describe "GET edit" do
|
57
|
+
before do
|
58
|
+
@<%= @singular %> = <%= @model %>.make
|
59
|
+
get "/<%= @pluralized %>/edit/#{@<%= @singular %>.to_param}"
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should display the <%= @pluralized %> edit page" do
|
63
|
+
assert_match "This is the edit page", body
|
64
|
+
end
|
65
|
+
end
|
66
|
+
# END GET edit
|
67
|
+
|
68
|
+
# START PUT update
|
69
|
+
describe "PUT update" do
|
70
|
+
before do
|
71
|
+
@<%= @singular %> = <%= @singular %>.make
|
72
|
+
put "/<%= @pluralized %>/update/#{@<%= @singular %>.to_param}", {}
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should return the update text" do
|
76
|
+
assert_match "This is the update action", body
|
77
|
+
end
|
78
|
+
end
|
79
|
+
# END PUT update
|
80
|
+
|
81
|
+
# START DELETE destroy
|
82
|
+
describe "on DELETE destroy" do
|
83
|
+
before do
|
84
|
+
@<%= @singular %> = <%= @singular %>.make
|
85
|
+
get "/<%= @pluralized %>/destroy/#{@<%= @singular %>.to_param}"
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should return a status of 405" do
|
89
|
+
assert_equal 405, status
|
90
|
+
end
|
91
|
+
end
|
92
|
+
# END DELETE destroy
|
93
|
+
|
94
|
+
<% end -%>
|
95
|
+
end
|