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_controller.rb b/app/controllers/admin/poll/questions/answers_controller.rb index 660339844..2bc442fe5 100644 --- a/app/controllers/admin/poll/questions/answers_controller.rb +++ b/app/controllers/admin/poll/questions/answers_controller.rb @@ -1,5 +1,5 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController - before_action :load_question + before_action :load_answer, only: [:update, :documents] load_and_authorize_resource :question, class: "::Poll::Question" @@ -18,13 +18,28 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController end end + def update + if @answer.update(answer_params) + redirect_to admin_question_path(@answer.question), 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_question - @question = ::Poll::Question.find(params[:question_id]) + def load_answer + @answer = ::Poll::Question::Answer.find(params[:id] || params[:answer_id]) 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/answer.rb b/app/models/poll/question/answer.rb index fb32de9fc..b3324e57f 100644 --- a/app/models/poll/question/answer.rb +++ b/app/models/poll/question/answer.rb @@ -1,5 +1,10 @@ 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' 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) %>
  • diff --git a/app/views/admin/poll/questions/answers/_form.html.erb b/app/views/admin/poll/questions/answers/_form.html.erb index 72ffb4771..fc16e7e36 100644 --- a/app/views/admin/poll/questions/answers/_form.html.erb +++ b/app/views/admin/poll/questions/answers/_form.html.erb @@ -15,7 +15,7 @@ label: false %> -
    +
    <%= f.submit(class: "button expanded", value: t("shared.save")) %>
    diff --git a/app/views/admin/poll/questions/answers/documents.html.erb b/app/views/admin/poll/questions/answers/documents.html.erb new file mode 100644 index 000000000..54dd25a18 --- /dev/null +++ b/app/views/admin/poll/questions/answers/documents.html.erb @@ -0,0 +1,55 @@ +<%= back_link_to %> + +

    <%= t("admin.questions.show.answers.documents_list") %>

    + + + +
    + <%= form_for(Poll::Question::Answer.new, + url: admin_answer_path(@answer), + method: :put) do |f| %> + + <%= render 'shared/errors', resource: @answer %> + +
    +
    +
    + <%= render 'documents/nested_documents', documentable: @answer, f: f %> +
    + +
    +
    + <%= f.submit(class: "button expanded", value: t("shared.save")) %> +
    +
    +
    +
    + <% end %> + + <% if @answer.documents.present? %> + + + + + + + <% @answer.documents.each do |document| %> + + + + + <% end %> +
    <%= 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' %> +
    + <% end %> +
    diff --git a/app/views/admin/poll/questions/show.html.erb b/app/views/admin/poll/questions/show.html.erb index 22068e96a..8a2d34c7c 100644 --- a/app/views/admin/poll/questions/show.html.erb +++ b/app/views/admin/poll/questions/show.html.erb @@ -31,7 +31,7 @@ - - - - + + + + <% @question.question_answers.each do |answer| %> @@ -51,12 +52,22 @@ - + admin_answer_images_path(answer) %> + + + <% end %>
    + <%= t('admin.questions.show.valid_answers') %> <%= link_to t("admin.questions.show.add_answer"), new_admin_question_answer_path(@question), @@ -41,9 +41,10 @@
    <%= t("admin.questions.show.answers.title") %><%= t("admin.questions.show.answers.description") %><%= t("admin.questions.show.answers.images") %><%= t("admin.questions.show.answers.videos") %><%= 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 %> <%= answer.description %> - (<%= answer.images.count %>)
    + (<%= answer.images.count %>) +
    <%= link_to t("admin.questions.show.answers.images_list"), - admin_answer_images_path(answer) %>
    <%= link_to t("admin.questions.show.answers.video_list", - count: answer.videos.count), - admin_answer_videos_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) %> +
    diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index 8eef7cb34..020803b91 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -608,6 +608,10 @@ en: video_list: Video list (%{count}) images: Images images_list: Images list + documents: Documents + documents_list: Documents list + document_title: Title + document_actions: Actions answers: new: title: New answer diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 94f07ec5a..3b9ff16ff 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -601,7 +601,6 @@ es: valid_answers: Respuestas válidas add_answer: Añadir respuesta video_url: Video externo - preview: Ver en la web answers: title: Respuesta description: Descripción @@ -609,6 +608,10 @@ es: video_list: Lista de vídeos (%{count}) images: Imágenes images_list: Lista de imágenes + documents: Documentos + documents_list: Lista de documentos + document_title: Título + document_actions: Acciones answers: new: title: "Nueva respuesta" diff --git a/config/routes.rb b/config/routes.rb index 600766227..156605801 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -300,10 +300,11 @@ Rails.application.routes.draw do end end - resources :questions, shallow: true do - resources :answers, only: [:new, :create], controller: 'questions/answers', shallow: true do + resources :questions do + resources :answers, only: [:new, :create, :update], controller: 'questions/answers', shallow: true do resources :images, controller: 'questions/answers/images' resources :videos, controller: 'questions/answers/videos' + get :documents, to: 'questions/answers#documents' end end end diff --git a/spec/shared/features/nested_documentable.rb b/spec/shared/features/nested_documentable.rb index 255f018e5..24d6631a8 100644 --- a/spec/shared/features/nested_documentable.rb +++ b/spec/shared/features/nested_documentable.rb @@ -191,9 +191,17 @@ shared_examples "nested documentable" do |login_as_name, documentable_factory_na documentable_attach_new_file(documentable_factory_name, 0, "spec/fixtures/files/empty.pdf") click_on submit_button + documentable_redirected_to_resource_show_or_navigate_to - expect(page).to have_content "Documents (1)" + expect(page).to have_content "Documents" + + find("#tab-documents-label").click + expect(page).to have_content "empty.pdf" + + #Review + #Doble check why the file is stored with a name different to empty.pdf + expect(page).to have_css("a[href$='.pdf']") end scenario "Should show resource with new document after successful creation with maximum allowed uploaded files", :js do diff --git a/spec/shared/features/nested_imageable.rb b/spec/shared/features/nested_imageable.rb index bd519a5bb..1eef9a269 100644 --- a/spec/shared/features/nested_imageable.rb +++ b/spec/shared/features/nested_imageable.rb @@ -146,7 +146,11 @@ shared_examples "nested imageable" do |imageable_factory_name, path, imageable_p send(fill_resource_method_name) if fill_resource_method_name click_on submit_button - expect(page).to have_content imageable_success_notice + if has_many_images + skip "no need to test, there are no attributes for the parent resource" + else + expect(page).to have_content imageable_success_notice + end end scenario "Should show successful notice when resource filled correctly and after valid file uploads", :js do