Module: Onotole::DefalutScripts

Included in:
AppBuilder
Defined in:
lib/onotole/default_scripts.rb

Instance Method Summary collapse

Instance Method Details

#add_custom_formbuilderObject



212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/onotole/default_scripts.rb', line 212

def add_custom_formbuilder
  config = <<-DATA

config.after_initialize do
  ActionView::Base.default_form_builder = FormBuilderExtension
end

  DATA
  inject_into_class 'config/application.rb', 'Application', config
  File.open('app/helpers/form_builder_extension.rb', 'w') do |f|
    f.write 'class FormBuilderExtension < ActionView::Helpers::FormBuilder'
    f.write "\nend"
  end
end

#add_dotenv_to_startupObject



198
199
200
# File 'lib/onotole/default_scripts.rb', line 198

def add_dotenv_to_startup
  inject_into_file('config/application.rb', "\nDotenv::Railtie.load\n", after: 'Bundler.require(*Rails.groups)')
end

#apply_vendorjs_folderObject



194
195
196
# File 'lib/onotole/default_scripts.rb', line 194

def apply_vendorjs_folder
  inject_into_file(AppBuilder.js_file, "//= require_tree ../../../vendor/assets/javascripts/.\n", before: '//= require_tree .')
end

#configure_background_jobs_for_rspecObject



85
86
87
# File 'lib/onotole/default_scripts.rb', line 85

def configure_background_jobs_for_rspec
  rails_generator 'delayed_job:active_record'
end

#configure_i18n_for_missing_translationsObject



96
97
98
99
# File 'lib/onotole/default_scripts.rb', line 96

def configure_i18n_for_missing_translations
  raise_on_missing_translations_in('development')
  raise_on_missing_translations_in('test')
end

#configure_rack_timeoutObject



101
102
103
104
# File 'lib/onotole/default_scripts.rb', line 101

def configure_rack_timeout
  rack_timeout_config = 'Rack::Timeout.timeout = (ENV["RACK_TIMEOUT"] || 10).to_i'
  append_file 'config/environments/production.rb', rack_timeout_config
end

#configure_support_pathObject



186
187
188
189
190
191
192
# File 'lib/onotole/default_scripts.rb', line 186

def configure_support_path
  mkdir_and_touch 'app/support'
  copy_file 'support.rb', 'config/initializers/support.rb'

  config = "\n    config.autoload_paths << Rails.root.join('app/support')\n"
  inject_into_class 'config/application.rb', 'Application', config
end

#configure_time_formatsObject

copy ru_locale here also. Update in future



90
91
92
93
94
# File 'lib/onotole/default_scripts.rb', line 90

def configure_time_formats
  remove_file 'config/locales/en.yml'
  template 'config_locales_en.yml.erb', 'config/locales/en.yml'
  template 'config_locales_ru.yml.erb', 'config/locales/ru.yml'
end

#copy_application_recordObject



227
228
229
# File 'lib/onotole/default_scripts.rb', line 227

def copy_application_record
  copy_file 'application_record.rb', 'app/models/application_record.rb', force: true
end

#copy_dotfilesObject



111
112
113
114
115
# File 'lib/onotole/default_scripts.rb', line 111

def copy_dotfiles
  directory 'dotfiles', '.', force: true
  template 'dotenv.erb', '.env'
  template 'dotenv_production.erb', '.env.production'
end

#copy_miscellaneous_filesObject



158
159
160
161
162
# File 'lib/onotole/default_scripts.rb', line 158

def copy_miscellaneous_files
  copy_file 'browserslist', 'browserslist'
  copy_file 'errors.rb', 'config/initializers/errors.rb'
  copy_file 'json_encoding.rb', 'config/initializers/json_encoding.rb'
end

#create_databaseObject



72
73
74
# File 'lib/onotole/default_scripts.rb', line 72

def create_database
  bundle_command 'exec rake db:drop db:create db:migrate db:seed'
end

#disallow_wrapping_parametersObject



61
62
63
# File 'lib/onotole/default_scripts.rb', line 61

def disallow_wrapping_parameters
  remove_file 'config/initializers/wrap_parameters.rb'
end

#enable_rack_canonical_hostObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/onotole/default_scripts.rb', line 164

def enable_rack_canonical_host
  config = <<-RUBY

  if ENV.fetch("HEROKU_APP_NAME", "").include?("staging-pr-")
ENV["#{app_name.upcase}_APPLICATION_HOST"] = ENV["HEROKU_APP_NAME"] + ".herokuapp.com"
  end

  # Ensure requests are only served from one, canonical host name
  config.middleware.use Rack::CanonicalHost, ENV.fetch("#{app_name.upcase}_APPLICATION_HOST")
  RUBY

  inject_into_file(
    'config/environments/production.rb',
    config,
    after: 'Rails.application.configure do'
  )
end

#enable_rack_deflaterObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/onotole/default_scripts.rb', line 18

def enable_rack_deflater
  config = <<-RUBY

  # Enable deflate / gzip compression of controller-generated responses
  # more info https://robots.thoughtbot.com/content-compression-with-rack-deflater
  # rack-mini-profiler does not work with this option
  config.middleware.use Rack::Deflater

  RUBY

  inject_into_file(
    'config/environments/production.rb',
    config,
    after: serve_static_files_line
  )
end

#fix_i18n_deprecation_warningObject



106
107
108
109
# File 'lib/onotole/default_scripts.rb', line 106

def fix_i18n_deprecation_warning
  config = '    config.i18n.enforce_available_locales = true'
  inject_into_class 'config/application.rb', 'Application', config
end

#provide_dev_prime_taskObject



14
15
16
# File 'lib/onotole/default_scripts.rb', line 14

def provide_dev_prime_task
  copy_file 'dev.rake', 'lib/tasks/dev.rake'
end

#provide_kill_postgres_connections_taskObject



202
203
204
# File 'lib/onotole/default_scripts.rb', line 202

def provide_kill_postgres_connections_task
  copy_file 'kill_postgress_conections.rake', 'lib/tasks/kill_postgress_conections.rake'
end

#provide_setup_scriptObject



9
10
11
12
# File 'lib/onotole/default_scripts.rb', line 9

def provide_setup_script
  template 'bin_setup', 'bin/setup', force: true
  run 'chmod a+x bin/setup'
end

#raise_on_unpermitted_parametersObject



4
5
6
7
# File 'lib/onotole/default_scripts.rb', line 4

def raise_on_unpermitted_parameters
  config = "\n    config.action_controller.action_on_unpermitted_parameters = :raise\n"
  inject_into_class 'config/application.rb', 'Application', config
end

#readmeObject



182
183
184
# File 'lib/onotole/default_scripts.rb', line 182

def readme
  template 'README.md.erb', 'README.md'
end

#remove_config_comment_linesObject



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/onotole/default_scripts.rb', line 122

def remove_config_comment_lines
  config_files = [
    'application.rb',
    'environment.rb',
    'environments/development.rb',
    'environments/production.rb',
    'environments/test.rb'
  ]

  config_files.each do |config_file|
    cleanup_comments File.join(destination_root, "config/#{config_file}")
  end
end

#remove_routes_comment_linesObject



136
137
138
139
140
# File 'lib/onotole/default_scripts.rb', line 136

def remove_routes_comment_lines
  replace_in_file 'config/routes.rb',
                  /Rails\.application\.routes\.draw do.*end/m,
                  "Rails.application.routes.draw do\nend"
end

#replace_gemfileObject



76
77
78
79
# File 'lib/onotole/default_scripts.rb', line 76

def replace_gemfile
  remove_file 'Gemfile'
  template 'Gemfile.erb', 'Gemfile'
end

#seeds_organisationObject



206
207
208
209
210
# File 'lib/onotole/default_scripts.rb', line 206

def seeds_organisation
  remove_file 'db/seeds.rb'
  copy_file 'seeds.rb', 'db/seeds.rb'
  mkdir_and_touch 'db/seeds'
end

#set_ruby_to_version_being_usedObject



81
82
83
# File 'lib/onotole/default_scripts.rb', line 81

def set_ruby_to_version_being_used
  create_file '.ruby-version', "#{Onotole::RUBY_VERSION}\n"
end

#set_up_foregoObject



35
36
37
# File 'lib/onotole/default_scripts.rb', line 35

def set_up_forego
  template 'Procfile.erb', 'Procfile', force: true
end

#setup_default_rake_taskObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/onotole/default_scripts.rb', line 142

def setup_default_rake_task
  append_file 'Rakefile' do
    <<-EOS
task(:default).clear
task default: [:spec]

if defined? RSpec
  task(:spec).clear
  RSpec::Core::RakeTask.new(:spec) do |t|
t.verbose = false
  end
end
    EOS
  end
end

#setup_secret_tokenObject



53
54
55
56
57
58
59
# File 'lib/onotole/default_scripts.rb', line 53

def setup_secret_token
  copy_file 'secrets.yml', 'config/secrets.yml', force: true
  # strange bug with ERB in ERB. solved this way
  replace_in_file 'config/secrets.yml',
                  "<%= ENV['SECRET_KEY_BASE'] %>",
                  "<%= ENV['#{app_name.upcase}_SECRET_KEY_BASE'] %>"
end

#setup_springObject



117
118
119
120
# File 'lib/onotole/default_scripts.rb', line 117

def setup_spring
  bundle_command 'exec spring binstub --all'
  bundle_command 'exec spring stop'
end

#setup_staging_environmentObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/onotole/default_scripts.rb', line 39

def setup_staging_environment
  staging_file = 'config/environments/staging.rb'
  copy_file 'staging.rb', staging_file

  config = <<-RUBY

Rails.application.configure do
  # ...
end
  RUBY

  append_file staging_file, config
end

#use_postgres_config_templateObject



65
66
67
68
69
70
# File 'lib/onotole/default_scripts.rb', line 65

def use_postgres_config_template
  template 'postgresql_database.yml.erb', 'config/database.yml',
           force: true
  template 'postgresql_database.yml.erb', 'config/database.yml.sample',
           force: true
end