Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eetun commitit #2

Open
wants to merge 133 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
133 commits
Select commit Hold shift + click to select a range
c23be5f
Merge pull request #17 from kiskolabs/main
zHarrowed Aug 21, 2023
c118ee5
Update README.md
zHarrowed Aug 21, 2023
316a1eb
Fix typo in week8.md
zHarrowed Aug 23, 2023
b09eb31
Fix typos in week8.md
zHarrowed Aug 23, 2023
ec9693c
Add missing file names for week8.md
zHarrowed Aug 23, 2023
c98104a
Make small fixes
akirataguchi115 Aug 31, 2023
cdf1a79
turbo frame
mluukkai Sep 8, 2023
5431b0c
turbo frame initial example
mluukkai Sep 8, 2023
e1ede9d
tweaks
mluukkai Sep 8, 2023
ad4b0e5
stuff
mluukkai Sep 8, 2023
b07d672
fixes
mluukkai Sep 8, 2023
8bc43b3
moar sstuff
mluukkai Sep 9, 2023
09a6758
p8 tweaks
mluukkai Sep 9, 2023
5426395
pagination and ordering
mluukkai Sep 9, 2023
409d6b6
Update week8.md
heikkivenhola Sep 11, 2023
88a9257
Merge pull request #22 from heikkivenhola/patch-1
mluukkai Sep 11, 2023
1acb113
Merge pull request #18 from akirataguchi115/patch-1
mluukkai Sep 11, 2023
8a573d8
Selvennys
heikkivenhola Sep 12, 2023
da70a2f
Merge pull request #23 from heikkivenhola/patch-2
mluukkai Sep 12, 2023
8d62f45
Update viikko1.md
heksaani Sep 12, 2023
f225890
Merge pull request #24 from heksaani/patch-1
mluukkai Sep 12, 2023
b8f3337
ex3
mluukkai Sep 12, 2023
bde44df
part 8 fixes
mluukkai Sep 12, 2023
f7090de
id fix
mluukkai Sep 12, 2023
e99e712
fides
mluukkai Sep 13, 2023
971424f
tweaks part 8
mluukkai Sep 13, 2023
2c7327c
tweaks part 8
mluukkai Sep 13, 2023
8b544ba
headers w8
mluukkai Sep 13, 2023
5c1c516
typo fixes w8
mluukkai Sep 13, 2023
99c0577
Turbo Frame under the hood
heikkivenhola Sep 14, 2023
769a886
Merge pull request #26 from heikkivenhola/patch-3
mluukkai Sep 14, 2023
2f6baad
turbo frames cmpltd
mluukkai Sep 15, 2023
af960d5
part8 betterments
mluukkai Sep 15, 2023
da60385
tweaks 28 turbo streams
mluukkai Sep 15, 2023
c63a6c6
tweaks w8
mluukkai Sep 15, 2023
c316599
part 8 betterments
mluukkai Sep 16, 2023
28d3279
action streams&cable
mluukkai Sep 16, 2023
1125f40
exercise numbering
mluukkai Sep 16, 2023
b8520e6
release note
mluukkai Sep 16, 2023
5ad398a
action cable tweak
mluukkai Sep 18, 2023
0549573
stimulus betterments
mluukkai Sep 18, 2023
c4f812f
moar exercises w8
mluukkai Sep 18, 2023
0925189
figs to p8
mluukkai Sep 18, 2023
184af4f
Fixes
Veikkosuhonen Sep 18, 2023
be1e307
Merge pull request #28 from Veikkosuhonen/patch-1
mluukkai Sep 19, 2023
75e3b4d
part 8
mluukkai Sep 19, 2023
2cf4364
part 8 submissions
mluukkai Sep 20, 2023
364b7fd
part 8 infotexts
mluukkai Sep 20, 2023
388e8a3
tweaks part 8
mluukkai Sep 20, 2023
7f54245
license
mluukkai Sep 20, 2023
b7b7521
Removed duplicate text
acidmole Sep 20, 2023
da1d8c0
Merge pull request #29 from acidmole/patch-1
mluukkai Sep 20, 2023
9ac3e07
betterments part 8
mluukkai Sep 21, 2023
f4e74b6
Update viikko1.md
heksaani Sep 28, 2023
eb1e84d
Merge pull request #30 from heksaani/patch-2
mluukkai Oct 2, 2023
1b50d3d
Fix typo
akirataguchi115 Oct 10, 2023
9bac589
Merge pull request #32 from akirataguchi115/patch-2
mluukkai Oct 10, 2023
389fddf
Fix typo
akirataguchi115 Oct 11, 2023
d733e8f
Add Module URL & Fix typo
akirataguchi115 Oct 13, 2023
284a2f7
Update truthy URLs
akirataguchi115 Oct 13, 2023
0cba1aa
QoL URL update
akirataguchi115 Oct 13, 2023
ca01e0b
Fix typo
akirataguchi115 Oct 13, 2023
13ac971
Merge pull request #37 from akirataguchi115/patch-7
mluukkai Oct 14, 2023
00cdd3a
Merge pull request #36 from akirataguchi115/patch-6
mluukkai Oct 14, 2023
ac32d46
Merge pull request #35 from akirataguchi115/patch-5
mluukkai Oct 14, 2023
12ef6a4
Merge pull request #34 from akirataguchi115/patch-4
mluukkai Oct 14, 2023
c8584fe
Merge pull request #33 from akirataguchi115/patch-3
mluukkai Oct 14, 2023
bb26877
Disable IndentationStyle
akirataguchi115 Oct 16, 2023
386ac0c
Merge pull request #38 from akirataguchi115/patch-8
mluukkai Oct 16, 2023
7145c70
add ":show" to ratings resources
heikkivenhola Oct 18, 2023
ea0ee95
Merge pull request #40 from heikkivenhola/patch-4
mluukkai Oct 18, 2023
61a770e
Move images outside block quotes
akirataguchi115 Oct 19, 2023
f5d449f
change VAT calculation formula
heikkivenhola Oct 19, 2023
9576284
Fix reference to right(?) exercise
heikkivenhola Oct 19, 2023
2462fb2
Merge pull request #43 from heikkivenhola/patch-6
mluukkai Oct 19, 2023
56598d7
Merge pull request #42 from heikkivenhola/patch-5
mluukkai Oct 19, 2023
de994f9
Merge pull request #41 from akirataguchi115/patch-9
mluukkai Oct 19, 2023
9feca29
Indent comment
akirataguchi115 Oct 23, 2023
938a70e
Merge pull request #44 from akirataguchi115/patch-10
mluukkai Oct 23, 2023
e73d474
Update week3.md
akirataguchi115 Oct 24, 2023
650516b
Update viikko4.md
ptrktn Oct 24, 2023
9f0f3b9
Merge pull request #46 from ptrktn/main
mluukkai Oct 24, 2023
d12b01a
Merge pull request #45 from akirataguchi115/patch-11
mluukkai Oct 24, 2023
70de4d9
Update viikko1.md
rvl-q Nov 4, 2023
cd91a09
Merge pull request #48 from rvl-q/patch-1
mluukkai Nov 4, 2023
2cc6c16
breweries_controller olut -> panimo
rvl-q Nov 7, 2023
4009cd6
Merge pull request #49 from rvl-q/patch-2
mluukkai Nov 8, 2023
6bfbdfe
removed programmableweb.com from week5, site does not exist
iritmaximus Nov 12, 2023
bb4fb4e
Update week1.md
akirataguchi115 Nov 15, 2023
49e4ba0
Merge pull request #50 from iritmaximus/programmable-web
mluukkai Nov 15, 2023
b4a1dc6
Merge pull request #51 from akirataguchi115/patch-13
mluukkai Nov 15, 2023
7bb893c
Fix typo
akirataguchi115 Nov 16, 2023
1951a10
Merge pull request #52 from akirataguchi115/patch-14
mluukkai Nov 16, 2023
d0c9495
Fix FactoryBot URL
akirataguchi115 Nov 29, 2023
09a226f
Fix Beck URL
akirataguchi115 Nov 29, 2023
95e5cbd
Fix linting
akirataguchi115 Nov 29, 2023
cfa6a01
Merge pull request #53 from akirataguchi115/patch-15
mluukkai Nov 29, 2023
3a30b41
Merge pull request #56 from akirataguchi115/patch-17
mluukkai Nov 29, 2023
3b02a18
Merge pull request #54 from akirataguchi115/patch-16
mluukkai Nov 29, 2023
4ee62e8
Update viikko6.md
rvl-q Dec 1, 2023
09bf954
Merge pull request #57 from rvl-q/patch-3
mluukkai Dec 2, 2023
998d41e
Update wadror.md
mluukkai Dec 4, 2023
aeb42e0
pic
mluukkai Dec 4, 2023
4626f9f
Update wadror.md
mluukkai Dec 4, 2023
6575a98
Update wadror.md
mluukkai Dec 4, 2023
e59fd79
Update wadror.md
mluukkai Dec 4, 2023
782b6c3
Update wadror.md
mluukkai Dec 4, 2023
550b47b
Update wadror-english.md
mluukkai Dec 4, 2023
9611f22
Add missing image
heikkivenhola Dec 5, 2023
f0a315f
Merge pull request #58 from heikkivenhola/patch-7
mluukkai Dec 5, 2023
d6c34c6
(viikko2.md) fix typo
valosorsa Dec 11, 2023
786f834
Merge pull request #59 from valosorsa/patch-1
mluukkai Dec 11, 2023
3b88d7a
Update viikko7.md
mluukkai Dec 14, 2023
aa45342
Update viikko7.md
mluukkai Dec 14, 2023
26e62fd
Update viikko7.md
mluukkai Dec 14, 2023
f3d82e0
Fix TDD url
akirataguchi115 Dec 15, 2023
ccff566
Add Enumerable docs
akirataguchi115 Dec 15, 2023
283dfd8
Merge pull request #60 from akirataguchi115/patch-18
mluukkai Dec 15, 2023
0ee7cfb
Merge pull request #61 from akirataguchi115/patch-19
mluukkai Dec 15, 2023
7d456f7
Fix dead link
akirataguchi115 Dec 19, 2023
40d3762
Fix dead link
akirataguchi115 Dec 19, 2023
8f616b6
Merge pull request #63 from akirataguchi115/patch-21
mluukkai Dec 19, 2023
ac2a29d
Merge pull request #62 from akirataguchi115/patch-20
mluukkai Dec 20, 2023
dd0f34d
Fix a bug in last page calculation
ptrktn May 29, 2024
0048158
Fix vertical vs horizontal concepts
ptrktn Jun 6, 2024
3d714db
Merge pull request #66 from ptrktn/main
mluukkai Jun 7, 2024
0e76598
change week 5 to match modern Rails way
heikkivenhola Aug 21, 2024
a84f0fd
Merge pull request #67 from heikkivenhola/main
mluukkai Aug 22, 2024
6368c84
update course deadlines for 2024 instance
LeeviHalme Nov 10, 2024
0e9f491
Merge pull request #69 from LeeviHalme/main
mluukkai Nov 11, 2024
7c1941a
update TDD links
LeeviHalme Nov 21, 2024
e3c1fd5
Merge pull request #70 from LeeviHalme/patch-1
mluukkai Nov 22, 2024
4429b75
Update README.md
mluukkai Nov 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Web-palvelinohjelmointi Ruby on Rails 2023, Tietojenkäsittelytieteen osasto, Helsingin Yliopisto
## Web-palvelinohjelmointi Ruby on Rails 2024, Tietojenkäsittelytieteen osasto, Helsingin Yliopisto

Kurssisivu https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/wadror.md

Expand All @@ -11,8 +11,9 @@ Kurssisivu https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/wadr
- [viikko 5](https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/web/viikko5.md)
- [viikko 6](https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/web/viikko6.md)
- [viikko 7](https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/web/viikko7.md)
- [viikko 8 (in English)](https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/english/week8.md)

## Web-development Ruby on Rails 2023, Department of Computer Science, University of Helsinki
## Web-development Ruby on Rails 2024, Department of Computer Science, University of Helsinki

Course page https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/wadror-english.md

Expand All @@ -25,3 +26,4 @@ Course page https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/wad
- [week 5](https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/english/week5.md)
- [week 6](https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/english/week6.md)
- [week 7](https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/english/week7.md)
- [week 8](https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/english/week8.md)
2 changes: 1 addition & 1 deletion english/week1.md
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ Go to the application directory and create a Fly.io application with `fly launch

Deploy your application to production with `fly deploy`. Use this command everytime you wish to push the current version of your application to the internet.

You can open your application in a browser with `fly open`.
You can open your application in a browser with `fly apps open`.

**Note** that (currently) there is nothing at the root of the application, eg. in my case in https://ratebeer.fly.dev/. Our beers can be found at https://ratebeer.fly.dev/beers and breweries at https://ratebeer.fly.dev/breweries.

Expand Down
12 changes: 6 additions & 6 deletions english/week2.md
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ If you remove beers with ratings from your application, the ratings which belong
>
> The error is caused by the fact that it tries to call <code>beer.name</code> from the method <code>to_s</code> of the rating object.
>
> Delete the orphan ratings by hand from the console. Try to think first of a command/some commands, which can help you to make a list of the orphan ratings. If you can't think of it yourself, you can find a ready-made answer for the exercise above in this page.
> Delete the orphan ratings by hand from the console. Try to think first of a command/some commands, which can help you to make a list of the orphan ratings. If you can't think of it yourself, you can find a ready-made answer for the exercise below in this page.

The ratings which belong to a beer can be deleted easily automatically. Alongside the beer model code <code>has_many :ratings</code>, you should mark that ratings are dependent on beers, and that they should be destroyed if beers are destroyed:

Expand All @@ -1189,7 +1189,7 @@ The orphan issue is solved now.
>
>If you can't yet access individual breweries from the all breweries page, fix it now!

## Inderect object connection
## Indirect object connection

Your application is created in a way so that ratings belong to beers and that beers belong to breweries. This means that a set of ratings belong to each brewery, indirectly. Rails provides you with a simple way to go from the breweries to the ratings directly:

Expand Down Expand Up @@ -1227,7 +1227,7 @@ You will see, that beer and brewery both a method called <code>average_rating</c
We notice that beer and brewery both have an identically named method <code>average_rating</code> that also work identically. It is not acceptable to leave our code this way.
> ## Exercise 15
>
> Ruby provides you with a way to share methods between two classes with the help of modules, see https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/web/rubyn_perusteita.md#moduuli
> Ruby provides you with a way to share methods between two classes with the help of modules, see https://ruby-doc.com/docs/ProgrammingRuby/html/tut_modules.html
>
> Modules have different uses – forming namespaces, for instance. However, now we are interested in the _mixin_ inheritance which can be implemented with modules.
>
Expand All @@ -1242,7 +1242,7 @@ We notice that beer and brewery both have an identically named method <code>aver
> end
> ```
>
> - Attention: if the name of your module is <code>RatingAverage</code>, exactly like in the example, because of Ruby naming conventions it has to be placed in the file <code>app/models/concerns/rating_average.rb</code>. In fact, even though classes names are CamelCase and start with capital letters, their files names follow the snake_case.rb style.
> - Attention: if the name of your module is <code>RatingAverage</code>, exactly like in the example, because of Ruby naming conventions it has to be placed in the file <code>app/models/concerns/rating_average.rb</code>. In fact, even though classes names are PascalCase and start with capital letters, their files names follow the snake_case.rb style.

After you have done the exercise, the class Brewery should look more or less like below (assuming your module is called RatingAverage):

Expand Down Expand Up @@ -1390,7 +1390,7 @@ http://api.rubyonrails.org/classes/ActionController/HttpAuthentication/Digest.ht
>```
>and try what values variables _admin_accounts_, _username_ and _password_ contain ja form the right command.
>
> HINT 2: The code block should be evaluated either as true or untrue depending on whether the password is correct. The value doesn't however necessarily have to be either _true_ or _false_ because Ruby interprets also other values as either true (truthy) or untrue (falsy). For example _nil_ is interpreted as untrue/falsy. See more eg. at https://learn.co/lessons/truthiness-in-ruby-readme.
> HINT 2: The code block should be evaluated either as true or untrue depending on whether the password is correct. The value doesn't however necessarily have to be either _true_ or _false_ because Ruby interprets also other values as either true (truthy) or untrue (falsy). For example _nil_ is interpreted as untrue/falsy. See more at [Truth value - Wikipedia](https://en.wikipedia.org/wiki/Truth_value#Computing), [class TrueClass](https://docs.ruby-lang.org/en/master/TrueClass.html) and [class FalseClass](https://docs.ruby-lang.org/en/master/FalseClass.html) .

## Application to Internet

Expand Down Expand Up @@ -1511,7 +1511,7 @@ Because it is a program in production, resetting the database (<code>rails db:dr

Commit all your changes and push the code to Github. Deploy to the newest version to Heroku or Fly.io, as well.

Mark the exercises you have done at https://studies.cs.helsinki.fi/stats/courses/rails2023.
Mark the exercises you have done at https://studies.cs.helsinki.fi/stats/courses/rails2023/submissions.

And let's continue coding: [week 3](https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/english/week3.md).

10 changes: 7 additions & 3 deletions english/week3.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,11 @@ The styling rules monitored by Rubocop are defined in _.rubocop.yml_ that is pla

The rules defined there are based on the [Relaxed Ruby](https://relaxed.ruby.style/) style, but they are a bit stricter on some points. The file contents also define that some files are to be left out of any style checks.

A code style check is executed with the command _rubocop_ on the command line.
A code style check is executed with the command
```
rubocop
```
on the command line.

There are quite a few problems in the code, for example:

Expand Down Expand Up @@ -480,7 +484,7 @@ Create a controller for sessions (in the file app/controllers/sessions_controlle
```ruby
class SessionsController < ApplicationController
def new
# render the signing up page
# render the signing up page
end

def create
Expand Down Expand Up @@ -988,7 +992,7 @@ http://guides.rubyonrails.org/active_record_validations.html and https://apidock
>
> Add the following validations to your program
> * beer and brewery names are not empty
> * the brewery founding year is an integer between 1040-2022
> * the brewery founding year is an integer between 1040-2023
> * the length of the username attribute of the User class is 3 – 30 characters

If you try to create a beer with an empty name, you get an error message
Expand Down
25 changes: 17 additions & 8 deletions english/week4.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ You can initialize rspec in your application running the following from command

The initialization creates a folder /spec in the application and the various tests – or specs – will be placed in its subfolders.

According to Rails standard but currently less common testing framework, the test are place in the folder /test. The folder will be useless after taking rspec, and you can delete it.
According to Rails standard but currently less common testing framework, the tests are placed in the folder /test. The folder will be useless after taking up rspec as the only testing tool, and you can delete it.

The tests – the correct words would be specs or specifications when it comes to rspec, we will be using the word test in the future however – can be written at different levels: unit tests for models and controllers, view tests, and integration tests for controllers. In addition to these, the application can be tested using a simulated browser with the help of the capybara gem https://github.com/jnicklas/capybara.

Expand Down Expand Up @@ -663,7 +663,7 @@ FactoryBot.create(:user)
FactoryBot.create(:user, username: 'Vilma')
```

More instructions for using FactoryBot at https://www.rubydoc.info/gems/factory_bot/file/GETTING_STARTED.md
More instructions for using FactoryBot at https://thoughtbot.github.io/factory_bot/

## Users favourite beers, breweries, and styles

Expand Down Expand Up @@ -744,7 +744,7 @@ end

Your test will not succeed, because your method does not do anything so far, and its return value is always <code>nil</code>.

Use [in the spirit of TDD](https://stanislaw.github.io/2016/01/25/notes-on-test-driven-development-by-example-by-kent-beck.html) a "fake solution", without trying to make the final working version yet:
Use [in the spirit of TDD](https://stanislaw.github.io/2016-01-25-notes-on-test-driven-development-by-example-by-kent-beck.html) a "fake solution", without trying to make the final working version yet:

```ruby
class User < ApplicationRecord
Expand All @@ -758,7 +758,15 @@ class User < ApplicationRecord
end
```

Make another test which will force you to make a real implementation [(see triangulation)](https://stanislaw.github.io/2016/01/25/notes-on-test-driven-development-by-example-by-kent-beck.html):
Make another test which will force you to make a real implementation [(see triangulation)](https://stanislaw.github.io/2016-01-25-notes-on-test-driven-development-by-example-by-kent-beck.html#triangulation):

> How do you most conservatively drive abstraction with tests? Abstract only when you have two or more examples. (p.153)
>
> If two receiving stations at a known distance from teach other can both measure the direction of a radio signal, then there is enough information to calculate the range and bearing of the signal. This calculation is called Triangulation.
>
> By analogy when we triangulate, we only generalize code when we have two examples or more... When the second example demands a more general solution, then and only then do we generalize (p.16).
>
> I only use Triangulation when I'm really, really unsure about the correct abstraction for the calculation. Otherwise I rely on either Obvious Implementation or Fake It. (p.154)

```ruby
it "is the one with highest rating if several rated" do
Expand Down Expand Up @@ -817,6 +825,7 @@ If you look at the documentation (http://guides.rubyonrails.org/active_record_qu
```ruby
def favorite_beer
return nil if ratings.empty?

ratings.order(score: :desc).limit(1).first.beer
end
```
Expand Down Expand Up @@ -1126,7 +1135,7 @@ If/when you ran into problems:
>
>Add information about the user's favourite style to their page.
>
> Do not do everything with one method (unless you solve the problem at database level with ActiveRecord or another elegantly compact solution), instead, define the suitable auxiliary methods! If you notice that you method is more than 6 lines long, you are doing either too much or something too complicated, so refactor your code. Ruby's collections have various auxiliary methods which might be useful for the exercise, see http://http://ruby-doc.org/core-2.5.1/Enumerable.html
> Do not do everything with one method (unless you solve the problem at database level with ActiveRecord or another elegantly compact solution), instead, define the suitable auxiliary methods! If you notice that you method is more than 6 lines long, you are doing either too much or something too complicated, so refactor your code. Ruby's collections have various auxiliary methods which might be useful for the exercise, see https://docs.ruby-lang.org/en/3.2/Enumerable.html

> ## Exercise 4
>
Expand Down Expand Up @@ -1360,7 +1369,7 @@ The test expects that clicking the _Create user_ button will cause the number of
You will have to take into consideration a small detail, that is, the method <code>expect</code> can be given parameters in two ways.
If the method has to test a value, the value is given between brackets, like <code>expect(current_path).to eq(signin_path)</code>. Instead, if it tests the impact of an operation (like the one above, <code>click_button('Create User')</code>) on the value of an application object (<code>User.count</code>), the operation to execute is given to <code>expect</code> in a code chunk.

Read more about this in Rspec documentation https://relishapp.com/rspec/rspec-expectations/docs/built-in-matchers
Read more about this in Rspec documentation https://rspec.info/features/3-12/rspec-expectations/built-in-matchers/

So the last test checked whether the operation executed at browser level created an object in the database. Should you make a separate test to see whether a username can sign in the system? Maybe. After all, the previous test did not questioned whether the user object was saved in the database correctly.

Expand Down Expand Up @@ -1403,7 +1412,7 @@ end
The test builds its brewery, two beers and a user with the method <code>let!</code> instead of <code>let</code> which we used earlier. In fact, the version without exclamation mark does not execute the operation immediately, but only once the code refers to the object explicitly. The object <code>beer1</code> is mentioned only at the end of the code, so if you had created it with the method <code>let</code>, you would have run into a problem creating the rating, because its beer would have not existed in the database yet, and the corresponding select element would not have been found.


The code contained in the <code>before</code> chunk of the test helps users to sign in the system. Most probably, the same code chunk will be useful in various different test files. You had better extract the test code needed in various different places and make a [module](https://relishapp.com/rspec/rspec-core/docs/helper-methods/define-helper-methods-in-a-module), which can be included in all test files which need it. Create a module <code>Helpers</code> in a file named _helpers.rb_ in the _specs_ directory and put the sign-in code there:
The code contained in the <code>before</code> chunk of the test helps users to sign in the system. Most probably, the same code chunk will be useful in various different test files. You had better extract the test code needed in various different places and make a [module](https://rspec.info/features/3-12/rspec-core/helper-methods/modules/), which can be included in all test files which need it. Create a module <code>Helpers</code> in a file named _helpers.rb_ in the _specs_ directory and put the sign-in code there:

```ruby
module Helpers
Expand Down Expand Up @@ -1795,4 +1804,4 @@ Commit all your changes and push the code to Github. Deploy to the newest versio

Mark the exercises you have done at https://studies.cs.helsinki.fi/stats/courses/rails2023.

And towards next week: [week 5](https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/english/week5.md)
And towards next week: [week 5](https://github.com/mluukkai/WebPalvelinohjelmointi2023/blob/main/english/week5.md)
29 changes: 4 additions & 25 deletions english/week5.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ You will continue to develop your application from the point you arrived at the

A great part of modern Internet services makes use of open interfaces which provides useful data to enrich applications functionality.

Interfaces for beers are also available, try to search for beer at http://www.programmableweb.com/

The best interface among the ones available seems to be Beermapping API (see http://www.programmableweb.com/api/beer-mapping ja http://beermapping.com/api/), which makes it possible to search for beer restaurants.
The best interface among the ones available seems to be Beermapping API <http://beermapping.com/api/>, which makes it possible to search for beer restaurants.

Applications which make use of beermaping API need a singular API key. You can retrieve a key at https://beermapping.com/api/, after logging in to the page (after logging in edit the url in your browser's address bar back to https://beermapping.com/api/). This is a common procedure in use for the larger part of modern free interfaces.

Expand Down Expand Up @@ -501,7 +499,7 @@ response = HTTParty.get "#{url}#{ERB::Util.url_encode(params[:city])}"

## Refactoring your Places controller

Rails controllers should not include application logic. It is a best practice to put external APIs in their own class. A good place for such class is in the _lib_ folder. Place the following code into the file _lib/beermapping_api.rb_:
Rails controllers should not include application logic. It is a best practice to put external APIs in their own class. A good place for such class is in the _services_ folder under the _app_ folder. Even though this folder is not automatically created, starting from Rails 5 the _services_ folder is autoloaded in boot sequence. Place the following code into the file _app/services/beermapping_api.rb_:

```ruby
class BeermappingApi
Expand All @@ -527,26 +525,7 @@ end

So the class defines a static method which returns a table of the beer restaurants which have been found in the towns defined by the parameter. If no restaurant is found, the table will be empty. The API class is not in its best format yet, because you cannot know completely what other methods you need.

To ensure that code in the _lib_ folder will work (not only on your computer but also in Heroku and Fly.io), you must add these two lines into the _config/application.rb_ file:

```ruby
config.autoload_paths << Rails.root.join("lib")
config.eager_load_paths << Rails.root.join("lib")
```

The addition should be placed inside _Application_ class definition

```ruby
module Ratebeer
class Application < Rails::Application
# ...

# add here
end
end
```

Restart the application for the changes to take effect.
If needed, restart the application.

The controller will be looking neat, by now:

Expand Down Expand Up @@ -665,7 +644,7 @@ mluukkai@melkki$ curl http://beermapping.com/webservice/loccity/731955affc547174
Now you could copy-paste the information returned in XML form by the HTTP reqest to your test. If you want to be sure you place the XML right in the string, you should use a quite particular syntax
see http://blog.jayfields.com/2006/12/ruby-multiline-strings-here-doc-or.html where the string is placed between <code><<-END_OF_STRING</code> and <code>END_OF_STRING</code>.

You find below the test code which should be placed into spec/lib/beermapping_api_spec.rb (deciding to place the code in the lib subfolder because the test destination is an auxiliary class in the lib folder):
You find below the test code which should be placed into spec/services/beermapping_api_spec.rb (deciding to place the code in the services subfolder because the test destination is an auxiliary class in the services folder):

```ruby
require 'rails_helper'
Expand Down
Loading