Skip to content

Commit

Permalink
Prefer Time over DateTime (decidim#3962)
Browse files Browse the repository at this point in the history
* Prefer `Time` over `DateTime`

* Prefer `Time.current` over `Time.zone.now`

* Prefer `to_time` over `to_datetime`

This should've been detected by `rubocop` but the fix is not yet
released.
  • Loading branch information
deivid-rodriguez authored and mrcasals committed Aug 6, 2018
1 parent bb59771 commit 8587988
Show file tree
Hide file tree
Showing 47 changed files with 74 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ Style/PreferredHashMethods:
EnforcedStyle: verbose

Style/DateTime:
Enabled: false
Enabled: true

Style/Documentation:
Enabled: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ module Decidim::Admin
let(:delivering_user) { create :user, :admin, :confirmed, organization: organization }

let!(:deliverable_users) do
create_list(:user, 5, :confirmed, organization: organization, newsletter_notifications_at: Time.zone.now)
create_list(:user, 5, :confirmed, organization: organization, newsletter_notifications_at: Time.current)
end

let!(:not_deliverable_users) do
create_list(:user, 3, organization: organization, newsletter_notifications_at: nil)
end
let!(:unconfirmed_users) do
create_list(:user, 3, organization: organization, newsletter_notifications_at: Time.zone.now)
create_list(:user, 3, organization: organization, newsletter_notifications_at: Time.current)
end

let(:command) { described_class.new(newsletter, delivering_user) }
Expand Down
8 changes: 4 additions & 4 deletions decidim-admin/spec/jobs/newsletter_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ module Admin
let!(:newsletter) { create(:newsletter, organization: organization, total_deliveries: 0) }
let!(:organization) { create(:organization) }
let!(:another_organization) { create(:organization) }
let!(:deliverable_user) { create(:user, :confirmed, newsletter_notifications_at: Time.zone.now, organization: organization) }
let!(:another_deliverable_user) { create(:user, :confirmed, newsletter_notifications_at: Time.zone.now, organization: another_organization) }
let!(:undeliverable_user) { create(:user, newsletter_notifications_at: Time.zone.now, organization: organization) }
let!(:deliverable_user) { create(:user, :confirmed, newsletter_notifications_at: Time.current, organization: organization) }
let!(:another_deliverable_user) { create(:user, :confirmed, newsletter_notifications_at: Time.current, organization: another_organization) }
let!(:undeliverable_user) { create(:user, newsletter_notifications_at: Time.current, organization: organization) }
let!(:non_deliverable_user) { create(:user, :confirmed, newsletter_notifications_at: nil, organization: organization) }
let!(:deleted_user) { create(:user, :confirmed, :deleted, newsletter_notifications_at: Time.zone.now, organization: organization) }
let!(:deleted_user) { create(:user, :confirmed, :deleted, newsletter_notifications_at: Time.current, organization: organization) }

it "delivers a newsletter to a the eligible users" do
expect(NewsletterDeliveryJob).to receive(:perform_later).with(deliverable_user, newsletter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
describe "Admin manages newsletters", type: :system do
let(:organization) { create(:organization) }
let(:user) { create(:user, :admin, :confirmed, name: "Sarah Kerrigan", organization: organization) }
let!(:deliverable_users) { create_list(:user, 5, :confirmed, newsletter_notifications_at: Time.zone.now, organization: organization) }
let!(:deliverable_users) { create_list(:user, 5, :confirmed, newsletter_notifications_at: Time.current, organization: organization) }

before do
switch_to_host(organization.host)
Expand Down
2 changes: 1 addition & 1 deletion decidim-budgets/spec/commands/cancel_order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Decidim::Budgets
let(:order) do
order = create(:order, user: user, component: component)
order.projects << project
order.checked_out_at = Time.zone.now
order.checked_out_at = Time.current
order.save!
order
end
Expand Down
2 changes: 1 addition & 1 deletion decidim-budgets/spec/models/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module Decidim::Budgets

describe "#checked_out?" do
it "returns true if the checked_out_at attribute is present" do
subject.checked_out_at = Time.zone.now
subject.checked_out_at = Time.current
expect(subject).to be_checked_out
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def after_accept_path_for(resource)
# When a managed user accepts the invitation is promoted to non-managed user.
def accept_resource
resource = resource_class.accept_invitation!(update_resource_params)
resource.update!(newsletter_notifications_at: Time.zone.now) if update_resource_params[:newsletter_notifications]
resource.update!(newsletter_notifications_at: Time.current) if update_resource_params[:newsletter_notifications]
resource.update!(managed: false) if resource.managed?
resource.update!(accepted_tos_version: resource.organization.tos_version)
resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class NotificationsSettingsForm < Form

def newsletter_notifications_at
return nil unless newsletter_notifications
Time.zone.now
Time.current
end
end
end
2 changes: 1 addition & 1 deletion decidim-core/app/forms/decidim/registration_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def user_group?

def newsletter_at
return nil unless newsletter?
Time.zone.now
Time.current
end

private
Expand Down
4 changes: 2 additions & 2 deletions decidim-core/app/models/decidim/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def self.create_or_update_from(handler)
def grant!
remove_verification_attachment!

update!(granted_at: Time.zone.now, verification_metadata: {})
update!(granted_at: Time.current, verification_metadata: {})
end

def granted?
Expand All @@ -57,7 +57,7 @@ def expires_at
end

def expired?
expires_at.present? && expires_at < Time.zone.now
expires_at.present? && expires_at < Time.current
end

private
Expand Down
2 changes: 1 addition & 1 deletion decidim-core/app/models/decidim/messaging/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Message < ApplicationRecord
# @param recipients [Array<Decidim::User>]
#
def envelope_for(recipients)
receipts.build(recipient: sender, read_at: Time.zone.now)
receipts.build(recipient: sender, read_at: Time.current)

recipients.each { |recipient| receipts.build(recipient: recipient) }
end
Expand Down
2 changes: 1 addition & 1 deletion decidim-core/app/models/decidim/messaging/receipt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Receipt < ApplicationRecord

# rubocop:disable Rails/SkipsModelValidations
def self.mark_as_read(user)
recipient(user).update_all(read_at: Time.zone.now)
recipient(user).update_all(read_at: Time.current)
end
# rubocop:enable Rails/SkipsModelValidations
end
Expand Down
2 changes: 1 addition & 1 deletion decidim-core/app/types/decidim/core/date_time_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Core
name "DateTime"
description "An ISO8601 date with time"
coerce_input ->(value, _ctx) { Time.iso8601(value) }
coerce_result ->(value, _ctx) { value.to_datetime.iso8601 }
coerce_result ->(value, _ctx) { value.to_time.iso8601 }
end
end
end
6 changes: 3 additions & 3 deletions decidim-core/lib/decidim/core/test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
end

trait :officialized do
officialized_at { Time.zone.now }
officialized_at { Time.current }
officialized_as { Decidim::Faker::Localized.sentence(3) }
end
end
Expand Down Expand Up @@ -419,7 +419,7 @@
application { build(:oauth_application) }
token { SecureRandom.hex(32) }
expires_in { 1.month.from_now }
created_at { Time.zone.now }
created_at { Time.current }
scopes { "public" }
end

Expand All @@ -432,7 +432,7 @@
locale { I18n.locale }
scope { resource.scope }
content_a { Faker::Lorem.sentence }
datetime { DateTime.current }
datetime { Time.current }
end

factory :content_block, class: "Decidim::ContentBlock" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let(:factory_name) { described_class.name.demodulize.underscore.to_sym }

let!(:published) do
create(factory_name, published_at: Time.zone.now)
create(factory_name, published_at: Time.current)
end

let!(:unpublished) do
Expand Down
2 changes: 1 addition & 1 deletion decidim-core/lib/decidim/friendly_dates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module FriendlyDates

# Returns the creation date in a friendly relative format.
def friendly_created_at
current_datetime = Time.zone.now
current_datetime = Time.current

if created_at > current_datetime.beginning_of_day
I18n.l(created_at, format: :time_of_day)
Expand Down
2 changes: 1 addition & 1 deletion decidim-core/lib/devise/models/decidim_newsletterable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def set_newsletter_token!
# Fill Opt-in value with current time &
# removes any token involved
def set_newsletter_opt_in
self.newsletter_notifications_at = Time.zone.now
self.newsletter_notifications_at = Time.current
self.newsletter_token = ""
end

Expand Down
12 changes: 6 additions & 6 deletions decidim-core/spec/commands/decidim/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ module Decidim
let(:term) { "black" }

context "when searchables are from the future" do
let(:datetime1) { DateTime.current + 10.seconds }
let(:datetime2) { DateTime.current + 20.seconds }
let(:datetime1) { Time.current + 10.seconds }
let(:datetime2) { Time.current + 20.seconds }

it "returns matches sorted by date descendently" do
described_class.call(term, current_organization) do
Expand All @@ -135,8 +135,8 @@ module Decidim
end

context "when searchables are from the past" do
let(:datetime1) { DateTime.current - 1.day }
let(:datetime2) { DateTime.current - 2.days }
let(:datetime1) { Time.current - 1.day }
let(:datetime2) { Time.current - 2.days }

it "returns matches sorted by date descendently" do
described_class.call(term, current_organization) do
Expand All @@ -151,8 +151,8 @@ module Decidim
end

context "when searchables are from the future and the past" do
let(:datetime1) { DateTime.current + 1.day }
let(:datetime2) { DateTime.current - 1.day }
let(:datetime1) { Time.current + 1.day }
let(:datetime2) { Time.current - 1.day }

it "returns matches sorted by date descendently" do
described_class.call(term, current_organization) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Decidim
end

context "when valid" do
let(:user) { create(:user, organization: organization, newsletter_notifications_at: Time.zone.now) }
let(:user) { create(:user, organization: organization, newsletter_notifications_at: Time.current) }

it "unsubscribes user" do
user.newsletter_notifications_at = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Decidim
let(:data) do
{
email_on_notification: "1",
newsletter_notifications_at: Time.zone.now
newsletter_notifications_at: Time.current
}
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Decidim
describe "GET /search" do
context "when having resources with the term 'Great' in their content" do
let!(:results) do
now = DateTime.current
now = Time.current
[create(:searchable_resource, organization: organization, content_a: "Great proposal of mine", datetime: now + 1.second),
create(:searchable_resource, organization: organization, content_a: "The great-est place of the world", datetime: now)]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module Decidim
let(:sent_at_time) { Time.zone.at(decrypted_string.split("-").second.to_i) }

context "and newsletter notifications is true" do
let!(:user) { create(:user, organization: organization, id: user_id, newsletter_notifications_at: Time.zone.now) }
let!(:user) { create(:user, organization: organization, id: user_id, newsletter_notifications_at: Time.current) }

it "unsubscribe user" do
get :unsubscribe, params: { u: encryptor.sent_at_encrypted(user_id, time) }
Expand Down
8 changes: 4 additions & 4 deletions decidim-core/spec/lib/exporters/excel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def serialize

let(:collection) do
[
OpenStruct.new(id: 1, name: { ca: "foocat", es: "fooes" }, ids: [1, 2, 3], float: 1.66, date: DateTime.civil(2017, 10, 1, 5, 0)),
OpenStruct.new(id: 2, name: { ca: "barcat", es: "bares" }, ids: [2, 3, 4], float: 0.55, date: DateTime.civil(2017, 9, 20))
OpenStruct.new(id: 1, name: { ca: "foocat", es: "fooes" }, ids: [1, 2, 3], float: 1.66, date: Time.zone.local(2017, 10, 1, 5, 0)),
OpenStruct.new(id: 2, name: { ca: "barcat", es: "bares" }, ids: [2, 3, 4], float: 0.55, date: Time.zone.local(2017, 9, 20))
]
end

Expand All @@ -42,10 +42,10 @@ def serialize
headers = worksheet.rows[0]
expect(headers).to eq(["id", "serialized_name/ca", "serialized_name/es", "other_ids", "float", "date"])
expect(worksheet.rows[1][0..4]).to eq([1, "foocat", "fooes", "1, 2, 3", 1.66])
expect(worksheet.rows[1].datetime(5)).to eq(DateTime.civil(2017, 10, 1, 5, 0))
expect(worksheet.rows[1].datetime(5)).to eq(Time.zone.local(2017, 10, 1, 5, 0))

expect(worksheet.rows[2][0..4]).to eq([2, "barcat", "bares", "2, 3, 4", 0.55])
expect(worksheet.rows[2].datetime(5)).to eq(DateTime.civil(2017, 9, 20))
expect(worksheet.rows[2].datetime(5)).to eq(Time.zone.local(2017, 9, 20))
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions decidim-core/spec/lib/friendly_dates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,31 @@ def initialize(date)
end

describe "when in the same day" do
let(:date) { Time.zone.now.beginning_of_day + 1.minute }
let(:date) { Time.current.beginning_of_day + 1.minute }

it "returns hour only" do
expect(enhanced_instance.friendly_created_at).to eq("00:01")
end
end

describe "when in the same week" do
let(:date) { Time.zone.now.beginning_of_week + 1.minute }
let(:date) { Time.current.beginning_of_week + 1.minute }

it "returns the day of the week" do
expect(enhanced_instance.friendly_created_at).to eq("Mon")
end
end

describe "when in the same year" do
let(:date) { Time.zone.now.beginning_of_year + 1.minute }
let(:date) { Time.current.beginning_of_year + 1.minute }

it "returns the date in short format" do
expect(enhanced_instance.friendly_created_at).to eq("Jan 01")
end
end

describe "when in previous years" do
let(:date) { Time.zone.now - 1.year }
let(:date) { Time.current - 1.year }

it "returns the full date" do
expect(enhanced_instance.friendly_created_at).to eq("06.02.44")
Expand Down
2 changes: 1 addition & 1 deletion decidim-core/spec/lib/resourceable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ module Decidim
subject { resource }

context "when all hierarchy is visible" do
before { subject.update(published_at: DateTime.current) }
before { subject.update(published_at: Time.current) }

it { is_expected.to be_visible }
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Decidim
component: component,
title: "The resource title",
address: "The resource address.",
published_at: DateTime.current
published_at: Time.current
)
end
let(:component) { create(:component, manifest_name: "dummy") }
Expand Down
4 changes: 2 additions & 2 deletions decidim-core/spec/models/decidim/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ module Decidim

context "when deleted" do
before do
user.deleted_at = Time.zone.now
user.deleted_at = Time.current
end

it "is valid" do
Expand All @@ -102,7 +102,7 @@ module Decidim
expect do
create(:user, organization: user.organization,
nickname: user.nickname,
deleted_at: Time.zone.now)
deleted_at: Time.current)
end.not_to raise_error
end
end
Expand Down
2 changes: 1 addition & 1 deletion decidim-core/spec/types/date_time_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Decidim
module Core
describe DateTimeType, type: :graphql do
include_context "with a graphql scalar type"
let(:model) { DateTime.civil(2018, 2, 22, 9, 47, 0, "+1") }
let(:model) { Time.new(2018, 2, 22, 9, 47, 0, "+01:00") }

it "returns the formatted date" do
expect(response).to eq("2018-02-22T09:47:00+01:00")
Expand Down
2 changes: 1 addition & 1 deletion decidim-core/spec/types/date_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Decidim
module Core
describe DateType, type: :graphql do
include_context "with a graphql scalar type"
let(:model) { DateTime.civil(2018, 2, 22, 9, 47, 0, "+1") }
let(:model) { Time.new(2018, 2, 22, 9, 47, 0, "+01:00") }

it "returns the formatted date" do
expect(response).to eq("2018-02-22")
Expand Down
2 changes: 1 addition & 1 deletion decidim-core/spec/types/participatory_space_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Core

describe "components" do
let!(:published_components) do
create_list(:dummy_component, 2, participatory_space: model, published_at: Time.zone.now)
create_list(:dummy_component, 2, participatory_space: model, published_at: Time.current)
end

let!(:unpublished_components) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def attributes
answer_url: form.answer_url
}

attrs[:answered_at] = DateTime.current if form.answer.present?
attrs[:answered_at] = Time.current if form.answer.present?

if current_user.admin?
attrs[:signature_start_date] = form.signature_start_date
Expand Down
Loading

0 comments on commit 8587988

Please sign in to comment.