Skip to content

Commit 93a60f1

Browse files
committed
rename notes
Signed-off-by: Yury Kaliada <fut.wrk@gmail.com>
1 parent f52cc85 commit 93a60f1

17 files changed

+2431
-0
lines changed

materials/Ruby testing/.ruby-gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
courses-ruby-testing

materials/Ruby testing/.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.2.2

materials/Ruby testing/Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'minitest'
4+
gem 'rspec'

materials/Ruby testing/Gemfile.lock

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
diff-lcs (1.2.5)
5+
minitest (5.6.1)
6+
rspec (3.2.0)
7+
rspec-core (~> 3.2.0)
8+
rspec-expectations (~> 3.2.0)
9+
rspec-mocks (~> 3.2.0)
10+
rspec-core (3.2.3)
11+
rspec-support (~> 3.2.0)
12+
rspec-expectations (3.2.1)
13+
diff-lcs (>= 1.2.0, < 2.0)
14+
rspec-support (~> 3.2.0)
15+
rspec-mocks (3.2.1)
16+
diff-lcs (>= 1.2.0, < 2.0)
17+
rspec-support (~> 3.2.0)
18+
rspec-support (3.2.2)
19+
20+
PLATFORMS
21+
ruby
22+
23+
DEPENDENCIES
24+
minitest
25+
rspec
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require_relative "train"
2+
require "minitest/autorun"
3+
require "minitest/pride"
4+
5+
describe Train do
6+
describe "when created without params" do
7+
before do
8+
@train = Train.new
9+
end
10+
11+
it "must have default name" do
12+
@train.name.must_equal "Uber train"
13+
end
14+
15+
it "must have default size" do
16+
@train.size.must_equal 10
17+
end
18+
end
19+
20+
describe "when created with specific params" do
21+
before do
22+
@train = Train.new 'Express', 1822
23+
end
24+
25+
it "must have default name" do
26+
@train.name.must_equal 'Express'
27+
end
28+
29+
it "must have default size" do
30+
@train.size.must_equal 1822
31+
end
32+
end
33+
end

materials/Ruby testing/rspec/train.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Train
2+
ON_HOLD = :on_hold
3+
RUNNING = :running
4+
5+
attr_reader :name, :size, :state
6+
7+
def initialize(name='Uber train', size=10)
8+
@name = name
9+
@size = size
10+
@state = ON_HOLD
11+
end
12+
13+
def run
14+
@state = RUNNING
15+
end
16+
17+
def stop
18+
@state = ON_HOLD
19+
end
20+
21+
def get_to_next_station(distance)
22+
sleep 0.00002 * distance * Math.log(distance)
23+
end
24+
25+
def will_arrive_by_the_end_of_the_day?(tripDuration)
26+
Time.now + tripDuration <= ::Date.today + 1.day
27+
end
28+
end

notes/10 - Routing.md

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
# Routing
2+
3+
```
4+
get '/patients/:id', to: 'patients#show'
5+
get 'profile', to: :show
6+
7+
get '/patients/:id', to: 'patients#show', as: 'patient'
8+
<%= link_to 'Patient Record', patient_path(@patient) %>
9+
```
10+
11+
## Links for resources
12+
```
13+
resources :photos
14+
15+
GET /photos index display a list of all photos
16+
GET /photos/new new return an HTML form for creating a new photo
17+
POST /photos create create a new photo
18+
GET /photos/:id show display a specific photo
19+
GET /photos/:id/edit edit return an HTML form for editing a photo
20+
PATCH/PUT /photos/:id update update a specific photo
21+
DELETE /photos/:id destroy delete a specific photo
22+
```
23+
24+
25+
## Links for resource
26+
```
27+
resource :geocoder
28+
29+
GET /geocoder/new new return an HTML form for creating the geocoder
30+
POST /geocoder create create the new geocoder
31+
GET /geocoder show display the one and only geocoder resource
32+
GET /geocoder/edit edit return an HTML form for editing the geocoder
33+
PATCH/PUT /geocoder update update the one and only geocoder resource
34+
DELETE /geocoder destroy delete the geocoder resource
35+
```
36+
37+
## Controller Namespaces and Routing
38+
```
39+
namespace :admin do
40+
resources :posts, :comments
41+
end
42+
43+
GET /admin/posts index admin_posts_path
44+
GET /admin/posts/new new new_admin_post_path
45+
POST /admin/posts create admin_posts_path
46+
GET /admin/posts/:id show admin_post_path(:id)
47+
GET /admin/posts/:id/edit edit edit_admin_post_path(:id)
48+
PATCH/PUT /admin/posts/:id update admin_post_path(:id)
49+
DELETE /admin/posts/:id destroy admin_post_path(:id)
50+
```
51+
52+
```
53+
scope '/admin' do
54+
resources :posts, :comments
55+
end
56+
57+
GET /admin/posts index posts_path
58+
GET /admin/posts/new new new_post_path
59+
POST /admin/posts create posts_path
60+
GET /admin/posts/:id show post_path(:id)
61+
GET /admin/posts/:id/edit edit edit_post_path(:id)
62+
PATCH/PUT /admin/posts/:id update post_path(:id)
63+
DELETE /admin/posts/:id destroy post_path(:id)
64+
```
65+
66+
## Nested Resources
67+
```
68+
class Magazine < ActiveRecord::Base
69+
has_many :ads
70+
end
71+
72+
class Ad < ActiveRecord::Base
73+
belongs_to :magazine
74+
end
75+
76+
77+
resources :magazines do
78+
resources :ads
79+
end
80+
81+
82+
GET /magazines/:magazine_id/ads index display a list of all ads for a specific magazine
83+
GET /magazines/:magazine_id/ads/new new return an HTML form for creating a new ad belonging to a specific magazine
84+
POST /magazines/:magazine_id/ads create create a new ad belonging to a specific magazine
85+
GET /magazines/:magazine_id/ads/:id show display a specific ad belonging to a specific magazine
86+
GET /magazines/:magazine_id/ads/:id/edit edit return an HTML form for editing an ad belonging to a specific magazine
87+
PATCH/PUT /magazines/:magazine_id/ads/:id update update a specific ad belonging to a specific magazine
88+
DELETE /magazines/:magazine_id/ads/:id destroy delete a specific ad belonging to a specific magazine
89+
90+
91+
resources :publishers do
92+
resources :magazines do
93+
resources :photos
94+
end
95+
end
96+
```
97+
98+
## Shallow nesting
99+
```
100+
resources :posts do
101+
resources :comments, only: [:index, :new, :create]
102+
end
103+
resources :comments, only: [:show, :edit, :update, :destroy]
104+
105+
resources :posts do
106+
resources :comments, shallow: true
107+
end
108+
109+
resources :posts, shallow: true do
110+
resources :comments
111+
resources :quotes
112+
resources :drafts
113+
end
114+
115+
shallow do
116+
resources :posts do
117+
resources :comments
118+
resources :quotes
119+
resources :drafts
120+
end
121+
end
122+
123+
scope shallow_path: "sekret" do
124+
resources :posts do
125+
resources :comments, shallow: true
126+
end
127+
end
128+
```
129+
130+
## Routing concerns
131+
```
132+
concern :commentable do
133+
resources :comments
134+
end
135+
136+
concern :image_attachable do
137+
resources :images, only: :index
138+
end
139+
140+
141+
142+
resources :messages, concerns: :commentable
143+
144+
resources :posts, concerns: [:commentable, :image_attachable]
145+
146+
147+
148+
resources :messages do
149+
resources :comments
150+
end
151+
152+
resources :posts do
153+
resources :comments
154+
resources :images, only: :index
155+
end
156+
157+
158+
namespace :posts do
159+
concerns :commentable
160+
end
161+
```
162+
163+
164+
## Links
165+
```
166+
<%= link_to 'Ad details', magazine_ad_path(@magazine, @ad) %>
167+
<%= link_to 'Ad details', url_for([@magazine, @ad]) %>
168+
<%= link_to 'Ad details', [@magazine, @ad] %>
169+
<%= link_to 'Magazine details', @magazine %>
170+
<%= link_to 'Edit Ad', [:edit, @magazine, @ad] %>
171+
```
172+
173+
## Adding More RESTful Actions
174+
```
175+
resources :photos do
176+
member do
177+
get 'preview'
178+
end
179+
end
180+
181+
resources :photos do
182+
get 'preview', on: :member
183+
end
184+
185+
resources :photos do
186+
collection do
187+
get 'search'
188+
end
189+
end
190+
191+
resources :photos do
192+
get 'search', on: :collection
193+
end
194+
```
195+
196+
197+
```
198+
get ':controller(/:action(/:id))'
199+
200+
get ':controller(/:action(/:id))', controller: /admin\/[^\/]+/
201+
202+
get ':controller/:action/:id/with_user/:user_id'
203+
204+
```
205+
206+
## HTTP Verb Constraints
207+
```
208+
match 'photos', to: 'photos#show', via: [:get, :post]
209+
210+
match 'photos', to: 'photos#show', via: :all
211+
```
212+
213+
## Segment Constraints
214+
```
215+
get 'photos/:id', to: 'photos#show', constraints: { id: /[A-Z]\d{5}/ }
216+
217+
get '/stories', to: redirect('/posts')
218+
get '/stories/:name', to: redirect('/posts/%{name}')
219+
get '/stories/:name', to: redirect {|params, req| "/posts/#{params[:name].pluralize}" }
220+
221+
222+
root to: 'pages#main'
223+
root 'pages#main' # shortcut for the above
224+
```
225+

notes/11 - Ruby testing.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Ruby testing
2+
3+
4+
```
5+
rails g model category name
6+
rails g model beer name category:references quantity:integer
7+
rails g model beer_action action:string beer:references quantity:integer
8+
rails g controller beers
9+
10+
```
11+
12+
```
13+
Category.create(name: 'Light'); Category.create(name: 'Dark')
14+
100.times { |i| Beer.create(name: "Beer #{i}", category: Category.all.sample, quantity: rand(1..20)) }
15+
16+
```
17+
18+
* show some basic UI
19+
* review tests
20+
* explain fixtures
21+
* switch to minitest
22+
23+
config.autoload_paths += Dir["#{config.root}/lib/"
24+
25+
26+
* rspec explanation
27+
* testing models
28+
* simple beer creating test
29+
* explaining database cleaner
30+
31+
32+
http://loudcoding.com/posts/quick-tutorial-starting-with-cucumber-and-capybara-bdd-on-rails-project/
33+
34+
35+
```
36+
1 Given(/^I bought (\d+) (\w+) beers$/) do |number, category|
37+
2 category = Category.find_or_create_by(name: category)
38+
3 number.to_i.times { |n| Beer.create(name: '', category: category) }
39+
4 end
40+
5
41+
6 When(/^Open Home page$/) do
42+
7 visit root_path
43+
8 end
44+
9
45+
10 Then(/^I should see (\d+) beers in list$/) do |arg1|
46+
11 page.has_css?('.table tbody tr:nth-child(2)')
47+
12 end
48+
```
49+
50+
51+
```
52+
1 Feature: Beer list
53+
2 Scenario: Open beer list
54+
3 Given I bought 2 Light beers
55+
4 When Open Home page
56+
5 Then I should see 2 beers in list
57+
```
58+

0 commit comments

Comments
 (0)