Skip to content

Commit

Permalink
Dry the index right up
Browse files Browse the repository at this point in the history
  • Loading branch information
pikesley committed Jan 29, 2016
1 parent 58a6341 commit 1a210d2
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 56 deletions.
8 changes: 6 additions & 2 deletions app/assets/stylesheets/fonts.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
@import url(//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css);
@import url(//fonts.googleapis.com/css?family=NTR);
@import url('//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css');
@import url('//fonts.googleapis.com/css?family=NTR');

$font-family-sans-serif: NTR, sans-serif;

body {
text-transform: lowercase;
}
7 changes: 7 additions & 0 deletions app/models/medication_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ def self.insulins
]
end

def self.fields
[
'amount',
'insulin'
]
end

validates :datetime, presence: true
validates :amount, presence: true, numericality: { greater_than: 0 }
validates :insulin, presence: true, inclusion: { in: insulins }
Expand Down
34 changes: 3 additions & 31 deletions app/views/glucose_measurements/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
<%= link_to 'Add new measurement', new_glucose_measurement_path, class: 'btn btn-success pull-right' %>
<h1>Glucose</h1>

<table class='table table-bordered table-striped' id='blood-glucose'>
<% date_sift(@metrics).each_pair do |day, values| %>
<tr>
<th colspan=2>
<% date = Time.parse(day) %>
<%= date.strftime "%A %B #{date.day.ordinalize}" %>
</th>
</tr>
<% values.each do |value| %>
<%= render partial: 'shared/table_row', locals: {
passed_value: value,
fields: ['value'],
edit_path: edit_glucose_measurement_path(value)
} %>
<% end %>
<% end %>
</table>

<script>
$('.glucose-measurement-value').each(function() {
if($(this).text() > 8.0) {
$(this).addClass('bg-high')
}
if($(this).text() < 4.0) {
$(this).addClass('bg-low')
}
})
</script>
<%= render partial: 'shared/index', locals: {
title: 'glucose'
} %>
23 changes: 3 additions & 20 deletions app/views/medication_events/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
<%= link_to 'Add new meds', new_medication_event_path, class: 'btn btn-success pull-right' %>
<h1>Meds</h1>

<table class='table table-bordered table-striped'>
<% date_sift(@metrics).each_pair do |day, values| %>
<tr>
<th colspan=3>
<% date = Time.parse(day) %>
<%= date.strftime "%A %B #{date.day.ordinalize}" %>
</th>
</tr>
<% values.each do |value| %>
<%= render partial: 'shared/table_row', locals: {
passed_value: value,
fields: ['amount', 'insulin'],
edit_path: edit_medication_event_path(value)
} %>
<% end %>
<% end %>
</table>
<%= render partial: 'shared/index', locals: {
title: 'meds'
} %>
8 changes: 8 additions & 0 deletions app/views/shared/_index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%= link_to "Add new #{title}",
send("new_#{@metrics.first.class.name.underscore}_path".to_sym),
class: 'btn btn-success pull-right' %>
<h1><%= title.titleize %></h1>

<%= render partial: 'shared/table', locals: {
metrics: @metrics
} %>
20 changes: 20 additions & 0 deletions app/views/shared/_table.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<table class='table table-bordered table-striped'>
<% date_sift(metrics).each_pair do |day, values| %>
<tr>
<th colspan=64> <!-- bootstrap ignores this maybe? -->
<% date = Time.parse(day) %>
<%= date.strftime "%A %B #{date.day.ordinalize}" %>
</th>
</tr>
<% values.each do |value| %>
<%= render partial: 'shared/table_row', locals: {
passed_value: value,
fields: begin
@metrics.first.class.fields
rescue NoMethodError
@metrics.first.attributes.keys[2..-3]
end
} %>
<% end %>
<% end %>
</table>
4 changes: 3 additions & 1 deletion app/views/shared/_table_row.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<tr>
<td>
<%= link_to passed_value.datetime.strftime('%H:%M'), edit_path %>
<%= link_to passed_value.datetime.strftime('%H:%M'),
send("edit_#{passed_value.class.name.underscore}_path".to_sym, passed_value) %>
<!-- this resolves to e.g. edit_medication_event_path -->
</td>

<% fields.each do |field| %>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20160129203307_move_column.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MoveColumn < ActiveRecord::Migration
def change
change_column :medication_events, :amount, :float, after: :insulin
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160128204322) do
ActiveRecord::Schema.define(version: 20160129203307) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/glucose_measurements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

click_link 'Delete'
expect(GlucoseMeasurement.count).to eq 2
expect(page).to have_link 'Add new measurement', href: '/glucose/new'
expect(page).to have_link 'Add new glucose', href: '/glucose/new'
within 'th' do
expect(page).to have_content 'Wednesday January 27th'
end
Expand Down
1 change: 1 addition & 0 deletions spec/requests/medication_events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
fill_in 'Amount', with: '10.5'
select 'humalog', from: 'Insulin'
click_button 'Create'
expect(page.all('td').map { |cell| cell.text }).to eq ['14:00', '10.5', 'humalog']

visit new_medication_event_url(as: user)
fill_in 'Date and time', with: '2016-02-25 10:00:00'
Expand Down

0 comments on commit 1a210d2

Please sign in to comment.