forked from fixin.me/fixin.me
Merge feature/measurements-wide-view (fixes: data-column, fetch error handling, controller tests)
This commit is contained in:
@@ -47,10 +47,8 @@ function buildWideTable() {
|
|||||||
groupMap.get(key).rows.push(r);
|
groupMap.get(key).rows.push(r);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Read column headers from compact thead
|
var takenAtHeader = (document.querySelector('[data-column="taken-at"]') || {}).textContent || '';
|
||||||
var ths = document.querySelectorAll('.measurements-compact thead th');
|
var createdAtHeader = (document.querySelector('[data-column="created-at"]') || {}).textContent || '';
|
||||||
var takenAtHeader = ths[3] ? ths[3].textContent : '';
|
|
||||||
var createdAtHeader = ths[4] ? ths[4].textContent : '';
|
|
||||||
|
|
||||||
var table = document.createElement('table');
|
var table = document.createElement('table');
|
||||||
table.className = 'items-table';
|
table.className = 'items-table';
|
||||||
@@ -243,7 +241,8 @@ function editMeasurementWide(url) {
|
|||||||
panel.scrollIntoView({behavior: 'smooth', block: 'nearest'});
|
panel.scrollIntoView({behavior: 'smooth', block: 'nearest'});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
})
|
||||||
|
.catch(err => console.error('editMeasurementWide failed:', err));
|
||||||
}
|
}
|
||||||
window.editMeasurementWide = editMeasurementWide
|
window.editMeasurementWide = editMeasurementWide
|
||||||
|
|
||||||
@@ -282,7 +281,8 @@ function setDefaultUnit(button) {
|
|||||||
}
|
}
|
||||||
return response.text();
|
return response.text();
|
||||||
})
|
})
|
||||||
.then(html => Turbo.renderStreamMessage(html));
|
.then(html => Turbo.renderStreamMessage(html))
|
||||||
|
.catch(err => console.error('setDefaultUnit failed:', err));
|
||||||
}
|
}
|
||||||
window.setDefaultUnit = setDefaultUnit
|
window.setDefaultUnit = setDefaultUnit
|
||||||
|
|
||||||
|
|||||||
@@ -22,8 +22,8 @@
|
|||||||
<th><%= Quantity.model_name.human %></th>
|
<th><%= Quantity.model_name.human %></th>
|
||||||
<th><%= Readout.human_attribute_name(:value) %></th>
|
<th><%= Readout.human_attribute_name(:value) %></th>
|
||||||
<th><%= Unit.model_name.human %></th>
|
<th><%= Unit.model_name.human %></th>
|
||||||
<th><%= Readout.human_attribute_name(:taken_at) %></th>
|
<th data-column="taken-at"><%= Readout.human_attribute_name(:taken_at) %></th>
|
||||||
<th><%= Readout.human_attribute_name(:created_at) %></th>
|
<th data-column="created-at"><%= Readout.human_attribute_name(:created_at) %></th>
|
||||||
<% if current_user.at_least(:active) %>
|
<% if current_user.at_least(:active) %>
|
||||||
<th></th>
|
<th></th>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
@@ -1,7 +1,65 @@
|
|||||||
require "test_helper"
|
require "test_helper"
|
||||||
|
|
||||||
class MeasurementsControllerTest < ActionDispatch::IntegrationTest
|
class MeasurementsControllerTest < ActionDispatch::IntegrationTest
|
||||||
# test "the truth" do
|
setup do
|
||||||
# assert true
|
host! '127.0.0.1'
|
||||||
# end
|
@user = users(:alice)
|
||||||
|
post new_user_session_path, params: { user: { email: @user.email, password: 'alice' } }
|
||||||
|
@quantity = @user.quantities.create!(name: 'Weight')
|
||||||
|
@unit = @user.units.create!(symbol: 'kg')
|
||||||
|
end
|
||||||
|
|
||||||
|
test "index returns ok" do
|
||||||
|
get measurements_path
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
test "index requires authentication" do
|
||||||
|
delete destroy_user_session_path
|
||||||
|
get measurements_path
|
||||||
|
assert_response :redirect
|
||||||
|
end
|
||||||
|
|
||||||
|
test "create records readout with taken_at" do
|
||||||
|
taken_at = 1.day.ago.change(usec: 0)
|
||||||
|
assert_difference -> { @user.readouts.count } do
|
||||||
|
post measurements_path, params: {
|
||||||
|
taken_at: taken_at.iso8601,
|
||||||
|
readouts: [{ quantity_id: @quantity.id, value: '82.5', unit_id: @unit.id }]
|
||||||
|
}, as: :turbo_stream
|
||||||
|
end
|
||||||
|
assert_response :success
|
||||||
|
assert_equal taken_at, @user.readouts.last.taken_at
|
||||||
|
end
|
||||||
|
|
||||||
|
test "create with no readouts selected shows alert" do
|
||||||
|
post measurements_path, params: { taken_at: Time.now.iso8601 }, as: :turbo_stream
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
test "destroy removes readout" do
|
||||||
|
readout = @user.readouts.create!(quantity: @quantity, unit: @unit, value: 82.5, taken_at: 1.day.ago)
|
||||||
|
assert_difference -> { @user.readouts.count }, -1 do
|
||||||
|
delete measurement_path(readout), as: :turbo_stream
|
||||||
|
end
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
test "destroy cannot remove another user's readout" do
|
||||||
|
other_quantity = users(:bob).quantities.create!(name: 'Weight')
|
||||||
|
other_unit = users(:bob).units.create!(symbol: 'kg')
|
||||||
|
readout = users(:bob).readouts.create!(quantity: other_quantity, unit: other_unit, value: 70.0, taken_at: 1.day.ago)
|
||||||
|
assert_no_difference -> { users(:bob).readouts.count } do
|
||||||
|
delete measurement_path(readout), as: :turbo_stream
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "update changes readout value" do
|
||||||
|
readout = @user.readouts.create!(quantity: @quantity, unit: @unit, value: 82.5, taken_at: 1.day.ago)
|
||||||
|
patch measurement_path(readout), params: {
|
||||||
|
readout: { value: '83.0', unit_id: @unit.id, taken_at: readout.taken_at.iso8601 }
|
||||||
|
}, as: :turbo_stream
|
||||||
|
assert_response :success
|
||||||
|
assert_in_delta 83.0, readout.reload.value
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user