Skip to content

Commit

Permalink
Merge pull request #1961 from wairbut-m2c/mlucena-poll-comments
Browse files Browse the repository at this point in the history
Poll comments
  • Loading branch information
voodoorai2000 committed Oct 10, 2017
2 parents bd62af9 + ffdbdac commit 0e3868c
Show file tree
Hide file tree
Showing 14 changed files with 596 additions and 5 deletions.
6 changes: 5 additions & 1 deletion app/controllers/polls_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class PollsController < ApplicationController

include PollsHelper

load_and_authorize_resource

has_filters %w{current expired incoming}
has_orders %w{most_voted newest oldest}, only: :show

::Poll::Answer # trigger autoload

Expand All @@ -15,11 +15,15 @@ def index
def show
@questions = @poll.questions.for_render.sort_for_list
@token = poll_voter_token(@poll, current_user)

@answers_by_question_id = {}
poll_answers = ::Poll::Answer.by_question(@poll.question_ids).by_author(current_user.try(:id))
poll_answers.each do |answer|
@answers_by_question_id[answer.question_id] = answer.answer
end

@commentable = @poll
@comment_tree = CommentTree.new(@commentable, params[:page], @current_order)
end

end
2 changes: 1 addition & 1 deletion app/helpers/flags_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def show_unflag_action?(flaggable)

def flagged?(flaggable)
if flaggable.is_a? Comment
@comment_flags[flaggable.id]
@comment_flags[flaggable.id] unless flaggable.commentable_type == "Poll"
else
Flag.flagged?(current_user, flaggable)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Comment < ActiveRecord::Base
include HasPublicAuthor
include Graphqlable

COMMENTABLE_TYPES = %w(Debate Proposal Budget::Investment Poll::Question Legislation::Question Legislation::Annotation Topic).freeze
COMMENTABLE_TYPES = %w(Debate Proposal Budget::Investment Poll::Question Legislation::Question Legislation::Annotation Topic Poll).freeze

acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
Expand Down
1 change: 1 addition & 0 deletions app/models/comment_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def send_reply_email
end

def email_on_comment?
return false if @comment.commentable.is_a?(Poll)
commentable_author = @comment.commentable.author
commentable_author != @author && commentable_author.email_on_comment?
end
Expand Down
5 changes: 4 additions & 1 deletion app/models/poll.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Poll < ActiveRecord::Base
include Imageable

acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
has_many :booth_assignments, class_name: "Poll::BoothAssignment"
has_many :booths, through: :booth_assignments
has_many :partial_results, through: :booth_assignments
Expand All @@ -9,8 +10,10 @@ class Poll < ActiveRecord::Base
has_many :officer_assignments, through: :booth_assignments
has_many :officers, through: :officer_assignments
has_many :questions
has_many :comments, as: :commentable

has_and_belongs_to_many :geozones
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'

validates :name, presence: true

Expand Down
2 changes: 1 addition & 1 deletion app/views/comments/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="row">
<div class="small-12 column margin-top">
<%= back_link_to commentable_path(@comment),
t("comments.show.return_to_commentable") + @comment.commentable.title %>
t("comments.show.return_to_commentable") + @comment.commentable.title unless @commentable.class == Poll %>
</div>
</div>

Expand Down
24 changes: 24 additions & 0 deletions app/views/polls/_comments.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<% cache [locale_and_user_status, @current_order, commentable_cache_key(@poll), @comment_tree.comments, @comment_tree.comment_authors, @poll.comments_count, @comment_flags] do %>
<div class="row comments">
<div id="comments" class="small-12 column">
<%= render 'shared/wide_order_selector', i18n_namespace: "comments" %>
<% if user_signed_in? %>
<%= render 'comments/form', {commentable: @poll, parent_id: nil, toggeable: false} %>
<% else %>
<br>

<div data-alert class="callout primary">
<%= t("polls.show.login_to_comment",
signin: link_to(t("votes.signin"), new_user_session_path),
signup: link_to(t("votes.signup"), new_user_registration_path)).html_safe %>
</div>
<% end %>
<% @comment_tree.root_comments.each do |comment| %>
<%= render 'comments/comment', comment: comment %>
<% end %>
<%= paginate @comment_tree.root_comments %>
</div>
</div>
<% end %>
14 changes: 14 additions & 0 deletions app/views/polls/_filter_subnav.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="row">
<div class="small-12 column">
<ul class="tabs" data-tabs id="polls-tabs">
<li class="tabs-title is-active">
<%= link_to "#tab-comments" do %>
<h3>
<%= t("polls.show.comments_tab") %>
<span class="js-comments-count">(<%= @poll.comments_count %>)</span>
</h3>
<% end %>
</li>
</ul>
</div>
</div>
9 changes: 9 additions & 0 deletions app/views/polls/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,14 @@
</div>
<% end %>
</div>

</div>
</div>

<div class="tabs-content" data-tabs-content="proposals-tabs" role="tablist">
<%= render "filter_subnav" %>

<div class="tabs-panel is-active" id="tab-comments">
<%= render "comments" %>
</div>
</div>
2 changes: 2 additions & 0 deletions config/locales/en/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ en:
already_voted_in_web: "You have already participated in this poll. If you vote again it will be overwritten."
back: Back to voting
cant_answer_not_logged_in: "You must %{signin} or %{signup} to participate."
comments_tab: Comments
login_to_comment: You must %{signin} or %{signup} to leave a comment.
signin: Sign in
signup: Sign up
cant_answer_verify_html: "You must %{verify_link} in order to answer."
Expand Down
2 changes: 2 additions & 0 deletions config/locales/es/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ es:
already_voted_in_web: "Ya has participado en esta votación. Si vuelves a votar se sobreescribirá tu resultado anterior."
back: Volver a votaciones
cant_answer_not_logged_in: "Necesitas %{signin} o %{signup} para participar."
comments_tab: Comentarios
login_to_comment: Necesitas %{signin} o %{signup} para comentar.
signin: iniciar sesión
signup: registrarte
cant_answer_verify_html: "Por favor %{verify_link} para poder responder."
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20171003143034_add_comments_count_to_polls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddCommentsCountToPolls < ActiveRecord::Migration
def change
add_column :polls, :comments_count, :integer, default: 0
add_column :polls, :author_id, :integer
add_column :polls, :hidden_at, :datetime
end
end
3 changes: 3 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,9 @@
t.boolean "geozone_restricted", default: false
t.text "summary"
t.text "description"
t.integer "comments_count", default: 0
t.integer "author_id"
t.datetime "hidden_at"
end

add_index "polls", ["starts_at", "ends_at"], name: "index_polls_on_starts_at_and_ends_at", using: :btree
Expand Down
Loading

0 comments on commit 0e3868c

Please sign in to comment.