Skip to content

Commit

Permalink
Adds config TimeZone.
Browse files Browse the repository at this point in the history
  • Loading branch information
mukaer committed Sep 5, 2013
1 parent 1f9b24e commit 31f2738
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,5 +1,6 @@
config/database.yml
config/mail.yml
config/timezone.yml
config/secret.token
*.log
*~
Expand Down
13 changes: 2 additions & 11 deletions app/models/content.rb
Expand Up @@ -30,16 +30,8 @@ class Content < ActiveRecord::Base
}
scope :already_published, lambda { where('published = ? AND published_at < ?', true, Time.now).order(default_order) }

scope :published_at_like, lambda { |date_at| where(:published_at => (
if date_at =~ /\d{4}-\d{2}-\d{2}/
DateTime.strptime(date_at, '%Y-%m-%d').beginning_of_day..DateTime.strptime(date_at, '%Y-%m-%d').end_of_day
elsif date_at =~ /\d{4}-\d{2}/
DateTime.strptime(date_at, '%Y-%m').beginning_of_month..DateTime.strptime(date_at, '%Y-%m').end_of_month
elsif date_at =~ /\d{4}/
DateTime.strptime(date_at, '%Y').beginning_of_year..DateTime.strptime(date_at, '%Y').end_of_year
else
date_at
end)
scope :published_at_like, lambda { |date_at|
where(:published_at => (PublifyTime.delta_like(date_at))
)}

serialize :whiteboard
Expand Down Expand Up @@ -144,4 +136,3 @@ class ContentTextHelpers
include ActionView::Helpers::TextHelper
extend ActionView::Helpers::SanitizeHelper::ClassMethods
end

5 changes: 5 additions & 0 deletions config/application.rb
Expand Up @@ -71,6 +71,11 @@ class Application < Rails::Application
:long_weekday => '%a %B %e, %Y %H:%M'
)

# TimeZone
if File.file? "#{::Rails.root.to_s}/config/timezone.yml"
Time.zone = YAML.load(File.read("#{::Rails.root.to_s}/config/timezone.yml"))
end

ActionMailer::Base.default :charset => 'utf-8'

# Work around interpolation deprecation problem: %d is replaced by
Expand Down
1 change: 1 addition & 0 deletions config/timezone.yml.jst
@@ -0,0 +1 @@
'Tokyo'
30 changes: 25 additions & 5 deletions lib/publify_time.rb
@@ -1,11 +1,31 @@
class PublifyTime
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
year = year.to_i unless year.nil?
month = month.to_i unless month.nil?
day = day.to_i unless day.nil?
from = Time.zone.local(year, month, day)
to = from.end_of_year
to = from.end_of_month unless month.blank?
to = from.end_of_day unless day.blank?
from..to
end

def self.delta_like(str)
case str
when /(\d{4})-(\d{2})-(\d{2})/
delta(Regexp.last_match(1),Regexp.last_match(2),Regexp.last_match(3))

when /(\d{4})-(\d{2})/
delta(Regexp.last_match(1),Regexp.last_match(2))

when /(\d{4})/
delta(Regexp.last_match(1))

else
str
end
end


end
158 changes: 152 additions & 6 deletions spec/lib/publify_time_spec.rb
Expand Up @@ -7,21 +7,167 @@
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)
start_at = Time.zone.local(2009,1,1,0,0,0)
end_at = start_at.end_of_year
expect(PublifyTime.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)
start_at = Time.zone.local(2009,10,1,0,0,0)
end_at = start_at.end_of_month
expect(PublifyTime.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)
start_at = Time.zone.local(2009,10,23,0,0,0)
end_at = start_at.end_of_day
expect(PublifyTime.delta(2009, 10, 23)).to eq(start_at..end_at)
end

it "returns year, when year given type string" do
start_at = Time.zone.local(2009,1,1,0,0,0)
end_at = start_at.end_of_year
expect(PublifyTime.delta("2009")).to eq(start_at..end_at)
end

it "returns year and month when year and month given type string" do
start_at = Time.zone.local(2009,9,1,0,0,0)
end_at = start_at.end_of_month
expect(PublifyTime.delta("2009", "09")).to eq(start_at..end_at)
end

it "returns year, month and day when year, month and day given type string" do
start_at = Time.zone.local(2009,1,1,0,0,0)
end_at = start_at.end_of_day
expect(PublifyTime.delta("2009", "01", "01")).to eq(start_at..end_at)
end

end

describe 'delta_like' do
it 'given year' do
start_at = Time.zone.local(2013,1,1,0,0,0)
end_at = start_at.end_of_year
expect(PublifyTime.delta_like("2013")).to eq(start_at..end_at)
end

it 'given year month' do
start_at = Time.zone.local(2013,9,1,0,0,0)
end_at = start_at.end_of_month
expect(PublifyTime.delta_like("2013-09")).to eq(start_at..end_at)
end

it 'given year month day' do
start_at = Time.zone.local(2013,8,1,0,0,0)
end_at = start_at.end_of_day
expect(PublifyTime.delta_like("2013-08-01")).to eq(start_at..end_at)
end

end
end

describe 'find Article date range ' do
let!(:blog) { create(:blog) }

before do
@timezone = Time.zone
end

after do
Time.zone = @timezone
end


describe 'UTC' do
before do
Time.zone = 'UTC'

@a = FactoryGirl.build(:article)
@a.published_at = "1 Jan 2013 01:00 UTC"
@a.save!

params = @a.permalink_url.gsub("http://myblog.net/","").split("/")
@year,@month,@day = params[0],params[1],params[2]
end

it 'delta given year' do
range = PublifyTime.delta(@year)
expect(Article.find_all_by_published_at(range)).to eq([@a])
end

it 'delta given year month' do
range = PublifyTime.delta(@year,@month)
expect(Article.find_all_by_published_at(range)).to eq([@a])
end

it 'delta given year month day' do
range = PublifyTime.delta(@year,@month,@day)
expect(Article.find_all_by_published_at(range)).to eq([@a])
end


it 'delta_like given year' do
range = PublifyTime.delta_like("#{@year}")
expect(Article.find_all_by_published_at(range)).to eq([@a])
end


it 'delta_like given year month' do
range = PublifyTime.delta_like("#{@year}-#{@month}")
expect(Article.find_all_by_published_at(range)).to eq([@a])
end


it 'delta_like given year month day' do
range = PublifyTime.delta_like("#{@year}-#{@month}-#{@day}")
expect(Article.find_all_by_published_at(range)).to eq([@a])
end
end


describe 'JST(+0900) ' do
before do
Time.zone = 'Tokyo'

@a = FactoryGirl.build(:article)
@a.published_at = "1 Jan 2013 01:00 +0900"
@a.save!

params = @a.permalink_url.gsub("http://myblog.net/","").split("/")
@year,@month,@day = params[0],params[1],params[2]
end

it 'delta given year' do
range = PublifyTime.delta(@year)
expect(Article.find_all_by_published_at(range)).to eq([@a])
end

it 'delta given year month' do
range = PublifyTime.delta(@year,@month)
expect(Article.find_all_by_published_at(range)).to eq([@a])
end

it 'delta given year month day' do
range = PublifyTime.delta(@year,@month,@day)
expect(Article.find_all_by_published_at(range)).to eq([@a])
end


it 'delta_like given year' do
range = PublifyTime.delta_like("#{@year}")
expect(Article.find_all_by_published_at(range)).to eq([@a])
end


it 'delta_like given year month' do
range = PublifyTime.delta_like("#{@year}-#{@month}")
expect(Article.find_all_by_published_at(range)).to eq([@a])
end


it 'delta_like given year month day' do
range = PublifyTime.delta_like("#{@year}-#{@month}-#{@day}")
expect(Article.find_all_by_published_at(range)).to eq([@a])
end
end
end
67 changes: 67 additions & 0 deletions spec/models/article_spec.rb
Expand Up @@ -491,10 +491,16 @@ def assert_sets_trigger(art)

describe "an article published just before midnight UTC" do
before do
@timezone = Time.zone
Time.zone = 'UTC'
@a = FactoryGirl.build(:article)
@a.published_at = "21 Feb 2011 23:30 UTC"
end

after do
Time.zone = @timezone
end

describe "#permalink_url" do
it "uses UTC to determine correct day" do
@a.permalink_url.should == "http://myblog.net/2011/02/21/a-big-article"
Expand All @@ -512,10 +518,16 @@ def assert_sets_trigger(art)

describe "an article published just after midnight UTC" do
before do
@timezone = Time.zone
Time.zone = 'UTC'
@a = FactoryGirl.build(:article)
@a.published_at = "22 Feb 2011 00:30 UTC"
end

after do
Time.zone = @timezone
end

describe "#permalink_url" do
it "uses UTC to determine correct day" do
@a.permalink_url.should == "http://myblog.net/2011/02/22/a-big-article"
Expand All @@ -531,6 +543,61 @@ def assert_sets_trigger(art)
end
end

describe "an article published just before midnight JST (+0900)" do
before do
@time_zone = Time.zone
Time.zone = "Tokyo"
@a = FactoryGirl.build(:article)
@a.published_at = "31 Dec 2012 23:30 +0900"
end

after do
Time.zone = @time_zone
end

describe "#permalink_url" do
it "uses JST to determine correct day" do
@a.permalink_url.should == "http://myblog.net/2012/12/31/a-big-article"
end
end

describe "#find_by_permalink" do
it "uses JST to determine correct day" do
@a.save
a = Article.find_by_permalink :year => 2012, :month => 12, :day => 31, :permalink => 'a-big-article'
a.should == @a
end
end
end

describe "an article published just after midnight JST (+0900)" do
before do
@time_zone = Time.zone
Time.zone = "Tokyo"
@a = FactoryGirl.build(:article)
@a.published_at = "1 Jan 2013 00:30 +0900"
end

after do
Time.zone = @time_zone
end

describe "#permalink_url" do
it "uses JST to determine correct day" do
@a.permalink_url.should == "http://myblog.net/2013/01/01/a-big-article"
end
end

describe "#find_by_permalink" do
it "uses JST to determine correct day" do
@a.save
a = Article.find_by_permalink :year => 2013, :month => 1, :day => 1, :permalink => 'a-big-article'
a.should == @a
end
end
end


describe "#published_comments" do
it 'should not include withdrawn comments' do
a = Article.new(title: 'foo')
Expand Down
5 changes: 2 additions & 3 deletions spec/views/articles/read_spec.rb
Expand Up @@ -59,10 +59,9 @@
end

it "should show the comment creation times in the comment list" do
rendered.should =~ /#{@c1.created_at.to_s}/
rendered.should =~ /#{@c2.created_at.to_s}/
rendered.should =~ /#{Regexp.escape(@c1.created_at.to_s)}/
rendered.should =~ /#{Regexp.escape(@c2.created_at.to_s)}/
end
end
end
end

0 comments on commit 31f2738

Please sign in to comment.