Skip to content

Commit fbaa2e0

Browse files
committed
added validation messages
1 parent 795c862 commit fbaa2e0

File tree

5 files changed

+59
-28
lines changed

5 files changed

+59
-28
lines changed

app/controllers/ds18b20s_controller.rb

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ def create
1111
temp_sensor.save
1212
redirect_to home_path
1313
else
14+
if temp_sensor.path.empty? or temp_sensor.file.empty?
15+
flash[:error] = 'Something is missing' + temp_sensor.path
16+
else
17+
flash[:error] = 'Path must have trailing /'
18+
end
1419
redirect_to new_ds18b20_path
1520
end
1621
end
@@ -29,6 +34,11 @@ def update
2934
if temp_sensor.valid?
3035
redirect_to home_path
3136
else
37+
if temp_sensor.path.empty? or temp_sensor.file.empty?
38+
flash[:error] = 'Something is missing' + temp_sensor.path
39+
else
40+
flash[:error] = 'Path must have trailing /'
41+
end
3242
redirect_to edit_ds18b20_path(temp_sensor)
3343
end
3444
end

app/models/ds18b20.rb

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ class Ds18b20 < ApplicationRecord
44
with: /\/\z/,
55
message: 'Path must end with a /'
66
}
7+
validates :file, presence: true
8+
validates :path, presence: true
9+
validates :name, presence: true
710

811
def creading
912
if File.exist?(path + file)

app/views/pages/home.html.erb

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<h1>
22
Sensor Manager
3-
<p>
4-
5-
<%= link_to I18n.t("views.home.link.ds18b20_create"), new_ds18b20_path%>
6-
<p>
7-
8-
<% @sensors = Ds18b20.all %>
9-
<% @sensors.each do |s| %>
10-
<%= link_to s.name, ds18b20_path(s) %>
11-
<% end %>
123
</h1>
4+
<p>
5+
6+
<%= link_to I18n.t("views.home.link.ds18b20_create"), new_ds18b20_path%>
7+
<p>
8+
<h2> Sensors list:</h2>
9+
<% @sensors = Ds18b20.all %>
10+
<% @sensors.each do |s| %>
11+
<%= link_to s.name, ds18b20_path(s) %>
12+
<p>
13+
<% end %>

spec/features/using_ds18b20_spec.rb

+33-19
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
feature 'Visitor' do
44
scenario 'creates a new DS18B20 sensor' do
55
sensor = build(:ds18b20)
6-
create_ds18b20(sensor)
6+
create_ds18b20_via_form(sensor)
7+
visit '/'
78

89
expect(page).to have_link('sensor1')
910
end
1011

1112
scenario 'views the sensor reading' do
12-
sensor = build(:ds18b20)
13-
create_ds18b20(sensor)
13+
sensor = create(:ds18b20)
14+
visit '/'
1415
click_link('sensor1')
1516

1617
expect(page).to have_content('156')
1718
end
1819

19-
scenario 'edits an existing ds10b20' do
20-
sensor = build(:ds18b20)
21-
create_ds18b20(sensor)
20+
scenario 'edits an existing ds18b20' do
21+
sensor = create(:ds18b20)
22+
visit '/'
2223
click_link('sensor1')
2324
click_link('edit')
2425
fill_in 'ds18b20_name', with: 'sensor2'
@@ -35,36 +36,49 @@
3536
visit '/'
3637
click_link(sensor.name)
3738
click_link('edit')
38-
fill_in 'ds18b20_name', with: 'sensor2'
3939
fill_in 'ds18b20_path', with: '/bad/path'
4040
click_button I18n.t('views.generic.buttons.save')
4141

4242
expect(page).to have_current_path(edit_ds18b20_path(sensor))
43-
43+
expect(page).to have_content('Path must have trailing /')
4444
end
4545

46-
scenario 'provides a bad file and/or path' do
47-
sensor = build(:ds18b20)
48-
sensor.file='bad_file'
46+
scenario 'edits and exsting ds18b20 and does not include path' do
47+
sensor = create(:ds18b20)
48+
visit '/'
49+
click_link(sensor.name)
50+
click_link('edit')
51+
fill_in 'ds18b20_path', with: ''
52+
click_button I18n.t('views.generic.buttons.save')
53+
54+
expect(page).to have_current_path(edit_ds18b20_path(sensor))
55+
expect(page).to have_content('Something is missing')
56+
end
4957

50-
create_ds18b20(sensor)
58+
scenario 'provides a bad file' do
59+
sensor = create(:ds18b20, file: 'bad_file')
60+
visit '/'
5161
click_link('sensor1')
5262

5363
expect(page).to have_content('-256')
5464
end
5565

56-
scenario 'does not provide a trailing / for the path' do
57-
sensor = build(:ds18b20)
58-
sensor.path.chop!
59-
create_ds18b20(sensor)
60-
61-
expect(page).to have_current_path(new_ds18b20_path)
66+
scenario 'provides no file' do
67+
sensor = build(:ds18b20, file: nil)
68+
create_ds18b20_via_form(sensor)
6269

70+
expect(page).to have_content('Something is missing')
6371
end
6472

73+
scenario 'provides no trailing / on path' do
74+
sensor = build(:ds18b20, path: '/bad/path')
75+
create_ds18b20_via_form(sensor)
76+
77+
expect(page).to have_content('Path must have trailing /')
78+
end
6579
private
6680

67-
def create_ds18b20(sensor)
81+
def create_ds18b20_via_form(sensor)
6882
visit '/'
6983
click_link I18n.t('views.home.link.ds18b20_create')
7084
fill_in 'ds18b20_name', with: sensor.name

spec/models/ds18b20_spec.rb

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
it {should respond_to :file}
77
it {should respond_to :creading }
88
it {should respond_to :freading }
9+
it {should validate_presence_of :file }
10+
it {should validate_presence_of :path }
11+
it {should validate_presence_of :name }
912

1013
context 'has a file to read' do
1114
it 'expects a temp reading from sensor1.txt' do

0 commit comments

Comments
 (0)