Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Desenvolvimento Web com Ruby on Rails (parte 5)

Desenvolvimento Web com Ruby on Rails (parte 5)

Desenvolvimento Web com Ruby on Rails

More Decks by João Lucas Pereira de Santana

Other Decks in Education

Transcript

  1. Desenvolvimento Web com Ruby on Rails João Lucas Pereira de

    Santana gtalk | linkedin | twitter: jlucasps
  2. Migration bills @jlucasps jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rails g migration CreateBills invoke active_record

    create db/migrate/20130618074102_create_bills.rb class CreateBills < ActiveRecord::Migration def change create_table :bills do |t| t.column :name, :string, :null => false t.column :description, :string t.references :user, :foreign_key => true t.column :date, :datetime, :null => false t.column :value, :decimal t.timestamps end end end Criar arquivo de migration e executá-lo
  3. Migration bills @jlucasps jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rake db:migrate == CreateBills: migrating ===============================================

    ===== -- create_table(:bills) -> 0.0073s -- add_index(:bills, :user_id) -> 0.0005s == CreateBills: migrated (0.0080s) =========================================== jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rake db:rollback == CreateBills: reverting =============================================== ===== -- remove_index("bills", {:column=>:user_id}) -> 0.0082s -- drop_table("bills") -> 0.0003s == CreateBills: reverted (0.0088s) ===========================================
  4. Migration bills @jlucasps jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rails g migration AddIndexToBills invoke active_record

    create db/migrate/20130618080649_add_index_to_bills.rb class AddIndexToBills < ActiveRecord::Migration def change add_index :bills, :user_id end end Adicionar índice à foreign_key user_id da tabela bills
  5. Migration bills @jlucasps jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rake db:migrate == AddIndexToBills: migrating ================================================

    -- add_index(:bills, :user_id) -> 0.0013s == AddIndexToBills: migrated (0.0015s) ======================================= jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rake db:rollback == AddIndexToBills: reverting ================================================ -- remove_index("bills", {:column=>:user_id}) -> 0.0306s == AddIndexToBills: reverted (0.0307s) =======================================
  6. Model Bill @jlucasps class Bill < ActiveRecord::Base # Attrs accessible

    attr_accessible :name, :description, :user_id, :date, :value # Validations validates :name, :presence => true, :allow_blank => false validates :user_id, :presence => true validates :date, :presence => true validates :value, :presence => true # Associations belongs_to :user # Scopes default_scope order("bills.date DESC") # Públic methods end
  7. Model User Alterar o model User para conter a associação

    com Bill @jlucasps class User < ActiveRecord::Base # Attrs accessible attr_accessible :name, :email, :age, :gender # Constants MALE = 1 FEMALE = 2 OTHER = 3 # Validations validates :name, :presence => true, :allow_blank => false validates :email, :presence => true, :allow_blank => false validates_uniqueness_of :email validates :gender, :presence => true, :if => :adulthood # Associations has_many :bills, :dependent => :destroy # Scopes # Públic methods def adulthood self.age.present? and age >= 18 end end
  8. Rails console @jlucasps jlucasps@lotus:/media/truecrypt1/handsonrails/first_app$ rails console Loading development environment (Rails

    3.2.13) irb(main):001:0> user = User.new(:name => "Joao Lucas", :email => "[email protected]") => #<User id: nil, name: "Joao Lucas", email: "[email protected]", age: nil, created_at: nil, updated_at: nil, gender: nil> irb(main):002:0> user.save (0.1ms) begin transaction User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 SQL (24.1ms) INSERT INTO "users" ("age", "created_at", "email", "gender", "name", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["age", nil], ["created_at", Tue, 18 Jun 2013 12:52:30 UTC +00:00], ["email", "[email protected]"], ["gender", nil], ["name", "Joao Lucas"], ["updated_at", Tue, 18 Jun 2013 12:52: 30 UTC +00:00]] (422.3ms) commit transaction => true irb(main):003:0> Criar um usuário utilizando o $ rails console
  9. Rails console Criar 2 objetos Bill e associá-los a um

    usuário. @jlucasps irb(main):004:0> bill_1 = Bill.new :name => "Conta 1", :date => Time. utc(2012, 06, 18), :value => 48.90 => #<Bill id: nil, name: "Conta 1", description: nil, user_id: nil, date: "2012-06- 18 00:00:00", value: #<BigDecimal:3fc45d8,'0.489E2',18(45)>, created_at: nil, updated_at: nil> irb(main):012:0> bill_1.user = user => #<User id: 7, name: "Joao Lucas", email: "[email protected]", age: nil, created_at: "2013-06-18 12:52:30", updated_at: "2013-06-18 12:52:30", gender: nil> irb(main):013:0> bill_1.save (0.1ms) begin transaction SQL (7.7ms) INSERT INTO "bills" ("created_at", "date", "description", "name", "updated_at", "user_id", "value") VALUES (?, ?, ?, ?, ?, ?, ?) [["created_at", Tue, 18 Jun 2013 13:03:13 UTC +00:00], ["date", Mon, 18 Jun 2012 00:00:00 UTC +00:00], ["description", nil], ["name", "Conta 1"], ["updated_at", Tue, 18 Jun 2013 13:03:13 UTC +00:00], ["user_id", 7], ["value", #<BigDecimal:31e9878,'0.489E2',18(45)>]] (413.9ms) commit transaction => true
  10. Rails console @jlucasps irb(main):014:0> bill_2 = Bill.new :name => "Conta

    2", :date => Time. utc(2012, 06, 17), :value => 31.50 => #<Bill id: nil, name: "Conta 2", description: nil, user_id: nil, date: "2012-06- 17 00:00:00", value: #<BigDecimal:437f150,'0.315E2',18(45)>, created_at: nil, updated_at: nil> irb(main):015:0> bill_2.user = user => #<User id: 7, name: "Joao Lucas", email: "[email protected]", age: nil, created_at: "2013-06-18 12:52:30", updated_at: "2013-06-18 12:52:30", gender: nil> irb(main):016:0> bill_2.save (0.1ms) begin transaction SQL (0.7ms) INSERT INTO "bills" ("created_at", "date", "description", "name", "updated_at", "user_id", "value") VALUES (?, ?, ?, ?, ?, ?, ?) [["created_at", Tue, 18 Jun 2013 13:05:13 UTC +00:00], ["date", Sun, 17 Jun 2012 00:00:00 UTC +00:00], ["description", nil], ["name", "Conta 2"], ["updated_at", Tue, 18 Jun 2013 13:05:13 UTC +00:00], ["user_id", 7], ["value", #<BigDecimal:3c1e0f0,'0.315E2',18(45)>]] (427.6ms) commit transaction => true
  11. Rails console @jlucasps irb(main):017:0> Bill.find_all_by_user_id(user.id) Bill Load (0.3ms) SELECT "bills".*

    FROM "bills" WHERE "bills"."user_id" = 7 ORDER BY bills.date DESC => [#<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012-06-18 00:00:00", value: #<BigDecimal:46f1428,'0.489E2',18(45)>, created_at: "2013- 06-18 13:03:13", updated_at: "2013-06-18 13:03:13">, #<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17 00:00:00", value: #<BigDecimal:46f8048,'0.315E2',18(45)>, created_at: "2013-06-18 13:05:13", updated_at: "2013-06-18 13:05:13">] irb(main):018:0> Bill.find_by_user_id user.id Bill Load (0.5ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 ORDER BY bills.date DESC LIMIT 1 => #<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012-06-18 00:00:00", value: #<BigDecimal:4706260,'0.489E2',18(45)>, created_at: "2013- 06-18 13:03:13", updated_at: "2013-06-18 13:03:13"> irb(main):019:0>
  12. Rails console @jlucasps irb(main):020:0> Bill.where(:user_id => user.id).sum(:value) (0.2ms) SELECT SUM("bills"."value")

    AS sum_id FROM "bills" WHERE "bills"."user_id" = 7 => #<BigDecimal:45230d8,'0.8040000000 000001E2',27(45)> irb(main):022:0> Bill.where(:user_id => user.id, :date => Time. utc(2012, 06, 17)).sum(:value) (0.3ms) SELECT SUM("bills"."value") AS sum_id FROM "bills" WHERE "bills"."user_id" = 7 AND "bills"."date" = '2012-06-17 00:00:00.000000' => #<BigDecimal:4a95c78,'0.315E2',18(45)> irb(main):029:0> Bill.where(:user_id => user.id).where("bills. date <= '2012-06-17 00:00:00.000000'").sum(:value) (0.3ms) SELECT SUM("bills"."value") AS sum_id FROM "bills" WHERE "bills"."user_id" = 7 AND (bills.date <= '2012-06-17 00:00:00.000000') => #<BigDecimal:49b44d0,'0.315E2',18(45)>
  13. Scopes @jlucasps # Scopes default_scope order("bills.date DESC") irb(main):039:0> reload! irb(main):040:0>

    user = User.find_by_email("[email protected]") User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 => #<User id: 7, name: "Joao Lucas", email: "[email protected]", age: nil, created_at: "2013-06-18 12:52:30", updated_at: "2013-06-18 12:52:30", gender: nil> irb(main):041:0> user.bills Bill Load (0.2ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 ORDER BY bills.date DESC => [#<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012- 06-18 00:00:00", value: #<BigDecimal:2d11fd0,'0.489E2',18(45)>, created_at: "2013-06-18 13:03:13", updated_at: "2013-06-18 13:03:13">, #<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17 00:00:00", value: #<BigDecimal:3d120b0,'0.315E2',18(45)>, created_at: "2013-06-18 13:05:13", updated_at: "2013-06-18 13:05:13">]
  14. Scopes @jlucasps # Scopes default_scope order("bills.date ASC") irb(main):044:0> reload! Reloading...

    => true irb(main):045:0> user = User.find_by_email("[email protected]") User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 => #<User id: 7, name: "Joao Lucas", email: "[email protected]", age: nil, created_at: "2013-06-18 12:52:30", updated_at: "2013-06-18 12:52:30", gender: nil> irb(main):046:0> user.bills Bill Load (0.1ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 ORDER BY bills.date ASC => [#<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17 00: 00:00", value: #<BigDecimal:4971270,'0.315E2',18(45)>, created_at: "2013-06-18 13: 05:13", updated_at: "2013-06-18 13:05:13">, #<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012-06-18 00:00:00", value: #<BigDecimal:4977eb8,'0.489E2', 18(45)>, created_at: "2013-06-18 13:03:13", updated_at: "2013-06-18 13:03:13">]
  15. Scopes @jlucasps irb(main):051:0> reload! irb(main):052:0> user = User.find_by_email("[email protected]") User Load

    (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 => #<User id: 7, name: "Joao Lucas", email: "[email protected]", age: nil, created_at: "2013-06-18 12: 52:30", updated_at: "2013-06-18 12:52:30", gender: nil> irb(main):053:0> user.bills Bill Load (0.1ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 ORDER BY bills.date DESC => [#<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012-06-18 00:00:00", value: #<BigDecimal:49105b0,'0.489E2',18(45)>, created_at: "2013-06-18 13:03:13", updated_at: "2013-06-18 13:03:13">, #<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17 00:00:00", value: #<BigDecimal:491b050,'0.315E2',18(45)>, created_at: "2013-06-18 13:05:13", updated_at: "2013-06-18 13:05:13">] irb(main):054:0> user.bills.recents Bill Load (0.4ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 ORDER BY bills.date DESC LIMIT 5 => [#<Bill id: 1, name: "Conta 1", description: nil, user_id: 7, date: "2012-06-18 00:00:00", value: #<BigDecimal:4938560,'0.489E2',18(45)>, created_at: "2013-06-18 13:03:13", updated_at: "2013-06-18 13:03:13">, #<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17 00:00:00", value: #<BigDecimal:49368f0,'0.315E2',18(45)>, created_at: "2013-06-18 13:05:13", updated_at: "2013-06-18 13:05:13">] # Scopes default_scope order("bills.date DESC") scope :recents, order("bills.date DESC").limit(5)
  16. Scopes @jlucasps irb(main):058:0> reload! irb(main):059:0> user = User.find_by_email("[email protected]") User Load

    (0.2ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1 => #<User id: 7, name: "Joao Lucas", email: "[email protected]", age: nil, created_at: "2013-06-18 12:52:30", updated_at: "2013-06-18 12:52:30", gender: nil> irb(main):060:0> user.bills.recents_by_date Time.utc(2012, 06, 17) Bill Load (0.3ms) SELECT "bills".* FROM "bills" WHERE "bills"."user_id" = 7 AND (bills.date <= '2012-06-17 00:00:00.000000') ORDER BY bills.date DESC LIMIT 5 => [#<Bill id: 2, name: "Conta 2", description: nil, user_id: 7, date: "2012-06-17 00:00:00", value: #<BigDecimal:4b07e90,'0.315E2',18(45)>, created_at: "2013- 06-18 13:05:13", updated_at: "2013-06-18 13:05:13">] scope :recents, order("bills.date DESC").limit(5) scope :recents_by_date, lambda {|date| where("bills.date <= '#{date.strftime('%Y-%m-%d %H:%M:%S.000000')}'").order ("bills.date DESC").limit(5) }
  17. Commit $ git add . $ git commit -m "Criação

    de scopes" Agora vamos adicionar relacionamentos entre as rotas de Usuários e Contas @jlucasps ... resources :users do resources :bills end ...
  18. Bills @jlucasps Alterar view de show do modelo user: /app/views/users/show.html.erb

    <p id="notice"><%= notice %></p> <p><b>Nome:</b><%= @user.name %></p> <p><b>email:</b><%= @user.email %></p> <p><b>Idade:</b><%= @user.age %></p> <p><b>Sexo:</b><%= @user.gender %></p> <div id='new_bill'> <%= render :partial => 'bills/new', :locals => {:bill => Bill.new, :user => @user} %> </div> <div id='bills'> <%= render :partial => 'bills/index', :locals => {:bills => @user.bills} %> </div> <%= link_to 'Edit', edit_user_path(@user), :class => "btn" %> | <%= link_to 'Back', users_path, :class => "btn" %>
  19. Bills @jlucasps Criar partial para lista contas: /app/views/bills/_index.html.erb <% if

    bills.any? %> <% bills.each do |bill| %> <div class='well well-small'> <h4><%= bill.name %></h4> <%= bill.description %> <h5><%= l(bill.date, :forma => :long) %></h5> <h3><%= bill.value.round(2) %></h3> </div> <% end %> <% else %> <div class='alert'> <%= t('no_bill_registered_to_this_user') %> </div> <% end %>
  20. Bills @jlucasps Criar partial com formulário de nova conta: /app/views/bills/_new.html.erb

    <hr> <h4><%= t('new_bill') %></h4> <%= form_for([user, bill], :remote => true) do |f| %> <%= render :partial => 'shared/error_messages' , :locals => {:resource => bill} % > <%= f.hidden_field :user_id, :value => user.id %> <%= f.label :name %> <%= f.text_field :name %> <%= f.label :description %> <%= f.text_field :description %> <%= f.label :date %> <%= f.text_field :date %> <%= f.label :value %> <%= f.text_field :value %> <% end %>
  21. Bills @jlucasps Criar controller bills e action create class BillsController

    < ApplicationController def create @bill = Bill.new(params[:bill]) flash[:notice] = (@bill.save ? t('saved_successfully') : t('error_while_saving')) end end
  22. Bills @jlucasps Criar view create /app/views/bills/create.js.erb <% if @bill.persisted? %>

    $("#bills").html("<%= j(render :partial => 'bills/index', :locals => {: bills => @bill.user.bills}) %>"); $("#bills").prepend("<%= j notice %>"); $("#new_bill").html("<%= j(render :partial => 'bills/new', :locals => {:bill => Bill.new, :user => @bill.user} ) %>"); <% else %> $("#new_bill").html("<%= j(render :partial => 'bills/new', :locals => {:bill => @bill, :user => @bill.user}) %>"); <% end %>
  23. Bills @jlucasps Alterar view de index para apresentar link para

    edit e destroy <hr> <h4><%= t('bills') %></h4> <% if bills.any? %> <% bills.each do |bill| %> <div id='bill_<%= bill.id %>' class='well well-small'> <h4><%= bill.name %></h4> <%= bill.description %> <h5><%= l(bill.date, :forma => :long) %></h5> <h3><%= bill.value.round(2) %></h3> <%= link_to t('edit'), edit_user_bill_path(bill.user, bill), :class => "btn btn- primary", :remote => true %> <%= link_to t('delete'), [bill.user, bill], :remote => true, :method => :delete, : class => "btn btn-danger" %> </div> <% end %> <% else %> <div class='alert'> <%= t('no_bill_registered_to_this_user') %> </div> <% end %>
  24. Bills @jlucasps Criar action e view destroy def destroy @bill

    = Bill.find(params[:id]) @bill.destroy end <% if @bill.destroyed? %> $("#bill_<%= @bill.id %>").remove(); <% else %> $("#bill_<%= @bill.id %>").prepend("<%= t ('error_while_deleting') %>"); <% end %>
  25. Bills @jlucasps Criar action e view edit def edit @bill

    = Bill.find(params[:id]) end $("#new_bill").html("<%= j(render :partial => 'bills/new', : locals => {:bill => @bill, :user => @bill.user}) %>"); $('html, body').animate({scrollTop: $("#new_bill").offset(). top});
  26. Bills @jlucasps Implementar action e view de update def update

    @bill = Bill.find(params[:id]) flash[:notice] = (@bill.update_attributes(params[:bill]) ? t ('updated_successfully') : t('error_while_updating')) end <% if @bill.valid? %> $("#bills").html("<%= j(render :partial => 'bills/index', :locals => {:bills => @bill.user.bills}) %>"); $("#bills").prepend("<%= j notice %>"); $("#new_bill").html("<%= j(render :partial => 'bills/new', :locals => {:bill => Bill.new, :user => @bill.user} ) %>"); <% else %> $("#new_bill").html("<%= j(render :partial => 'bills/new', :locals => {:bill => @bill, :user => @bill.user}) %>"); <% end %>
  27. Desenvolvimento Web com Ruby on Rails João Lucas Pereira de

    Santana gtalk | linkedin | twitter: jlucasps Obrigado!