Skip to content

Commit

Permalink
Factor these bits out
Browse files Browse the repository at this point in the history
  • Loading branch information
pikesley committed Feb 28, 2016
1 parent 107c2f3 commit 0ed9bb7
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
18 changes: 6 additions & 12 deletions app/controllers/generic_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class GenericController < ApplicationController
include ControllerHelpers

before_action :require_login

def index
Expand All @@ -10,23 +12,15 @@ def index
@model = find_class
@metrics = @model.where(datetime: (Time.now - @hours.hours)..Time.now)

if @metrics == []
if @metrics.count < 3
@metrics = @model.first(3)
end

@bucketed_metrics = @metrics.group_by { |g| g.datetime.strftime "%Y-%m-%d" }
@bucketed_metrics = bucket @metrics

@with_year = begin
@metrics.first.datetime.year < Time.now.year
rescue NoMethodError
nil
end
@with_year = with_year @metrics

@no_picker = begin
((Time.now - @metrics.first.datetime) / 3600) > 168
rescue NoMethodError
true
end
@no_picker = no_picker @metrics

@has_charts = false
end
Expand Down
10 changes: 4 additions & 6 deletions app/controllers/longterm_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class LongtermController < ApplicationController
include ControllerHelpers

before_action :require_login

def index
Expand All @@ -9,12 +11,8 @@ def index
BloodPressure
].each do |model|
metrics = model.all
bucketed_metrics = metrics.group_by { |g| g.datetime.strftime "%Y-%m-%d" }
with_year = begin
metrics.first.datetime.year < Time.now.year
rescue NoMethodError
nil
end
bucketed_metrics = bucket metrics
with_year = with_year metrics

@sets.push({
model: model,
Expand Down
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ class Application < Rails::Application
config.after_initialize do |app|
app.config.paths.add 'app/presenters', :eager_load => true
end

config.autoload_paths << Rails.root.join('lib')
end
end
21 changes: 21 additions & 0 deletions lib/controller_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module ControllerHelpers
def with_year metrics
begin
metrics.first.datetime.year < Time.now.year
rescue NoMethodError
nil
end
end

def no_picker metrics
begin
((Time.now - metrics.first.datetime) / 3600) > 168
rescue NoMethodError
true
end
end

def bucket metrics
metrics.group_by { |g| g.datetime.strftime "%Y-%m-%d" }
end
end

0 comments on commit 0ed9bb7

Please sign in to comment.