Class: Effective::Poll

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/effective/poll.rb

Constant Summary collapse

AUDIENCES =
['All Users', 'Individual Users', 'Selected Users']

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#skip_started_validationObject

Returns the value of attribute skip_started_validation.



3
4
5
# File 'app/models/effective/poll.rb', line 3

def skip_started_validation
  @skip_started_validation
end

Instance Method Details

#audience_scopeObject



138
139
140
# File 'app/models/effective/poll.rb', line 138

def audience_scope
  Array(super) - [nil, '']
end

#available?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'app/models/effective/poll.rb', line 116

def available?
  started? && !ended?
end

#available_dateObject



128
129
130
131
132
133
134
135
136
# File 'app/models/effective/poll.rb', line 128

def available_date
  if start_at && end_at && start_at.to_date == end_at.to_date
    "#{start_at.strftime('%F at %H:%M')} to #{end_at.strftime('%H:%M')}"
  elsif start_at && end_at
    "#{start_at.strftime('%F at %H:%M')} to #{end_at.strftime('%F %H:%M')}"
  elsif start_at
    "#{start_at.strftime('%F at %H:%M')}"
  end
end

#available_for?(user) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
# File 'app/models/effective/poll.rb', line 88

def available_for?(user)
  raise('expected a user') unless user.kind_of?(User)
  available? && users.include?(user)
end

#emails(exclude_completed: false) ⇒ Object



108
109
110
111
112
113
114
# File 'app/models/effective/poll.rb', line 108

def emails(exclude_completed: false)
  if exclude_completed
    users.where.not(id: completed_ballots.select('user_id as id')).pluck(:email)
  else
    users.pluck(:email)
  end
end

#ended?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'app/models/effective/poll.rb', line 124

def ended?
  end_at.present? && end_at < Time.zone.now
end

#poll_results(poll_question: nil) ⇒ Object

Returns all completed_ballot_responses



143
144
145
146
# File 'app/models/effective/poll.rb', line 143

def poll_results(poll_question: nil)
  return completed_ballot_responses if poll_question.nil?
  completed_ballot_responses.select { |br| br.poll_question_id == poll_question.id }
end

#started?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'app/models/effective/poll.rb', line 120

def started?
  start_at_was.present? && Time.zone.now >= start_at_was
end

#to_sObject



84
85
86
# File 'app/models/effective/poll.rb', line 84

def to_s
  title.presence || 'New Poll'
end

#usersObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/effective/poll.rb', line 93

def users
  case audience
  when 'All Users'
    User.all
  when 'Individual Users'
    User.where(id: audience_scope)
  when 'Selected Users'
    collection = User.none
    audience_scope.each { |scope| collection = collection.or(User.send(scope)) }
    collection
  else
    raise('unexpected audience')
  end
end