Class: RailPass::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/rail_pass/install_generator.rb

Constant Summary collapse

DIRECTORIES =
%w(app public spec vendor)
FILES_TO_REMOVE =
%w(README.rdoc public/index.html public/500.html public/404.html public/422.html app/assets/images/rails.png app/assets/javascripts/application.js app/assets/javascripts/application.js.coffee app/assets/stylesheets/application.css app/assets/stylesheets/application.css.scss app/views/layouts/application.html.erb app/helpers/application_helper.rb test/ vendor/plugins/)

Instance Method Summary collapse

Instance Method Details

#add_filesObject

Add default files & resources



105
106
107
108
109
110
# File 'lib/generators/rail_pass/install_generator.rb', line 105

def add_files
  DIRECTORIES.each do |dir|
    directory dir, dir
  end
  create_file ".rspec", "--color"
end

#add_gemsObject

Add necessary gems



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
# File 'lib/generators/rail_pass/install_generator.rb', line 39

def add_gems
  gem_group :development do
    gem 'quiet_assets'
    gem 'letter_opener'
    gem 'thin'
    gem 'awesome_print'
    # gem 'hirb'
    gem 'better_errors'
    gem 'binding_of_caller'
    gem 'pry'
  end
  gem_group :development, :test do
    gem 'rspec-rails'
    gem 'database_cleaner'
    gem 'dotenv'
  end
  gem_group :test do
    gem 'capybara'
    gem 'shoulda-matchers'
    gem 'spork-rails'
  end
  gem 'exception_notification', '2.6.1'
  gem 'slim'
  gem 'boarding_pass'
  gem 'foreman'

  if options[:database] == 'mongodb'
    gem 'bson_ext'
    gem 'mongoid'
  else
    gem 'pg',        group: :production
    gem 'sqlite3',   group: :development
    # gem 'rails-erd', group: :development
  end

  if options[:'app-server'] == 'puma'
    gem 'puma'
  elsif options[:'app-server'] == 'thin'
    gem 'thin'
  else
    gem 'unicorn'
  end

  if options[:deployment] == 'capistrano'
    gem 'capistrano'
  else
    gem 'newrelic_rpm'
  end

  Bundler.with_clean_env do
    run "bundle install"
  end
end

#application_configurationObject

Application configuration



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
# File 'lib/generators/rail_pass/install_generator.rb', line 115

def application_configuration
  # Routes
  route 'mount RailPass::Engine, :at => "styleguide"'
  route 'root :to => "static#index"'
  # Time zone
  gsub_file 'config/application.rb', /# config.time_zone = '.+'/ do
    "config.time_zone = \"Central Time (US & Canada)\""
  end
  # Email - Development
  inject_into_class "config/environments/development.rb", "Application" do
    dev_email = <<-eos.gsub(/^ {10}/,'')
    # Open emails in browser
    # 
    config.action_mailer.delivery_method = :letter_opener
    # config.action_mailer.default_url_options = { host: "localhost:3000" }  # FIXME replace with correct :host
    eos
    dev_email
  end
  # Render 404
  inject_into_file "app/controllers/application_controller.rb", :before => "end" do
    render_error = <<-eos.gsub(/^ {10}/,'')
    def render_404
      render "errors/404", status: :not_found
    end
    eos
    render_error
  end
  # Assets to precompile
  gsub_file 'config/environments/production.rb', /# config.assets.precompile.*/ do
    "config.assets.precompile += %w( html5.js polyfills.js )"
  end
  %w(.powder .env).each do |ignored|
    append_file ".gitignore", ignored + "\n"
  end
end

#configure_databaseObject

Database configuration



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
# File 'lib/generators/rail_pass/install_generator.rb', line 203

def configure_database
  if options[:database] == "mongodb"
    remove_file "config/database.yml"
    generate "mongoid:config"
    gsub_file 'config/application.rb', /require 'rails\/all'/ do
      mongo_railties = <<-eos.gsub(/^ {10}/,'')
      require "action_controller/railtie"
      require "action_mailer/railtie"
      require "active_resource/railtie"
      require "sprockets/railtie"
      require "rails/test_unit/railtie"
      eos
      mongo_railties
    end
    gsub_file 'config/application.rb', "config.active_record.whitelist_attributes = true" do
      "# config.active_record.whitelist_attributes = true"
    end
    gsub_file 'config/environments/development.rb', "config.active_record.auto_explain_threshold_in_seconds = 0.5" do
      "# config.active_record.auto_explain_threshold_in_seconds = 0.5"
    end
    gsub_file 'config/environments/development.rb', "config.active_record.mass_assignment_sanitizer = :strict" do
      "# config.active_record.mass_assignment_sanitizer = :strict"
    end
  end
end

#configure_deploymentObject

Deployment configuration



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
# File 'lib/generators/rail_pass/install_generator.rb', line 154

def configure_deployment
  if options[:deployment] == "capistrano"
    %w(Capfile config/deploy.rb).each do |file|
      copy_file file
    end
    directory "config/recipes"
  else
    %w(Procfile config/newrelic.yml config/initializers/new_relic.rb).each do |file|
      copy_file file
    end
    if options[:'app-server'] == "unicorn"
      copy_file "config/unicorn.rb"
    end
    # Heroku config for asset compiling, compression, and caching
    inject_into_class "config/application.rb", "Application" do
      compile_assets_config  = "# Enable compiling assets on deploy for Heroku\n"
      compile_assets_config += "config.assets.initialize_on_precompile = false"
      compile_assets_config
    end
    inject_into_file "config.ru", :before => "run " do
      "use Rack::Deflater\n"
    end
    cache_assets_config  = "config.serve_static_assets  = true\n"
    cache_assets_config += "config.static_cache_control = \"public, max-age=31536000\""
    gsub_file 'config/environments/production.rb', /config.serve_static_assets.*/, cache_assets_config
    # Email via SendGrid
    inject_into_class "config/environments/development.rb", "Application" do
      production_email = <<-eos.gsub(/^ {10}/,'')
      # Sending Email :: Mandrill
      # 
      # config.action_mailer.default_url_options = { host: "EXAMPLE.COM" }  # FIXME replace with proper :host
      config.action_mailer.delivery_method = :smtp
      config.action_mailer.smtp_settings = {
        :port           => '587',
        :address        => 'smtp.mandrillapp.com',
        :user_name      => ENV['MANDRILL_USERNAME'],
        :password       => ENV['MANDRILL_APIKEY'],
        :domain         => 'heroku.com',
        :authentication => :plain
      }
      eos
      production_email
    end
  end
end

#deploymentObject

Options



17
18
# File 'lib/generators/rail_pass/install_generator.rb', line 17

class_option :deployment, :type => :string, :default => "heroku", :banner => "NAME",
:desc => "Deployment method: 'heroku' or 'capistrano'", :aliases => "-d"

#remove_filesObject

Delete files on first-run



96
97
98
99
100
# File 'lib/generators/rail_pass/install_generator.rb', line 96

def remove_files
  FILES_TO_REMOVE.each do |file|
    remove_file file
  end
end

#shit_gonna_get_crazyObject

Warn about destructive changes & confirm



29
30
31
32
33
34
# File 'lib/generators/rail_pass/install_generator.rb', line 29

def shit_gonna_get_crazy
  unless options[:destructive]
    accept = ask("\n#{set_color(set_color("[?]", Thor::Shell::Color::ON_BLACK), Thor::Shell::Color::RED)} These changes are #{set_color("VERY DESTRUCTIVE", Thor::Shell::Color::RED)} and only intended for #{set_color("brand-new projects", Thor::Shell::Color::BLUE)}. Proceeding with the installation will wipe out large portions of an existing project. Type 'yes' to continue, anything else to cancel.\n:")
    exit unless accept == "yes"
  end
end