Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polls v2 #96

Merged
merged 36 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
ed83e63
Adding more controls to the polls
spaquet Jul 24, 2023
0e375cf
Enable selectors
spaquet Jul 24, 2023
06dc7ec
Display is now aligned with the selectors values
spaquet Jul 24, 2023
335f08a
better on screen help
spaquet Jul 24, 2023
77bef8f
changed poll option minimum to 1
spaquet Jul 24, 2023
4a80eac
Updating validation to support num_votes and max_votes and duration
spaquet Jul 24, 2023
f5e1c7f
Adding selectors validation
spaquet Jul 24, 2023
d348372
Adjusting the backend and front-end logics
spaquet Jul 25, 2023
e05bda0
Adding poll participation
spaquet Jul 25, 2023
8796177
Refactoring the participation
spaquet Jul 25, 2023
cbf561f
Deleting entries in poll_participations when deleting a poll
spaquet Jul 25, 2023
0d60a68
Removing participants from the polls database and adding is_anonymous
spaquet Jul 25, 2023
6a6057a
Few gem updates
spaquet Jul 25, 2023
c016f97
Upgrading @honeybadger-io/js to version 6.3.0 & chart.js to 4.3.1
spaquet Jul 25, 2023
cbc78fd
anonymous feature added to the polls
spaquet Jul 25, 2023
6b2c78b
Updating the poll result UI
spaquet Jul 25, 2023
8757ea6
Adding some better code format
spaquet Jul 25, 2023
753e6c7
Upgrading appmap, rubocop and websocket-driver
spaquet Jul 25, 2023
cbc34ac
Adding Code Triage
spaquet Jul 25, 2023
a48a0c1
Better english ;-)
spaquet Jul 26, 2023
2f7330a
Upgrading minitest to 5.19.0 and net-image to 0.3.7
spaquet Jul 26, 2023
48c515b
Display poll indication only during the vote phase
spaquet Jul 26, 2023
69e1249
Removing the chart from the poll for now
spaquet Jul 26, 2023
89e837d
Upgrading to chart.js 4.3.2
spaquet Jul 26, 2023
2fbd8af
Order desc
spaquet Jul 26, 2023
482d027
Working
spaquet Jul 26, 2023
19d4e4d
Upgrading aws-partitions, reline, rexml
spaquet Jul 27, 2023
09b8c9d
Fixin an issue with cancel?
spaquet Jul 27, 2023
c1890cb
Changing the serialization
spaquet Jul 28, 2023
cd84c4b
Updatig comments
spaquet Jul 28, 2023
2f87764
Broadcasting live update for polls
spaquet Jul 28, 2023
8bca88d
Improving some comments
spaquet Jul 28, 2023
ace5cc6
Removing console.log
spaquet Jul 28, 2023
6683f4b
Working
spaquet Jul 28, 2023
c183149
Still working
spaquet Jul 28, 2023
94ac5a0
Still working
spaquet Jul 28, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ruby '3.2.2'

# IMPORTANT: This must be the first gem listed
# Add support to appmap in development and test
gem 'appmap', '0.102.0', :groups => [:development, :test]
gem 'appmap', '0.102.1', :groups => [:development, :test]


# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
Expand Down
34 changes: 17 additions & 17 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GIT
remote: https://github.com/faker-ruby/faker.git
revision: 56fc3c844d29dfa2e8175ba79168c498a8672b5a
revision: 0839df200586fefa2a522c63b67a48326edfdc20
specs:
faker (3.2.0)
i18n (>= 1.8.11, < 2)
Expand Down Expand Up @@ -90,7 +90,7 @@ GEM
annotate (3.2.0)
activerecord (>= 3.2, < 8.0)
rake (>= 10.4, < 14.0)
appmap (0.102.0)
appmap (0.102.1)
activesupport
method_source
rack
Expand All @@ -100,17 +100,17 @@ GEM
ffi-compiler (~> 1.0)
ast (2.4.2)
aws-eventstream (1.2.0)
aws-partitions (1.790.0)
aws-sdk-core (3.178.0)
aws-partitions (1.793.0)
aws-sdk-core (3.180.0)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.651.0)
aws-sigv4 (~> 1.5)
jmespath (~> 1, >= 1.6.1)
aws-sdk-kms (1.71.0)
aws-sdk-core (~> 3, >= 3.177.0)
aws-sigv4 (~> 1.1)
aws-sdk-s3 (1.131.0)
aws-sdk-core (~> 3, >= 3.177.0)
aws-sdk-s3 (1.132.0)
aws-sdk-core (~> 3, >= 3.179.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.6)
aws-sigv4 (1.6.0)
Expand Down Expand Up @@ -161,7 +161,7 @@ GEM
caxlsx_rails (0.6.3)
actionpack (>= 3.1)
caxlsx (>= 3.0)
chartkick (5.0.2)
chartkick (5.0.3)
chronic (0.10.2)
concurrent-ruby (1.2.2)
connection_pool (2.4.1)
Expand Down Expand Up @@ -247,11 +247,11 @@ GEM
method_source (1.0.0)
mini_magick (4.12.0)
mini_mime (1.1.2)
minitest (5.18.1)
minitest (5.19.0)
msgpack (1.7.2)
multi_xml (0.6.0)
multipart-post (2.3.0)
net-imap (0.3.6)
net-imap (0.3.7)
date
net-protocol
net-pop (0.1.2)
Expand Down Expand Up @@ -355,23 +355,23 @@ GEM
redis-client (0.14.1)
connection_pool
regexp_parser (2.8.1)
reline (0.3.6)
reline (0.3.7)
io-console (~> 0.5)
request_store (1.5.1)
rack (>= 1.4)
reverse_markdown (2.1.1)
nokogiri
rexml (3.2.5)
rexml (3.2.6)
rolify (6.0.1)
rubocop (1.54.2)
rubocop (1.55.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.2.2.3)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.28.0, < 2.0)
rubocop-ast (>= 1.28.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.29.0)
Expand Down Expand Up @@ -447,7 +447,7 @@ GEM
activemodel (>= 3.0.0)
public_suffix
version_gem (1.1.3)
view_component (3.4.0)
view_component (3.5.0)
activesupport (>= 5.2.0, < 8.0)
concurrent-ruby (~> 1.0)
method_source (~> 1.0)
Expand All @@ -461,7 +461,7 @@ GEM
rubyzip (>= 1.3.0)
selenium-webdriver (~> 4.0)
websocket (1.2.9)
websocket-driver (0.7.5)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
whenever (1.0.0)
Expand All @@ -470,7 +470,7 @@ GEM
nokogiri (~> 1.8)
xsv (1.2.1)
rubyzip (>= 1.3, < 3)
zeitwerk (2.6.8)
zeitwerk (2.6.9)

PLATFORMS
arm64-darwin-22
Expand All @@ -480,7 +480,7 @@ DEPENDENCIES
active_storage_validations (~> 1.0.0)
ahoy_matey
annotate (~> 3.2.0)
appmap (= 0.102.0)
appmap (= 0.102.1)
argon2 (~> 2.2.0)
aws-sdk-s3
bootsnap
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

[![Version](https://img.shields.io/github/v/release/the-pew-inc/the-pew?display_name=tag)]()

## SUPPORT US
[![Open Source Helpers](https://www.codetriage.com/the-pew-inc/the-pew/badges/users.svg)](https://www.codetriage.com/the-pew-inc/the-pew)

## DESCRIPTION

As a comprehensive audience analytics platform, THEPEW enables you to understand your employees, team members, prospects, and more. By analyzing audience responses and behavior over time, you can identify patterns and trends that provide insights into their needs, expectations, feature requests, and communication gaps.
Expand Down
30 changes: 30 additions & 0 deletions app/controllers/poll_options_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class PollOptionsController < ApplicationController
before_action :authenticate_user!

def create
# Retrieve the poll
poll = Poll.find(params[:poll_id])

# Check that users are allowed to add options
# If not, we redirect to the poll with an error message
if !poll.add_option
logger.warn "User #{current_user.id} tried to add an option to poll #{poll.id}"
redirect_to(poll_path(params[:poll_id]), alert: 'This poll does not allow users to add options.')
end

# Create a new PollOption
poll_option = PollOption.create(poll_id: params[:poll_id], title: params[:title], user_id: current_user.id)

if poll_option.save!
# The user who creates the new option is automatically casting a positive vote for it.
vote = Vote.find_or_create_by(user_id: current_user.id, votable_id: poll_option.id, votable_type: "PollOption")
vote.up_vote!

# Redirect the poll stats
redirect_to(poll_path(params[:poll_id]), notice: 'Your option will be added to the poll.')
else
redirect_to(poll_path(params[:poll_id]), alert: 'An error occured preveting your option from being added to the poll.')
end
end

end
28 changes: 28 additions & 0 deletions app/controllers/poll_participations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class PollParticipationsController < ApplicationController
before_action :authenticate_user!
before_action :set_poll

# POST /poll_participations
def create
respond_to do |format|
if !PollParticipation.participated?(current_user, @poll)
PollParticipation.create(user: current_user, poll: @poll)

# Broadcast to all the other connected users
Broadcasters::Votes::Updated.new(@poll).call

# Render for the current user
format.turbo_stream { render :success, locals: { add_option: @poll.add_option } }
else
format.turbo_stream { render :errors }
end
end
end

private

def set_poll
@poll = Poll.find(params[:poll_id])
@table_data = VoteCounterService.count_by_poll_option_and_choice(@poll)
end
end
25 changes: 7 additions & 18 deletions app/controllers/polls_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class PollsController < ApplicationController
before_action :redirect_if_unauthenticated

def index
@polls = current_user.organization.polls.includes(:user)
@polls = current_user.organization.polls.order(created_at: :desc).includes(:user)
end

def new
Expand Down Expand Up @@ -40,9 +40,8 @@ def edit

def show
@poll = Poll.find(params[:id])
@labels = []
@data = []
poll_stats

@table_data = VoteCounterService.count_by_poll_option_and_choice(@poll)
end

def destroy
Expand All @@ -58,20 +57,10 @@ def destroy
private

def poll_params
params.require(:poll).permit(:add_option, :poll_type, :status, :title,
:duration, :display, :description,
poll_options_attributes: [:id, :title, :_destroy])
end

def poll_stats
# stats = Vote.joins("INNER JOIN poll_options ON votes.votable_id = poll_options.id AND votes.votable_type = 'PollOption'").where(poll_options: { id: @poll.poll_option_ids }).group('poll_options.title').count
stats = Vote.joins("INNER JOIN poll_options ON votes.votable_id = poll_options.id AND votes.votable_type = 'PollOption'").where(poll_options: { id: @poll.poll_option_ids }).group('poll_options.title').sum(:choice)
@table_data = Vote.joins("JOIN poll_options ON votes.votable_id = poll_options.id AND votes.votable_type = 'PollOption'").where(poll_options: { id: @poll.poll_option_ids }).group('poll_options.title').group(:choice).count

stats.each do |item|
@labels << item[0]
@data << item[1]
end
params.require(:poll).permit(:add_option, :description,
:display, :duration, :is_anonymous, :max_votes, :num_votes,
:poll_type, :status, :title, selectors: [],
poll_options_attributes: [:id, :title, :is_answer, :_destroy])
end

end
1 change: 1 addition & 0 deletions app/controllers/votes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def show
@vote.voted(params[:choice])
@vote.broadcast_update_later_to(@vote.votable.room.id, target: "#{dom_id(@vote.votable)}_count", html: @vote.votable.up_votes)
when "PollOption"
Rails.logger.debug '### Poll Option'
@poll = Poll.find(params[:poll_id])
@vote.poll_voted(@poll, current_user, params[:choice])
else
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/poll_options_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PollOptionsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/poll_participations_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PollParticipationsHelper
end
13 changes: 13 additions & 0 deletions app/helpers/polls_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
module PollsHelper

def voting_guide_message(num_votes, max_votes)
if max_votes.present?
if num_votes.present? && num_votes < max_votes
"Please select at least #{num_votes} out of #{max_votes} options below"
else
"Please express your opinion for #{pluralize(max_votes, "option")}"
end
elsif num_votes.present?
"Please express your opinion for at least #{pluralize(num_votes, "option")}"
end
end

end
37 changes: 37 additions & 0 deletions app/javascript/controllers/add_poll_option_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Controller } from "@hotwired/stimulus";
import { Modal } from "flowbite";

// Connects to data-controller="add-poll-option"
export default class extends Controller {
static values = {
allowUserOption: { type: Boolean, default: false },
};

connect() {
if (this.allowUserOptionValue) {
this.observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === "childList") {
for (let node of mutation.removedNodes) {
if (node.id === "poll-submission") {
// "poll-submission" section removed then open the modal
const $targetEl = document.getElementById("optionModal");
if ($targetEl) {
const optionModal = new Modal($targetEl, null);
optionModal.show();
} else {
console.error("optionModal element not found");
}
}
}
}
}
});
this.observer.observe(document.body, { childList: true, subtree: true });
}
}

disconnect() {
this.observer.disconnect();
}
}
6 changes: 3 additions & 3 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import { application } from "./application"

import AddPollOptionController from "./add_poll_option_controller"
application.register("add-poll-option", AddPollOptionController)

import AlertController from "./alert_controller"
application.register("alert", AlertController)

Expand Down Expand Up @@ -37,9 +40,6 @@ application.register("mode", ModeController)
import OrderQuestionsController from "./order_questions_controller"
application.register("order-questions", OrderQuestionsController)

import PollChartController from "./poll_chart_controller"
application.register("poll-chart", PollChartController)

import PollController from "./poll_controller"
application.register("poll", PollController)

Expand Down
Loading