Permalink
Browse files

Fix Rails/TimeZone offenses

  • Loading branch information...
mvz committed Sep 23, 2017
1 parent ff330be commit c3cba7b55736f7a689c38d867ae6ea940ecff4ff
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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.
@@ -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
@@ -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
@@ -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
@@ -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)
@@ -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 %>
@@ -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>
@@ -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
@@ -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
@@ -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) }
@@ -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
@@ -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 } }
@@ -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) }
@@ -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
@@ -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) }
@@ -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
@@ -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))
@@ -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) }
@@ -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
@@ -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) }
@@ -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)
@@ -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
@@ -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
View
@@ -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) }
@@ -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

0 comments on commit c3cba7b

Please sign in to comment.