13 July 2015

Creating new application

Setup

RVM Gemset

Create new gemset for the application

rvm gemset create <appname>
  rvm gemset use <appname>

Create configurations files .ruby-version & .ruby-gemset
Source: https://rvm.io/workflow/projects#project-file-ruby-version

To create both files in the project folder (after creating the new app with rails)

$ rvm --ruby-version use 2.2.0@my_app --create

Use the corresponding Ruby versión and app name. Append –create if the gems doesn't exist yet.

Creation

With no TEST, with PosgreSQL and skip bundle

$ rails new <new_app_name> -B -T -d postgresql
$ cd <new_app_name>/

.gitignore

Setup .gitignore Add

  1. secrets.yml
  2. database.yml
  3. public/uploads

Copy secrets and database to .sample

gemfile

Edit Gemfile and add

ruby '2.2.0'
group :development, :test do
  gem 'spring'
  gem 'spring-commands-rspec'
  gem 'guard-rspec'
  gem 'pry'
  gem 'pry-byebug'
  gem 'rspec-rails', '~> 3.1'
  gem 'factory_girl_rails'
  gem 'faker'
end
group :development do
  gem "capistrano", '~> 3.3.5'
  gem 'capistrano-rails', '~> 1.1.1'
  gem "capistrano-rvm", '~> 0.1.1'
  # IO lib to hide password input when doing capistrano
  gem 'highline', '~> 1.7.1'
  gem 'rvm1-capistrano3', require: false
end
gem 'thin'
gem 'ckeditor'
gem 'carrierwave'
gem 'mini_magick'

# speed up development environment
group :development do
  gem 'rails-dev-boost', :git => 'git://github.com/thedarkone/rails-dev-boost.git'
  gem 'rb-inotify', '>= 0.8.8'
end
# authentication
gem 'devise'
gem 'devise-i18n'
# user authorization
gem 'pundit'
bundle install

rspec

$ rails generate rspec:install

database.yml

Configure database.yml with postgres host, username and password

default:
  host: localhost
  username: alfredo
  password: alfredo
production:
  username: shk
  password: <%= ENV['SHK_DATABASE_PASSWORD'] %>

If deploying with capistrano, For production, configure secrets.yml with secret (rake secret) and database.yml with user and password.

RAILS_ENV=production rake secret

Add them in .gitignore.

Create database

rake db:create

production.rb

Set serve_static_files to true (add default value true)

#config/environments/production.rb
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present? || true

App Code

$ rails g scaffold product name:string price:decimal family:string size:integer dressing:string sauce:string
$ rake db:create
$ rake db:migrate
$ rails generate uploader Photo
$ rails g migration addPhotoToProducts photo:string
$ rake db:migrate

add bcrypt to gemfile and password and password_confirmation to _form

$ rails g migration addPasswordDigestToCustomer password_digest:string
$ rails g scaffold order date:datetime customer_id:integer
$ rails g scaffold orderline product_id:integer order_id:integer quantity:integer
$ rails g migration add_index_to_customers_email

in migration file add:

add_index :customers, :email, unique: true

Add authenticity_token manually

<%= form_tag('/welcome', remote: true) do %>
  <%= hidden_field_tag :authenticity_token, form_authenticity_token %>

Rails console in a sandbox

$ rails console --sandbox
Loading development environment in sandbox
Any modifications you make will be rolled back on exit
>>

manymany

Test has_and_belongs_to_many association

rails g scaffold product name:string type:string price:decimal photo:string
rails g scaffold ingredient name:string
rake db:create
rake db:migrate
rails g migration CreateJoinTableProductIngredient product ingredient

Creates the following migration. An index must be uncommented.

class CreateJoinTableProductIngredient < ActiveRecord::Migration
  def change
    create_join_table :products, :ingredients do |t|
      t.index [:product_id, :ingredient_id]
      # t.index [:ingredient_id, :product_id]
    end
  end
end

Creates the following db schema

  create_table "ingredients_products", id: false, force: true do |t|
    t.integer "product_id",    null: false
    t.integer "ingredient_id", null: false
  end

  add_index "ingredients_products", ["product_id", "ingredient_id"], name: "index_ingredients_products_on_product_id_and_ingredient_id", using: :btree

The next has to be added manually to models.

class Ingredient < ActiveRecord::Base
  has_and_belongs_to_many :products, join_table: :ingredients_products
end

class Product < ActiveRecord::Base
  has_and_belongs_to_many :ingredients, join_table: :ingredients_products
end

Test relations

$ rails new testrelations -T -d postgresql
$ cd testrelations/

Create new gemset for the application

rvm gemset create <appname>
rvm gemset use <appname>

Create configurations files .ruby-version & .ruby-gemset
Source: https://rvm.io/workflow/projects#project-file-ruby-version

To create both files in the project folder:

$ rvm --ruby-version use 2.2.0@my_app --create

Use the corresponding Ruby versión and app name. Append –create if the gems doesn't exist yet.

Edit Gemfile and add

group :development, :test do
  gem 'pry'
  gem 'pry-byebug'
  gem 'rspec-rails', '~> 3.0.0'
  gem 'factory_girl_rails'
  gem 'faker'
end

gem 'carrierwave'
$ rails generate rspec:install
$ rails g scaffold pipe name:string
$ rails g scaffold company name:string
$ rails g migration AddCompanyToPipe company:references
$ rails g scaffold user lastname:string 
$ rails g migration AddCompanyToUser company:references
$ rails g scaffold area name:string
$ rails g migration CreateJoinTableAreaPipe area pipe
$ rails g scaffold gate name:string coord_x:decimal coord_y:decimal
$ rails g scaffold loop name:string coord_x:decimal coord_y:decimal
$ rails g migration CreateJoinTableGateLoop gate loop
$ rails g scaffold product name:string
$ rails g migration AddProductToPipe product:references
$ rails g migration AddAreaToLoop area:references
$ rails g scaffold role
$ rails g migration AddRoleToUser role:references
$ rails g migration AddNameToRole name
$ rails g scaffold affectation name
$ rails g scaffold area_company affectation:references
$ rails g migration AddCompanyToAreaCompany company:references
$ rails g migration AddAreaToAreaCompany area:references
$ rails g scaffold emergency
$ rails g migration AddSimulacrumToEmergency simulacrum:boolean
$ rails g migration AddDescriptionToEmergency description:string
$ rails g migration AddLoopToEmergency loop:references
$ rails g migration AddPipeToEmergency pipe:references
$ rails g scaffold scenario
$ rails g migration AddFieldsToScenario code:string description:string
$ rails g migration AddScenarioToEmergency scenario:references
$ rails g scaffold physical_state state:string
$ rails g migration AddFieldsToProduct onu:integer physical_state:references complementary_info:string
$ rails g migration AddIndexToProduct name:string:uniq  (and erase the add_column line)
$ rails g uploader product_card
$ rails g migration AddProductCardToProduct product_card:string
$ rails g migration AddFieldsToUser firstname title phone1:integer phone2:integer email:string:uniq
$ rails g migration AddNameIndexToCompany name:string:uniq ( and erase the add_column line)
$ rails g migration AddFieldsToCompany enabled:boolean phone1:integer phone2:integer phone3:integer phone2_is_alter:boolean always_call:boolean (and edit manually to complete)

I can do (in rails console)

> Area.first.loops << Loop.first
> Company.first.pipes.all
> Pipe.first.update(product: Product.first)
> Area.first.pipes.first.product.name
> Area.first.gates.count
> Gate.first.loops.first.area.pipes.first.product.name
> Area.first.companies
> Company.first.users.count
> Role.find(2).users << User.first
> User.first.role.name
> User.all.map{|u| u.role.name}
> Company.first.users.responsibles.first.lastname  (with scopes in user) --> better
> Company.first.operators.first.lastname (with condition in has_many association)

> AreaCompany.create affectation: Affectation.find_by(name: "Affected")
> ac=AreaCompany.first
> aa=Area.find(2)
> aa.area_companies << ac
> shell=Company.find_by(name: "SHELL")
> shell.area_companies << ac
> ac.save
> shell.reload
> shell.area_companies.first.affectation.name

> e=Emergency.create description:"fuego en la base"
> e.update(loop: Loop.first)
> e.loop.name

> e.pipe = Pipe.first
> e.save
> e.pipe.name
> e.pipe.product.name
> e.pipe.company.name
> e.pipe.company.users.responsibles.first.lastname
> e.scenario = Scenario.first
> e.save

Setting up installed gems

RSpec

rails generate rspec:install

Sorcery

Bootstrap material design

Source: http://fezvrasta.github.io/bootstrap-material-design/

bower install bootstrap-material-design

Pretty title

http://railscasts.com/episodes/30-pretty-page-title?autoplay=true

# application_helper.rb
def title(page_title)
  content_for(:title) { page_title }
end
# application.html.erb
<head>
  <title>Shoppery - <%= yield(:title) || "The Place to Buy Stuff" %></title>
</head>
<body>
  <%= yield(:title) %>
  <%= yield %>
</body>
# in each view title is defined
<%= title "title" %>