Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 2b040d5

Browse files
authored
Upgrading to Rails 5 (#41)
Closes #5
1 parent b46a191 commit 2b040d5

File tree

9 files changed

+160
-71
lines changed

9 files changed

+160
-71
lines changed

pathfinder.rb

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_relative 'recipes/assets'
44
require_relative 'recipes/bower_rails'
55
require_relative 'recipes/carrier_wave'
6+
require_relative 'recipes/configuration'
67
require_relative 'recipes/devise'
78
require_relative 'recipes/git_ignore'
89
require_relative 'recipes/postgres'
@@ -63,80 +64,22 @@ def call
6364
ask_for_recipes
6465
@template.instance_exec(self) do |pathfinder|
6566
utils = Recipes::Utils.new(self)
67+
configuration = Recipes::Configuration.new(self)
6668

6769
remove_file 'Gemfile'
6870
run 'touch Gemfile'
6971
add_source 'https://rubygems.org'
7072

7173
append_file 'Gemfile', "ruby \'#{utils.ask_with_default('Which version of ruby do you want to use?', default: RUBY_VERSION)}\'"
7274

73-
gem 'rails', Rails.version
74-
# Model
75-
gem 'aasm'
76-
gem 'keynote'
77-
gem 'paranoia' if yes?('Do you want to use Soft Deletes?')
78-
# Searchs
79-
gem 'ransack' if yes?('Do you want to use Ransack?')
80-
gem 'kaminari'
81-
gem 'searchkick' if yes?('Are you going to use ElasticSearch?')
82-
# Jobs
83-
gem 'redis'
84-
gem 'sidekiq'
85-
gem 'sinatra', require: nil
86-
# Emails
87-
gem 'premailer-rails'
88-
89-
pathfinder.generate_gems
90-
91-
gem_group :development, :test do
92-
gem 'bundler-audit', require: false
93-
gem 'byebug'
94-
gem 'rspec-rails'
95-
gem 'spring'
96-
gem 'quiet_assets'
97-
gem 'figaro'
98-
gem 'mailcatcher'
99-
gem 'factory_girl_rails'
100-
gem 'faker'
101-
gem 'pry-rails'
102-
gem 'pry-coolline'
103-
gem 'pry-byebug'
104-
gem 'rubocop', require: false
105-
end
106-
107-
gem_group :development do
108-
gem 'spring-commands-rspec', require: false
109-
gem 'better_errors'
110-
end
111-
112-
gem_group :test do
113-
gem 'simplecov', require: false
114-
gem 'capybara', require: false
115-
gem 'capybara-webkit', require: false
116-
gem 'database_cleaner', require: false
117-
gem 'fakeredis', require: false
118-
gem 'poltergeist', require: false
119-
gem 'shoulda-matchers', require: false
75+
configuration.gems do
76+
pathfinder.generate_gems
12077
end
12178

12279
after_bundle do
12380
run 'spring stop'
12481

125-
inside 'config' do
126-
create_file 'application.yml'
127-
create_file 'application.yml.example'
128-
remove_file 'routes.rb'
129-
create_file 'routes.rb' do <<~RUBY
130-
Rails.application.routes.draw do
131-
end
132-
RUBY
133-
end
134-
end
135-
136-
create_file '.rubocop.yml' do <<~CODE
137-
inherit_from: https://raw.githubusercontent.com/MarsBased/marstyle/master/ruby/.rubocop.yml
138-
CODE
139-
end
82+
configuration.init_file
14083

14184
pathfinder.generate_initializers
14285

recipes/airbrake.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@ def gems
88
def init_file
99
@template.initializer 'airbrake.rb', <<~CODE
1010
Airbrake.configure do |config|
11-
config.api_key = ENV['AIRBRAKE_API_KEY']
11+
config.project_id = ENV['AIRBRAKE_PROJECT_ID']
12+
config.project_key = ENV['AIRBRAKE_PROJECT_KEY']
13+
config.root_directory = Rails.root
14+
config.logger = Rails.logger
15+
config.environment = Rails.env
16+
config.ignore_environments = %w(test)
17+
config.blacklist_keys = [/password/i, /authorization/i]
1218
end
1319
CODE
1420

1521
@template.inside 'config' do
16-
@template.append_file 'application.yml.example', "\nAIRBRAKE_API_KEY: ''"
17-
@template.append_file 'application.yml', "\nAIRBRAKE_API_KEY: ''"
22+
@template.append_file 'application.yml.example', "\nAIRBRAKE_PROJECT_ID: ''"
23+
@template.append_file 'application.yml.example', "\nAIRBRAKE_PROJECT_KEY: ''"
24+
@template.append_file 'application.yml', "\nAIRBRAKE_PROJECT_ID: ''"
25+
@template.append_file 'application.yml', "\nAIRBRAKE_PROJECT_KEY: ''"
1826
end
1927
end
2028
end

recipes/assets.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module Recipes
22
class Assets < Base
33

44
def gems
5-
# Assets
65
@template.gem 'bootstrap-sass', '~> 3.3.3'
76
@template.gem 'bootstrap-datepicker-rails', '~> 1.6.0' if @template.yes?('Do you want to use Bootstrap datepicker?')
87
@template.gem 'font-awesome-sass', '~> 4.3.0'
@@ -15,5 +14,6 @@ def gems
1514
@template.gem 'uglifier', '>= 1.3.0'
1615
@template.gem 'sdoc', '~> 0.4.0', group: :doc
1716
end
17+
1818
end
1919
end

recipes/carrier_wave.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def init_file
1515
config.fog_directory = ENV['AWS_S3_BUCKET']
1616
config.fog_public = true
1717
config.storage = :fog
18-
config.cache_dir = Rails.root.join('tmp/cache')
18+
config.cache_dir = Rails.root.join('tmp', 'cache')
1919
2020
config.fog_credentials = {
2121
provider: 'AWS',

recipes/configuration.rb

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
module Recipes
2+
class Configuration < Base
3+
def initialize(template)
4+
@template = template
5+
end
6+
7+
def gems
8+
@template.gem 'rails', '~> 5.0'
9+
# Model
10+
@template.gem 'aasm'
11+
@template.gem 'keynote'
12+
@template.gem 'paranoia' if @template.yes?('Do you want to use Soft Deletes?')
13+
# Searchs
14+
@template.gem 'ransack' if @template.yes?('Do you want to use Ransack?')
15+
@template.gem 'kaminari'
16+
@template.gem 'searchkick' if @template.yes?('Are you going to use ElasticSearch?')
17+
# Emails
18+
@template.gem 'premailer-rails'
19+
20+
yield if block_given?
21+
22+
@template.gem_group :development, :test do |group|
23+
group.gem 'bundler-audit', require: false
24+
group.gem 'byebug'
25+
group.gem 'rspec-rails'
26+
group.gem 'spring'
27+
group.gem 'figaro'
28+
group.gem 'mailcatcher'
29+
group.gem 'factory_girl_rails'
30+
group.gem 'faker'
31+
group.gem 'listen'
32+
group.gem 'pry-rails'
33+
group.gem 'pry-coolline'
34+
group.gem 'pry-byebug'
35+
group.gem 'rubocop', require: false
36+
end
37+
38+
@template.gem_group :development do |group|
39+
group.gem 'spring-commands-rspec', require: false
40+
group.gem 'better_errors'
41+
end
42+
43+
@template.gem_group :test do |group|
44+
group.gem 'simplecov', require: false
45+
group.gem 'capybara', require: false
46+
group.gem 'capybara-webkit', require: false
47+
group.gem 'database_cleaner', require: false
48+
group.gem 'fakeredis', require: false
49+
group.gem 'poltergeist', require: false
50+
group.gem 'shoulda-matchers', require: false
51+
end
52+
end
53+
54+
def init_file
55+
create_application_yml
56+
create_routes_file
57+
set_error_handling
58+
add_rubocop
59+
end
60+
61+
private
62+
63+
def create_application_yml
64+
@template.create_file 'config/application.yml'
65+
@template.create_file 'config/application.yml.example'
66+
end
67+
68+
def create_routes_file
69+
@template.remove_file 'config/routes.rb'
70+
@template.create_file 'config/routes.rb' do <<~RUBY
71+
Rails.application.routes.draw do
72+
end
73+
RUBY
74+
end
75+
end
76+
77+
def set_error_handling
78+
@template.insert_into_file 'config/routes.rb', after: "Rails.application.routes.draw do\n" do <<~RUBY
79+
match '/404', to: 'errors#not_found', via: :all
80+
match '/500', to: 'errors#internal_error', via: :all
81+
RUBY
82+
end
83+
@template.insert_into_file 'config/application.rb', after: "class Application < Rails::Application\n" do <<~RUBY
84+
\tconfig.exceptions_app = self.routes
85+
RUBY
86+
end
87+
@template.run 'rails g controller errors not_found internal_error --no-assets --skip-routes --no-controller-specs --no-view-specs --no-helper'
88+
@template.remove_file 'app/controllers/errors_controller.rb'
89+
@template.copy_file(File.join(File.dirname(__FILE__), 'controllers', 'errors_controller.rb'), 'app/controllers/errors_controller.rb')
90+
end
91+
92+
def add_rubocop
93+
@template.create_file '.rubocop.yml' do <<~CODE
94+
inherit_from: https://raw.githubusercontent.com/MarsBased/marstyle/master/ruby/.rubocop.yml
95+
CODE
96+
end
97+
end
98+
99+
end
100+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class ErrorsController < ApplicationController
2+
3+
def not_found
4+
render(status: 404)
5+
end
6+
7+
def internal_error
8+
render(status: 500)
9+
end
10+
11+
end

recipes/redis.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
module Recipes
22
class Redis < Base
33

4+
def gems
5+
@template.gem 'redis'
6+
end
7+
48
def init_file
59
@template.initializer 'redis.rb', <<~CODE
610
Redis.current = Redis.new(Rails.application.config_for(:redis))

recipes/sidekiq.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
module Recipes
22
class Sidekiq < Base
33

4+
def gems
5+
@template.gem 'sidekiq'
6+
@template.gem 'sinatra', require: nil
7+
end
8+
49
def init_file
10+
add_initializer
11+
add_configuration
12+
add_sidekiq_web
13+
set_job_queue
14+
end
15+
16+
private
17+
18+
def add_initializer
519
@template.initializer 'sidekiq.rb', <<~CODE
620
redis_host = Redis.current.client.host
721
redis_port = Redis.current.client.port
@@ -14,7 +28,9 @@ def init_file
1428
config.redis = { url: "redis://\#{redis_host}:\#{redis_port}" }
1529
end
1630
CODE
31+
end
1732

33+
def add_configuration
1834
@template.inside 'config' do
1935
@template.create_file 'sidekiq.yml' do <<~EOF
2036
---
@@ -30,7 +46,9 @@ def init_file
3046
EOF
3147
end
3248
end
49+
end
3350

51+
def add_sidekiq_web
3452
@template.insert_into_file 'config/routes.rb', before: "Rails.application.routes.draw do\n" do <<~RUBY
3553
require 'sidekiq/web'
3654
RUBY
@@ -42,8 +60,13 @@ def init_file
4260
mount Sidekiq::Web => '/sidekiq'
4361
end
4462
end
45-
get '/404', to: 'errors#not_found'
46-
get '/500', to: 'errors#internal_error'
63+
RUBY
64+
end
65+
end
66+
67+
def set_job_queue
68+
@template.insert_into_file 'config/application.rb', after: "class Application < Rails::Application\n" do <<~RUBY
69+
\tconfig.active_job.queue_adapter = :sidekiq
4770
RUBY
4871
end
4972
end

template.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
if Rails.version[0].to_i >= 5
1+
if Rails.version[0].to_i < 5
22
puts ''
3-
puts "\e[31mPathfinder only works with Rails versions lower than 5.0.\e[0m"
3+
puts "\e[31mPathfinder only works with Rails versions greater or equal to 5.0.\e[0m"
44
exit
55
end
66

0 commit comments

Comments
 (0)