Ruby on Rails
Ruby on Rails е framework
за уеб приложения.
Неготово мото е:
“Web development doesn’t hurt”
Ruby
Създаден от японеца Юкихиро
Мацумото „Мац“.
Първата публична версия излиза
през 1995.
Версия 2.0 се очакава да излезе за
Коледа / не се казва коя /
class System {
// ... code ...
private $_categories = null;
public function getVisibleCategories() {
if ($this->_categories === null) {
$this->_categories = $this->findCategories(array(
'visible' => true
));
}
return $this->_categories;
}
}
class System
# ... code ...
def visible_categories()
return @categories ||= self.find_categories({:visible => t
end
end
class System
# ... code ...
def visible_categories()
return @categories ||= self.find_categories({:visible => t
end
end
class System
# ... code ...
def visible_categories
return @categories ||= self.find_categories({:visible => t
end
end
class System
# ... code ...
def visible_categories
return @categories ||= self.find_categories({:visible => t
end
end
class System
# ... code ...
def visible_categories
return @categories ||= self.find_categories({:visible => t
end
end
class System
# ... code ...
def visible_categories
@categories ||= self.find_categories({:visible => true})
end
end
class System
# ... code ...
def visible_categories
@categories ||= self.find_categories({:visible => true})
end
end
class System
# ... code ...
def visible_categories
@categories ||= self.find_categories({:visible => true})
end
end
class System
# ... code ...
def visible_categories
@categories ||= self.find_categories({:visible => true})
end
end
class System
# ... code ...
def visible_categories
@categories ||= find_categories({:visible => true})
end
end
class System
# ... code ...
def visible_categories
@categories ||= find_categories({:visible => true})
end
end
class System
# ... code ...
def visible_categories
@categories ||= find_categories :visible => true
end
end
Ruby
• димично определяне на типа данните
• модули
• отворени класове
• блокове
• анонимни функции
• всяка операция връща резултат
• всичко е изпълним код
• изчистен синтаксис
• мета програмиране
• промяна на обектите по време на изпълнение
• вградени регулярни изрази
Ruby on Rails
Разработен от David
Heinemeier Hansson от 37
Signals.
Първата публична версия е
излиза през юли 2004.
Вървим бавно, но сигурно
към 3.1
Основни принципи на
Ruby on Rails
• Convention over Configuration
• DRY - Don’t Repeat Yourself
• KISS - Keep it simple, Stupid!
• Skinny Controller, Fat Model
• Тest Driven Development / Behavior Driven Development
• Less is More
• Code Generation
• REST
Основни принципи на
Ruby on Rails
• Convention over Configuration
• DRY - Don’t Repeat Yourself
• KISS - Keep it simple, Stupid!
• Skinny Controller, Fat Model
• Тest Driven Development / Behavior Driven Development
• Less is More
• Code Generation
• REST
N !
VE FU
HA
REST
Method Path Controller Action
GET /tasks tasks index
GET /tasks/new tasks new
POST /tasks tasks create
GET /tasks/:id tasks show
GET /tasks/:id/edit tasks edit
PUT /tasks/:id tasks update
DELETE /tasks/:id tasks delete
Url формати
Method Path Shows Action
GET /tasks/1 html show
GET /tasks/1.xml xml show
GET /tasks/1.json json show
GET /tasks/1.print html show
PUT /tasks/1 redirect update
PUT(xhr) /tasks/1 html/js update
PUT /tasks/1.json json update
Model
class Task < ActiveRecord::Base
belongs_to :user
has_many :comments
validates_presence_of :user, :text
validates_inclusion_of :status, :in => ["opened", "completed"]
attr_readonly :user_id
def editable?
status == "opened"
end
end
Model
class Task < ActiveRecord::Base
belongs_to :user
has_many :comments
validates_presence_of :user, :text
validates_inclusion_of :status, :in => ["opened", "completed"]
attr_readonly :user_id
def editable?
status == "opened"
end
end
Model
class Task < ActiveRecord::Base
belongs_to :user
has_many :comments
validates_presence_of :user, :text
validates_inclusion_of :status, :in => ["opened", "completed"]
attr_readonly :user_id
def editable?
status == "opened"
end
end
Model
class Task < ActiveRecord::Base
belongs_to :user
has_many :comments
validates_presence_of :user, :text
validates_inclusion_of :status, :in => ["opened", "completed"]
attr_readonly :user_id
def editable?
status == "opened"
end
end
Model
class Task < ActiveRecord::Base
belongs_to :user
has_many :comments
validates_presence_of :user, :text
validates_inclusion_of :status, :in => ["opened", "completed"]
attr_readonly :user_id
def editable?
status == "opened"
end
end
Controller
class TasksController < ApplicationController
before_filter :require_user
def index
@tasks = Task.where(:status => "opened")
end
def show
@task = Task.find(params[:id])
end
def create
@task = current_user.tasks.build(params[:task])
if @task.save
redirect_to tasks_path
else
render :partial => "new"
end
end
end
Controller
class TasksController < ApplicationController
before_filter :require_user
def index
@tasks = Task.where(:status => "opened")
end
def show
@task = Task.find(params[:id])
end
def create
@task = current_user.tasks.build(params[:task])
if @task.save
redirect_to tasks_path
else
render :partial => "new"
end
end
end
Controller
class TasksController < ApplicationController
before_filter :require_user
def index
@tasks = Task.where(:status => "opened")
end
def show
@task = Task.find(params[:id])
end
def create
@task = current_user.tasks.build(params[:task])
if @task.save
redirect_to tasks_path
else
render :partial => "new"
end
end
end
Controller
class TasksController < ApplicationController
before_filter :require_user
def index
@tasks = Task.where(:status => "opened")
end
def show
@task = Task.find(params[:id])
end
def create
@task = current_user.tasks.build(params[:task])
if @task.save
redirect_to tasks_path
else
render :partial => "new"
end
end
end
Controller
class TasksController < ApplicationController
before_filter :require_user
def index
@tasks = Task.where(:status => "opened")
end
def show
@task = Task.find(params[:id])
end
def create
@task = current_user.tasks.build(params[:task])
if @task.save
redirect_to tasks_path
else
render :partial => "new"
end
end
end
Test Driven Development
1 Добавя се тест
... за несъществуващ код
2 Пише се код
... колкото само тестът да мине
Test Driven Development
1 Добавя се тест
... за несъществуващ код
2 Пише се код
... колкото само тестът да мине
3 Правят се подобрения
... подобрява се качеството на кода
Test Driven Development
1 Добавя се тест
... за несъществуващ код
2 Пише се код
... колкото само тестът да мине
3 Правят се подобрения
... подобрява се качеството на кода
Cucumber feature
Feature: Tasks
In order to get more productive and organize
As a user
I want able to manage my tasks
Scenario: Adding tasks
Given I am on the tasks page
When I follow "New task"
And I fill in "Text" with "Present Ruby"
And I select "opened" from "Status"
And I press "Create task"
Then I should have opened task "Present Ruby"
Scenario: Completing tasks
Given I am on the tasks page
And I have opened task "Present Ruby"
When I follow "Edit tasks"
And I select "completed" from "Status"
And I press "Update task"
Then I should have completed task "Present Ruby"
Тесване на Controller
describe TasksController do
describe "GET 'show'" do
before { Task.should_recive(:find).with("1").and_return task }
before { get :show, :id => "1" }
it { should assign_to(:task).with(task) }
it { should render_temlate("show") }
end
end
describe Task do
it { should belong_to(:user) }
it { should have_many(:notes) }
it { should validate_presence_of(:user) }
it { should validate_presence_of(:text) }
describe "#editable?" do
it "returns true if task status is 'opened'" do
Task.new(:status => "opened").should be_editable
end
it "returns false if task status is 'rejected'" do
Task.new(:status => "rejected").should_not be_editable
end
it "returns false if task status is 'completed" do
Task.new(:status => "completed").should_not be_editable
end
end
end
Тестване на View
describe "tasks/show.html.erb" do
before do
assign :task, mock_model(Task, {
:id => 1,
:text => "task text"
})
render
end
it "renders task text" do
rendered.should contain("task text")
end
it "renders link for editing task" do
rendered.should have_selector('a[href="/tasks/1/edit"]')
end
end
... и още, и още
• database migrations
• gem dependency management
• css/javascript management (from Rails 3.1)
• nested forms
• automatic deploys
• Rack
• generations
• send/receive mails
• caching
• internalization (i18n)
• Ruby language extentions
• a LOT plugins, tools and services
• ... и т.н.
Погрешни схващания
• Rails CAN'T SCALE
• Rails is SLOW!!!!
• Hard to deploy
• Too much magic
• Hard to learn every thing
• Impossible to extend rails!
• No good IDE
• Bad windows support
• Documentation
Погрешни схващания
• Rails CAN'T SCALE
• Rails is SLOW!!!!
• Hard to deploy
• Too much magic
• Hard to learn every thing
• Impossible to extend rails!
• No good IDE
• Bad windows support
• Documentation
Погрешни схващания
• Rails CAN'T SCALE
• Rails is SLOW!!!!
• Hard to deploy
• Too much magic
• Hard to learn every thing
• Impossible to extend rails!
• No good IDE
• Bad windows support
• Documentation
Погрешни схващания
• Rails CAN'T SCALE
• Rails is SLOW!!!!
• Hard to deploy
• Too much magic
• Hard to learn every thing
• Impossible to extend rails!
• No good IDE
• Bad windows support
• Documentation
Погрешни схващания
• Rails CAN'T SCALE
• Rails is SLOW!!!!
• Hard to deploy
• Too much magic
• Hard to learn every thing
• Impossible to extend rails!
• No good IDE
• Bad windows support
• Documentation
Погрешни схващания
• Rails CAN'T SCALE
• Rails is SLOW!!!!
• Hard to deploy
• Too much magic
• Hard to learn every thing
• Impossible to extend rails!
• No good IDE
• Bad windows support
• Documentation
Погрешни схващания
• Rails CAN'T SCALE
• Rails is SLOW!!!!
• Hard to deploy
• Too much magic
• Hard to learn every thing
• Impossible to extend rails!
• No good IDE
• Bad windows support
• Documentation
Погрешни схващания
• Rails CAN'T SCALE
• Rails is SLOW!!!!
• Hard to deploy
• Too much magic
• Hard to learn every thing
• Impossible to extend rails!
• No good IDE
• Bad windows support
• Documentation
Погрешни схващания
• Rails CAN'T SCALE
• Rails is SLOW!!!!
• Hard to deploy
• Too much magic
• Hard to learn every thing
• Impossible to extend rails!
• No good IDE
• Bad windows support
• Documentation