llab-generators 0.1

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.
Files changed (59) hide show
  1. data/CHANGELOG +1 -0
  2. data/LICENSE +21 -0
  3. data/Manifest +57 -0
  4. data/README +8 -0
  5. data/Rakefile +2 -0
  6. data/lib/generators/scaffold/USAGE +13 -0
  7. data/lib/generators/scaffold/scaffold_generator.rb +184 -0
  8. data/lib/generators/scaffold/templates/actions/create.rb +8 -0
  9. data/lib/generators/scaffold/templates/actions/destroy.rb +5 -0
  10. data/lib/generators/scaffold/templates/actions/edit.rb +3 -0
  11. data/lib/generators/scaffold/templates/actions/index.rb +3 -0
  12. data/lib/generators/scaffold/templates/actions/new.rb +3 -0
  13. data/lib/generators/scaffold/templates/actions/show.rb +3 -0
  14. data/lib/generators/scaffold/templates/actions/update.rb +8 -0
  15. data/lib/generators/scaffold/templates/controller.rb +3 -0
  16. data/lib/generators/scaffold/templates/helper.rb +2 -0
  17. data/lib/generators/scaffold/templates/migration.rb +16 -0
  18. data/lib/generators/scaffold/templates/model.rb +3 -0
  19. data/lib/generators/scaffold/templates/views/haml/_form.html.haml +12 -0
  20. data/lib/generators/scaffold/templates/views/haml/edit.html.haml +11 -0
  21. data/lib/generators/scaffold/templates/views/haml/index.html.haml +26 -0
  22. data/lib/generators/scaffold/templates/views/haml/new.html.haml +8 -0
  23. data/lib/generators/scaffold/templates/views/haml/show.html.haml +15 -0
  24. data/lib/generators/setup/USAGE +14 -0
  25. data/lib/generators/setup/setup_generator.rb +91 -0
  26. data/lib/generators/setup/templates/css/_constants.sass +17 -0
  27. data/lib/generators/setup/templates/css/_mixins.sass +156 -0
  28. data/lib/generators/setup/templates/css/blueprint.css +254 -0
  29. data/lib/generators/setup/templates/css/ie.css +60 -0
  30. data/lib/generators/setup/templates/css/print.css +61 -0
  31. data/lib/generators/setup/templates/css/screen.sass +289 -0
  32. data/lib/generators/setup/templates/devise/authorization_rules.rb +23 -0
  33. data/lib/generators/setup/templates/devise/devise_migration.rb +26 -0
  34. data/lib/generators/setup/templates/devise/devise_user.rb +30 -0
  35. data/lib/generators/setup/templates/devise/users_controller.rb +52 -0
  36. data/lib/generators/setup/templates/devise/views/_form.html.haml +29 -0
  37. data/lib/generators/setup/templates/devise/views/change_password.haml +15 -0
  38. data/lib/generators/setup/templates/devise/views/edit.html.haml +7 -0
  39. data/lib/generators/setup/templates/devise/views/forgot_password.haml +10 -0
  40. data/lib/generators/setup/templates/devise/views/index.html.haml +28 -0
  41. data/lib/generators/setup/templates/devise/views/login.haml +18 -0
  42. data/lib/generators/setup/templates/devise/views/new.html.haml +6 -0
  43. data/lib/generators/setup/templates/devise/views/show.html.haml +11 -0
  44. data/lib/generators/setup/templates/images/alert.gif +0 -0
  45. data/lib/generators/setup/templates/images/bullet_black.gif +0 -0
  46. data/lib/generators/setup/templates/images/edit.gif +0 -0
  47. data/lib/generators/setup/templates/images/error.gif +0 -0
  48. data/lib/generators/setup/templates/images/help_triangle.gif +0 -0
  49. data/lib/generators/setup/templates/images/logo_learninglab.gif +0 -0
  50. data/lib/generators/setup/templates/images/navbar_bkg.gif +0 -0
  51. data/lib/generators/setup/templates/images/new.gif +0 -0
  52. data/lib/generators/setup/templates/images/notice.gif +0 -0
  53. data/lib/generators/setup/templates/info_controller.rb +4 -0
  54. data/lib/generators/setup/templates/layout.erb +62 -0
  55. data/lib/generators/setup/templates/layout_helper.rb +20 -0
  56. data/lib/generators/setup/templates/root_index.erb +4 -0
  57. data/lib/generators/setup/templates/sass_config.rb +2 -0
  58. data/llab-generators.gemspec +30 -0
  59. metadata +183 -0
data/CHANGELOG ADDED
@@ -0,0 +1 @@
1
+ v0.1 initial submission
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2010 Luca Tironi - Luca Ongaro - PrimeGap
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/Manifest ADDED
@@ -0,0 +1,57 @@
1
+ CHANGELOG
2
+ LICENSE
3
+ README
4
+ Rakefile
5
+ lib/generators/scaffold/USAGE
6
+ lib/generators/scaffold/scaffold_generator.rb
7
+ lib/generators/scaffold/templates/actions/create.rb
8
+ lib/generators/scaffold/templates/actions/destroy.rb
9
+ lib/generators/scaffold/templates/actions/edit.rb
10
+ lib/generators/scaffold/templates/actions/index.rb
11
+ lib/generators/scaffold/templates/actions/new.rb
12
+ lib/generators/scaffold/templates/actions/show.rb
13
+ lib/generators/scaffold/templates/actions/update.rb
14
+ lib/generators/scaffold/templates/controller.rb
15
+ lib/generators/scaffold/templates/helper.rb
16
+ lib/generators/scaffold/templates/migration.rb
17
+ lib/generators/scaffold/templates/model.rb
18
+ lib/generators/scaffold/templates/views/haml/_form.html.haml
19
+ lib/generators/scaffold/templates/views/haml/edit.html.haml
20
+ lib/generators/scaffold/templates/views/haml/index.html.haml
21
+ lib/generators/scaffold/templates/views/haml/new.html.haml
22
+ lib/generators/scaffold/templates/views/haml/show.html.haml
23
+ lib/generators/setup/USAGE
24
+ lib/generators/setup/setup_generator.rb
25
+ lib/generators/setup/templates/css/_constants.sass
26
+ lib/generators/setup/templates/css/_mixins.sass
27
+ lib/generators/setup/templates/css/blueprint.css
28
+ lib/generators/setup/templates/css/ie.css
29
+ lib/generators/setup/templates/css/print.css
30
+ lib/generators/setup/templates/css/screen.sass
31
+ lib/generators/setup/templates/devise/authorization_rules.rb
32
+ lib/generators/setup/templates/devise/devise_migration.rb
33
+ lib/generators/setup/templates/devise/devise_user.rb
34
+ lib/generators/setup/templates/devise/users_controller.rb
35
+ lib/generators/setup/templates/devise/views/_form.html.haml
36
+ lib/generators/setup/templates/devise/views/change_password.haml
37
+ lib/generators/setup/templates/devise/views/edit.html.haml
38
+ lib/generators/setup/templates/devise/views/forgot_password.haml
39
+ lib/generators/setup/templates/devise/views/index.html.haml
40
+ lib/generators/setup/templates/devise/views/login.haml
41
+ lib/generators/setup/templates/devise/views/new.html.haml
42
+ lib/generators/setup/templates/devise/views/show.html.haml
43
+ lib/generators/setup/templates/images/alert.gif
44
+ lib/generators/setup/templates/images/bullet_black.gif
45
+ lib/generators/setup/templates/images/edit.gif
46
+ lib/generators/setup/templates/images/error.gif
47
+ lib/generators/setup/templates/images/help_triangle.gif
48
+ lib/generators/setup/templates/images/logo_learninglab.gif
49
+ lib/generators/setup/templates/images/navbar_bkg.gif
50
+ lib/generators/setup/templates/images/new.gif
51
+ lib/generators/setup/templates/images/notice.gif
52
+ lib/generators/setup/templates/info_controller.rb
53
+ lib/generators/setup/templates/layout.erb
54
+ lib/generators/setup/templates/layout_helper.rb
55
+ lib/generators/setup/templates/root_index.erb
56
+ lib/generators/setup/templates/sass_config.rb
57
+ Manifest
data/README ADDED
@@ -0,0 +1,8 @@
1
+ Learning Lab Generators
2
+
3
+ Inspired by Nifty Generators by Ryan Bates
4
+ Updated with Thor
5
+
6
+ Setup: "rails generate setup 'App Name'" will generate the initial setup of a common application, provided with layout, styles, user authentication and authorization.
7
+
8
+ Scaffold: "rails generate scaffold Thing" will generate a scaffolding for a Thing model.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'echoe'
2
+ Echoe.new('llab-generators')
@@ -0,0 +1,13 @@
1
+ Description:
2
+ Scaffold generator.
3
+
4
+ Example:
5
+ rails generate scaffold Thing name:string content:text new show destroy
6
+
7
+ This will create:
8
+ app/models/thing.rb
9
+ app/controller/things_controller.rb
10
+ app/views/things/new.html.haml
11
+ app/views/things/show.html.haml
12
+ app/views/things/_form.html.haml
13
+ db/migrate/create_things.rb
@@ -0,0 +1,184 @@
1
+ require 'rails/generators/migration'
2
+ require 'rails/generators/generated_attribute'
3
+
4
+ class ScaffoldGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+ no_tasks { attr_accessor :model_name, :model_attributes, :controller_actions }
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+ argument :model_name, :type => :string, :required => true, :banner => 'ModelName'
10
+ argument :args_for_c_m, :type => :array, :default => [], :banner => 'controller_actions and model:attributes'
11
+
12
+ class_option :skip_model, :desc => 'Don\'t generate a model or migration file.', :type => :boolean
13
+ class_option :skip_migration, :desc => 'Dont generate migration file for model.', :type => :boolean
14
+ class_option :skip_timestamps, :desc => 'Don\'t add timestamps to migration file.', :type => :boolean
15
+ class_option :skip_controller, :desc => 'Don\'t generate controller, helper, or views.', :type => :boolean
16
+ class_option :haml, :desc => 'Generate HAML views instead of ERB.', :type => :boolean, :default => true
17
+
18
+ def initialize(*args, &block)
19
+ super
20
+
21
+ @controller_actions = []
22
+ @model_attributes = []
23
+ @skip_model = options.skip_model?
24
+
25
+ args_for_c_m.each do |arg|
26
+ if arg.include?(':')
27
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
28
+ else
29
+ @controller_actions << arg
30
+ @controller_actions << 'create' if arg == 'new'
31
+ @controller_actions << 'update' if arg == 'edit'
32
+ end
33
+ end
34
+
35
+ @controller_actions.uniq!
36
+ @model_attributes.uniq!
37
+
38
+ if @controller_actions.empty?
39
+ @controller_actions = all_actions
40
+ end
41
+
42
+ if @model_attributes.empty?
43
+ @skip_model = true # skip model if no attributes
44
+ if model_exists?
45
+ model_columns_for_attributes.each do |column|
46
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
47
+ end
48
+ else
49
+ @model_attributes << Rails::Generators::GeneratedAttribute.new('name', 'string')
50
+ end
51
+ end
52
+ end
53
+
54
+ def create_model
55
+ unless @skip_model
56
+ template "model.rb", "app/models/#{singular_name}.rb"
57
+ end
58
+ end
59
+
60
+ def create_migration
61
+ unless @skip_model || options.skip_migration?
62
+ migration_template "migration.rb", "db/migrate/create_#{plural_name}.rb"
63
+ end
64
+ end
65
+
66
+ def create_controller
67
+ unless options.skip_controller?
68
+ template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
69
+
70
+ template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"
71
+
72
+ controller_actions.each do |action|
73
+ if %w[index show new edit].include?(action) # Actions with templates
74
+ template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/#{plural_name}/#{action}.html.#{view_language}"
75
+ end
76
+ end
77
+
78
+ if form_partial?
79
+ template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{plural_name}/_form.html.#{view_language}"
80
+ end
81
+
82
+ route "resources #{plural_name.to_sym.inspect}"
83
+ end
84
+ end
85
+
86
+ private
87
+ def form_partial?
88
+ actions? :new, :edit
89
+ end
90
+
91
+ def all_actions
92
+ %w[index show new create edit update destroy]
93
+ end
94
+
95
+ def action?(name)
96
+ controller_actions.include? name.to_s
97
+ end
98
+
99
+ def actions?(*names)
100
+ names.all? { |name| action? name }
101
+ end
102
+
103
+ def singular_name
104
+ model_name.underscore
105
+ end
106
+
107
+ def plural_name
108
+ model_name.underscore.pluralize
109
+ end
110
+
111
+ def class_name
112
+ model_name.camelize
113
+ end
114
+
115
+ def plural_class_name
116
+ plural_name.camelize
117
+ end
118
+
119
+ def controller_methods(dir_name)
120
+ controller_actions.map do |action|
121
+ read_template("#{dir_name}/#{action}.rb")
122
+ end.join(" \n").strip
123
+ end
124
+
125
+ def render_form
126
+ if form_partial?
127
+ if options.haml?
128
+ "= render :partial => 'form', :locals => { :f => f, :submit_button => 'Go' }"
129
+ else
130
+ "<%= render :partial => 'form', :locals => { :f => f, :submit_button => 'Go' } %>"
131
+ end
132
+ else
133
+ read_template("views/#{view_language}/_form.html.#{view_language}")
134
+ end
135
+ end
136
+
137
+ def items_path(suffix = 'path')
138
+ if action? :index
139
+ "#{plural_name}_#{suffix}"
140
+ else
141
+ "root_#{suffix}"
142
+ end
143
+ end
144
+
145
+ def item_path(suffix = 'path')
146
+ if action? :show
147
+ "@#{singular_name}"
148
+ else
149
+ items_path(suffix)
150
+ end
151
+ end
152
+
153
+ def model_columns_for_attributes
154
+ class_name.constantize.columns.reject do |column|
155
+ column.name.to_s =~ /^(id|created_at|updated_at)$/
156
+ end
157
+ end
158
+
159
+ def model_exists?
160
+ File.exist? destination_path("app/models/#{singular_name}.rb")
161
+ end
162
+
163
+ def read_template(relative_path)
164
+ ERB.new(File.read(find_in_source_paths(relative_path)), nil, '-').result(binding)
165
+ end
166
+
167
+ def destination_path(path)
168
+ File.join(destination_root, path)
169
+ end
170
+
171
+ def view_language
172
+ options.haml? ? 'haml' : 'erb'
173
+ end
174
+
175
+ # FIXME: Should be proxied to ActiveRecord::Generators::Base
176
+ # Implement the required interface for Rails::Generators::Migration.
177
+ def self.next_migration_number(dirname) #:nodoc:
178
+ if ActiveRecord::Base.timestamped_migrations
179
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
180
+ else
181
+ "%.3d" % (current_migration_number(dirname) + 1)
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,8 @@
1
+ def create
2
+ @<%= singular_name %> = <%= class_name %>.new(params[:<%= singular_name %>])
3
+ if @<%= singular_name %>.save
4
+ redirect_to(<%= item_path('url') %>, :notice => "Successfully created <%= model_name.underscore.humanize.downcase %>.")
5
+ else
6
+ render :new
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ def destroy
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ @<%= singular_name %>.destroy
4
+ redirect_to(<%= items_path('url') %>, :notice => "Successfully destroyed <%= model_name.underscore.humanize.downcase %>.")
5
+ end
@@ -0,0 +1,3 @@
1
+ def edit
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ end
@@ -0,0 +1,3 @@
1
+ def index
2
+ @<%= plural_name %> = <%= class_name %>.all
3
+ end
@@ -0,0 +1,3 @@
1
+ def new
2
+ @<%= singular_name %> = <%= class_name %>.new
3
+ end
@@ -0,0 +1,3 @@
1
+ def show
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ end
@@ -0,0 +1,8 @@
1
+ def update
2
+ @<%= singular_name %> = <%= class_name %>.find(params[:id])
3
+ if @<%= singular_name %>.update_attributes(params[:<%= singular_name %>])
4
+ redirect_to(<%= items_path('url') %>, :notice => "Successfully updated <%= model_name.underscore.humanize.downcase %>.")
5
+ else
6
+ render :edit
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ class <%= plural_class_name %>Controller < ApplicationController
2
+ <%= controller_methods :actions %>
3
+ end
@@ -0,0 +1,2 @@
1
+ module <%= plural_class_name %>Helper
2
+ end
@@ -0,0 +1,16 @@
1
+ class Create<%= plural_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= plural_name %> do |t|
4
+ <%- for attribute in model_attributes -%>
5
+ t.<%= attribute.type %> :<%= attribute.name %>
6
+ <%- end -%>
7
+ <%- unless options[:skip_timestamps] -%>
8
+ t.timestamps
9
+ <%- end -%>
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :<%= plural_name %>
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ class <%= class_name %> < ActiveRecord::Base
2
+ attr_accessible <%= model_attributes.map { |a| ":#{a.name}" }.join(", ") %>
3
+ end
@@ -0,0 +1,12 @@
1
+ - if @<%= singular_name %>.errors.any?
2
+ %ul
3
+ - @<%= singular_name %>.errors.full_messages.each do |msg|
4
+ %li= msg
5
+ <%- for attribute in model_attributes -%>
6
+ %p
7
+ = f.label :<%= attribute.name %>
8
+ %br
9
+ = f.<%= attribute.field_type %> :<%= attribute.name %>
10
+ <%- end -%>
11
+ %button.button.positive{:type => "submit"}
12
+ = submit_button
@@ -0,0 +1,11 @@
1
+ - title "Edit <%= singular_name.titleize %>"
2
+
3
+ - form_for @<%= singular_name %> do |f|
4
+ <%= render_form %>
5
+
6
+ <%- if action? :destroy -%>
7
+ = link_to "Delete <%= singular_name.titleize %>", @<%= singular_name %>, :confirm =>"Are you sure?", :method => :delete, :class => "button negative"
8
+ <%- end -%>
9
+ <%- if action? :index -%>
10
+ = link_to "Back", <%= plural_name %>_path, :class => "button"
11
+ <%- end -%>
@@ -0,0 +1,26 @@
1
+ - title "<%= plural_name.titleize %>"
2
+
3
+ <%- if actions? :new -%>
4
+ %p.buttons
5
+ = link_to "Add New <%= singular_name.titleize %>", new_<%= singular_name %>_path, :class => "button positive"
6
+ <%- end -%>
7
+
8
+ %table
9
+ %tr
10
+ <%- for attribute in model_attributes -%>
11
+ %th <%= attribute.human_name %>
12
+ <%- end -%>
13
+ %th Actions
14
+ - for <%= singular_name %> in @<%= plural_name %>
15
+ - tr_class = cycle 'even', 'odd'
16
+ %tr{:class => tr_class}
17
+ <%- for attribute in model_attributes -%>
18
+ %td= <%= singular_name %>.<%= attribute.name %>
19
+ <%- end -%>
20
+ %td
21
+ <%- if action? :show -%>
22
+ = link_to "Show", <%= singular_name %>, :class => "button positive"
23
+ <%- end -%>
24
+ <%- if action? :edit -%>
25
+ = link_to "Edit", edit_<%= singular_name %>_path(<%= singular_name %>), :class => "button"
26
+ <%- end -%>
@@ -0,0 +1,8 @@
1
+ - title "New <%= singular_name.titleize %>"
2
+
3
+ - form_for @<%= singular_name %> do |f|
4
+ <%= render_form %>
5
+
6
+ <%- if action? :index -%>
7
+ = link_to "Back", <%= plural_name %>_path, :class => "button"
8
+ <%- end -%>
@@ -0,0 +1,15 @@
1
+ - title "<%= singular_name.titleize %>"
2
+
3
+ <%- for attribute in model_attributes -%>
4
+ %p
5
+ %strong <%= attribute.human_name.titleize %>:
6
+ = @<%= singular_name %>.<%= attribute.name %>
7
+ <%- end -%>
8
+
9
+ %p.buttons
10
+ <%- if action? :edit -%>
11
+ = link_to "Edit <%= singular_name.titleize %>", edit_<%= singular_name %>_path(@<%= singular_name %>), :class => "button positive"
12
+ <%- end -%>
13
+ <%- if action? :index -%>
14
+ = link_to "Back", <%= plural_name %>_path, :class => "button"
15
+ <%- end -%>