Ruby On Rails
Deé Zsombor
PrimalGrasp LLC
Ruby?
Nem hallottam róla ...
Csak egy szkript nyelv?
Valaki használja?
Nem játékszer:
http://www.rubygarden.org/ruby?RealWorldRuby
100% OO
1.class == Fixnum
5.times do print "hello" end
/hello/.class == RegExp
hello = lambda { print "hello"}
hello.call
primary design consideration is to make programmers happy!
Elme részesedés
Egy a sok jobb nyelv közül ...
Ruby on Rails
MVC keretrendszer a Webre
Egy jobb keretrendszer
Valós termékből kiemelve
További termékekből folytonosan bővülve
Konvenció
vs.
Konfiguráció
Az arany út
csak ha letérsz róla, izzadj
- ArticlesController
- app/controllers/articles_controllers.rb
- http://application.com/articles/edit/1
- ArticlesController#edit
- params[:id] == 1
- app/views/articles/edit.rhtml
- app/views/layouts/article.rhtml
Flexibilis?
Nem minden URL oly tiszta ...
ActionController::Routing::Routes.draw do |map|
map.archive(':year/:month/page/:page',
:controller=>'home',
:action => 'archive',
:requirements => {
:year => /(19|20)\d\d/,
:month => /[01]?\d/,
:page => /\d+/} )
map.connect(':controller/:action/:id' )
end
OK, viszont hol a Modell?
# article.rb,
# adatbázisban megfelel az `articles` tábla
class Article < ActiveRecord::Base
belongs_to :author
end
# author.rb,
# adatbázisban megfelel az `authors` tábla
class Author < ActiveRecord::Base
has_many :articles, :dependent => true
has_many( :drafts,
:class_name => "Article",
:condition => 'is_draft=1')
end
author = Author.find(1)
# SELECT * FROM authors WHERE (id=1);
author.articles
# SELECT * FROM articles
# WHERE (articles.author_id = 1);
author.articles.count
# SELECT count(*) FROM articles
# WHERE (articles.author_id = 1);
author.drafts.first
# SELECT * FROM articles
# WHERE articles.author_id = 1
# AND is_draft=1 LIMIT 1;
author.destroy
# BEGIN
# SELECT * FROM articles WHERE
# articles.author_id = 1;
# DELETE FROM articles WHERE id = 4143
# DELETE FROM articles WHERE id = 1323
# DELETE FROM articles WHERE id = 9428
# DELETE FROM authors WHERE id = 1
# COMMIT
class Article < ActiveRecord::Base
def after_destroy(article)
File.rm(article.lead_image)
end
end
author.articles.find_by_title("My life without me")
# SELECT * FROM articles
# WHERE (articles.author_id = 1
# and articles.title = "My life without me");
...
Flexibilis?
Nem minden adatmodell ily egyszerű ...
M:N relációk
has_and_belongs_to_many
has_many(:trough)
Pragmatikus
find_by_sql
Fenntartható?
Teszt barát környezet
rake
rake recent
Dinamikus Ruby
unit tesztelés
class PublicRelationsAlerter
def find_top_ten_technorati_hits(term)
#hálózati hívások sorozata itt
end
end
class PublicRelationsAlerter
def find_top_ten_technorati_hits(term)
#mesterséges hálózati hibák
end
end
FlexMock
funkcionális tesztelés
class AccountControllerTest < Test::Unit::TestCase
def test_index__without_super_user
@request.session[:user] = users(:average_joe)
get :index
assert_response :success
assert_no_tag(:tag => 'a',
:attributes => {
:id =>'create-new',
:href =>/signup/},
:content => "Create a new user")
end
end
integrácios tesztelés (1.1)
validácios tesztelés (Selinum)
Hibák nélkül!
Email on Error
Félóránként egy release
Capistrano Switchtower
rake deploy
rake rollback
Kérdések
zsombor@primalgrasp.com