Immediately display poll results to poll author (#10187)
* Immediately display poll results to poll author * Refactor Poll#loaded_options and add Poll#voted? to improve DRYness
This commit is contained in:
		
							parent
							
								
									75cb93676b
								
							
						
					
					
						commit
						054bbb3da2
					
				@ -41,17 +41,17 @@ class Poll < ApplicationRecord
 | 
				
			|||||||
  after_commit :reset_parent_cache, on: :update
 | 
					  after_commit :reset_parent_cache, on: :update
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def loaded_options
 | 
					  def loaded_options
 | 
				
			||||||
    options.map.with_index { |title, key| Option.new(self, key.to_s, title, cached_tallies[key]) }
 | 
					    options.map.with_index { |title, key| Option.new(self, key.to_s, title, show_totals_now? ? cached_tallies[key] : nil) }
 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  def unloaded_options
 | 
					 | 
				
			||||||
    options.map.with_index { |title, key| Option.new(self, key.to_s, title, nil) }
 | 
					 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def possibly_stale?
 | 
					  def possibly_stale?
 | 
				
			||||||
    remote? && last_fetched_before_expiration? && time_passed_since_last_fetch?
 | 
					    remote? && last_fetched_before_expiration? && time_passed_since_last_fetch?
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def voted?(account)
 | 
				
			||||||
 | 
					    account.id == account_id || votes.where(account: account).exists?
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  delegate :local?, to: :account
 | 
					  delegate :local?, to: :account
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def remote?
 | 
					  def remote?
 | 
				
			||||||
@ -95,4 +95,8 @@ class Poll < ApplicationRecord
 | 
				
			|||||||
  def time_passed_since_last_fetch?
 | 
					  def time_passed_since_last_fetch?
 | 
				
			||||||
    last_fetched_at.nil? || last_fetched_at < 1.minute.ago
 | 
					    last_fetched_at.nil? || last_fetched_at < 1.minute.ago
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def show_totals_now?
 | 
				
			||||||
 | 
					    expired? || !hide_totals?
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
				
			|||||||
@ -122,11 +122,7 @@ class ActivityPub::NoteSerializer < ActiveModel::Serializer
 | 
				
			|||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def poll_options
 | 
					  def poll_options
 | 
				
			||||||
    if !object.poll.expired? && object.poll.hide_totals?
 | 
					    object.poll.loaded_options
 | 
				
			||||||
      object.poll.unloaded_options
 | 
					 | 
				
			||||||
    else
 | 
					 | 
				
			||||||
      object.poll.loaded_options
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def poll_and_multiple?
 | 
					  def poll_and_multiple?
 | 
				
			||||||
 | 
				
			|||||||
@ -4,7 +4,7 @@ class REST::PollSerializer < ActiveModel::Serializer
 | 
				
			|||||||
  attributes :id, :expires_at, :expired,
 | 
					  attributes :id, :expires_at, :expired,
 | 
				
			||||||
             :multiple, :votes_count
 | 
					             :multiple, :votes_count
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  has_many :dynamic_options, key: :options
 | 
					  has_many :loaded_options, key: :options
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  attribute :voted, if: :current_user?
 | 
					  attribute :voted, if: :current_user?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -12,20 +12,12 @@ class REST::PollSerializer < ActiveModel::Serializer
 | 
				
			|||||||
    object.id.to_s
 | 
					    object.id.to_s
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def dynamic_options
 | 
					 | 
				
			||||||
    if !object.expired? && object.hide_totals?
 | 
					 | 
				
			||||||
      object.unloaded_options
 | 
					 | 
				
			||||||
    else
 | 
					 | 
				
			||||||
      object.loaded_options
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  def expired
 | 
					  def expired
 | 
				
			||||||
    object.expired?
 | 
					    object.expired?
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def voted
 | 
					  def voted
 | 
				
			||||||
    object.votes.where(account: current_user.account).exists?
 | 
					    object.voted?(current_user.account)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def current_user?
 | 
					  def current_user?
 | 
				
			||||||
 | 
				
			|||||||
@ -1,10 +1,8 @@
 | 
				
			|||||||
- options      = (!poll.expired? && poll.hide_totals?) ? poll.unloaded_options : poll.loaded_options
 | 
					- show_results = (user_signed_in? && poll.voted?(current_account)) || poll.expired?
 | 
				
			||||||
- voted        = user_signed_in? && poll.votes.where(account: current_account).exists?
 | 
					 | 
				
			||||||
- show_results = voted || poll.expired?
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
.poll
 | 
					.poll
 | 
				
			||||||
  %ul
 | 
					  %ul
 | 
				
			||||||
    - options.each do |option|
 | 
					    - poll.loaded_options.each do |option|
 | 
				
			||||||
      %li
 | 
					      %li
 | 
				
			||||||
        - if show_results
 | 
					        - if show_results
 | 
				
			||||||
          - percent = poll.votes_count > 0 ? 100 * option.votes_count / poll.votes_count : 0
 | 
					          - percent = poll.votes_count > 0 ? 100 * option.votes_count / poll.votes_count : 0
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user