Introdução Ruby On Rails

  • 334 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
334
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
2
Comments
1
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Ruby On Rails na prática Lukas AlexandreSaturday, January 19, 13
  • 2. O que é? Framework; Open Source; Full stack; Focado em Apps com base de dados; MVC - Model / View / Controller;Saturday, January 19, 13
  • 3. Overview Ruby é uma linguagem de programação; Rails é um framework que provê infraestrutura web; Rails é escrito em Ruby;Saturday, January 19, 13
  • 4. Filosofia do Rails Convention Over Configuration; Don’t Repeat Yourself - DRY; Rails tem opiniões fortes;Saturday, January 19, 13
  • 5. Arquitetura do Rails Aplicações Rails usam Model-View-Controller (MVC); Model - ActiveRecord; View - ActionView; Controller - ActionController;Saturday, January 19, 13
  • 6. E o Ruby? É uma linguagem interpretada; É extremamente orientada a objetos; TUDO é um objeto; Vem de Perl, Smalltalk e Lisp;Saturday, January 19, 13
  • 7. RSS Reader - Criando gem install rails rails new rss_reader cd rss_reader rails server http://localhost:3000 Welcome Aboard!!!Saturday, January 19, 13
  • 8. Estrutura de diretórios /app /controllers /helpers /models /views/nome_do_controllerSaturday, January 19, 13
  • 9. Estrutura de diretórios /config /log /db /public /doc /test /lib /tmpSaturday, January 19, 13
  • 10. Modos de execução Desenvolvimento (development) Teste (test) Produção (production)Saturday, January 19, 13
  • 11. Welcome Aboard - E agora? No terminal: rails generate scaffold feed name:string rake db:migrate rm public/index.html routes.rb root :to => feeds#indexSaturday, January 19, 13
  • 12. Res...what?? Restful! Index - Listagem dos recursos Show - Detalhes do recurso New - Preenchendo um novo recurso Create - Criando um novo recurso Edit - Preenchendo os dados de um recurso existente Update - Atualizando um recurso Destroy - Removendo um recursoSaturday, January 19, 13
  • 13. Verbalizando a Web (http verbs) Get Post Put DeleteSaturday, January 19, 13
  • 14. Rotas...hã? http://localhost:3000/feeds/hello_world http://localhost:3000 - endereco e porta do servidor feeds - nome do controlador hello_world - nome da ação (método no controller)Saturday, January 19, 13
  • 15. Armazenando os dados config/database.yml development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000Saturday, January 19, 13
  • 16. Models, o que são e para onde vão? class Feed < ActiveRecord::Base attr_accessible :name end Negociar; Representar;Saturday, January 19, 13
  • 17. Migrations Ficam em db/migrations; Versionamento do banco; Devem ser criadas a cada alteração da estrutura de dados; Esqueleto do banco (schema.rb); Reversíveis;Saturday, January 19, 13
  • 18. Como se parecem: class CreateFeeds < ActiveRecord::Migration def change create_table :feeds do |t| t.string :name t.timestamps end end endSaturday, January 19, 13
  • 19. E as Views?! app/views/feeds/index.html.erb <% @feeds.each do |feed| %> <tr> <td><%= feed.name %></td> <td><%= link_to Show, feed %></td> <td><%= link_to Edit, edit_feed_path(feed) %></td> <td><%= link_to Destroy, feed, method: :delete, data: { confirm: Are you sure? } %></td> </tr> <% end %>Saturday, January 19, 13
  • 20. Convention over Configuration O model de Feed “automagicamente” procura por uma tabela em seu plural; O controller “automagicamente” acha e renderiza as views corretas usando seu nome e ação executada (views/feeds/index.html);Saturday, January 19, 13
  • 21. Feed sem nome?? Validações nele! validates_presence_of validates_length_of validates_acceptance_of validates_uniqueness_of validates_format_of validates_numericality_of validates_inclusion_in validates_exclusion_of Entre outros...Saturday, January 19, 13
  • 22. E como ficaria? class Feed < ActiveRecord::Base attr_accessible :name validates_presence_of :name endSaturday, January 19, 13
  • 23. E os itens do feed? rails generate scaffold feed_item feed:references title:string content:text rake db:migrateSaturday, January 19, 13
  • 24. Associations...a rede social dos models has_one belongs_to has_many has_and_belongs_to_many has_many :model1, :through => :model2 has_one :model1, :through => :model2Saturday, January 19, 13
  • 25. E no nosso caso? class FeedItem < ActiveRecord::Base belongs_to :feed attr_accessible :content, :title end class Feed < ActiveRecord::Base has_many :feed_items ... endSaturday, January 19, 13
  • 26. Mostrando itens por feed app/views/feeds/show.html.erb <section> <% @feed.feed_items.each do |feed_item| %> <article> <header> <h1><%= feed_item.title %></h1> </header> <p> <%= feed_item.content %> </p> </article> <% end -%> </section>Saturday, January 19, 13
  • 27. Mergulhando no ActiveRecord Querys SQL complexas; Join Left Join Agrupando; Ordenando;Saturday, January 19, 13
  • 28. Porque Rails? Resolve 90% dos problemas; Foco no negócio; Ecossistema gigantesco; Comunidade receptiva; Ready to go;Saturday, January 19, 13
  • 29. Porque Ruby? Intuitiva; POUCO verbosa; Extremamente dinâmica; Gostosa de escrever;Saturday, January 19, 13
  • 30. Ferramentas de trabalho Sublime Text 2 / VIM; Total Terminal; Patterns (Regex); Kindle (armado até os dentes!);Saturday, January 19, 13
  • 31. Contato pessoal: lukasalexandre@me.com profissional: lukas@codelogic.me http://github.com/lukasalexandreSaturday, January 19, 13
  • 32. Perguntas?Saturday, January 19, 13
  • 33. Obrigado!Saturday, January 19, 13
  • 34. Referências guides.rubyonrails.org Agile Web Development with RailsSaturday, January 19, 13