RUBY ON RAILS ZHR.pl extreme II 13.12.2008 Warszawa Szymon Fiedler fidel@zhr.pl
CO TO WŁAŚCIWIE ROR? Framework Opensource Do szybkiego tworzenia aplikacji webowych Twórca: David Heinemeier Hansson Ruby, architektura MVC
FRUSTRACJA... A POTEM NADESZŁY RAILSY Java, PHP,.Net Od zabawki do światowego fenomenu w kilka miesięcy Liczne nagrody i web 2.0 Popularne narzędzie, nie tylko dla hardcore owców
PROSTY, NIE ZNACZY KIEPSKI Stosują go profesjonaliści (37signals.com, Facebook.com, CESEI.zhr.pl :) Próba czasu - nowoczesne i profesjonalne tworzenie oraz implementacja Wgryźli się i zobaczyli, że to nie tylko zabawka
RUBY, RUBY, RUBY, RUBY... 1995 - Yukihiro Matsumoto (Matz) Nowoczesny Obiektowo zorientowany (wszystko jest obiektem) Naturalne i przejrzyste wyrażanie myśli w kodzie Łatwość pisania i odczytywania (nawet po wielu miesiącach) Boom 2003-2005 (za sprawą Rails)
BAJECZKI W RAILS Pisanie kodu jak powieści czyni życie łatwiejszym Programy są krótsze i bardziej czytelne Wiele informacji w kilku linijkach kodu
Prepared exclusively for Krzysztof Lis PHP Ruby Ruby the details. Ruby has some interesting rules that we must learn to write and more vocabulary than PHP. effective methods and has some magic to discover. and more Understanding vocabularythese than PHP. effective methods and has some magic to discover. Understanding these will go long way to helping you understand Let swhat s pretend really we are happening developing an application t will go a long way to helping you understandlet s what s pretend really happening we are developing an applicati when when you you start start digging digging into into tion interface. Only users who are the administr RUBY Rails Rails programming. programming. VS. tionphp functions interface. of the Only application, users who and are we the want admin to h whether functions a User of the model application, has the administrator and we want Defining a Method priv ply whether return. a User model has the administrator The basics of ofdefining aamethod are are the the same same in both in both Ruby Ruby and and PHP. PHP. but let s look at ply return. We ve seen them both before, but let s look at Inthem PHP, again again this quickly might quickly lookbefore like the following. exploring deeper. Here is isananexample of of a method a In PHP, with with this one one might parameter look like the following. PHP Download beginning_ruby_code/php/control_if_not.php in PHP. PHP Download beginning_ruby_code/php/method_basic.php class Parrot { public function say($word) { public function say($word) { // say it here // say it here } PHP PHP public Downloadfunction beginning_ruby_code/php/control_if_not.php edit() { if (! $this->user->isadmin() ) { public function edit() { return; if (! $this->user->isadmin() ) { } } return; } } } That s pretty straightforward PHP code and wou Here s that same code written the Ruby way. do it. Let s translate that code directly into Ruby Here s that same code written the Ruby way. That s pretty straightforward PHP code and Download beginning_ruby_code/ruby/method_basic.rb Ruby Download beginning_ruby_code/ruby/control_if_not.rb do it. Let s translate that code directly into R RUBY Download beginning_ruby_code/ruby/method_basic.rb class Parrot class def say(word) Parrot def # say(word) it here end# say it here end end end Prepared exclusively for Krzysztof Lis Prepared exclusively for Krzysztof Lis Ruby RUBY def edit if! @user.admin? return def edit end if! @user.admin? return #... end end Download beginning_ruby_code/ruby/control_if_not.rb #... end Report erratum this copy is (First printing, January 2008) Report erratum this copy is (First printing, January 2008)
DON T REPEAT YOURSELF Mówisz co chcesz w jednym miejscu Miejsce sugeruje architektura MVC Jedziesz z koksem :) Wolisz zmieniać w jednym miejscu, czy kilku(nastu, dziesięciu)?
KONWENCJA PONAD KONFIGURACJĄ Domyślność w każdym aspekcie klecenia aplikacji Idź za konwencją i stwórz aplikację w Railsach używając mniej kodu, niż w innych frameworkach Jeżeli chcesz ominąć konwencję, Rails na to pozwala Niezliczona ilość wtyczek (logowanie, praca z obrazami, edycja tekstu - Adam coś o tym wie)
STANDARDY Rails jest nowy i jego developerzy rozumieją nowy WEB Rails nie goni standardów, on je tworzy! Pełna integracja z Ajax em
MODELS, VIEWS, AND CONTROLLERS! '()#*(++&*!12*(.%&*1%&)0%1*&34&%# "1'()#*(++&*1-)#&*"5#%1.-#617(0&+ #1'()#*(++&*1-)8(9&%18-&. $1,-&.1*&)0&*%1)&:#1$*(.%&*1%5*&&) $ # ",-&. /(0&+!"#"$"%& Figure 2.1: The Model-View-Controller Architecture MODEL-VIEW-CONTROLLER be a view that displays product information on a catalog page and another set of views used by administrators to add and edit products. RYS1. ARCHITEKTURA MVC Controllers orchestrate the application. Controllers receive events from the AGILE WEB DEVELOPMENT WITH RAILS 3 VER. 4 outside world (normally user input), interact with the model, and display an
BUDOWA ActiveRecord - mechanizm ORM ActionPack ActionController ActionView ActiveSupport - rozszerzenia m.in. Time, String ActionMailer - wysyłka maili
IMPLEMENTACJA Uruchamianie na dowolnej liczbie serwerów jednym poleceniem: mongrel_rails start -d -p 3000 -e production mongrel_rails cluster::start -d -p 3000 -e production -n 3 Webrick, Mongrel, Thin Samodzielny albo ukryty za Apachem, czy Lighttpd
BAZY DANYCH MySQL, PostgreSQL, Oracle, MS SQL Server, Sqlite (domyślny), IBM DB2, OpenBase, Sybase Wystarczy biblioteka Ruby i przyuczony AcitveRecord Kompatybilność vs. jawna składnia SQL Migracje - zmiana struktury tabel, niezależnie od typu, korzystając z Ruby ego XML, YML, CSV i wszystko, co potrafi ruszyć Ruby
BEZPIECZEŃSTWO Escape html <%=h @user.name %> Automatyczne zabezpieczenie zapytań SQL Sprawdzanie autentyczności użytkownika przed wykonaniem akcji before_filter :login_required
RAILS IS AGILE Indywidualność i interakcje ponad procesami i narzędziami Działająca aplikacja ponad tonami dokumentacji Współdziałanie z klientem ponad negocjacjami nad kontraktem Odpowiadanie na zmiany ponad trzymanie się planu
KSIĄŻKI
SCREENCASTY railscasts.com pragprog.com/screencasts peepcode.com