Class: Effective::Poll
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Effective::Poll
- Defined in:
- app/models/effective/poll.rb
Constant Summary collapse
- AUDIENCES =
['All Users', 'Individual Users', 'Selected Users']
Instance Method Summary collapse
- #audience_class ⇒ Object
- #audience_scope ⇒ Object
- #available? ⇒ Boolean
- #available_date ⇒ Object
- #available_for?(user) ⇒ Boolean
- #ended? ⇒ Boolean
-
#poll_results(poll_question: nil) ⇒ Object
Returns all completed_ballot_responses.
- #started? ⇒ Boolean
- #to_s ⇒ Object
- #users(except_completed: false) ⇒ Object
Instance Method Details
#audience_class ⇒ Object
90 91 92 93 94 |
# File 'app/models/effective/poll.rb', line 90 def audience_class klass = audience_class_name.safe_constantize raise('expected an effective_polls_user klass') unless klass.try(:effective_polls_user?) klass end |
#audience_scope ⇒ Object
149 150 151 |
# File 'app/models/effective/poll.rb', line 149 def audience_scope Array(self[:audience_scope]) - [nil, ''] end |
#available? ⇒ Boolean
127 128 129 |
# File 'app/models/effective/poll.rb', line 127 def available? started? && !ended? end |
#available_date ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'app/models/effective/poll.rb', line 139 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
85 86 87 88 |
# File 'app/models/effective/poll.rb', line 85 def available_for?(user) raise('expected an effective_polls_user') unless user.class.try(:effective_polls_user?) available? && users.include?(user) end |
#ended? ⇒ Boolean
135 136 137 |
# File 'app/models/effective/poll.rb', line 135 def ended? end_at.present? && end_at < Time.zone.now end |
#poll_results(poll_question: nil) ⇒ Object
Returns all completed_ballot_responses
154 155 156 157 |
# File 'app/models/effective/poll.rb', line 154 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
131 132 133 |
# File 'app/models/effective/poll.rb', line 131 def started? start_at_was.present? && Time.zone.now >= start_at_was end |
#to_s ⇒ Object
81 82 83 |
# File 'app/models/effective/poll.rb', line 81 def to_s title.presence || model_name.human end |
#users(except_completed: false) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/models/effective/poll.rb', line 96 def users(except_completed: false) klass = audience_class() resource = klass.new users = case audience when 'All Users' klass.try(:unarchived) || klass.all when 'Individual Users' (klass.try(:unarchived) || klass.all).where(id: audience_scope) when 'Selected Users' collection = klass.none audience_scope.each do |scope| relation = resource.poll_audience_scope(scope) raise("invalid poll_audience_scope for #{scope}") unless relation.kind_of?(ActiveRecord::Relation) collection = collection.or(relation) end collection else raise('unexpected audience') end if except_completed users = users.where.not(id: completed_ballots.select('user_id as id')) end users end |