diff --git a/.rubocop.yml b/.rubocop.yml index 1625ee1bb..8c2e682ca 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -624,7 +624,7 @@ Rails/Date: Description: >- Checks the correct usage of date aware methods, such as Date.today, Date.current etc. - Enabled: false + Enabled: true Rails/FindBy: Description: 'Prefer find_by over where.first.' diff --git a/Gemfile b/Gemfile index 4a611f513..187bb9ca9 100644 --- a/Gemfile +++ b/Gemfile @@ -148,5 +148,6 @@ group :test do gem "test-prof", "~> 0.7" gem "timecop", "~> 0.9" gem "webmock", "~> 3.4" + gem "zonebie", "~> 0.6.1" end # rubocop:enable LineLength diff --git a/Gemfile.lock b/Gemfile.lock index a3a4c2cae..b04ae6382 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -914,6 +914,7 @@ GEM xpath (3.1.0) nokogiri (~> 1.8) yajl-ruby (1.4.0) + zonebie (0.6.1) PLATFORMS ruby @@ -1047,6 +1048,7 @@ DEPENDENCIES webmock (~> 3.4) webpacker (~> 3.5) webpush (~> 0.3) + zonebie (~> 0.6.1) RUBY VERSION ruby 2.5.1p57 diff --git a/app/controllers/api/v0/users_controller.rb b/app/controllers/api/v0/users_controller.rb index d0ac961d7..6ab90806d 100644 --- a/app/controllers/api/v0/users_controller.rb +++ b/app/controllers/api/v0/users_controller.rb @@ -24,7 +24,7 @@ module Api end def less_than_one_day_old?(user) - range = (Time.now.beginning_of_day - 1.day)..(Time.now) + range = 1.day.ago.beginning_of_day..Time.current user_identity_age = user.github_created_at || user.twitter_created_at || 8.days.ago # last one is a fallback in case both are nil diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index f253514c2..6728259c8 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -45,7 +45,7 @@ class ArticlesController < ApplicationController @article = if @tag.present? && @user&.editor_version == "v2" authorize Article Article.new(body_markdown: "", cached_tag_list: @tag.name, - processed_html: "") + processed_html: "") elsif @tag&.submission_template.present? && @user authorize Article Article.new(body_markdown: @tag.submission_template_customized(@user.name), @@ -104,7 +104,7 @@ class ArticlesController < ApplicationController @article.tag_list = [] @article.main_image = nil edited_at_date = if @article.user == current_user && @article.published - Time.now + Time.current else @article.edited_at end diff --git a/app/controllers/email_subscriptions_controller.rb b/app/controllers/email_subscriptions_controller.rb index 9e9dca354..5378d5675 100644 --- a/app/controllers/email_subscriptions_controller.rb +++ b/app/controllers/email_subscriptions_controller.rb @@ -3,7 +3,7 @@ class EmailSubscriptionsController < ApplicationController def unsubscribe verified_params = Rails.application.message_verifier(:unsubscribe).verify(params[:ut]) - if verified_params[:expires_at] > Time.now + if verified_params[:expires_at] > Time.current user = User.find(verified_params[:user_id]) user.update(verified_params[:email_type] => false) @email_type = preferred_email_name(verified_params[:email_type]) diff --git a/app/controllers/giveaways_controller.rb b/app/controllers/giveaways_controller.rb index c772107f5..a82613694 100644 --- a/app/controllers/giveaways_controller.rb +++ b/app/controllers/giveaways_controller.rb @@ -40,11 +40,13 @@ class GiveawaysController < ApplicationController render :edit return end + + now = Time.current @user.onboarding_package_requested_again = true if @user.onboarding_package_requested @user.onboarding_package_requested = true - @user.onboarding_package_form_submmitted_at = Time.now.to_datetime - @user.personal_data_updated_at = Time.now.to_datetime - @user.shipping_validated_at = Time.now.to_datetime if user_params[:shipping_validated] == "1" + @user.onboarding_package_form_submmitted_at = now + @user.personal_data_updated_at = now + @user.shipping_validated_at = now if user_params[:shipping_validated] == "1" if @user.save! format.html { redirect_to "/freestickers/edit" } format.json { render :show, status: :ok, location: @user } diff --git a/app/controllers/internal/articles_controller.rb b/app/controllers/internal/articles_controller.rb index ede0c57ec..d226a3b52 100644 --- a/app/controllers/internal/articles_controller.rb +++ b/app/controllers/internal/articles_controller.rb @@ -52,7 +52,7 @@ class Internal::ArticlesController < Internal::ApplicationController where(published: true). or(Article.where(published_from_feed: true)). where(featured: true). - where("featured_number > ?", Time.now.to_i). + where("featured_number > ?", Time.current.to_i). includes(:user). includes(:buffer_updates). limited_columns_internal_select. diff --git a/app/controllers/internal/dogfood_controller.rb b/app/controllers/internal/dogfood_controller.rb index 08f67d8f5..349dac275 100644 --- a/app/controllers/internal/dogfood_controller.rb +++ b/app/controllers/internal/dogfood_controller.rb @@ -12,7 +12,7 @@ class Internal::DogfoodController < Internal::ApplicationController user_ids = @team_members.map(&:id) @comment_totals_this_week = Comment. - users_with_number_of_comments(user_ids, Date.today.beginning_of_week) + users_with_number_of_comments(user_ids, Time.zone.today.beginning_of_week) @comment_totals_24_hours = Comment. users_with_number_of_comments(user_ids, 24.hours.ago) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index cc601fef2..114b63d89 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -47,7 +47,7 @@ class MessagesController < ApplicationController username: new_message.user.username, profile_image_url: ProfileImage.new(new_message.user).get(90), message: new_message.message_html, - timestamp: Time.now, + timestamp: Time.current, color: new_message.preferred_user_color, reception_method: "pushed" }.to_json diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index a4660eccd..a28780723 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -4,7 +4,7 @@ class RegistrationsController < Devise::RegistrationsController def new if user_signed_in? - redirect_to "/dashboard?signed-in-already&t=#{Time.now.to_i}" + redirect_to "/dashboard?signed-in-already&t=#{Time.current.to_i}" return end super diff --git a/app/labor/bufferizer.rb b/app/labor/bufferizer.rb index 1c1d5d7b3..8ba7b1913 100644 --- a/app/labor/bufferizer.rb +++ b/app/labor/bufferizer.rb @@ -11,18 +11,18 @@ class Bufferizer BufferUpdate.buff!(article.id, twitter_buffer_text, tag.buffer_profile_id_code, "twitter", tag.id) end end - article.update(last_buffered: Time.now) + article.update(last_buffered: Time.current) end def main_teet! BufferUpdate.buff!(article.id, twitter_buffer_text, ApplicationConfig["BUFFER_TWITTER_ID"], "twitter", nil) - article.update(last_buffered: Time.now) + article.update(last_buffered: Time.current) end def facebook_post! BufferUpdate.buff!(article.id, fb_buffer_text, ApplicationConfig["BUFFER_FACEBOOK_ID"], "facebook") BufferUpdate.buff!(article.id, fb_buffer_text, ApplicationConfig["BUFFER_LINKEDIN_ID"], "linkedin") - article.update(facebook_last_buffered: Time.now) + article.update(facebook_last_buffered: Time.current) end private diff --git a/app/labor/cache_buster.rb b/app/labor/cache_buster.rb index 72b1b9b41..ef39d3df1 100644 --- a/app/labor/cache_buster.rb +++ b/app/labor/cache_buster.rb @@ -12,13 +12,13 @@ class CacheBuster end def bust_comment(commentable, username) - if commentable.featured_number.to_i > (Time.now.to_i - 5.hours.to_i) + if commentable.featured_number.to_i > 5.hours.ago.to_i bust("/") bust("/?i=i") bust("?i=i") end if commentable.decorate.cached_tag_list_array.include?("discuss") && - commentable.featured_number.to_i > (Time.now.to_i - 35.hours.to_i) + commentable.featured_number.to_i > 35.hours.ago.to_i bust("/") bust("/?i=i") bust("?i=i") @@ -58,7 +58,7 @@ class CacheBuster end def bust_home_pages(article) - if article.featured_number.to_i > Time.now.to_i + if article.featured_number.to_i > Time.current.to_i bust("/") bust("?i=i") end diff --git a/app/labor/email_logic.rb b/app/labor/email_logic.rb index 228714f9e..9a453c790 100644 --- a/app/labor/email_logic.rb +++ b/app/labor/email_logic.rb @@ -12,7 +12,7 @@ class EmailLogic end def analyze - @last_email_sent_at = get_last_digest_email_user_recieved + @last_email_sent_at = get_last_digest_email_user_received @open_percentage = get_open_rate @days_until_next_email = get_days_until_next_email @ready_to_receive_email = get_user_readiness @@ -75,11 +75,11 @@ class EmailLogic def get_user_readiness return true unless @last_email_sent_at - # Has it been atleast x days since @user receive an email? - (Time.now.utc - @last_email_sent_at) >= @days_until_next_email.days.to_i + # Has it been at least x days since @user received an email? + Time.current - @last_email_sent_at >= @days_until_next_email.days.to_i end - def get_last_digest_email_user_recieved + def get_last_digest_email_user_received @user.email_messages.where(mailer: "DigestMailer#digest_email").last&.sent_at end diff --git a/app/liquid_tags/github_tag/github_issue_tag.rb b/app/liquid_tags/github_tag/github_issue_tag.rb index 3ea1903ef..b9ed8e473 100644 --- a/app/liquid_tags/github_tag/github_issue_tag.rb +++ b/app/liquid_tags/github_tag/github_issue_tag.rb @@ -19,7 +19,7 @@ class GithubTag username = content_json[:user][:login] user_html_url = content_json[:user][:html_url] user_avatar_url = content_json[:user][:avatar_url] - date = Date.parse(content_json[:created_at].to_s).strftime("%b %d, %Y") + date = Time.zone.parse(content_json[:created_at].to_s).utc.strftime("%b %d, %Y") date_link = content_json[:html_url] title = generate_title html = "" \ diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 39b2645c4..fc8da1928 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -7,7 +7,7 @@ class ApplicationMailer < ActionMailer::Base Rails.application.message_verifier(:unsubscribe).generate( user_id: id, email_type: email_type.to_sym, - expires_at: Time.now + 31.days, + expires_at: 31.days.from_now, ) end end diff --git a/app/models/article.rb b/app/models/article.rb index 1033bcdf7..2002e8f03 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -28,7 +28,7 @@ class Article < ApplicationRecord url: { allow_blank: true, no_local: true, schemes: ["https", "http"] }, uniqueness: { allow_blank: true } # validates :description, length: { in: 10..170, if: :published? } - validates :body_markdown, uniqueness: { scope: [:user_id, :title] } + validates :body_markdown, uniqueness: { scope: %i[user_id title] } validate :validate_tag validate :validate_video validates :video_state, inclusion: { in: %w(PROGRESSING COMPLETED) }, allow_nil: true @@ -288,15 +288,13 @@ class Article < ApplicationRecord end def evaluate_markdown - begin - fixed_body_markdown = MarkdownFixer.fix_all(body_markdown || "") - parsed = FrontMatterParser::Parser.new(:md).call(fixed_body_markdown) - parsed_markdown = MarkdownParser.new(parsed.content) - self.processed_html = parsed_markdown.finalize - evaluate_front_matter(parsed.front_matter) - rescue StandardError => e - errors[:base] << ErrorMessageCleaner.new(e.message).clean - end + fixed_body_markdown = MarkdownFixer.fix_all(body_markdown || "") + parsed = FrontMatterParser::Parser.new(:md).call(fixed_body_markdown) + parsed_markdown = MarkdownParser.new(parsed.content) + self.processed_html = parsed_markdown.finalize + evaluate_front_matter(parsed.front_matter) + rescue StandardError => e + errors[:base] << ErrorMessageCleaner.new(e.message).clean end def has_frontmatter? @@ -331,7 +329,7 @@ class Article < ApplicationRecord def readable_publish_date relevant_date = crossposted_at.present? ? crossposted_at : published_at - if relevant_date && relevant_date.year == Time.now.year + if relevant_date && relevant_date.year == Time.current.year relevant_date&.strftime("%b %e") else relevant_date&.strftime("%b %e '%y") @@ -395,12 +393,11 @@ class Article < ApplicationRecord end def parsed_date(date) - today_date = Time.now.to_datetime - return published_at || today_date unless date - given_date = date.to_datetime + now = Time.current + return published_at || now unless date error_msg = "must be entered in DD/MM/YYYY format with current or past date" - return errors.add(:date_time, error_msg) if given_date > today_date - given_date + return errors.add(:date_time, error_msg) if date > now + date end def validate_tag @@ -448,18 +445,18 @@ class Article < ApplicationRecord def set_published_date if published && published_at.blank? - self.published_at = Time.now + self.published_at = Time.current user.delay.resave_articles # tack-on functionality HACK organization&.delay&.resave_articles # tack-on functionality HACK end end def set_featured_number - self.featured_number = Time.now.to_i if featured_number.blank? && published + self.featured_number = Time.current.to_i if featured_number.blank? && published end def set_crossposted_at - self.crossposted_at = Time.now if published && crossposted_at.blank? && published_from_feed + self.crossposted_at = Time.current if published && crossposted_at.blank? && published_from_feed end def set_last_comment_at diff --git a/app/models/badge_achievement.rb b/app/models/badge_achievement.rb index 4b70c99f8..73a502da9 100644 --- a/app/models/badge_achievement.rb +++ b/app/models/badge_achievement.rb @@ -42,7 +42,7 @@ class BadgeAchievement < ApplicationRecord end def activity_target - "badge_#{Time.now}" + "badge_#{Time.current}" end def remove_from_feed diff --git a/app/models/comment.rb b/app/models/comment.rb index c483637aa..36f9683dd 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -171,7 +171,7 @@ class Comment < ApplicationRecord end def activity_target - "comment_#{Time.now}" + "comment_#{Time.current}" end def remove_from_feed @@ -203,7 +203,7 @@ class Comment < ApplicationRecord end def readable_publish_date - if created_at.year == Time.now.year + if created_at.year == Time.current.year created_at.strftime("%b %e") else created_at.strftime("%b %e '%y") diff --git a/app/models/event.rb b/app/models/event.rb index 78cbda915..df1fc770e 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -12,12 +12,12 @@ class Event < ApplicationRecord before_validation :evaluate_markdown scope :in_the_future_and_published, -> { - where("starts_at > ?", Time.now). + where("starts_at > ?", Time.current). where(published: true) } scope :in_the_past_and_published, -> { - where("starts_at < ?", Time.now). + where("starts_at < ?", Time.current). where(published: true) } diff --git a/app/models/mention.rb b/app/models/mention.rb index 5808d0e04..fd5c5c889 100644 --- a/app/models/mention.rb +++ b/app/models/mention.rb @@ -59,7 +59,7 @@ class Mention < ApplicationRecord end def activity_target - "mention_#{Time.now}" + "mention_#{Time.current}" end def remove_from_feed diff --git a/app/models/notification.rb b/app/models/notification.rb index ecfa4abf0..65c35dc7e 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -64,7 +64,7 @@ class Notification < ApplicationRecord end def activity_target - "#{notifiable_type.downcase}_#{Time.now}" + "#{notifiable_type.downcase}_#{Time.current.utc}" end def activity_notify diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 8ab6ef396..642cfd1b7 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -95,7 +95,7 @@ class Tweet < ApplicationRecord tweet.extended_entities_serialized = tweeted.attrs[:extended_entities] tweet.full_fetched_object_serialized = tweeted.attrs tweet.tweeted_at = tweeted.attrs[:created_at] - tweet.last_fetched_at = Time.now + tweet.last_fetched_at = Time.current tweet.user_is_verified = tweeted.user.verified? tweet.is_quote_status = tweeted.attrs[:is_quote_status] tweet.save! diff --git a/app/models/user.rb b/app/models/user.rb index 4fbdf7d31..2e3c0f988 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -298,7 +298,7 @@ class User < ApplicationRecord end def scholar - valid_pass = workshop_expiration.nil? || workshop_expiration > Time.now + valid_pass = workshop_expiration.nil? || workshop_expiration > Time.current has_role?(:workshop_pass) && valid_pass end @@ -550,11 +550,11 @@ class User < ApplicationRecord def mentorship_status_update if mentor_description_changed? || offering_mentorship_changed? - self.mentor_form_updated_at = Time.now + self.mentor_form_updated_at = Time.current end if mentee_description_changed? || seeking_mentorship_changed? - self.mentee_form_updated_at = Time.now + self.mentee_form_updated_at = Time.current end end end diff --git a/app/services/authorization_service.rb b/app/services/authorization_service.rb index 9a9d99f4d..676e56b08 100644 --- a/app/services/authorization_service.rb +++ b/app/services/authorization_service.rb @@ -106,9 +106,9 @@ class AuthorizationService def account_less_than_a_week_old?(user, logged_in_identity) user_identity_age = user.github_created_at || user.twitter_created_at || - Time.parse(logged_in_identity.auth_data_dump.extra.raw_info.created_at) + Time.zone.parse(logged_in_identity.auth_data_dump.extra.raw_info.created_at) # last one is a fallback in case both are nil - range = (Time.now.beginning_of_day - 1.week)..(Time.now) + range = 1.week.ago.beginning_of_day..Time.current range.cover?(user_identity_age) end diff --git a/app/services/membership_service.rb b/app/services/membership_service.rb index f437eefa9..54c272087 100644 --- a/app/services/membership_service.rb +++ b/app/services/membership_service.rb @@ -13,7 +13,7 @@ class MembershipService true if create_subscription && assign_membership_role && user.update(monthly_dues: monthly_dues, - membership_started_at: Time.now, + membership_started_at: Time.current, email_membership_newsletter: true, stripe_id_code: customer.id) && send_welcome_email diff --git a/app/services/moderation_service.rb b/app/services/moderation_service.rb index c4a639303..41fb6e453 100644 --- a/app/services/moderation_service.rb +++ b/app/services/moderation_service.rb @@ -12,7 +12,7 @@ class ModerationService notifiable_type: object.class.name, action: "Moderation", ) - moderator.update_column(:last_moderation_notification, Time.now) + moderator.update_column(:last_moderation_notification, Time.current) end end handle_asynchronously :send_moderation_notification diff --git a/app/views/fields/user_scholar_field/_form.html.erb b/app/views/fields/user_scholar_field/_form.html.erb index b3bc6f6b1..1efce549a 100644 --- a/app/views/fields/user_scholar_field/_form.html.erb +++ b/app/views/fields/user_scholar_field/_form.html.erb @@ -5,7 +5,7 @@