Skip to content

Commit

Permalink
Fix Rails/TimeZone offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
mvz committed Oct 8, 2017
1 parent ff330be commit c3cba7b
Show file tree
Hide file tree
Showing 23 changed files with 48 additions and 38 deletions.
2 changes: 1 addition & 1 deletion publify_core/app/controllers/admin/content_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def autosave

if @article.save
flash[:success] = I18n.t('admin.content.autosave.success')
@must_update_calendar = (params[:article][:published_at] and params[:article][:published_at].to_time.to_i < Time.now.to_time.to_i and @article.parent_id.nil?)
@must_update_calendar = (params[:article][:published_at] and params[:article][:published_at].to_time.to_i < Time.zone.now.to_time.to_i and @article.parent_id.nil?)
respond_to do |format|
format.js
end
Expand Down
3 changes: 1 addition & 2 deletions publify_core/app/controllers/admin/dashboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ class Admin::DashboardController < Admin::BaseController
require 'rexml/document'

def index
t = Time.new
today = t.strftime('%Y-%m-%d 00:00')
today = Time.zone.now.strftime('%Y-%m-%d 00:00')

# Since last visit
last_sign_in = current_user.last_sign_in_at
Expand Down
2 changes: 1 addition & 1 deletion publify_core/app/controllers/admin/notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def create
note.state = 'published'
note.attributes = params[:note].permit!
note.text_filter ||= current_user.default_text_filter
note.published_at ||= Time.now
note.published_at ||= Time.zone.now
if note.save
if params[:push_to_twitter] && note.twitter_id.blank?
unless note.send_to_twitter
Expand Down
2 changes: 1 addition & 1 deletion publify_core/app/controllers/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def edit

def create
@page = Page.new(page_params)
@page.published_at = Time.now
@page.published_at = Time.zone.now
@page.blog = this_blog
@page.user_id = current_user.id

Expand Down
2 changes: 1 addition & 1 deletion publify_core/app/controllers/admin/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def upload_avatar
'text/plain'
end

Resource.create(upload: file, mime: mime, created_at: Time.now)
Resource.create(upload: file, mime: mime, created_at: Time.zone.now)
end

# Use callbacks to share common setup or constraints between actions.
Expand Down
2 changes: 1 addition & 1 deletion publify_core/app/helpers/xml_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def collection_lastmod(collection)
times.push article_published.updated_at if article_published

if times.empty?
Time.at(0).xmlschema
Time.zone.at(0).xmlschema
else
times.max.xmlschema
end
Expand Down
2 changes: 1 addition & 1 deletion publify_core/app/models/trigger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def post_action(due_at, item, method = 'came_due')
end

def fire
where('due_at <= ?', Time.now).destroy_all
where('due_at <= ?', Time.zone.now).destroy_all
true
end

Expand Down
10 changes: 5 additions & 5 deletions publify_core/app/services/title_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ def build(item, settings, parameters)

def substitute_time(s, settings)
# Other
s = s.gsub('%currentdate%', Time.now.strftime(settings.date_format))
s = s.gsub('%currenttime%', Time.now.strftime(settings.time_format))
s = s.gsub('%currentmonth%', Time.now.strftime('%B'))
s = s.gsub('%currentyear%', Time.now.year.to_s)
s = s.gsub('%currentdate%', Time.zone.now.strftime(settings.date_format))
s = s.gsub('%currenttime%', Time.zone.now.strftime(settings.time_format))
s = s.gsub('%currentmonth%', Time.zone.now.strftime('%B'))
s = s.gsub('%currentyear%', Time.zone.now.year.to_s)
s
end

Expand Down Expand Up @@ -70,7 +70,7 @@ def parse_date(string, params)
format << '%B ' if params[:month]
format << '%Y' if params[:year]

string.gsub('%date%', Time.mktime(*params.values_at(:year, :month, :day)).strftime(format))
string.gsub('%date%', Time.zone.local(*params.values_at(:year, :month, :day)).strftime(format))
end

def parse_page(_string, params)
Expand Down
4 changes: 2 additions & 2 deletions publify_core/app/views/admin/content/autosave.js.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
$('#autosave').html('<%= hidden_field_tag('article[id]', @article.id) %>');
$('#preview_link').replaceWith('<%= link_to('Preview', {:controller => '/articles', :action => 'preview', :id => @article.id}, {:target => 'new', :class => 'btn btn-default'}); %>');
<% if @article.state.to_s.downcase == "draft" %>
$("#article_published_at").val(<% Time.now() %>);
<% end %>
$("#article_published_at").val(<% Time.zone.now() %>);
<% end %>
4 changes: 2 additions & 2 deletions publify_core/app/views/admin/seo/_permalinks.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
<%= radio_button(:setting, :permalink_format, '/%year%/%month%/%day%/%title%') %>
<%= t(".date_and_title") %>
</label>
<small class='help-block'><%= "#{this_blog.base_url}/#{Time.now.strftime('%Y/%m/%d')}/sample-article" %></small>
<small class='help-block'><%= "#{this_blog.base_url}/#{Time.zone.now.strftime('%Y/%m/%d')}/sample-article" %></small>
<label for="setting_permalink_format_yearmonthtitle">
<%= radio_button(:setting, :permalink_format, '/%year%/%month%/%title%') %>
<%= t(".month_and_title") %>
</label>
<small class='help-block'><%= "#{this_blog.base_url}/#{Time.now.strftime('%Y/%m')}/sample-article" %></small>
<small class='help-block'><%= "#{this_blog.base_url}/#{Time.zone.now.strftime('%Y/%m')}/sample-article" %></small>
<label for="setting_permalink_format_title">
<%= radio_button(:setting, :permalink_format, '/%title%') %> <%= t(".title_only") %>
</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def base_article(options = {})

context 'writing for the future' do
let(:article_params) do
{ title: 'posted via tests!', body: 'a good boy', published_at: (Time.now + 1.hour).to_s }
{ title: 'posted via tests!', body: 'a good boy', published_at: 1.hour.from_now.to_s }
end

before do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def base_page(options = {})
it 'should create a page published in the future without a redirect' do
# TODO: published_at parameter is currently ignored
skip
post :create, params: { 'page' => base_page(published_at: (Time.now + 1.hour).to_s) }
post :create, params: { 'page' => base_page(published_at: 1.hour.from_now.to_s) }
expect(assigns(:page).redirect).to be_nil
end
end
Expand Down
2 changes: 1 addition & 1 deletion publify_core/spec/controllers/articles_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
describe ArticlesController, 'feeds', type: :controller do
let!(:blog) { create(:blog) }

let!(:article1) { create(:article, created_at: Time.now - 1.day) }
let!(:article1) { create(:article, created_at: 1.day.ago) }
let!(:article2) { create(:article, published_at: '2004-04-01 12:00:00') }

let(:trackback) { create(:trackback, article: article1, published_at: 1.day.ago) }
Expand Down
2 changes: 1 addition & 1 deletion publify_core/spec/controllers/authors_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe AuthorsController, type: :controller do
let!(:blog) { create(:blog, limit_article_display: 1) }
let(:now) { DateTime.new(2012, 12, 23, 3, 45) }
let(:now) { DateTime.new(2012, 12, 23, 3, 45).in_time_zone }

describe '#show' do
describe 'With an empty profile' do
Expand Down
15 changes: 14 additions & 1 deletion publify_core/spec/controllers/notes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,20 @@
end

context 'in reply' do
let(:reply) { { 'id_str' => '123456789', 'created_at' => DateTime.new(2014, 1, 23, 13, 47), 'user' => { 'screen_name' => 'a screen name', 'entities' => { 'url' => { 'urls' => [{ 'expanded_url' => 'an url' }] } } } } }
let(:reply) do
{
'id_str' => '123456789',
'created_at' => DateTime.new(2014, 1, 23, 13, 47).in_time_zone,
'user' => {
'screen_name' => 'a screen name',
'entities' => {
'url' => {
'urls' => [{ 'expanded_url' => 'an url' }]
}
}
}
}
end
let(:permalink) { "#{create(:note, in_reply_to_message: reply.to_json).id}-this-is-a-note" }

before(:each) { get :show, params: { permalink: permalink } }
Expand Down
2 changes: 1 addition & 1 deletion publify_core/spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
sequence(:guid) { |n| "deadbeef#{n}" }
sequence(:label) { |n| "lab_#{n}" }
sequence(:file_name) { |f| "file_name_#{f}" }
sequence(:time) { |n| DateTime.new(2012, 3, 26, 19, 56) - n }
sequence(:time) { |n| DateTime.new(2012, 3, 26, 19, 56).in_time_zone - n }

factory :user do
login { FactoryGirl.generate(:user) }
Expand Down
6 changes: 3 additions & 3 deletions publify_core/spec/models/article_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@
# TODO: Needs a fix for Rails ticket #5105: has_many: Dependent deleting does not work with STI
skip
art = blog.articles.create!(title: 'title', body: 'body',
published_at: Time.now + 1.hour)
published_at: 1.hour.from_now)
assert_equal 1, Trigger.count
art.destroy
assert_equal 0, Trigger.count
Expand Down Expand Up @@ -686,7 +686,7 @@

context 'with two articles with differents states and no params' do
let(:params) { nil }
let(:now) { DateTime.new(2011, 3, 12) }
let(:now) { DateTime.new(2011, 3, 12).in_time_zone }
let!(:article) { create(:article, state: 'published', created_at: now) }
let!(:last_draft_article) { create(:article, state: 'draft', created_at: now + 2.days) }
let!(:draft_article) { create(:article, state: 'draft', created_at: now + 20.days) }
Expand Down Expand Up @@ -762,7 +762,7 @@
end

describe 'published_since' do
let(:time) { DateTime.new(2010, 11, 3, 23, 34) }
let(:time) { DateTime.new(2010, 11, 3, 23, 34).in_time_zone }
it 'empty when no articles' do
expect(Article.published_since(time)).to be_empty
end
Expand Down
4 changes: 2 additions & 2 deletions publify_core/spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
describe Comment, type: :model do
let(:blog) { build_stubbed :blog }

let(:published_article) { build_stubbed(:article, published_at: Time.now - 1.hour, blog: blog) }
let(:published_article) { build_stubbed(:article, published_at: 1.hour.ago, blog: blog) }

def valid_comment(options = {})
Comment.new({ author: 'Bob', article: published_article, body: 'nice post', ip: '1.2.3.4' }.merge(options))
Expand Down Expand Up @@ -240,7 +240,7 @@ def valid_comment(options = {})
end

describe 'last_published', integration: true do
let(:date) { DateTime.new(2012, 12, 23, 12, 47) }
let(:date) { DateTime.new(2012, 12, 23, 12, 47).in_time_zone }
let!(:comment_1) { create(:comment, body: '1', created_at: date + 1.day) }
let!(:comment_4) { create(:comment, body: '4', created_at: date + 4.days) }
let!(:comment_2) { create(:comment, body: '2', created_at: date + 2.days) }
Expand Down
2 changes: 1 addition & 1 deletion publify_core/spec/models/content_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
context 'with an article published_at' do
let(:params) { { published_at: '2012-02' } }
let!(:article) { create(:article) }
let!(:match_article) { create(:article, published_at: DateTime.new(2012, 2, 13)) }
let!(:match_article) { create(:article, published_at: DateTime.new(2012, 2, 13).in_time_zone) }
it { expect(subject).to eq([match_article]) }
end

Expand Down
2 changes: 1 addition & 1 deletion publify_core/spec/models/feedback_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
end

context 'order by created at' do
let(:now) { DateTime.new(2009, 12, 6, 14, 56) }
let(:now) { DateTime.new(2009, 12, 6, 14, 56).in_time_zone }

describe '#paginated' do
let!(:a_comment) { create(:comment, created_at: now) }
Expand Down
2 changes: 1 addition & 1 deletion publify_core/spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def assert_rss20_feed(parsed_feed, count)
expect(parsed_feed.entries.count).to eq count
end

def stub_full_article(time = Time.now, blog: Blog.first)
def stub_full_article(time = Time.zone.now, blog: Blog.first)
author = FactoryGirl.build_stubbed(:user, name: 'User Name')
text_filter = FactoryGirl.build(:textile)

Expand Down
8 changes: 4 additions & 4 deletions publify_core/spec/views/comments/html_sanitization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
shared_examples_for 'CommentSanitization' do
before do
@blog = build_stubbed(:blog)
@article = build_stubbed(:article, created_at: Time.now, published_at: Time.now, blog: @blog)
@article = build_stubbed(:article, created_at: Time.zone.now, published_at: Time.zone.now, blog: @blog)
allow(Article).to receive(:find).and_return(@article)
@blog.plugin_avatar = ''
@blog.lang = 'en_US'

Comment.with_options(body: 'test foo <script>do_evil();</script>',
author: 'Bob', article: @article,
created_at: Time.now) do |klass|
created_at: Time.zone.now) do |klass|
@comment = klass.new(comment_options)
end

Expand Down Expand Up @@ -118,15 +118,15 @@ def comment_options
shared_examples_for 'CommentSanitizationWithDofollow' do
before do
@blog = create(:blog)
@article = create(:article, created_at: Time.now, published_at: Time.now, blog: @blog)
@article = create(:article, created_at: Time.zone.now, published_at: Time.zone.now, blog: @blog)
allow(Article).to receive(:find).and_return(@article)
@blog.plugin_avatar = ''
@blog.lang = 'en_US'
@blog.dofollowify = true

Comment.with_options(body: 'test foo <script>do_evil();</script>',
author: 'Bob', article: @article,
created_at: Time.now) do |klass|
created_at: Time.zone.now) do |klass|
@comment = klass.new(comment_options)
end

Expand Down
4 changes: 1 addition & 3 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
sequence(:guid) { |n| "deadbeef#{n}" }
sequence(:label) { |n| "lab_#{n}" }
sequence(:file_name) { |f| "file_name_#{f}" }
sequence(:time) { |n| DateTime.new(2012, 3, 26, 19, 56) - n }
sequence(:time) { |n| DateTime.new(2012, 3, 26, 19, 56).utc - n }

factory :user do
login { FactoryGirl.generate(:user) }
Expand Down Expand Up @@ -294,8 +294,6 @@
url 'http://www.example.com'
excerpt 'This is an excerpt'
guid 'dsafsadffsdsf'
created_at Time.now
updated_at Time.now
end

factory :sidebar do
Expand Down

0 comments on commit c3cba7b

Please sign in to comment.