diff --git a/app/controllers/generic_controller.rb b/app/controllers/generic_controller.rb index cece9d4..a8aef33 100644 --- a/app/controllers/generic_controller.rb +++ b/app/controllers/generic_controller.rb @@ -1,4 +1,6 @@ class GenericController < ApplicationController + include ControllerHelpers + before_action :require_login def index @@ -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 diff --git a/app/controllers/longterm_controller.rb b/app/controllers/longterm_controller.rb index e46155e..86ed546 100644 --- a/app/controllers/longterm_controller.rb +++ b/app/controllers/longterm_controller.rb @@ -1,4 +1,6 @@ class LongtermController < ApplicationController + include ControllerHelpers + before_action :require_login def index @@ -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, diff --git a/config/application.rb b/config/application.rb index b76843d..884a564 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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 diff --git a/lib/controller_helpers.rb b/lib/controller_helpers.rb new file mode 100644 index 0000000..a775650 --- /dev/null +++ b/lib/controller_helpers.rb @@ -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