From 363fabd3e7f19651884628fce1362c3cb469455a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 12 Mar 2014 12:58:41 +0100 Subject: [PATCH] add tests for ListController.best_period_for --- app/controllers/list_controller.rb | 4 ++-- spec/controllers/list_controller_spec.rb | 27 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb index 8dd87b27b..83b27462b 100644 --- a/app/controllers/list_controller.rb +++ b/app/controllers/list_controller.rb @@ -302,7 +302,7 @@ class ListController < ApplicationController topic_query = TopicQuery.new(current_user, options) if current_user.present? - periods = [best_period_for(current_user.previous_visit_at)] + periods = [ListController.best_period_for(current_user.previous_visit_at)] else periods = TopTopic.periods end @@ -312,7 +312,7 @@ class ListController < ApplicationController top end - def best_period_for(date) + def self.best_period_for(date) date ||= 1.year.ago return :yearly if date < 180.days.ago return :monthly if date < 35.days.ago diff --git a/spec/controllers/list_controller_spec.rb b/spec/controllers/list_controller_spec.rb index 6adb4ce95..c4f681dd2 100644 --- a/spec/controllers/list_controller_spec.rb +++ b/spec/controllers/list_controller_spec.rb @@ -215,4 +215,31 @@ describe ListController do end end + describe "best_period_for" do + + it "returns yearly for more than 180 days" do + ListController.best_period_for(nil).should == :yearly + ListController.best_period_for(180.days.ago).should == :yearly + end + + it "returns monthly when less than 180 days and more than 35 days" do + (35...180).each do |date| + ListController.best_period_for(date.days.ago).should == :monthly + end + end + + it "returns weekly when less than 35 days and more than 8 days" do + (8...35).each do |date| + ListController.best_period_for(date.days.ago).should == :weekly + end + end + + it "returns daily when less than 8 days" do + (0...8).each do |date| + ListController.best_period_for(date.days.ago).should == :daily + end + end + + end + end