Awesome
Railstom
Rails Custom
App configuration
Dependencies
Ruby
You must use ruby 2.0.0-p247 or higher.
Libs
Linux
Install ImageMagick (for photo processing):
$ sudo apt-get install imagemagick
MacOS
$ brew install v8
$ brew install imagemagick
$ brew install phantomjs
Redis
MacOS X
$ brew install redis
(...)
==> Caveats
If this is your first install, automatically load on login with:
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/redis/2.6.2/homebrew.mxcl.redis.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
If this is an upgrade and you already have the homebrew.mxcl.redis.plist loaded:
launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
cp /usr/local/Cellar/redis/2.6.2/homebrew.mxcl.redis.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist
To start redis manually:
redis-server /usr/local/etc/redis.conf
To access the server:
redis-cli
==> Summary
Debian
$ sudo apt-get install redis-server
Will run on system boot. Use redis-cli to manage.
Basic
- copy
.ruby-version.example
to.ruby-version
- copy
.rspec.example
to.rspec
and edit - copy
config/database.yml.example
toconfig/database.yml
and edit - copy
config/application.yml.example
toconfig/application.yml
and edit
Create databases and run:
$ rake db:create
$ rake db:schema:load
$ RAILS_ENV=test rake db:schema:load
You can also use the reseed.sh
script to drop current database and create a fresh one with seed data.
$ script/reseed.sh
Rename application:
$ rails g rename:app_to YourAppName
Set your default metadata in en.yml
and pl.yml
:
# config/locale/en.yml
layouts:
application:
metadata:
title: 'Railstom'
description: ''
keywords: ''
Additional configuration
TODO informations
Run rake notes
to find useful TODO informations inside of the app.
Database pool
In config/database.yml
file the pool
setting is equal 25 because sidekiq creates 25 processors. More info: Sidekiq Concurrency.
Database populate
You can use ffaker or forgery gem to populate your database.
To populate db with users run:
$ bundle exec rake db:populate:users
Template engine
Set your default template engine:
# config/application.rb
config.generators do |g|
# use one of those: :erb, :haml
g.template_engine :erb
end
I18n-js
Edit config/i18n-js.yml
to add a new translation namespace for I18n-js.
AngularJS templates
Put your angular template your_template_name.html.erb
in app/views/pages/templates/
if you need use erb/haml. Access to this template is through the route /templates/your_template_name
.
If you use only html without erb/haml then put your angular template your_template_name.html
in /public/templates/
.
HighVoltage
Put your page contact.html.erb
in app/views/pages/
then it will be accessible via page_path(:locale, 'contact')
or default_page_path('contact')
=> /:locale/pages/contact
.
You can also split contact.html.erb
for separate language. Put it here app/views/pages/en/
and app/views/pages/pl/
etc.
Then you can use helper to access this page: locale_page_path(:locale, 'contact')
or default_locale_page_path('contact')
=> /:locale/locale_pages/contact
.
Cache store
Perform caching is enabled in config/environments/development.rb
:
config.action_controller.perform_caching = true
Tools
Foreman
Using foreman you can declare the various processes that are needed to run your application using a Procfile
.
$ foreman start
Foreman will start sidekiq and guard.
Sidekiq
To run sidekiq:
$ bundle exec sidekiq -C config/sidekiq.yml
MailCatcher
Please install MailCatcher. App is already configured to work with it in development environment.
$ gem install mailcatcher
To run mailcatcher in foreground:
$ mailcatcher -f
Starting MailCatcher
==> smtp://127.0.0.1:1025
==> http://127.0.0.1:1080
*** MailCatcher runs as a daemon by default. Go to the web interface to quit.
Code coverage
To generate a fresh code coverage report in coverage
directory, run:
$ SIMPLECOV=1 RCOV=1 rspec spec
Tips
Rails Panel Chrome Extension
You can use devtools panel for Rails development.
Rails Best Practices
rails_best_practices is a code metric tool to check the quality of rails codes.
Run:
$ rails_best_practices -f html .
Brakeman
Brakeman is a static analysis tool which checks Ruby on Rails applications for security vulnerabilities.
Run:
$ brakeman -o brakeman.html
Pagination with Kaminari
Creating friendly URLs and caching. Another advantage of this approach is that you can switch language and keep page number in url.
Setting the locale
I18n.locale
depends on params[:locale]
from url or if missing then depends on current session[:locale]
or HTTP_ACCEPT_LANGUAGE
. More detail you will find in method ApplicationController#set_locale
.
Detect browser time zone
We use jsTimezoneDetect to detect browser time zone (see: app/assets/javascripts/application.js.coffee
). In ApplicationController#set_time_zone
we set time zone per each request. When user visits page for the first time he doesn't have browser.time_zone
cookie so time zone will be set as default which is defined in config/application.rb
.
Testing
Rspec
Basic way to run specs:
$ rspec spec
Run specs via guard. If you change some code or spec file then proper spec will run automatically. First start foreman which runs all services:
$ foreman start
// wait until loaded and press Enter to run all specs
// or set flag to run all specs on start
$ GUARD_ALL_ON_START=1 foreman start
Jasmine
Run jasmine specs:
$ rake spec:javascript
Run jasmine specs in browser: http://127.0.0.1:3000/specs
Features specs
Example how to run js spec and how to open it in browser:
# spec/features/home_feature_spec.rb
feature 'Home Page' do
before { visit root_path }
# run with webkit without opening browser
scenario 'should have text Hello', :js do
expect(page).to have_content 'Hello'
end
# run with selenium in firefox
scenario 'should have text World', :selenium do
expect(page).to have_content 'World'
end
end
Railstom specs
If you don't need specs related to Railstom you can just turn them off. Put this in your .rspec
file to ignore specs with tag railstom
.
# .rspec
--tag=~railstom
Sidekiq Testing
Sidekiq provides a few options for testing your workers.
Testing Worker Queueing. To enable sidekiq testing for specific spec add flag sidekiq
.
Testing Workers Inline. To enable sidekiq testing inline for specific spec add flag sidekiq_inline
.
Deploy
Server config files
/script/examples/unicorn.cfg.rb
/script/examples/railstom-production-unicorn
/script/examples/vhost/railstom-production.com
/script/examples/vhost/www.railstom-production.com
First deploy
$ cap production deploy:setup
$ cap production deploy:check
$ cap production deploy
$ cap production deploy:migrate
$ cap production deploy:start
Tips
# start all services
$ cap production deploy:start
# restart all services
$ cap production deploy:restart
# stop all services
$ cap production deploy:stop
# Sidekiq service
$ cap production deploy:services:sidekiq:start
$ cap production deploy:services:sidekiq:restart
$ cap production deploy:services:sidekiq:stop
$ cap production deploy:services:sidekiq:status
# force assets compilation
$ cap production deploy:assets:force_precompile
$ cap production deploy:restart
Run a task on a remote server:
$ cap staging do:rake:invoke task=a_certain_task