survey_on_rails 0.3.2

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 (76) hide show
  1. checksums.yaml +15 -0
  2. data/lib/generators/survey/USAGE +5 -0
  3. data/lib/generators/survey/metamodels/app.rb +88 -0
  4. data/lib/generators/survey/metamodels/base.rb +120 -0
  5. data/lib/generators/survey/metamodels/main.rb +12 -0
  6. data/lib/generators/survey/metamodels/model.rb +140 -0
  7. data/lib/generators/survey/metamodels/style.rb +202 -0
  8. data/lib/generators/survey/metamodels/types.rb +10 -0
  9. data/lib/generators/survey/metamodels/validation.rb +28 -0
  10. data/lib/generators/survey/metamodels/view.rb +384 -0
  11. data/lib/generators/survey/survey_generator.rb +276 -0
  12. data/lib/generators/survey/templates/app/app.tpl +15 -0
  13. data/lib/generators/survey/templates/app/assets/assets.tpl +10 -0
  14. data/lib/generators/survey/templates/app/assets/javascripts/application.tpl +32 -0
  15. data/lib/generators/survey/templates/app/assets/stylesheets/static_stylesheet.tpl +337 -0
  16. data/lib/generators/survey/templates/app/assets/stylesheets/stylesheet.tpl +128 -0
  17. data/lib/generators/survey/templates/app/assets/stylesheets/survey_stylesheet.tpl +102 -0
  18. data/lib/generators/survey/templates/app/controllers/application_controller.tpl +15 -0
  19. data/lib/generators/survey/templates/app/controllers/controllers.tpl +12 -0
  20. data/lib/generators/survey/templates/app/controllers/sessions_controller.tpl +26 -0
  21. data/lib/generators/survey/templates/app/controllers/static_pages_controller.tpl +14 -0
  22. data/lib/generators/survey/templates/app/controllers/survey_controller.tpl +105 -0
  23. data/lib/generators/survey/templates/app/controllers/users_controller.tpl +82 -0
  24. data/lib/generators/survey/templates/app/helpers/application_helper.tpl +26 -0
  25. data/lib/generators/survey/templates/app/helpers/helpers.tpl +9 -0
  26. data/lib/generators/survey/templates/app/helpers/sessions_helper.tpl +60 -0
  27. data/lib/generators/survey/templates/app/helpers/survey_helper.tpl +25 -0
  28. data/lib/generators/survey/templates/app/helpers/users_helper.tpl +14 -0
  29. data/lib/generators/survey/templates/app/models/answer.tpl +20 -0
  30. data/lib/generators/survey/templates/app/models/models.tpl +10 -0
  31. data/lib/generators/survey/templates/app/models/question.tpl +20 -0
  32. data/lib/generators/survey/templates/app/models/survey.tpl +17 -0
  33. data/lib/generators/survey/templates/app/models/user.tpl +28 -0
  34. data/lib/generators/survey/templates/app/models/validator.tpl +47 -0
  35. data/lib/generators/survey/templates/app/views/form/checkbox_group.tpl +13 -0
  36. data/lib/generators/survey/templates/app/views/form/choice_question.tpl +10 -0
  37. data/lib/generators/survey/templates/app/views/form/dropdown.tpl +15 -0
  38. data/lib/generators/survey/templates/app/views/form/element.tpl +43 -0
  39. data/lib/generators/survey/templates/app/views/form/form.tpl +35 -0
  40. data/lib/generators/survey/templates/app/views/form/group.tpl +14 -0
  41. data/lib/generators/survey/templates/app/views/form/horizontal_radio_button_group.tpl +7 -0
  42. data/lib/generators/survey/templates/app/views/form/image.tpl +10 -0
  43. data/lib/generators/survey/templates/app/views/form/pagination.tpl +60 -0
  44. data/lib/generators/survey/templates/app/views/form/radio_button_group.tpl +10 -0
  45. data/lib/generators/survey/templates/app/views/form/table.tpl +54 -0
  46. data/lib/generators/survey/templates/app/views/form/textfield.tpl +47 -0
  47. data/lib/generators/survey/templates/app/views/layouts/application.tpl +25 -0
  48. data/lib/generators/survey/templates/app/views/layouts/footer.tpl +28 -0
  49. data/lib/generators/survey/templates/app/views/layouts/header.tpl +32 -0
  50. data/lib/generators/survey/templates/app/views/layouts/shim.tpl +7 -0
  51. data/lib/generators/survey/templates/app/views/layouts/static_element.tpl +52 -0
  52. data/lib/generators/survey/templates/app/views/questions/question.tpl +8 -0
  53. data/lib/generators/survey/templates/app/views/sessions/new.tpl +28 -0
  54. data/lib/generators/survey/templates/app/views/shared/error_messages.tpl +16 -0
  55. data/lib/generators/survey/templates/app/views/shared/static_page.tpl +5 -0
  56. data/lib/generators/survey/templates/app/views/surveys/index.tpl +25 -0
  57. data/lib/generators/survey/templates/app/views/surveys/survey.tpl +14 -0
  58. data/lib/generators/survey/templates/app/views/users/edit.tpl +19 -0
  59. data/lib/generators/survey/templates/app/views/users/fields.tpl +24 -0
  60. data/lib/generators/survey/templates/app/views/users/index.tpl +17 -0
  61. data/lib/generators/survey/templates/app/views/users/new.tpl +15 -0
  62. data/lib/generators/survey/templates/app/views/users/show.tpl +17 -0
  63. data/lib/generators/survey/templates/app/views/users/user.tpl +11 -0
  64. data/lib/generators/survey/templates/app/views/views.tpl +30 -0
  65. data/lib/generators/survey/templates/config/application.tpl +26 -0
  66. data/lib/generators/survey/templates/config/config.tpl +8 -0
  67. data/lib/generators/survey/templates/config/routes.tpl +57 -0
  68. data/lib/generators/survey/templates/db/db.tpl +9 -0
  69. data/lib/generators/survey/templates/db/migrate/create_answers.tpl +20 -0
  70. data/lib/generators/survey/templates/db/migrate/create_questions.tpl +20 -0
  71. data/lib/generators/survey/templates/db/migrate/create_surveys.tpl +20 -0
  72. data/lib/generators/survey/templates/db/migrate/create_users.tpl +22 -0
  73. data/lib/generators/survey/templates/main.tpl +11 -0
  74. data/lib/survey_on_rails/utils/builder.rb +56 -0
  75. data/lib/survey_on_rails/utils/string.rb +22 -0
  76. metadata +116 -0
@@ -0,0 +1,384 @@
1
+ module SurveyMetamodel extend RGen::MetamodelBuilder::ModuleExtension
2
+ class ViewElement < StyleElement
3
+ abstract
4
+ contains_one 'model_element', ModelElement, 'element', :unsettable => true
5
+ end
6
+
7
+ class Form < ViewElement
8
+ has_attr 'author', String
9
+ contains_many 'elements', ViewElement, 'form'
10
+
11
+ has_attr 'page_elements', Object
12
+ end
13
+
14
+ class Input < ViewElement
15
+ abstract
16
+ has_attr 'info', String
17
+ contains_one 'for', Input, 'element'
18
+ end
19
+
20
+ class Textfield < Input
21
+ has_attr 'type', TextFieldType
22
+ has_attr 'prefix', String
23
+ has_attr 'suffix', String
24
+ end
25
+
26
+ class SelectionGroup < Input
27
+ abstract
28
+ has_attr 'dropdown', Boolean
29
+ end
30
+
31
+ class CheckboxGroup < SelectionGroup
32
+ end
33
+
34
+ class RadioButtonGroup < SelectionGroup
35
+ end
36
+
37
+ class HorizontalRadioButtonGroup < SelectionGroup
38
+ end
39
+
40
+ class Image < ViewElement
41
+ has_attr 'source', String
42
+ has_attr 'url', String
43
+ has_attr 'width', Integer
44
+ has_attr 'height', Integer
45
+ end
46
+
47
+ class Table < Input
48
+ has_attr 'dropdown', Boolean
49
+
50
+ has_attr 'images_s', String
51
+ contains_many 'images', Image, 'matrix'
52
+ end
53
+
54
+ class LayoutElement < ViewElement
55
+ abstract
56
+ contains_many 'elements', ViewElement, 'container'
57
+ end
58
+
59
+ class Group < LayoutElement
60
+ end
61
+
62
+ class Page < LayoutElement
63
+ has_attr 'break', Boolean
64
+ end
65
+
66
+ class StaticElement < ViewElement
67
+ abstract
68
+ end
69
+
70
+ class PageView < ViewElement
71
+ abstract
72
+ contains_many 'elements', StaticElement, 'page'
73
+ end
74
+
75
+ class StaticPage < PageView
76
+ has_attr 'root', Boolean
77
+ has_attr 'file', String
78
+ end
79
+
80
+ class Header < PageView
81
+ end
82
+
83
+ class Footer < PageView
84
+ end
85
+
86
+ class Label < StaticElement
87
+ end
88
+
89
+ class Link < StaticElement
90
+ has_attr 'url', String
91
+ has_attr 'path', Boolean
92
+ end
93
+
94
+ class SurveyManagement < StaticElement
95
+ end
96
+
97
+ class UserManagement < StaticElement
98
+ end
99
+ end
100
+
101
+ module SurveyMetamodel
102
+ module ViewElement::ClassModule
103
+ def get_form
104
+ cont = self
105
+ while true
106
+ if !cont.container.nil?
107
+ cont = cont.container
108
+ else
109
+ break
110
+ end
111
+ end
112
+ return cont.form
113
+ end
114
+
115
+ def rules
116
+ model_element.rules if !model_element.nil?
117
+ end
118
+
119
+ def is_mandatory?
120
+ return (!model_element.nil? and model_element.rules.any? { |r| r.is_a?(PresenceRule) })
121
+ end
122
+
123
+ def has_label?
124
+ return self.is_a?(Input)
125
+ end
126
+
127
+ def number_title(i)
128
+ suffix = (is_mandatory?)? " *": ""
129
+ real_title = "#{title}#{suffix}"
130
+ num_title = (i > 0)? ("#{i}. #{real_title}"): real_title
131
+ return num_title
132
+ end
133
+
134
+ def set_model_element
135
+ if self.is_a?(Group) or (self.is_a?(Page) and !self.elements.empty?)
136
+ elements.each do |element|
137
+ element.set_model_element
138
+ end
139
+ end
140
+
141
+ if self.is_a?(Input) and !self.for.nil?
142
+ self.name = self.for.name
143
+ self.title = self.for.title
144
+ self.for.model_element = nil
145
+ end
146
+
147
+ cont = self.get_form
148
+ view = cont.view
149
+ survey = view.app.model.surveys.select { |s| s.name == cont.name }.first
150
+ model_elements = survey.questions.select { |q| q.name == self.name or q.name == self.snake_class }
151
+
152
+ if !model_elements.nil? and model_elements.count == 1
153
+ self.model_element = model_elements.first.dup
154
+ self.title = model_element.title if !title? and model_element.title?
155
+ end
156
+ if !model_element.nil? and model_element.name == self.snake_class
157
+ self.name = self.snake_class
158
+ self.class_name = self.snake_base_class
159
+ end
160
+
161
+ raise "Names cannot be nil and can only consist of letters, numbers and underscore (name='#{self.name}')!" if has_name and !(self.name.scan(/\w+/).count == 1)
162
+ end
163
+
164
+ def is_layout_element?
165
+ return (self.is_a?(Group) or self.is_a?(Page) or self.is_a?(Image))
166
+ end
167
+
168
+ def model_element?
169
+ return !model_element.nil?
170
+ end
171
+
172
+ def has_model_element?
173
+ return (model_element? and !is_layout_element?)
174
+ end
175
+
176
+ def has_valid_model_element?
177
+ return (model_element? or is_layout_element?)
178
+ end
179
+
180
+ private
181
+
182
+ def custom_html?
183
+ !custom_html.blank?
184
+ end
185
+ end
186
+
187
+ module Form::ClassModule
188
+ def author
189
+ model_element.author if !model_element.nil?
190
+ end
191
+
192
+ def inputs
193
+ elements.select { |e| e.is_a?(Input) }
194
+ end
195
+
196
+ def pages_number
197
+ pages.count
198
+ end
199
+
200
+ def actual_pages_number
201
+ page_elements.count
202
+ end
203
+
204
+ def has_multiple_pages
205
+ actual_pages_number > 1
206
+ end
207
+
208
+ def pages
209
+ elements.select { |e| e.is_a?(Page) }
210
+ end
211
+
212
+ def input_elements
213
+ page_elements = self.elements.select { |e| e.is_a?(Page) }.elements
214
+ other_elements = page_elements.select { |e| !e.is_a?(Group) }
215
+ group_elements = page_elements.select { |e| e.is_a?(Group) }.elements
216
+ input_elems = (inputs + other_elements + group_elements)
217
+ return input_elems
218
+ end
219
+
220
+ def set_model_element
221
+ model_elements = self.view.app.model.surveys.select { |s| s.name == self.name }
222
+ if !model_elements.nil? and model_elements.count == 1
223
+ self.model_element = model_elements.first
224
+ self.title = model_element.title if !title? and model_element.title?
225
+ end
226
+ end
227
+
228
+ def set_page_elements
229
+ _pages = pages
230
+ first_page_break = _pages.first
231
+
232
+ self.page_elements = Array.new
233
+ self.page_elements << elements if _pages.count == 0
234
+ if _pages.any?
235
+ _pages.each do |page_break|
236
+ if !page_break.elements.nil? and !page_break.elements.empty?
237
+ self.page_elements << page_break.elements.reject { |e| e.is_a?(Page) }
238
+ else
239
+ if page_break == first_page_break
240
+ self.page_elements << page_elements_before_first
241
+ self.page_elements << page_elements_after(page_break)
242
+ else
243
+ self.page_elements << page_elements_after(page_break)
244
+ end
245
+ end
246
+ end
247
+ end
248
+ end
249
+
250
+ private
251
+
252
+ def page_elements_before_first
253
+ page_elements = Array.new
254
+ add = true
255
+
256
+ elements.each do |elem|
257
+ add = false if elem.is_a?(Page)
258
+ page_elements << elem if add
259
+ end
260
+ return page_elements
261
+ end
262
+
263
+ def page_elements_after(page)
264
+ page_elements = Array.new
265
+ add = false
266
+
267
+ elements.each do |elem|
268
+ if elem.is_a?(Page) and elem == page
269
+ add = true
270
+ elsif elem.is_a?(Page) and elem != page
271
+ add = false
272
+ end
273
+ page_elements << elem if !elem.is_a?(Page) and add
274
+ end
275
+ return page_elements
276
+ end
277
+ end
278
+
279
+ module Input::ClassModule
280
+ def info?
281
+ !info.blank?
282
+ end
283
+
284
+ def dependencies?
285
+ return (!model_element.nil? and !model_element.dependencies.empty?)
286
+ end
287
+
288
+ def dependencies
289
+ model_element.dependencies if !model_element.nil?
290
+ end
291
+
292
+ def conditions
293
+ model_element.conditions if !model_element.nil?
294
+ end
295
+ end
296
+
297
+ module StaticPage::ClassModule
298
+ def file_path
299
+ raise "The referenced static page does not exist. Please define it first." if !file.nil? and !File.exists?(file)
300
+ return (!file.nil?)? file: "gen/assets/static_pages/#{name}.html.erb"
301
+ end
302
+
303
+ def create_page_file
304
+ File.new(file_path, 'w') unless File.exists?(file_path)
305
+ end
306
+ end
307
+
308
+ module Textfield::ClassModule
309
+ def prefix?
310
+ !prefix.blank?
311
+ end
312
+
313
+ def suffix?
314
+ !suffix.blank?
315
+ end
316
+ end
317
+
318
+ module SelectionGroup::ClassModule
319
+ def choices
320
+ model_element.real_choices if !model_element.nil?
321
+ end
322
+
323
+ def params_s
324
+ choices_string_a = Array.new
325
+ choices.each { |choice| choices_string_a << "params[:#{name}_#{choice.name}].to_s" }
326
+ return "[#{choices_string_a.join(', ')}].reject(&:empty?).join(', ')"
327
+ end
328
+ end
329
+
330
+ module Table::ClassModule
331
+ def type
332
+ model_element.type if !model_element.nil?
333
+ end
334
+
335
+ def rows
336
+ model_element.real_rows if !model_element.nil?
337
+ end
338
+
339
+ def columns
340
+ model_element.real_columns if !model_element.nil?
341
+ end
342
+
343
+ def choices
344
+ model_element.real_choices if !model_element.nil?
345
+ end
346
+
347
+ def has_images?
348
+ return (images.any? or !images_s.blank?)
349
+ end
350
+
351
+ def real_images
352
+ imgs = nil
353
+ if !images_s.blank? and images.empty?
354
+ imgs = Array.new
355
+ images_s.split(",").each { |image_s| imgs << Image.new(:source => image_s.strip) }
356
+ else
357
+ imgs = images
358
+ end
359
+ raise "The number of images cannot be more than the number of cells." if imgs.count > (rows.count * columns.count)
360
+ return imgs
361
+ end
362
+
363
+ def params_s
364
+ cells_string_a = Array.new
365
+ rows.each do |row|
366
+ cells_string_a << "params[:#{name}_#{row.name}]"
367
+ columns.each do |column|
368
+ cells_string_a << "params[:#{name}_#{row.name}_#{column.name}]"
369
+ end
370
+ end
371
+ return "[#{cells_string_a.join(', ')}].uniq.reject(&:blank?).join(', ')"
372
+ end
373
+ end
374
+
375
+ module Image::ClassModule
376
+ def source?
377
+ !source.blank?
378
+ end
379
+
380
+ def url?
381
+ !url.blank?
382
+ end
383
+ end
384
+ end
@@ -0,0 +1,276 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ class SurveyGenerator < Rails::Generators::Base
4
+ argument :model_path, :type => :string, :default => "#{Rails.root.to_s}/gen/models/app.rb"
5
+ class_option :init, :type => :boolean, :default => false, :desc => "Performs all actions"
6
+ class_option :dynamic, :type => :boolean, :default => true, :desc => "Generates dynamic part of the model"
7
+ class_option :static, :type => :boolean, :default => true, :desc => "Generates static part of the model"
8
+ class_option :gems, :type => :boolean, :default => false, :desc => "Initializes the application"
9
+ class_option :migrate, :type => :boolean, :default => false, :desc => "Performs database migrations"
10
+ class_option :populate, :type => :boolean, :default => false, :desc => "Populates database with users"
11
+ class_option :silent, :type => :boolean, :default => false, :desc => "Prevents console feedback"
12
+
13
+ def generate_survey
14
+ start_time = Time.now
15
+ out "Initializing Survey on Rails (SoR)"
16
+
17
+ init_app if options.gems?
18
+ if options.init?
19
+ out "Initializing the application with default configuration"
20
+ run 'rails g survey --gems --migrate --populate --no-static --no-dynamic --silent'
21
+ end
22
+
23
+ out "Generating ruby on rails source code for the survey in '#{model_path}'"
24
+
25
+ if File.exists?(model_path)
26
+ require "#{File.dirname(model_path)}/model.rb"
27
+ require model_path
28
+
29
+ if !(defined?(APPLICATION)).nil?
30
+ if !APPLICATION.view.nil?
31
+ generate_all
32
+ else
33
+ init_model("Model is empty or no survey has been defined", false)
34
+ end
35
+ else
36
+ init_model("Model has not been defined", true)
37
+ end
38
+ else
39
+ init_model("Model file does not exist", true)
40
+ end
41
+
42
+ migrate if options.migrate?
43
+ populate if options.populate?
44
+
45
+ out "Generation process has finished after #{(Time.now - start_time).round(3).seconds.to_s} seconds"
46
+ end
47
+
48
+ protected
49
+
50
+ def init_model(error, create)
51
+ out(error)
52
+ create_model if create
53
+ generate_static
54
+ end
55
+
56
+ def generate_all
57
+ generate(true, true)
58
+ end
59
+
60
+ def generate_static
61
+ generate(true, false)
62
+ end
63
+
64
+ def generate(static, dynamic)
65
+ load_requirements
66
+ remove_migrations
67
+
68
+ templateContainer = RGen::TemplateLanguage::DirectoryTemplateContainer.new(SurveyMetamodel, Rails.root.to_s)
69
+ templateContainer.load(File.dirname(__FILE__) + '/templates')
70
+
71
+ out "Preprocessing the model"
72
+ APPLICATION.first.preprocessing
73
+
74
+ if options.static? and static
75
+ out "Generating static parts of model"
76
+ templateContainer.expand('main::static', :for => APPLICATION)
77
+ end
78
+
79
+ if options.dynamic? and dynamic
80
+ out "Generating dynamic parts of model"
81
+ templateContainer.expand('main::dynamic', :for => APPLICATION.first)
82
+ end
83
+
84
+ copy_assets
85
+ end
86
+
87
+ def load_requirements
88
+ require 'rgen/template_language'
89
+ require 'rgen/array_extensions'
90
+ require 'metamodels/main'
91
+ suppress_warnings { load model_path }
92
+ end
93
+
94
+ def init_app
95
+ out "Initializing application (adding needed gems, removing unused files, bundling)"
96
+
97
+ file_name = "Gemfile"
98
+ File.new(file_name) unless File.exists?(file_name)
99
+ File.open(file_name, 'r+') do |file|
100
+ file.puts("source 'https://rubygems.org'")
101
+
102
+ file.puts("\n#For all environments")
103
+ file.puts("gem 'rails', '3.2.13'")
104
+ file.puts("gem 'bootstrap-sass', '2.1'")
105
+ file.puts("gem 'bcrypt-ruby', '3.0.1'")
106
+ file.puts("gem 'faker', '1.0.1'")
107
+ file.puts("gem 'will_paginate', '3.0.3'")
108
+ file.puts("gem 'bootstrap-will_paginate', '0.0.6'")
109
+ file.puts("gem 'jquery-rails'")
110
+ file.puts("gem 'rgen'")
111
+ file.puts("gem 'survey_generator'")
112
+
113
+ file.puts("\n#Group 'development' and 'test'")
114
+ file.puts("group :development, :test do")
115
+ file.puts("\tgem 'sqlite3'")
116
+ file.puts("\tgem 'rspec-rails', '2.11.0'")
117
+ file.puts("end")
118
+
119
+ file.puts("\n#Group 'test'")
120
+ file.puts("group :test do")
121
+ file.puts("\tgem 'capybara', '1.1.2'")
122
+ file.puts("\tgem 'factory_girl_rails', '4.1.0'")
123
+ file.puts("\tgem 'cucumber-rails', '1.2.1', :require => false")
124
+ file.puts("\tgem 'database_cleaner', '0.7.0'")
125
+ file.puts("end")
126
+
127
+ file.puts("\n#Group 'production'")
128
+ file.puts("group :production do")
129
+ file.puts("\tgem 'pg', '0.12.2'")
130
+ file.puts("end")
131
+
132
+ file.puts("\n#Group 'assets'")
133
+ file.puts("group :assets do")
134
+ file.puts("\tgem 'sass-rails','~> 3.2.3'")
135
+ file.puts("\tgem 'coffee-rails', '~> 3.2.1'")
136
+ file.puts("\tgem 'uglifier', '>= 1.0.3'")
137
+ file.puts("end")
138
+ end
139
+
140
+ if File.exists? "#{Rails.root.to_s}/public/index.html"
141
+ run "rm public/index.html"
142
+ end
143
+
144
+ run "bundle install"
145
+ end
146
+
147
+ def create_model
148
+ out "Creating model file and base definition"
149
+
150
+ model_directory = File.dirname(model_path)
151
+ create_dir(model_directory)
152
+
153
+ if create_file(model_path)
154
+ File.open(model_path, 'r+') do |file|
155
+ file.puts("Dir['#{Rails.root.to_s}/gen/models/*.rb'].each { |file| require file }")
156
+ file.puts("\nAPPLICATION = SurveyOnRails::Builder::build do")
157
+ file.puts("\tapplication do")
158
+ file.puts("\t\tmodel(:surveys => SURVEYS)")
159
+ file.puts("\n\t\t")
160
+ file.puts("\t\tview(:static_pages => STATIC_PAGES, :header => HEADER.first, :footer => FOOTER.first, :styles => STYLES, :forms => FORMS)")
161
+ file.puts("\tend")
162
+ file.puts("end")
163
+ end
164
+ end
165
+
166
+ model_file_path = "#{model_directory}/model.rb"
167
+ if create_file(model_file_path)
168
+ File.open(model_file_path, 'r+') do |file|
169
+ file.puts("\nSURVEYS = SurveyOnRails::Builder::build do")
170
+ file.puts("end")
171
+ end
172
+ end
173
+
174
+ style_file_path = "#{model_directory}/styles.rb"
175
+ if create_file(style_file_path)
176
+ File.open(style_file_path, 'r+') do |file|
177
+ file.puts("\nSTYLES = SurveyOnRails::Builder::build do")
178
+ file.puts("end")
179
+ end
180
+ end
181
+
182
+ static_file_path = "#{model_directory}/static.rb"
183
+ if create_file(static_file_path)
184
+ File.open(static_file_path, 'r+') do |file|
185
+ file.puts("STATIC_PAGES = SurveyOnRails::Builder::build do")
186
+ file.puts("end")
187
+
188
+ file.puts("\nHEADER = SurveyOnRails::Builder::build do")
189
+ file.puts("\theader do")
190
+ file.puts("\tend")
191
+ file.puts("end")
192
+
193
+ file.puts("\nFOOTER = SurveyOnRails::Builder::build do")
194
+ file.puts("\tfooter do")
195
+ file.puts("\tend")
196
+ file.puts("end")
197
+ end
198
+ end
199
+
200
+ view_file_path = "#{model_directory}/view.rb"
201
+ if create_file(view_file_path)
202
+ File.open(view_file_path, 'r+') do |file|
203
+ file.puts("FORMS = SurveyOnRails::Builder::build do")
204
+ file.puts("end")
205
+ end
206
+ end
207
+ end
208
+
209
+ def migrate
210
+ out "Performing database migrations"
211
+
212
+ rake "db:reset" if File.exists?("#{Rails.root.to_s}/db/schema.rb")
213
+ rake "db:migrate"
214
+ end
215
+
216
+ def populate
217
+ out "Populating database with users"
218
+
219
+ admin = User.create!(name: "Example User", email: "example@railstutorial.org", password: "foobar", password_confirmation: "foobar")
220
+ admin.toggle!(:admin)
221
+ admin2 = User.create!(name: "Daniel Kemper", email: "dkemp04@gmail.com", password: "foobar", password_confirmation: "foobar")
222
+ admin2.toggle!(:admin)
223
+
224
+ 99.times do |n|
225
+ name = Faker::Name.name
226
+ email = "example-#{n + 1}@railstutorial.org"
227
+ password = "password"
228
+ User.create!(name: name, email: email, password: password, password_confirmation: password)
229
+ end
230
+ end
231
+
232
+ def copy_assets
233
+ out "Copying assets to target directory"
234
+
235
+ ["images", "javascripts", "stylesheets"].each { |dir| copy_dir("gen/assets/#{dir}", "app/assets/#{dir}") }
236
+ copy_dir("gen/assets/static_pages", "app/views/static_pages")
237
+ end
238
+
239
+ def remove_migrations
240
+ migrations_folder = "db/migrate"
241
+ FileUtils.rm_rf(migrations_folder) if File.exists?(migrations_folder)
242
+ end
243
+
244
+ private
245
+
246
+ def copy_dir(source, dest)
247
+ create_dir(source)
248
+ create_dir(dest)
249
+ FileUtils.cp_r(Dir["#{source}/*"], "#{dest}")
250
+ end
251
+
252
+ def create_dir(dir)
253
+ FileUtils.mkdir_p(dir) unless File.exists?(dir)
254
+ end
255
+
256
+ def create_file(file)
257
+ if !File.exists?(file)
258
+ File.new(file, 'w')
259
+ return true
260
+ else
261
+ return false
262
+ end
263
+ end
264
+
265
+ def out(text)
266
+ puts "[SoR]: #{text}" if !options.silent?
267
+ end
268
+
269
+ def suppress_warnings
270
+ original_verbosity = $VERBOSE
271
+ $VERBOSE = nil
272
+ result = yield
273
+ $VERBOSE = original_verbosity
274
+ return result
275
+ end
276
+ end
@@ -0,0 +1,15 @@
1
+ <% define 'static' do %>
2
+ <% expand 'assets/assets::static' %>
3
+ <% expand 'controllers/controllers::static' %>
4
+ <% expand 'helpers/helpers::static' %>
5
+ <% expand 'models/models::static' %>
6
+ <% expand 'views/views::static' %>
7
+ <% end %>
8
+
9
+ <% define 'dynamic' do %>
10
+ <% expand 'assets/assets::dynamic' %>
11
+ <% expand 'controllers/controllers::dynamic' %>
12
+ <% expand 'helpers/helpers::dynamic' %>
13
+ <% expand 'models/models::dynamic' %>
14
+ <% expand 'views/views::dynamic' %>
15
+ <% end %>
@@ -0,0 +1,10 @@
1
+ <% define 'static' do %>
2
+ <% expand 'stylesheets/static_stylesheet::application_stylesheet' %>
3
+ <% expand 'stylesheets/static_stylesheet::static_stylesheet' %>
4
+ <% expand 'javascripts/application::static' %>
5
+ <% end %>
6
+
7
+ <% define 'dynamic' do %>
8
+ <% expand 'stylesheets/stylesheet::stylesheet', :for => view %>
9
+ <% expand 'javascripts/application::dynamic', :foreach => view.forms %>
10
+ <% end %>