7. Ruby on Rails
Ruby on Rails е framework
за уеб приложения.
Неготово мото е:
“Web development doesn’t hurt”
8. Ruby
Създаден от японеца Юкихиро
Мацумото „Мац“.
Първата публична версия излиза
през 1995.
Версия 2.0 се очакава да излезе за
Коледа / не се казва коя /
9.
10. class System {
// ... code ...
private $_categories = null;
public function getVisibleCategories() {
if ($this->_categories === null) {
$this->_categories = $this->findCategories(array(
'visible' => true
));
}
return $this->_categories;
}
}
11. class System
# ... code ...
def visible_categories()
return @categories ||= self.find_categories({:visible => t
end
end
12. class System
# ... code ...
def visible_categories()
return @categories ||= self.find_categories({:visible => t
end
end
13. class System
# ... code ...
def visible_categories
return @categories ||= self.find_categories({:visible => t
end
end
14. class System
# ... code ...
def visible_categories
return @categories ||= self.find_categories({:visible => t
end
end
15. class System
# ... code ...
def visible_categories
return @categories ||= self.find_categories({:visible => t
end
end
16. class System
# ... code ...
def visible_categories
@categories ||= self.find_categories({:visible => true})
end
end
17. class System
# ... code ...
def visible_categories
@categories ||= self.find_categories({:visible => true})
end
end
18. class System
# ... code ...
def visible_categories
@categories ||= self.find_categories({:visible => true})
end
end
19. class System
# ... code ...
def visible_categories
@categories ||= self.find_categories({:visible => true})
end
end
20. class System
# ... code ...
def visible_categories
@categories ||= find_categories({:visible => true})
end
end
21. class System
# ... code ...
def visible_categories
@categories ||= find_categories({:visible => true})
end
end
22. class System
# ... code ...
def visible_categories
@categories ||= find_categories :visible => true
end
end
37. Ruby
• димично определяне на типа данните
• модули
• отворени класове
• блокове
• анонимни функции
• всяка операция връща резултат
• всичко е изпълним код
• изчистен синтаксис
• мета програмиране
• промяна на обектите по време на изпълнение
• вградени регулярни изрази
38.
39.
40.
41.
42.
43.
44. Ruby on Rails
Разработен от David
Heinemeier Hansson от 37
Signals.
Първата публична версия е
излиза през юли 2004.
Вървим бавно, но сигурно
към 3.1
56. Основни принципи на
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
57. Основни принципи на
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
60. 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
61. 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
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81. 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
82. 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
83. 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
84. 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
85. 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
86. 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
87. 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
88. 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
89. 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
90. 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
95. Test Driven Development
1 Добавя се тест
... за несъществуващ код
2 Пише се код
... колкото само тестът да мине
96. Test Driven Development
1 Добавя се тест
... за несъществуващ код
2 Пише се код
... колкото само тестът да мине
3 Правят се подобрения
... подобрява се качеството на кода
97. Test Driven Development
1 Добавя се тест
... за несъществуващ код
2 Пише се код
... колкото само тестът да мине
3 Правят се подобрения
... подобрява се качеството на кода
98. 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"
99. Тесване на 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
100. 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
101. Тестване на 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
102. ... и още, и още
• 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
• ... и т.н.
103. Погрешни схващания
• 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
104. Погрешни схващания
• 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
105. Погрешни схващания
• 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
106. Погрешни схващания
• 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
107. Погрешни схващания
• 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
108. Погрешни схващания
• 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
109. Погрешни схващания
• 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
110. Погрешни схващания
• 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
111. Погрешни схващания
• 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