Class: PinnedLink

Inherits:
ApplicationRecord show all
Includes:
MaybeCommunityRelated
Defined in:
/var/apps/qpixel/app/models/pinned_link.rb

Instance Method Summary collapse

Methods inherited from ApplicationRecord

#attributes_print, fuzzy_search, match_search, #match_search, sanitize_for_search, sanitize_name, sanitize_sql_in, useful_err_msg, with_lax_group_rules

Instance Method Details

#check_periodObject



62
63
64
65
66
67
68
# File '/var/apps/qpixel/app/models/pinned_link.rb', line 62

def check_period
  return unless shown_before.present? && shown_after.present?

  if shown_before < shown_after
    errors.add(:base, 'end date cannot be earlier than the start date')
  end
end

#check_post_or_urlObject



70
71
72
73
74
# File '/var/apps/qpixel/app/models/pinned_link.rb', line 70

def check_post_or_url
  unless post_id.present? || link.present?
    errors.add(:base, 'either a post or a URL must be set')
  end
end

#current?(now = DateTime.now) ⇒ Boolean

Is the link not timed or started in the past & hasn’t ended yet?

Parameters:

  • now (DateTime, nil) (defaults to: DateTime.now)

    timestamp to compare to

Returns:

  • (Boolean)

    check result



38
39
40
# File '/var/apps/qpixel/app/models/pinned_link.rb', line 38

def current?(now = DateTime.now)
  !timed? || !(future?(now) || past?(now))
end

#future?(now = DateTime.now) ⇒ Boolean

Does the link start in the future?

Parameters:

  • now (DateTime, nil) (defaults to: DateTime.now)

    timestamp to compare to

Returns:

  • (Boolean)

    check result



45
46
47
# File '/var/apps/qpixel/app/models/pinned_link.rb', line 45

def future?(now = DateTime.now)
  shown_after.present? && shown_after > now && (shown_before.nil? || shown_before >= now)
end

#past?(now = DateTime.now) ⇒ Boolean

Has the link ended in the past?

Parameters:

  • now (DateTime, nil) (defaults to: DateTime.now)

    timestamp to compare to

Returns:

  • (Boolean)

    check result



52
53
54
# File '/var/apps/qpixel/app/models/pinned_link.rb', line 52

def past?(now = DateTime.now)
  shown_before.present? && shown_before < now
end

#timed?Boolean

Is the link time-constrained?

Returns:

  • (Boolean)

    check result



58
59
60
# File '/var/apps/qpixel/app/models/pinned_link.rb', line 58

def timed?
  shown_before.present? || shown_after.present?
end