entropi-bootstrap-rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. data/.DS_Store +0 -0
  2. data/.gitignore +7 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +21 -0
  5. data/README.rdoc +87 -0
  6. data/Rakefile +12 -0
  7. data/bootstrap-rails.gemspec +32 -0
  8. data/features/bootstrap_layout.feature +12 -0
  9. data/features/bootstrap_scaffold.feature +80 -0
  10. data/features/step_definitions/common_steps.rb +62 -0
  11. data/features/step_definitions/rails_setup_steps.rb +6 -0
  12. data/features/support/env.rb +6 -0
  13. data/features/support/matchers.rb +7 -0
  14. data/lib/.DS_Store +0 -0
  15. data/lib/bootstrap-rails.rb +1 -0
  16. data/lib/bootstrap/rails.rb +10 -0
  17. data/lib/bootstrap/rails/engine.rb +7 -0
  18. data/lib/bootstrap/rails/railtie.rb +17 -0
  19. data/lib/bootstrap/rails/version.rb +9 -0
  20. data/lib/generators/.DS_Store +0 -0
  21. data/lib/generators/bootstrap.rb +28 -0
  22. data/lib/generators/bootstrap/.DS_Store +0 -0
  23. data/lib/generators/bootstrap/layout/USAGE +25 -0
  24. data/lib/generators/bootstrap/layout/layout_generator.rb +21 -0
  25. data/lib/generators/bootstrap/layout/templates/error_messages_helper.rb +23 -0
  26. data/lib/generators/bootstrap/layout/templates/layout.html.erb +29 -0
  27. data/lib/generators/bootstrap/layout/templates/layout_helper.rb +22 -0
  28. data/lib/generators/bootstrap/scaffold/.DS_Store +0 -0
  29. data/lib/generators/bootstrap/scaffold/USAGE +51 -0
  30. data/lib/generators/bootstrap/scaffold/scaffold_generator.rb +313 -0
  31. data/lib/generators/bootstrap/scaffold/templates/actions/create.rb +8 -0
  32. data/lib/generators/bootstrap/scaffold/templates/actions/destroy.rb +5 -0
  33. data/lib/generators/bootstrap/scaffold/templates/actions/edit.rb +3 -0
  34. data/lib/generators/bootstrap/scaffold/templates/actions/index.rb +3 -0
  35. data/lib/generators/bootstrap/scaffold/templates/actions/new.rb +3 -0
  36. data/lib/generators/bootstrap/scaffold/templates/actions/show.rb +3 -0
  37. data/lib/generators/bootstrap/scaffold/templates/actions/update.rb +8 -0
  38. data/lib/generators/bootstrap/scaffold/templates/controller.rb +3 -0
  39. data/lib/generators/bootstrap/scaffold/templates/fixtures.yml +9 -0
  40. data/lib/generators/bootstrap/scaffold/templates/helper.rb +2 -0
  41. data/lib/generators/bootstrap/scaffold/templates/migration.rb +16 -0
  42. data/lib/generators/bootstrap/scaffold/templates/model.rb +4 -0
  43. data/lib/generators/bootstrap/scaffold/templates/tests/rspec/actions/create.rb +11 -0
  44. data/lib/generators/bootstrap/scaffold/templates/tests/rspec/actions/destroy.rb +6 -0
  45. data/lib/generators/bootstrap/scaffold/templates/tests/rspec/actions/edit.rb +4 -0
  46. data/lib/generators/bootstrap/scaffold/templates/tests/rspec/actions/index.rb +4 -0
  47. data/lib/generators/bootstrap/scaffold/templates/tests/rspec/actions/new.rb +4 -0
  48. data/lib/generators/bootstrap/scaffold/templates/tests/rspec/actions/show.rb +4 -0
  49. data/lib/generators/bootstrap/scaffold/templates/tests/rspec/actions/update.rb +11 -0
  50. data/lib/generators/bootstrap/scaffold/templates/tests/rspec/controller.rb +8 -0
  51. data/lib/generators/bootstrap/scaffold/templates/tests/rspec/model.rb +7 -0
  52. data/lib/generators/bootstrap/scaffold/templates/tests/shoulda/actions/create.rb +13 -0
  53. data/lib/generators/bootstrap/scaffold/templates/tests/shoulda/actions/destroy.rb +8 -0
  54. data/lib/generators/bootstrap/scaffold/templates/tests/shoulda/actions/edit.rb +6 -0
  55. data/lib/generators/bootstrap/scaffold/templates/tests/shoulda/actions/index.rb +6 -0
  56. data/lib/generators/bootstrap/scaffold/templates/tests/shoulda/actions/new.rb +6 -0
  57. data/lib/generators/bootstrap/scaffold/templates/tests/shoulda/actions/show.rb +6 -0
  58. data/lib/generators/bootstrap/scaffold/templates/tests/shoulda/actions/update.rb +13 -0
  59. data/lib/generators/bootstrap/scaffold/templates/tests/shoulda/controller.rb +5 -0
  60. data/lib/generators/bootstrap/scaffold/templates/tests/shoulda/model.rb +7 -0
  61. data/lib/generators/bootstrap/scaffold/templates/tests/testunit/actions/create.rb +11 -0
  62. data/lib/generators/bootstrap/scaffold/templates/tests/testunit/actions/destroy.rb +6 -0
  63. data/lib/generators/bootstrap/scaffold/templates/tests/testunit/actions/edit.rb +4 -0
  64. data/lib/generators/bootstrap/scaffold/templates/tests/testunit/actions/index.rb +4 -0
  65. data/lib/generators/bootstrap/scaffold/templates/tests/testunit/actions/new.rb +4 -0
  66. data/lib/generators/bootstrap/scaffold/templates/tests/testunit/actions/show.rb +4 -0
  67. data/lib/generators/bootstrap/scaffold/templates/tests/testunit/actions/update.rb +11 -0
  68. data/lib/generators/bootstrap/scaffold/templates/tests/testunit/controller.rb +5 -0
  69. data/lib/generators/bootstrap/scaffold/templates/tests/testunit/model.rb +7 -0
  70. data/lib/generators/bootstrap/scaffold/templates/views/erb/_form.html.erb +14 -0
  71. data/lib/generators/bootstrap/scaffold/templates/views/erb/edit.html.erb +14 -0
  72. data/lib/generators/bootstrap/scaffold/templates/views/erb/index.html.erb +39 -0
  73. data/lib/generators/bootstrap/scaffold/templates/views/erb/new.html.erb +7 -0
  74. data/lib/generators/bootstrap/scaffold/templates/views/erb/show.html.erb +23 -0
  75. data/vendor/assets/css/bootstrap.css +1987 -0
  76. data/vendor/assets/css/bootstrap.min.css +264 -0
  77. data/vendor/assets/css/jquery.bootstrap.css +140 -0
  78. data/vendor/assets/javascripts/bootstrap.js +103 -0
  79. data/vendor/assets/javascripts/jquery.bootstrap.js +143 -0
  80. data/vendor/assets/javascripts/jquery.tablesorter.2.0.5.js +0 -0
  81. metadata +227 -0
@@ -0,0 +1,21 @@
1
+ require 'generators/bootstrap'
2
+
3
+ module Bootstrap
4
+ module Generators
5
+ class LayoutGenerator < Base
6
+ argument :layout_name, :type => :string, :default => 'application', :banner => 'layout_name'
7
+
8
+ def create_layout
9
+ template 'layout.html.erb', "app/views/layouts/#{file_name}.html.erb"
10
+ copy_file 'layout_helper.rb', 'app/helpers/layout_helper.rb'
11
+ copy_file 'error_messages_helper.rb', 'app/helpers/error_messages_helper.rb'
12
+ end
13
+
14
+ private
15
+
16
+ def file_name
17
+ layout_name.underscore
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ module ErrorMessagesHelper
2
+ # Render error messages for the given objects. The :message and :header_message options are allowed.
3
+ def error_messages_for(*objects)
4
+ options = objects.extract_options!
5
+ options[:header_message] ||= I18n.t(:"activerecord.errors.header", :default => "Invalid Fields")
6
+ options[:message] ||= I18n.t(:"activerecord.errors.message", :default => "Correct the following errors and try again.")
7
+ messages = objects.compact.map { |o| o.errors.full_messages }.flatten
8
+ unless messages.empty?
9
+ content_tag(:div, :class => "error_messages") do
10
+ list_items = messages.map { |msg| content_tag(:li, msg.html_safe) }
11
+ content_tag(:h2, options[:header_message].html_safe) + content_tag(:p, options[:message].html_safe) + content_tag(:ul, list_items.join.html_safe)
12
+ end
13
+ end
14
+ end
15
+
16
+ module FormBuilderAdditions
17
+ def error_messages(options = {})
18
+ @template.error_messages_for(@object, options)
19
+ end
20
+ end
21
+ end
22
+
23
+ ActionView::Helpers::FormBuilder.send(:include, ErrorMessagesHelper::FormBuilderAdditions)
@@ -0,0 +1,29 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title><%%= content_for?(:title) ? yield(:title) : "Untitled" %></title>
5
+ <%%= stylesheet_link_tag "application" %>
6
+ <%%= javascript_include_tag "application" %>
7
+ <%%= csrf_meta_tag %>
8
+ <%%= yield(:head) %>
9
+ </head>
10
+ <body>
11
+ <div class="topbar">
12
+ <div class="fill">
13
+ <div class="container">
14
+ <h3><%%= link_to 'Project Name', '#' %></h3>
15
+ </div>
16
+ </div>
17
+ </div>
18
+ <div class="container">
19
+ <%% flash.each do |name, msg| %>
20
+ <%%= content_tag :div, msg, :id => "flash_#{name}" %>
21
+ <%% end %>
22
+ <%%= content_tag :h1, yield(:title) if show_title? %>
23
+ <%%= yield %>
24
+ <footer>
25
+ <p>&copy; Company <%%= Time.now.year %></p>
26
+ </footer>
27
+ </div>
28
+ </body>
29
+ </html>
@@ -0,0 +1,22 @@
1
+ # These helper methods can be called in your template to set variables to be used in the layout
2
+ # This module should be included in all views globally,
3
+ # to do so you may need to add this line to your ApplicationController
4
+ # helper :layout
5
+ module LayoutHelper
6
+ def title(page_title, show_title = true)
7
+ content_for(:title) { h(page_title.to_s) }
8
+ @show_title = show_title
9
+ end
10
+
11
+ def show_title?
12
+ @show_title
13
+ end
14
+
15
+ def stylesheet(*args)
16
+ content_for(:head) { stylesheet_link_tag(*args) }
17
+ end
18
+
19
+ def javascript(*args)
20
+ content_for(:head) { javascript_include_tag(*args) }
21
+ end
22
+ end
@@ -0,0 +1,51 @@
1
+ Description:
2
+ Scaffolds an entire resource, from model and migration to controller and
3
+ views. The resource is ready to use as a starting point for your restful,
4
+ resource-oriented application. Tests or specs are also generated depending
5
+ on if you have a "spec" directory or not.
6
+
7
+ IMPORTANT: This generator uses the "title" helper method which is generated
8
+ by the nifty_layout generator. You may want to run that generator first.
9
+
10
+ Usage:
11
+ Pass the name of the model, either CamelCased or under_scored, as the first
12
+ argument along with an optional list of attribute pairs and controller actions.
13
+
14
+ If no controller actions are specified, they will default to index, show,
15
+ new, create, edit, update, and destroy.
16
+
17
+ IMPORTANT: If no attribute pairs are specified, no model will be generated.
18
+ It will try to determine the attributes from an existing model.
19
+
20
+ Attribute pairs are column_name:sql_type arguments specifying the
21
+ model's attributes. Timestamps are added by default, so you don't have to
22
+ specify them by hand as 'created_at:datetime updated_at:datetime'.
23
+
24
+ For example, `nifty:scaffold post name:string content:text hidden:boolean`
25
+ gives you a model with those three attributes, a controller that handles
26
+ the create/show/update/destroy, forms to create and edit your posts, and
27
+ an index that lists them all, as well as a map.resources :posts
28
+ declaration in config/routes.rb.
29
+
30
+ Adding an "!" in the mix of arguments will invert the passed controller
31
+ actions. This will include all 7 controller actitons except the ones
32
+ mentioned. This option doesn't affect model attributes.
33
+
34
+ Examples:
35
+ rails generate nifty:scaffold post
36
+
37
+ Will create a controller called "posts" it will contain all seven
38
+ CRUD actions along with the views. A model will NOT be created,
39
+ instead it will look for an existing model and use those attributes.
40
+
41
+ rails generate nifty:scaffold post name:string content:text index new edit
42
+
43
+ Will create a Post model and migration file with the name and content
44
+ attributes. It will also create a controller with index, new, create,
45
+ edit, and update actions. Notice the create and update actions are
46
+ added automatically with new and edit.
47
+
48
+ rails generate nifty:scaffold post ! show new
49
+
50
+ Creates a posts controller (no model) with index, edit, update, and
51
+ destroy actions.
@@ -0,0 +1,313 @@
1
+ require 'generators/bootstrap'
2
+ require 'rails/generators/migration'
3
+ require 'rails/generators/generated_attribute'
4
+
5
+ module Bootstrap
6
+ module Generators
7
+ class ScaffoldGenerator < Base
8
+ include ::Rails::Generators::Migration
9
+ no_tasks { attr_accessor :scaffold_name, :model_attributes, :controller_actions }
10
+
11
+ argument :scaffold_name, :type => :string, :required => true, :banner => 'ModelName'
12
+ argument :args_for_c_m, :type => :array, :default => [], :banner => 'controller_actions and model:attributes'
13
+
14
+ class_option :skip_model, :desc => 'Don\'t generate a model or migration file.', :type => :boolean
15
+ class_option :skip_migration, :desc => 'Don\'t generate migration file for model.', :type => :boolean
16
+ class_option :skip_timestamps, :desc => 'Don\'t add timestamps to migration file.', :type => :boolean
17
+ class_option :skip_controller, :desc => 'Don\'t generate controller, helper, or views.', :type => :boolean
18
+ class_option :invert, :desc => 'Generate all controller actions except these mentioned.', :type => :boolean
19
+ class_option :namespace_model, :desc => 'If the resource is namespaced, include the model in the namespace.', :type => :boolean
20
+
21
+ class_option :testunit, :desc => 'Use test/unit for test files.', :group => 'Test framework', :type => :boolean
22
+ class_option :rspec, :desc => 'Use RSpec for test files.', :group => 'Test framework', :type => :boolean
23
+ class_option :shoulda, :desc => 'Use shoulda for test files.', :group => 'Test framework', :type => :boolean
24
+
25
+ def initialize(*args, &block)
26
+ super
27
+
28
+ print_usage unless scaffold_name.underscore =~ /^[a-z][a-z0-9_\/]+$/
29
+
30
+ @controller_actions = []
31
+ @model_attributes = []
32
+ @skip_model = options.skip_model?
33
+ @namespace_model = options.namespace_model?
34
+ @invert_actions = options.invert?
35
+
36
+ args_for_c_m.each do |arg|
37
+ if arg == '!'
38
+ @invert_actions = true
39
+ elsif arg.include?(':')
40
+ @model_attributes << ::Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
41
+ else
42
+ @controller_actions << arg
43
+ @controller_actions << 'create' if arg == 'new'
44
+ @controller_actions << 'update' if arg == 'edit'
45
+ end
46
+ end
47
+
48
+ @controller_actions.uniq!
49
+ @model_attributes.uniq!
50
+
51
+ if @invert_actions || @controller_actions.empty?
52
+ @controller_actions = all_actions - @controller_actions
53
+ end
54
+
55
+ if @model_attributes.empty?
56
+ @skip_model = true # skip model if no attributes
57
+ if model_exists?
58
+ model_columns_for_attributes.each do |column|
59
+ @model_attributes << ::Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
60
+ end
61
+ else
62
+ @model_attributes << ::Rails::Generators::GeneratedAttribute.new('name', 'string')
63
+ end
64
+ end
65
+ end
66
+
67
+ def add_gems
68
+ add_gem "mocha", :group => :test
69
+ end
70
+
71
+ def create_model
72
+ unless @skip_model
73
+ template 'model.rb', "app/models/#{model_path}.rb"
74
+ if test_framework == :rspec
75
+ template "tests/rspec/model.rb", "spec/models/#{model_path}_spec.rb"
76
+ template 'fixtures.yml', "spec/fixtures/#{model_path.pluralize}.yml"
77
+ else
78
+ template "tests/#{test_framework}/model.rb", "test/unit/#{model_path}_test.rb"
79
+ template 'fixtures.yml', "test/fixtures/#{model_path.pluralize}.yml"
80
+ end
81
+ end
82
+ end
83
+
84
+ def create_migration
85
+ unless @skip_model || options.skip_migration?
86
+ migration_template 'migration.rb', "db/migrate/create_#{model_path.pluralize.gsub('/', '_')}.rb"
87
+ end
88
+ end
89
+
90
+ def create_controller
91
+ unless options.skip_controller?
92
+ template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
93
+
94
+ template 'helper.rb', "app/helpers/#{plural_name}_helper.rb"
95
+
96
+ controller_actions.each do |action|
97
+ if %w[index show new edit].include?(action) # Actions with templates
98
+ template "views/#{view_language}/#{action}.html.#{view_language}", "app/views/#{plural_name}/#{action}.html.#{view_language}"
99
+ end
100
+ end
101
+
102
+ if form_partial?
103
+ template "views/#{view_language}/_form.html.#{view_language}", "app/views/#{plural_name}/_form.html.#{view_language}"
104
+ end
105
+
106
+ namespaces = plural_name.split('/')
107
+ resource = namespaces.pop
108
+ route namespaces.reverse.inject("resources :#{resource}") { |acc, namespace|
109
+ "namespace(:#{namespace}){ #{acc} }"
110
+ }
111
+
112
+ if test_framework == :rspec
113
+ template "tests/#{test_framework}/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
114
+ else
115
+ template "tests/#{test_framework}/controller.rb", "test/functional/#{plural_name}_controller_test.rb"
116
+ end
117
+ end
118
+ end
119
+
120
+ private
121
+
122
+ def form_partial?
123
+ actions? :new, :edit
124
+ end
125
+
126
+ def all_actions
127
+ %w[index show new create edit update destroy]
128
+ end
129
+
130
+ def action?(name)
131
+ controller_actions.include? name.to_s
132
+ end
133
+
134
+ def actions?(*names)
135
+ names.all? { |name| action? name }
136
+ end
137
+
138
+ def singular_name
139
+ scaffold_name.underscore
140
+ end
141
+
142
+ def plural_name
143
+ scaffold_name.underscore.pluralize
144
+ end
145
+
146
+ def table_name
147
+ if scaffold_name.include?('::') && @namespace_model
148
+ plural_name.gsub('/', '_')
149
+ end
150
+ end
151
+
152
+ def class_name
153
+ if @namespace_model
154
+ scaffold_name.camelize
155
+ else
156
+ scaffold_name.split('::').last.camelize
157
+ end
158
+ end
159
+
160
+ def model_path
161
+ class_name.underscore
162
+ end
163
+
164
+ def plural_class_name
165
+ plural_name.camelize
166
+ end
167
+
168
+ def instance_name
169
+ if @namespace_model
170
+ singular_name.gsub('/','_')
171
+ else
172
+ singular_name.split('/').last
173
+ end
174
+ end
175
+
176
+ def instances_name
177
+ instance_name.pluralize
178
+ end
179
+
180
+ def controller_methods(dir_name)
181
+ controller_actions.map do |action|
182
+ read_template("#{dir_name}/#{action}.rb")
183
+ end.join("\n").strip
184
+ end
185
+
186
+ def render_form
187
+ if form_partial?
188
+ "<%= render \"form\" %>"
189
+ else
190
+ read_template("views/#{view_language}/_form.html.#{view_language}")
191
+ end
192
+ end
193
+
194
+ def item_resource
195
+ scaffold_name.underscore.gsub('/','_')
196
+ end
197
+
198
+ def items_path
199
+ if action? :index
200
+ "#{item_resource.pluralize}_path"
201
+ else
202
+ "root_path"
203
+ end
204
+ end
205
+
206
+ def item_path(options = {})
207
+ name = options[:instance_variable] ? "@#{instance_name}" : instance_name
208
+ suffix = options[:full_url] ? "url" : "path"
209
+ if options[:action].to_s == "new"
210
+ "new_#{item_resource}_#{suffix}"
211
+ elsif options[:action].to_s == "edit"
212
+ "edit_#{item_resource}_#{suffix}(#{name})"
213
+ else
214
+ if scaffold_name.include?('::') && !@namespace_model
215
+ namespace = singular_name.split('/')[0..-2]
216
+ "[:#{namespace.join(', :')}, #{name}]"
217
+ else
218
+ name
219
+ end
220
+ end
221
+ end
222
+
223
+ def item_url
224
+ if action? :show
225
+ item_path(:full_url => true, :instance_variable => true)
226
+ else
227
+ items_url
228
+ end
229
+ end
230
+
231
+ def items_url
232
+ if action? :index
233
+ item_resource.pluralize + '_url'
234
+ else
235
+ "root_url"
236
+ end
237
+ end
238
+
239
+ def item_path_for_spec(suffix = 'path')
240
+ if action? :show
241
+ "#{item_resource}_#{suffix}(assigns[:#{instance_name}])"
242
+ else
243
+ if suffix == 'path'
244
+ items_path
245
+ else
246
+ items_url
247
+ end
248
+ end
249
+ end
250
+
251
+ def item_path_for_test(suffix = 'path')
252
+ if action? :show
253
+ "#{item_resource}_#{suffix}(assigns(:#{instance_name}))"
254
+ else
255
+ if suffix == 'path'
256
+ items_path
257
+ else
258
+ items_url
259
+ end
260
+ end
261
+ end
262
+
263
+ def model_columns_for_attributes
264
+ class_name.constantize.columns.reject do |column|
265
+ column.name.to_s =~ /^(id|created_at|updated_at)$/
266
+ end
267
+ end
268
+
269
+ def view_language
270
+ 'erb'
271
+ end
272
+
273
+ def test_framework
274
+ return @test_framework if defined?(@test_framework)
275
+ if options.testunit?
276
+ return @test_framework = :testunit
277
+ elsif options.rspec?
278
+ return @test_framework = :rspec
279
+ elsif options.shoulda?
280
+ return @test_framework = :shoulda
281
+ else
282
+ return @test_framework = default_test_framework
283
+ end
284
+ end
285
+
286
+ def default_test_framework
287
+ File.exist?(destination_path("spec")) ? :rspec : :testunit
288
+ end
289
+
290
+ def model_exists?
291
+ File.exist? destination_path("app/models/#{singular_name}.rb")
292
+ end
293
+
294
+ def read_template(relative_path)
295
+ ERB.new(File.read(find_in_source_paths(relative_path)), nil, '-').result(binding)
296
+ end
297
+
298
+ def destination_path(path)
299
+ File.join(destination_root, path)
300
+ end
301
+
302
+ # FIXME: Should be proxied to ActiveRecord::Generators::Base
303
+ # Implement the required interface for Rails::Generators::Migration.
304
+ def self.next_migration_number(dirname) #:nodoc:
305
+ if ActiveRecord::Base.timestamped_migrations
306
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
307
+ else
308
+ "%.3d" % (current_migration_number(dirname) + 1)
309
+ end
310
+ end
311
+ end
312
+ end
313
+ end