Browse files

Extract delta time calcul from article to a TypoTime object

  • Loading branch information...
1 parent d2f173f commit 82fa692cc2fd822798d277de72008a0b7ef33a61 Yannick Francois committed Feb 23, 2013
Showing with 41 additions and 13 deletions.
  1. +2 −13 app/models/article.rb
  2. +1 −0 config/application.rb
  3. +11 −0 lib/typo_time.rb
  4. +27 −0 spec/lib/typo_time_spec.rb
View
15 app/models/article.rb
@@ -53,7 +53,7 @@ def spam
scope :drafts, lambda { where(state: 'draft').order('created_at DESC') }
scope :child_of, lambda { |article_id| where(parent_id: article_id) }
scope :published, lambda { where(published: true, published_at: Time.at(0)..Time.now).order('published_at DESC') }
- scope :published_at, lambda {|time_params| published.where(published_at: Article.time_delta(*time_params)).order('published_at DESC')}
+ scope :published_at, lambda {|time_params| published.where(published_at: TypoTime.delta(*time_params)).order('published_at DESC')}
scope :published_since, lambda {|time| published.where('published_at > ?', time).order('published_at DESC') }
scope :withdrawn, lambda { where(state: 'withdrawn').order('published_at DESC') }
scope :pending, lambda { where('state = ? and published_at > ?', 'publication_pending', Time.now).order('published_at DESC') }
@@ -196,7 +196,7 @@ def self.find_by_published_at
# Finds one article which was posted on a certain date and matches the supplied dashed-title
# params is a Hash
def self.find_by_permalink(params)
- date_range = self.time_delta(params[:year], params[:month], params[:day])
+ date_range = TypoTime.delta(params[:year], params[:month], params[:day])
req_params = {}
req_params[:permalink] = params[:title] if params[:title]
@@ -360,17 +360,6 @@ def ensure_settings_type
end
end
- def self.time_delta(year = nil, month = nil, day = nil)
- return nil if year.nil? && month.nil? && day.nil?
- from = Time.utc(year, month || 1, day || 1)
-
- to = from.next_year
- to = from.next_month unless month.blank?
- to = from + 1.day unless day.blank?
- to = to - 1 # pull off 1 second so we don't overlap onto the next day
- from..to
- end
-
private
def permalink_url_options
View
1 config/application.rb
@@ -53,6 +53,7 @@ class Application < Rails::Application
# require 'spam_protection'
require 'stateful'
require 'transforms'
+ require 'typo_time'
require 'typo_guid'
## Required by the plugins themselves.
# require 'typo_plugins'
View
11 lib/typo_time.rb
@@ -0,0 +1,11 @@
+class TypoTime
+ def self.delta(year = nil, month = nil, day = nil)
+ return nil if year.nil? && month.nil? && day.nil?
+ from = Time.utc(year, month || 1, day || 1)
+ to = from.next_year
+ to = from.next_month unless month.blank?
+ to = from + 1.day unless day.blank?
+ to = to - 1 # pull off 1 second so we don't overlap onto the next day
+ from..to
+ end
+end
View
27 spec/lib/typo_time_spec.rb
@@ -0,0 +1,27 @@
+require 'spec_helper'
+
+describe TypoTime do
+ describe "delta" do
+ it "returns nil when nil year, nil month and nil day" do
+ expect(TypoTime.delta).to be_nil
+ end
+
+ it "returns year when year given" do
+ start_at = Time.utc(2009,1,1,0,0,0)
+ end_at = Time.utc(2009,12,31,23,59,59)
+ expect(TypoTime.delta(2009)).to eq(start_at..end_at)
+ end
+
+ it "returns year and month when year and month given" do
+ start_at = Time.utc(2009,10,1,0,0,0)
+ end_at = Time.utc(2009,10,31,23,59,59)
+ expect(TypoTime.delta(2009, 10)).to eq(start_at..end_at)
+ end
+
+ it "returns year, month and day when year, month and day given" do
+ start_at = Time.utc(2009,10,23,0,0,0)
+ end_at = Time.utc(2009,10,23,23,59,59)
+ expect(TypoTime.delta(2009, 10, 23)).to eq(start_at..end_at)
+ end
+ end
+end

0 comments on commit 82fa692

Please sign in to comment.