osem-fcy/app/models/survey.rb
Andrew Kvalheim b565be2f46 Correct assumption of survey start and end dates
The logic used for the `:reply` ability incompletely duplicated that of
`Survey#active?` and incorrectly assumed that surveys always have start
and end dates.

Resolves:

    Failures:

      1) Survey as an attendee respond to a survey during registration
         Failure/Error: survey.start_date > Time.current || survey.end_date < Time.current

         ActionView::Template::Error:
           undefined method `>' for nil:NilClass

                 survey.start_date > Time.current || survey.end_date < Time.current
                                   ^
         # ./app/models/ability.rb:126:in `block in signed_in'
         # ./app/views/surveys/show.html.haml:28:in `block in _app_views_surveys_show_html_haml___3404959267043700678_138180'
         # ./app/views/surveys/show.html.haml:19:in `_app_views_surveys_show_html_haml___3404959267043700678_138180'

    Failed examples:

    rspec ./spec/features/surveys_spec.rb:37 # Survey as an attendee respond to a survey during registration
2022-03-16 14:12:01 -07:00

39 lines
1.2 KiB
Ruby

# frozen_string_literal: true
class Survey < ActiveRecord::Base
belongs_to :surveyable, polymorphic: true
has_many :survey_questions, dependent: :destroy
has_many :survey_submissions, dependent: :destroy
enum target: [:after_conference, :during_registration, :after_event]
validates :title, presence: true
##
# Finds active surveys
# * if a survey has either start or end date, but not both
# check is performed only on the attribute that exists
# * if a survey does not have start/end dates, then it is marked active
# further check is expected, where appropriate, depending on the survey's target
# ====Returns
# * +true+ -> If the survey is active (will accept replies)
# * +false+ -> If the survey is closed
def active?
return true unless start_date || end_date
# Find timezone of conference (survyeable is Conference or Event)
timezone = surveyable.is_a?(Conference) ? surveyable.timezone : surveyable.conference.timezone
now = Time.current.in_time_zone(timezone)
if start_date && end_date
now >= start_date && now <= end_date
elsif start_date && !end_date
now >= start_date
elsif !start_date && end_date
now <= end_date
end
end
def closed?
!active?
end
end