7. Ruby on Rails
Ruby on Rails е framework
за уеб приложения.
Неготово мото е:
“Web development doesn’t hurt”
8. Ruby
Създаден от японеца Юкихиро
Мацумото „Мац“.
Първата публична версия излиза
през 1995.
Версия 2.0 се очакава да излезе за
Коледа / не се казва коя /
10. private $_products = null;
public function getProducts($categoryId){
if ($this->_products === null){
$this->_products = Product::where(array(
'category_id' => $categoryId
));
}
return $this->products;
}
32. Ruby
• димично определяне на типа данните
• модули
• отворени класове
• блокове
• анонимни функции
• всяка операция връща резултат
• всичко е изпълним код
• изчистен синтаксис
• мета програмиране
• промяна на обектите по време на изпълнение
• вградени регулярни изрази
36. Ruby on Rails
Разработен от David
Heinemeier Hansson от 37
Signals.
Първата публична версия е
излиза през юли 2004.
Текущата стабилна версия
е 3.0.3
67. 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
68. 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
69. 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
70. 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
71. 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
72. Controller
class TasksController < ApplicationController
def index
@tasks = Tasks.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
73. Controller
class TasksController < ApplicationController
def index
@tasks = Tasks.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
74. Controller
class TasksController < ApplicationController
def index
@tasks = Tasks.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
75. Controller
class TasksController < ApplicationController
def index
@tasks = Tasks.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
79. Test Driven Development
1 Добавя се тест
... за несъществуващ код
2 Пише се код
... колкото само тестът да мине
80. Test Driven Development
1 Добавя се тест
... за несъществуващ код
2 Пише се код
... колкото само тестът да мине
3 Правят се подобрения
... подобрява се качеството на кода
81. Test Driven Development
1 Добавя се тест
... за несъществуващ код
2 Пише се код
... колкото само тестът да мине
3 Правят се подобрения
... подобрява се качеството на кода
82. 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
83. Тесване на 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
84. Тестване на 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