diff --git a/app/controllers/admin/poll/questions/answers/images_controller.rb b/app/controllers/admin/poll/questions/answers/images_controller.rb index 4074eb371..1bf30907d 100644 --- a/app/controllers/admin/poll/questions/answers/images_controller.rb +++ b/app/controllers/admin/poll/questions/answers/images_controller.rb @@ -23,7 +23,8 @@ class Admin::Poll::Questions::Answers::ImagesController < Admin::Poll::BaseContr private def images_params - params.permit(images_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]) + params.require(:poll_question_answer).permit(:answer_id, + images_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]) end def load_answer diff --git a/app/controllers/admin/poll/questions/answers/videos_controller.rb b/app/controllers/admin/poll/questions/answers/videos_controller.rb new file mode 100644 index 000000000..a231c1a20 --- /dev/null +++ b/app/controllers/admin/poll/questions/answers/videos_controller.rb @@ -0,0 +1,57 @@ +class Admin::Poll::Questions::Answers::VideosController < Admin::Poll::BaseController + before_action :load_answer, only: [:index, :new, :create] + before_action :load_video, only: [:edit, :update, :destroy] + + def index + end + + def new + @video = ::Poll::Question::Answer::Video.new + end + + def create + @video = ::Poll::Question::Answer::Video.new(video_params) + + if @video.save + redirect_to admin_answer_videos_path(@answer), + notice: t("flash.actions.create.poll_question_answer_video") + else + render :new + end + end + + def edit + end + + def update + if @video.update(video_params) + redirect_to admin_answer_videos_path(@video.answer_id), + notice: t("flash.actions.save_changes.notice") + else + render :edit + end + end + + def destroy + if @video.destroy + notice = t("flash.actions.destroy.poll_question_answer_video") + else + notice = t("flash.actions.destroy.error") + end + redirect_to :back, notice: notice + end + + private + + def video_params + params.require(:poll_question_answer_video).permit(:title, :url, :answer_id) + end + + def load_answer + @answer = ::Poll::Question::Answer.find(params[:answer_id]) + end + + def load_video + @video = ::Poll::Question::Answer::Video.find(params[:id]) + end +end diff --git a/app/controllers/admin/poll/questions/answers_controller.rb b/app/controllers/admin/poll/questions/answers_controller.rb index 660339844..45f0abcd7 100644 --- a/app/controllers/admin/poll/questions/answers_controller.rb +++ b/app/controllers/admin/poll/questions/answers_controller.rb @@ -1,5 +1,6 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController - before_action :load_question + before_action :load_question, except: [:show, :edit, :update] + before_action :load_answer, only: [:show, :edit, :update, :documents] load_and_authorize_resource :question, class: "::Poll::Question" @@ -18,13 +19,39 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController end end + def show + end + + def edit + end + + def update + if @answer.update(answer_params) + redirect_to admin_answer_path(@answer), + notice: t("flash.actions.save_changes.notice") + else + redirect_to :back + end + end + + def documents + @documents = @answer.documents + + render 'admin/poll/questions/answers/documents' + end + private def answer_params - params.require(:poll_question_answer).permit(:title, :description, :question_id) + params.require(:poll_question_answer).permit(:title, :description, :question_id, documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]) + end + + def load_answer + @answer = ::Poll::Question::Answer.find(params[:id] || params[:answer_id]) end def load_question @question = ::Poll::Question.find(params[:question_id]) end + end diff --git a/app/controllers/admin/poll/questions_controller.rb b/app/controllers/admin/poll/questions_controller.rb index 107c06740..5cf587735 100644 --- a/app/controllers/admin/poll/questions_controller.rb +++ b/app/controllers/admin/poll/questions_controller.rb @@ -56,8 +56,7 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController private def question_params - params.require(:poll_question).permit(:poll_id, :title, :question, :proposal_id, :valid_answers, :video_url, - documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]) + params.require(:poll_question).permit(:poll_id, :title, :question, :proposal_id, :valid_answers, :video_url) end def search_params diff --git a/app/controllers/polls/questions_controller.rb b/app/controllers/polls/questions_controller.rb index 39fc18252..407e6d984 100644 --- a/app/controllers/polls/questions_controller.rb +++ b/app/controllers/polls/questions_controller.rb @@ -10,6 +10,7 @@ class Polls::QuestionsController < ApplicationController token = params[:token] answer.answer = params[:answer] + answer.touch if answer.persisted? answer.save! answer.record_voter_participation(token) diff --git a/app/helpers/polls_helper.rb b/app/helpers/polls_helper.rb index acc84b49c..dd4018be7 100644 --- a/app/helpers/polls_helper.rb +++ b/app/helpers/polls_helper.rb @@ -44,5 +44,9 @@ module PollsHelper def poll_voter_token(poll, user) Poll::Voter.where(poll: poll, user: user, origin: "web").first&.token || '' end + + def voted_before_sign_in(question) + question.answers.where(author: current_user).any? { |vote| current_user.current_sign_in_at >= vote.updated_at } + end end diff --git a/app/models/direct_upload.rb b/app/models/direct_upload.rb index 192f06d04..e597f9f12 100644 --- a/app/models/direct_upload.rb +++ b/app/models/direct_upload.rb @@ -19,7 +19,9 @@ class DirectUpload if @resource_type.present? && @resource_relation.present? && (@attachment.present? || @cached_attachment.present?) @resource = @resource_type.constantize.find_or_initialize_by(id: @resource_id) - if @resource.respond_to?(:images) + #Refactor + if @resource.respond_to?(:images) && + ((@attachment.present? && !@attachment.content_type.match(/pdf/)) || @cached_attachment.present?) @relation = @resource.images.send("build", relation_attributtes) elsif @resource.class.reflections[@resource_relation].macro == :has_one @relation = @resource.send("build_#{resource_relation}", relation_attributtes) diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index 1e94e0180..e6ef482a9 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -1,11 +1,6 @@ class Poll::Question < ActiveRecord::Base include Measurable include Searchable - include Documentable - documentable max_documents_allowed: 1, - max_file_size: 3.megabytes, - accepted_content_types: [ "application/pdf" ] - accepts_nested_attributes_for :documents, allow_destroy: true acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases diff --git a/app/models/poll/question/answer.rb b/app/models/poll/question/answer.rb index 1746c480c..b3324e57f 100644 --- a/app/models/poll/question/answer.rb +++ b/app/models/poll/question/answer.rb @@ -1,7 +1,13 @@ class Poll::Question::Answer < ActiveRecord::Base include Galleryable + include Documentable + documentable max_documents_allowed: 3, + max_file_size: 3.megabytes, + accepted_content_types: [ "application/pdf" ] + accepts_nested_attributes_for :documents, allow_destroy: true belongs_to :question, class_name: 'Poll::Question', foreign_key: 'question_id' + has_many :videos, class_name: 'Poll::Question::Answer::Video' validates :title, presence: true diff --git a/app/models/poll/question/answer/video.rb b/app/models/poll/question/answer/video.rb new file mode 100644 index 000000000..3d214af96 --- /dev/null +++ b/app/models/poll/question/answer/video.rb @@ -0,0 +1,16 @@ +class Poll::Question::Answer::Video < ActiveRecord::Base + belongs_to :answer, class_name: 'Poll::Question::Answer', foreign_key: 'answer_id' + + VIMEO_REGEX = /vimeo.*(staffpicks\/|channels\/|videos\/|video\/|\/)([^#\&\?]*).*/ + YOUTUBE_REGEX = /youtu.*(be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/ + + validates :title, presence: true + validate :valid_url? + + def valid_url? + return if url.blank? + return if url.match(VIMEO_REGEX) + return if url.match(YOUTUBE_REGEX) + errors.add(:url, :invalid) + end +end diff --git a/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb index 81e1ae9bd..21dc7cffb 100644 --- a/app/views/admin/_menu.html.erb +++ b/app/views/admin/_menu.html.erb @@ -53,7 +53,6 @@ <% end %> - <% if feature?(:polls) %>
<%= t("proposals.form.proposal_video_url_note") %>
diff --git a/app/views/admin/poll/questions/answers/_form.html.erb b/app/views/admin/poll/questions/answers/_form.html.erb index 72ffb4771..3bb2ebe39 100644 --- a/app/views/admin/poll/questions/answers/_form.html.erb +++ b/app/views/admin/poll/questions/answers/_form.html.erb @@ -2,20 +2,17 @@ <%= render 'shared/errors', resource: @answer %> - <%= f.hidden_field :question_id, value: @question.id %> + <%= f.hidden_field :question_id, value: @answer.question_id || @question.id %> - <%= f.label :title, t('admin.questions.new.form.title') %> - <%= f.text_field :title, label: false %> + <%= f.text_field :title %>| <%= t("admin.questions.show.answers.document_title") %> | +<%= t("admin.questions.show.answers.document_actions") %> | +
|---|---|
| + <%= link_to document.title, document.attachment.url %> + | ++ <%= link_to t('documents.buttons.download_document'), + document.attachment.url, + target: "_blank", + rel: "nofollow", + class: 'button hollow' %> + | +
+ <%= t("admin.answers.show.title") %>
+
+ <%= @answer.title %>
+
+ <%= t("admin.answers.show.description") %> + <%= @answer.description %> +
+ +
+ <%= t("admin.answers.show.images") %>
+
+ <%= link_to t("admin.answers.show.images_list"), admin_answer_images_path(@answer) %>
+
| <%= t("admin.answers.videos.index.video_title") %> | +<%= t("admin.answers.videos.index.video_url") %> | ++ <%= t("admin.actions.actions") %> + | +
|---|---|---|
| <%= video.title %> | +<%= link_to "#{video.url}", video.url %> | ++ + <%= link_to t("shared.edit"), + edit_admin_video_path(video), + class: "button hollow" %> + + <%= link_to t("shared.delete"), + admin_video_path(video), + class: "button hollow alert", + method: :delete %> + | +
| + | <%= t('admin.questions.show.valid_answers') %> <%= link_to t("admin.questions.show.add_answer"), new_admin_question_answer_path(@question), @@ -41,19 +41,33 @@ | ||||||
|---|---|---|---|---|---|---|---|
| <%= t("admin.questions.show.answers.title") %> | -<%= t("admin.questions.show.answers.description") %> | -<%= t("admin.questions.show.answers.images") %> | +<%= t("admin.questions.show.answers.description") %> | +<%= t("admin.questions.show.answers.images") %> | +<%= t("admin.questions.show.answers.documents") %> | +<%= t("admin.questions.show.answers.videos") %> | |
| <%= answer.title %> | +<%= link_to answer.title, admin_answer_path(answer) %> | <%= answer.description %> |
- (<%= answer.images.count %>) + (<%= answer.images.count %>) + <%= link_to t("admin.questions.show.answers.images_list"), admin_answer_images_path(answer) %> |
+
+ (<%= answer.documents.count rescue 0 %>)
+ + <%= link_to t("admin.questions.show.answers.documents_list"), + admin_answer_documents_path(answer) %> + |
+ + <%= link_to t("admin.questions.show.answers.video_list", + count: answer.videos.count), + admin_answer_videos_path(answer) %> + | ||
- <%= t("admin.questions.show.documents") %>
-
- <%= @question.documents.first.title %>
-