Skip to content

Commit

Permalink
Refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
pikesley committed Feb 25, 2016
1 parent 210bc34 commit f68274a
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/models/blood_pressure.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class BloodPressure < ActiveRecord::Base
include PresenterExtension

def self.short_name
'Blood Pressure'
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/carbohydrate_intake.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class CarbohydrateIntake < ActiveRecord::Base
include PresenterExtension

def self.short_name
'Carbs'
end
Expand Down
11 changes: 11 additions & 0 deletions app/models/concerns/presenter_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module PresenterExtension
extend ActiveSupport::Concern

module ClassMethods
def presenter
GlucoseMeasurementPresenter
end
end
end

ActiveRecord::Base.send(:include, PresenterExtension)
2 changes: 2 additions & 0 deletions app/models/glucose_measurement.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class GlucoseMeasurement < ActiveRecord::Base
include PresenterExtension

def self.short_name
'Glucose'
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/glycated_haemoglobin.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class GlycatedHaemoglobin < ActiveRecord::Base
include PresenterExtension

def self.short_name
'HbA1c'
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/medication_event.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class MedicationEvent < ActiveRecord::Base
include PresenterExtension

def self.short_name
'Meds'
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/physical_exercise.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class PhysicalExercise < ActiveRecord::Base
include PresenterExtension

def self.short_name
'Exercise'
end
Expand Down
4 changes: 4 additions & 0 deletions app/presenters/generic_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ def wrapped_model
}
end

def short_name
model.class.short_name
end

private

def model
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_new.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1>Add <%= class_name(@metric).short_name %></h1>
<% @decorated_metric = "#{@metric.class.name}Presenter".constantize.new @metric %>
<h1>Add <%= @decorated_metric.short_name %></h1>

<%= render partial: 'shared/form' %>
5 changes: 5 additions & 0 deletions spec/models/active_record_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe GlucoseMeasurement, type: :model do
it 'knows what its presenter is' do
expect(described_class.presenter).to eq GlucoseMeasurementPresenter
end
end
1 change: 1 addition & 0 deletions spec/presenters/glucose_measurement_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
expect(decorated_bg.wrapped_model[:name]).to eq 'GlucoseMeasurement'
expect(decorated_bg.wrapped_model[:underscore]).to eq 'glucose_measurement'
expect(decorated_bg.wrapped_model[:url_friendly]).to eq 'glucose-measurement'
expect(decorated_bg.short_name).to eq 'Glucose'
end
end
end

0 comments on commit f68274a

Please sign in to comment.