Skip to content

Instantly share code, notes, and snippets.

@elchingon
Last active July 26, 2022 08:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elchingon/46518583bca2387c634a11ecee1360c5 to your computer and use it in GitHub Desktop.
Save elchingon/46518583bca2387c634a11ecee1360c5 to your computer and use it in GitHub Desktop.
Rails 5 Initialize commands for Doorkeeper, Devise, Rolify, Rspec
# Create Rails App
```
rails new app_name -d mysql
```
### cp config/database.example.yml
```
rake db:create
```
# Doorkeeper
```
rails generate doorkeeper:install
rails generate doorkeeper:migration
```
# Devise
```
rails generate devise:install
```
```
rake secret
```
Use generated key to replace config.secret_key = "" in config/initializers/devise.rb
### Add config/environments/development.rb, test.rb and production
```
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
```
```
rails generate devise User
Uncomment Trackable fields
rake db:migrate
rails g serializer user
```
Add serialization_scope to app/controllers/application_controller.rb
```
serialization_scope :current_ability
```
# Rolify
```
rails g rolify Role User
rake db:migrate
```
# Cancancan
```
rails g cancan:ability
```
# Simple Form
```
rails generate simple_form:install --bootstrap
```
# Additional optional commands
## Add name to user
```
rails g migration AddNameToUser name
rake db:migrate db:test:prepare
```
I like to add this before running migration to keep table field order neat
```
, after: :email
```
## Active Storage
```
rails active_storage:install
rake db:migrate db:test:prepare
```
## Set the master encryption key
```
EDITOR="vim" rails credentials:edit
```
## Rspec AND test env
```
rails generate rspec:install
```
### Copy over spec/support directory and add to rails_helper.rb:
```
require 'shoulda/matchers'
require 'capybara/rails'
require 'capybara/rspec'
require "cancan/matchers"
require "paperclip/matchers"
```
### Uncomment
```
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
```
### Add in Rspec.config in rails helper
```
config.include OAuthHelper
config.include FactoryBot::Syntax::Methods
config.include Paperclip::Shoulda::Matchers
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
# Uncomment if using VCR
#VCR.configure do |c|
# c.allow_http_connections_when_no_cassette = true
# c.cassette_library_dir = 'spec/cassettes'
# c.hook_into :webmock
# #c.default_cassette_options = { record: :new_episodes }
# #c.ignore_hosts 'codeclimate.com'
#end
config.before :each do
if Capybara.current_driver == :rack_test
DatabaseCleaner.strategy = :transaction
else
DatabaseCleaner.strategy = :truncation
end
DatabaseCleaner.start
end
config.after do
DatabaseCleaner.clean
end
```
### Add in rails_helper.rb after Rspec.config do ... end
```
Shoulda::Matchers.configure do |config|
config.integrate do |with|
# Choose a test framework:
with.test_framework :rspec
#with.test_framework :minitest
#with.test_framework :minitest_4
#with.test_framework :test_unit
# Choose one or more libraries:
#with.library :active_record
#with.library :active_model
#with.library :action_controller
# Or, choose the following (which implies all of the above):
with.library :rails
end
end
Capybara.default_max_wait_time = 8
```
## Add to users factory
```
factory :user do
sequence(:email) {|n| "user#{n}@example.org" }
password { 'blahblah' * 10 }
after(:create) {|user| user.add_role(:person)}
end
factory :admin, class: User do
sequence(:email) {|n| "admin#{n}@example.org" }
password { 'blahblah'* 10 }
after(:create) {|user| user.add_role(:admin)}
end
```
### Add oauth factories
spec/factories/oauth_access_token.rb
```
FactoryBot.define do
factory :oauth_access_token do
association :resource_owner, factory: :user
token { 'cabee01dead23beef89' }
refresh_token { 'dead23beef89cabee01' }
expires_in { 2592000 }
end
end
```
app/models/oauth_access_token.rb
```
class OauthAccessToken < ApplicationRecord
# needed for testing with FactoryGirl
end
```
spec/factories/oauth_application.rb
```
FactoryBot.define do
factory :oauth_application do
name { 'TestApp' }
uid { 'TestApp' }
secret { 'deadbeef' }
redirect_uri { 'http://e7.com/help/mobile' }
end
end
```
app/models/oauth_application.rb
```
class OauthApplication < ApplicationRecord
# needed for testing with FactoryGirl
end
```
## seed file
```
users = [
{ name:"Alexii Carey", email: "myemail@gmail.com", password: "MYPASSWORD", password_confirmation: "MYPASSWORD", role: :admin }
]
users.each do |user|
User.find_or_initialize_by(email: user[:email]).tap do |u|
u.name = user[:name]
u.password = user[:password]
u.password_confirmation = user[:password_confirmation]
u.save!
u.add_role user[:role]
end
end
roles = [
{ name: 'admin' }
]
roles.each do |role|
Role.find_or_initialize_by(name: role[:name]).tap do |r|
r.name = role[:name]
r.save!
end
end
# Don't forget to add the uid and secret here. I ususally copy or generate new ones
oauth_applications = [
{ name: "Mobile App", uid: "", secret: "", redirect_uri: "urn:ietf:wg:oauth:2.0:oob" },
]
oauth_applications.each do |oauth|
OauthApplication.find_or_initialize_by(uid: oauth[:uid]).tap do |oa|
oa.name = oauth[:name]
oa.secret = oauth[:secret]
oa.redirect_uri = oauth[:redirect_uri]
oa.save!
end
end
```
```
rake db:seed
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment