Class: InlineForms::Creator

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
bin/inline_forms

Constant Summary collapse

DATABASE_OPTIONS =
%w(sqlite mysql)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.databaseObject



42
43
44
# File 'bin/inline_forms', line 42

def self.database
  DATABASE_OPTIONS.include?(options[:database]) ? options[:database] : 'sqlite'
end

.dry_run?Boolean

Returns:



34
35
36
# File 'bin/inline_forms', line 34

def self.dry_run?
  options[:dry]
end

.emailObject



50
51
52
# File 'bin/inline_forms', line 50

def self.email
  options[:email]
end

.install_example?Boolean

Returns:



38
39
40
# File 'bin/inline_forms', line 38

def self.install_example?
  options[:example]
end

.passwordObject



54
55
56
# File 'bin/inline_forms', line 54

def self.password
  options[:password]
end

.source_rootObject



20
21
22
# File 'bin/inline_forms', line 20

def self.source_root
  File.dirname(__FILE__)+"/.."
end

.using_sqlite?Boolean

Returns:



46
47
48
# File 'bin/inline_forms', line 46

def self.using_sqlite?
  database == 'sqlite'
end

Instance Method Details

#create(app_name) ⇒ Object



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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'bin/inline_forms', line 32

def create(app_name)

  def self.dry_run?
    options[:dry]
  end

  def self.install_example?
    options[:example]
  end

  def self.database
    DATABASE_OPTIONS.include?(options[:database]) ? options[:database] : 'sqlite'
  end
  
  def self.using_sqlite?
    database == 'sqlite'
  end

  def self.email
    options[:email]
  end

  def self.password
    options[:password]
  end

  if install_example? && dry_run?
    say "--example and --dry-run can not be used together", :red
    exit 1
  end

  if install_example? && !using_sqlite?
    say "--example can only be used with an sqlite development database", :red
    exit 1
  end

  say "This is a dry run. I hope you know what you are doing...", :red if dry_run?

  say "Creating #{app_name} with inline_forms v#{VERSION} and development database #{database}...", :green

  regex = /\A[0-9a-zA-Z][0-9a-zA-Z_-]+[0-9a-zA-Z]\Z/
  if ! regex.match(app_name)
    say "Error: APP must match #{regex.source}", :red
    exit 1
  end

  if File.exists?(app_name)
    say "Error: APP exists", :red
    exit 1
  end

  say "- Generating Rails app '#{app_name}'..."
  if dry_run?
    empty_directory(app_name)
  else
    if ! run("rails new #{app_name}")
      say "Rails could not create the app '#{app_name}', maybe because it is a reserved word...", :red
      exit 1
    end
  end

  say "- Creating .ruby-version..."
  ruby_version = (%x[rvm current]).gsub(/@.*/,'')
  create_file "#{app_name}/.ruby-version", "#{ruby_version.chop}"

  say "- Changing to '#{app_name}'..."
  chdir(app_name) do
  run("rvm gemset create #{app_name}")
  run("rvm gemset use #{app_name}")
    say "- Working directory is now #{`pwd`}"
    say "- RVM gemset is now #{%x[rvm current]}"

    say "- Recreating Gemfile..."
  
    remove_file "#{app_name}/Gemfile" # the one that 'rails new' created
    create_file "#{app_name}/Gemfile", "      # generated by inline_forms v\#{VERSION}\n\n      source 'http://rubygems.org'\n\n      gem 'test-unit'\n      gem 'rails', '~> 3.2.12'\n      gem 'rake', '10.0.4'\n    gem 'jquery-rails', '~> 2.3.0'\n      gem 'jquery-ui-rails'\n      gem 'capistrano'\n      gem 'will_paginate', :git => 'git://github.com/acesuares/will_paginate.git'\n      gem 'tabs_on_rails', :git => 'git://github.com/acesuares/tabs_on_rails.git', :branch => 'update_remote'\n      gem 'ckeditor', :git => 'git://github.com/acesuares/ckeditor.git', :branch => 'master'\n      gem 'cancan', :git => 'git://github.com/acesuares/cancan.git', :branch => '2.0'\n      gem 'carrierwave'\n      gem 'remotipart', '~> 1.0'\n      gem 'paper_trail'\n      gem 'devise'\n      gem 'inline_forms'\n      gem 'validation_hints'\n      gem 'mini_magick'\n      gem 'jquery-ui-rails'\n      gem 'yaml_db'\n      gem 'rails-i18n'\n      gem 'i18n-active_record', :git => 'git://github.com/acesuares/i18n-active_record.git'\n      gem 'unicorn'\n      gem 'rvm'\n      gem 'rvm-capistrano'\n\n      # Include everything needed to run rake, tests, features, etc.\n      group :development do\n        gem 'sqlite3'\n        gem 'rspec-rails'\n        gem 'shoulda', '>= 0'\n        gem 'bundler'\n        gem 'jeweler'\n        # gem 'rcov', '>= 0'\n      end\n\n      # these are just for production\n      group :production do\n        gem 'mysql2'\n        gem 'therubyracer'\n        gem 'uglifier'\n      end\n    END_GEMFILE\n\n    say \"- Running bundle...\"\n    system \"bundle install\" unless dry_run?\n  \n    say \"- Database setup: creating config/database.yml with development database \#{database}\"\n    remove_file \"\#{app_name}/config/database.yml\" # the one that 'rails new' created\n    if using_sqlite?\n      create_file \"\#{app_name}/config/database.yml\", <<-END_DATABASEYML.strip_heredoc_with_indent\n      development:\n        adapter: sqlite3\n        database: db/development.sqlite3\n        pool: 5\n        timeout: 5000\n\n      END_DATABASEYML\n    else\n      create_file \"\#{app_name}/config/database.yml\", <<-END_DATABASEYML.strip_heredoc_with_indent\n      development:\n        adapter: mysql2\n        database: \#{app_name}_dev\n        username: \#{app_name}\n        password: \#{app_name}\n\n      END_DATABASEYML\n    end\n    append_file \"\#{app_name}/config/database.yml\", <<-END_DATABASEYML.strip_heredoc_with_indent\n      production:\n        adapter: mysql2\n        database: \#{app_name}_p\n        username: \#{app_name}\n        password: \#{app_name}444\n    END_DATABASEYML\n\n    say \"- Devise install...\"\n    run \"bundle exec rails g devise:install\" unless dry_run?\n\n    say \"- Devise User model install with added name and locale field...\"\n    run \"bundle exec rails g devise User name:string locale:string\" unless dry_run?\n\n    say \"- Replace Devise route and add path_prefix...\"\n    gsub_file \"\#{app_name}/config/routes.rb\", /devise_for :users/, \"devise_for :users, :path_prefix => 'auth'\"\n    insert_into_file \"\#{app_name}/config/routes.rb\", <<-ROUTE.strip_heredoc_with_indent(2), :after => \"devise_for :users, :path_prefix => 'auth'\\n\"\n      resources :users do\n        post 'revert', :on => :member\n      end\n    ROUTE\n\n    say \"- Create User Controller...\"\n    create_file \"\#{app_name}/app/controllers/users_controller.rb\", <<-USERS_CONTROLLER.strip_heredoc_with_indent\n      class UsersController < InlineFormsController\n        set_tab :user\n      end\n    USERS_CONTROLLER\n\n    say \"- Recreate User Model...\"\n    remove_file \"\#{app_name}/app/models/user.rb\" # the one that 'devise:install' created\n    create_file \"\#{app_name}/app/models/user.rb\", <<-USER_MODEL.strip_heredoc_with_indent\n      class User < ActiveRecord::Base\n\n        # devise options\n        devise :database_authenticatable\n        # devise :registerable # uncomment this if you want people to be able to register\n        devise :recoverable\n        devise :rememberable\n        devise :trackable\n        devise :validatable\n        # devise :token_authenticatable\n        # devise :confirmable,\n        # devise :lockable\n        # devise :timeoutable\n        # devise :omniauthable\n\n        # Setup accessible (or protected) attributes for your model\n        attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :locale\n        attr_writer :inline_forms_attribute_list\n\n        # validations\n        validates :name, :presence => true\n\n        # pagination\n        attr_reader :per_page\n        @per_page = 7\n\n        has_paper_trail\n\n        def _presentation\n          \"\\\#{name}\"\n        end\n\n        def inline_forms_attribute_list\n          @inline_forms_attribute_list ||= [\n            [ :name , 'name', :text_field ],\n            [ :email , 'email', :text_field ],\n            [ :password , 'Nieuw wachtwoord', :devise_password_field ],\n            [ :encrypted_password , 'encrypted_password', :info ],\n            [ :reset_password_token , 'reset_password_token', :info ],\n            [ :reset_password_sent_at , 'reset_password_sent_at', :info],\n            [ :remember_created_at , 'remember_created_at', :info ],\n            [ :sign_in_count , 'sign_in_count', :info ],\n            [ :current_sign_in_at , 'current_sign_in_at', :info ],\n            [ :last_sign_in_at , 'last_sign_in_at', :info ],\n            [ :current_sign_in_ip , 'current_sign_in_ip', :info ],\n            [ :last_sign_in_ip , 'last_sign_in_ip', :info ],\n            [ :created_at , 'created_at', :info ],\n            [ :updated_at , 'updated_at', :info ],\n          ]\n        end\n\n        def self.not_accessible_through_html?\n          false\n        end\n\n        def self.order_by_clause\n          'name'\n        end\n\n      end\n    USER_MODEL\n\n    say \"- Install ckeditor...\"\n    run \"bundle exec rails g ckeditor:install\" unless dry_run?\n\n    say \"- Create ckeditor config.js\"\n    copy_file \"lib/app/assets/javascripts/ckeditor/config.js\", \"\#{app_name}/app/assets/javascripts/ckeditor/config.js\"\n\n    say \"- Add remotipart to application.js...\"\n    create_file \"\#{app_name}/app/assets/javascripts/application.js\", \"//= require_tree .\\n\" if dry_run?\n    insert_into_file \"\#{app_name}/app/assets/javascripts/application.js\", \"//= require jquery.remotipart\\n\", :before => \"//= require_tree .\\n\"\n\n    say \"- Paper_trail install...\"\n    run \"bundle exec rails g paper_trail:install\" unless dry_run?\n\n    say \"- Generate models and tables and views for translations...\"\n    run 'rails g inline_forms InlineFormsLocale name:string inline_forms_translations:belongs_to _enabled:yes _presentation:\\'\#{name}\\''\n    run 'rails g inline_forms InlineFormsKey name:string inline_forms_translations:has_many inline_forms_translations:associated _enabled:yes _presentation:\\'\#{name}\\''\n    run 'rails g inline_forms InlineFormsTranslation inline_forms_key:belongs_to inline_forms_locale:dropdown value:text interpolations:text is_proc:boolean _presentation:\\'\#{value}\\''\n\n    sleep 1 # to get unique migration number\n    create_file \"\#{app_name}/db/migrate/\" + \n      Time.now.utc.strftime(\"%Y%m%d%H%M%S\") +\n      \"_\" +\n      \"inline_forms_create_view_for_translations.rb\", <<-VIEW_MIGRATION.strip_heredoc_with_indent\n      class InlineFormsCreateViewForTranslations < ActiveRecord::Migration\n\n        def self.up\n          execute 'CREATE VIEW translations\n                   AS\n                     SELECT L.name AS locale,\n                            K.name AS thekey,\n                            T.value AS value,\n                            T.interpolations AS interpolations,\n                            T.is_proc AS is_proc\n                       FROM inline_forms_keys K, inline_forms_locales L, inline_forms_translations T\n                         WHERE T.inline_forms_key_id = K.id AND T.inline_forms_locale_id = L.id '\n        end\n\n        def self.down\n          execute 'DROP VIEW translations'\n        end\n\n      end\n    VIEW_MIGRATION\n    \n    say \"- Migrating Database\"\n    run \"bundle exec rake db:migrate\" unless (dry_run? || !using_sqlite?)\n\n    say \"- Adding admin user with email: \#{email}, password: \#{password} to seeds.rb\"\n    append_to_file \"\#{app_name}/db/seeds.rb\", \"User.new({ :email => '\#{email}', :name => 'Admin', :password => '\#{password}', :password_confirmation => '\#{password}'}).save\"\n\n    say \"- Seeding the database\"\n    run \"bundle exec rake db:seed\" unless (dry_run? || !using_sqlite?)\n\n    say \"- Creating header in app/views/inline_forms/_header.html.erb...\"\n    create_file \"\#{app_name}/app/views/inline_forms/_header.html.erb\", <<-END_HEADER.strip_heredoc_with_indent\n        <div id='Header'>\n          <div id='title'>\n            \#{app_name} v<%= inline_forms_version -%>\n          </div>\n          <% if current_user -%>\n          <div id='logout'>\n            <%= link_to \\\"Afmelden: \\\#{current_user.name}\\\", destroy_user_session_path, :method => :delete %>\n          </div>\n          <% end -%>\n          <div style='clear: both;'></div>\n        </div>\n    END_HEADER\n\n    say \"- Recreating ApplicationHelper to set application_name and application_title...\"\n    remove_file \"\#{app_name}/app/helpers/application_helper.rb\" # the one that 'rails new' created\n    create_file \"\#{app_name}/app/helpers/application_helper.rb\", <<-END_APPHELPER.strip_heredoc_with_indent\n      module ApplicationHelper\n        def application_name\n          '\#{app_name}'\n        end\n        def application_title\n          '\#{app_name}'\n        end\n      end\n    END_APPHELPER\n\n    say \"- Recreating ApplicationController to add devise, cancan, I18n stuff...\"\n    remove_file \"\#{app_name}/app/controllers/application_controller.rb\" # the one that 'rails new' created\n    create_file \"\#{app_name}/app/controllers/application_controller.rb\", <<-END_APPCONTROLLER.strip_heredoc_with_indent\n      class ApplicationController < InlineFormsApplicationController\n        protect_from_forgery\n\n        # Comment next two lines if you don't want Devise authentication\n        before_filter :authenticate_user!\n        layout 'devise' if :devise_controller?\n\n        # Comment next 6 lines if you want CanCan authorization\n        enable_authorization :unless => :devise_controller?\n\n        rescue_from CanCan::Unauthorized do |exception|\n          sign_out :user if user_signed_in?\n          redirect_to new_user_session_path, :alert => exception.message\n        end\n\n        # Uncomment next line if you want I18n (based on subdomain)\n        # before_filter :set_locale\n\n        # Uncomment next line and specify default locale\n        # I18n.default_locale = :en\n\n        # Uncomment next line and specify available locales\n        # I18n.available_locales = [ :en, :nl, :pp ]\n\n        # Uncomment next nine line if you want locale based on subdomain, like 'it.example.com, de.example.com'\n        # def set_locale\n        #   I18n.locale = extract_locale_from_subdomain || I18n.default_locale\n        # end\n        #\n        # def extract_locale_from_subdomain\n        #   locale = request.subdomains.first\n        #   return nil if locale.nil?\n        #   I18n.available_locales.include?(locale.to_sym) ? locale.to_s : nil\n        # end\n      end\n    END_APPCONTROLLER\n\n    say \"- Creating Ability model so that the user with id = 1 can access all...\"\n    create_file \"\#{app_name}/app/models/ability.rb\", <<-END_ABILITY.strip_heredoc_with_indent\n      class Ability\n        include CanCan::Ability\n\n        def initialize(user)\n          # See the wiki for details: https://github.com/ryanb/cancan/wiki/Defining-Abilities\n\n          user ||= user.new # guest user\n\n          if user.id == 1 #quick hack\n            can :access, :all\n          else\n            # put restrictions for other users here\n          end\n        end\n      end\n    END_ABILITY\n\n    # create environments/production.rb if it's a dry run\n    create_file \"\#{app_name}/config/environments/production.rb\", \"  # config.assets.precompile += %w( search.js )\\nend\\n\" if dry_run?\n\n    say \"- Injecting precompile assets stuff in environments/production.rb...\"\n    insert_into_file \"\#{app_name}/config/environments/production.rb\",\n      \"  config.assets.precompile += %w(inline_forms_application.js inline_forms_application.css devise.css)\\n\",\n      :after => \"  # config.assets.precompile += %w( search.js )\\n\"\n\n    say \"- Injecting devise mailer stuff in environments/production.rb...\"\n    insert_into_file \"\#{app_name}/config/environments/production.rb\", <<-DEVISE_MAILER_STUFF.strip_heredoc_with_indent(2), :before => \"end\\n\"\n\n      # for devise\n      config.action_mailer.default_url_options = { :protocol => 'https', :host => 'YOURHOSTNAME' }\n      config.action_mailer.delivery_method = :smtp\n      config.action_mailer.smtp_settings = {\n        :address => 'YOURMAILSERVER',\n        :enable_starttls_auto => true,\n        :password => 'YOURPASSWORD',\n        :user_name => 'YOURUSERNAME'\n      }\n      \n    DEVISE_MAILER_STUFF\n\n    say \"- Setting config.assets.compile to true in environments/production.rb (needed for ckeditor)...\"\n    insert_into_file \"\#{app_name}/config/environments/production.rb\", \"  config.assets.compile = false\\n\", :before => \"end\\n\" if dry_run?\n    gsub_file \"\#{app_name}/config/environments/production.rb\", /config.assets.compile = false/, \"config.assets.compile = true\"\n\n    say \"- Capify...\"\n    run 'capify .'\n    remove_file \"\#{app_name}/config/deploy.rb\" # remove the file capify created!\n    copy_file \"lib/generators/templates/deploy.rb\", \"\#{app_name}/config/deploy.rb\"\n\n    say \"- Unicorn Config...\"\n    copy_file \"lib/generators/templates/unicorn.rb\", \"\#{app_name}/config/unicorn.rb\"\n\n    say \"- Initializing git...\"\n    run 'git init'\n    create_file \"\#{app_name}/.gitignore\", \"/tmp\\n\" if dry_run?\n    insert_into_file \"\#{app_name}/.gitignore\", <<-GITIGNORE.strip_heredoc_with_indent, :after => \"/tmp\\n\"\n      # netbeans\n      nbproject\n      # remotipart uploads\n      public/uploads\n    GITIGNORE\n\n    run 'git add .'\n    run 'git commit -a -m \" * Initial\"'\n\n    if install_example?\n      say \"\\nInstalling example application...\"\n      run 'rails g inline_forms Picture name:string caption:string image:image_field description:text apartment:belongs_to _presentation:\\'\#{name}\\''\n      run 'rails generate uploader Image'\n      run 'rails g inline_forms Apartment name:string title:string description:text pictures:has_many pictures:associated _enabled:yes _presentation:\\'\#{name}\\''\n      run 'bundle exec rake db:migrate'\n      say \"\\nDone! Now point your browser to http://localhost:3000/apartments !\", :yellow\n      say \"\\nPress ctlr-C to quit...\", :yellow\n      run 'rails s'\n    else\n      say \"\\nDone! Now make your tables with 'rails g inline_forms ...\", :yellow\n      #say \"- Don't forget: edit .rvmrc, config/{routes.rb, deploy.rb}, .git/config, delete public/index.html\\n\"\n    end\n\n  end\nend\n".strip_heredoc_with_indent