From 1cf9bc4ad134fa000256affa937bc67d975963b6 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 4 Oct 2017 21:42:57 +0200 Subject: [PATCH] adds images to anwers --- .../questions/answers/images_controller.rb | 33 ++++++++++ app/models/concerns/galleryable.rb | 12 ++++ app/models/direct_upload.rb | 5 +- app/models/poll/question/answer.rb | 2 + .../questions/answers/images/index.html.erb | 9 +++ .../questions/answers/images/new.html.erb | 66 +++++++++++++++++++ app/views/admin/poll/questions/show.html.erb | 5 ++ config/routes.rb | 5 +- .../questions/answers/images/images_spec.rb | 31 +++++++++ spec/shared/features/nested_imageable.rb | 1 + 10 files changed, 166 insertions(+), 3 deletions(-) create mode 100644 app/controllers/admin/poll/questions/answers/images_controller.rb create mode 100644 app/models/concerns/galleryable.rb create mode 100644 app/views/admin/poll/questions/answers/images/index.html.erb create mode 100644 app/views/admin/poll/questions/answers/images/new.html.erb create mode 100644 spec/features/admin/poll/questions/answers/images/images_spec.rb diff --git a/app/controllers/admin/poll/questions/answers/images_controller.rb b/app/controllers/admin/poll/questions/answers/images_controller.rb new file mode 100644 index 000000000..39ff5100d --- /dev/null +++ b/app/controllers/admin/poll/questions/answers/images_controller.rb @@ -0,0 +1,33 @@ +class Admin::Poll::Questions::Answers::ImagesController < Admin::Poll::BaseController + before_action :load_answer + + def index + end + + def new + @answer = ::Poll::Question::Answer.find(params[:answer_id]) + end + + def create + @answer = ::Poll::Question::Answer.find(params[:answer_id]) + @answer.attributes = images_params + + if @answer.save + redirect_to admin_answer_images_path(@answer), + notice: "Image uploaded successfully" + else + render :new + end + end + + private + + def images_params + params.require(:poll_question_answer) + .permit(images_attributes: [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]) + end + + def load_answer + @answer = ::Poll::Question::Answer.find(params[:answer_id]) + end +end diff --git a/app/models/concerns/galleryable.rb b/app/models/concerns/galleryable.rb new file mode 100644 index 000000000..1063c5571 --- /dev/null +++ b/app/models/concerns/galleryable.rb @@ -0,0 +1,12 @@ +module Galleryable + extend ActiveSupport::Concern + + included do + has_many :images, as: :imageable, dependent: :destroy + accepts_nested_attributes_for :images, allow_destroy: true, update_only: true + + def image_url(style) + image.attachment.url(style) if image && image.attachment.exists? + end + end +end \ No newline at end of file diff --git a/app/models/direct_upload.rb b/app/models/direct_upload.rb index a6b6ef276..348c2641e 100644 --- a/app/models/direct_upload.rb +++ b/app/models/direct_upload.rb @@ -19,8 +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.class.reflections[@resource_relation].macro == :has_one - @relation = @resource.send("build_#{resource_relation}", relation_attributtes) + if true #@resource.class.reflections[@resource_relation].macro == :has_one + #@relation = @resource.send("build_#{resource_relation}", relation_attributtes) + @relation = @resource.images.send("build", relation_attributtes) else @relation = @resource.send(@resource_relation).build(relation_attributtes) end diff --git a/app/models/poll/question/answer.rb b/app/models/poll/question/answer.rb index 3f8f4a172..1746c480c 100644 --- a/app/models/poll/question/answer.rb +++ b/app/models/poll/question/answer.rb @@ -1,4 +1,6 @@ class Poll::Question::Answer < ActiveRecord::Base + include Galleryable + belongs_to :question, class_name: 'Poll::Question', foreign_key: 'question_id' validates :title, presence: true diff --git a/app/views/admin/poll/questions/answers/images/index.html.erb b/app/views/admin/poll/questions/answers/images/index.html.erb new file mode 100644 index 000000000..09d055783 --- /dev/null +++ b/app/views/admin/poll/questions/answers/images/index.html.erb @@ -0,0 +1,9 @@ +<%= back_link_to admin_question_path(@answer.question) %> + +<% @answer.images.each do |image| %> + <%= render_image(image, :large, true) if image.present? %> +<% end %> + +
+ <%= link_to "Add image", new_admin_answer_image_path(@answer) %> +
\ No newline at end of file diff --git a/app/views/admin/poll/questions/answers/images/new.html.erb b/app/views/admin/poll/questions/answers/images/new.html.erb new file mode 100644 index 000000000..17e728a21 --- /dev/null +++ b/app/views/admin/poll/questions/answers/images/new.html.erb @@ -0,0 +1,66 @@ +<%= form_for(Poll::Question::Answer.new, + url: admin_answer_images_path(@answer), + method: :post) do |f| %> +
+ <%# render 'images/nested_image', imageable: @answer, f: f %> + + <%= f.label :images, t("images.form.title") %> +
+ +

<%# imageables_note(imageable) %>

+ +
+ <%= f.fields_for :images do |image_builder| %> + <%# render 'images/image_fields', f: image_builder, imageable: imageable %> +
+ <%= f.hidden_field :id %> + <%= f.hidden_field :user_id, value: current_user.id %> + <%= f.hidden_field :cached_attachment %> + +
+ <%= f.text_field :title, placeholder: t("images.form.title_placeholder") %> +
+ + <%= render_image(f.object, :thumb, false) if f.object.attachment.exists? %> + +
+
+ <%= render_image_attachment(f, @answer, f.object) %> +
+
+ <%= render_destroy_image_link(f, f.object) %> +
+
+ +
+

+ <%= image_attachment_file_name(f.object) %> +

+
+ +
+
+
+ +
+
+ <% end %> +
+ + <%= link_to_add_association t('images.form.add_new_image'), f, :images, + force_non_association_create: true, + partial: "images/image_fields", + id: "new_image_link", + class: "button hollow #{"hide" if @answer.images.present?}", + render_options: { + locals: { imageable: @answer } + }, + data: { + association_insertion_node: "#nested-image", + association_insertion_method: "append" + } %> + + + + <%= f.submit "Save image" %> +<% end %> \ No newline at end of file diff --git a/app/views/admin/poll/questions/show.html.erb b/app/views/admin/poll/questions/show.html.erb index 7037afd5f..024c30cd5 100644 --- a/app/views/admin/poll/questions/show.html.erb +++ b/app/views/admin/poll/questions/show.html.erb @@ -39,12 +39,17 @@ <%= t("admin.questions.show.answers.title") %> <%= t("admin.questions.show.answers.description") %> + Imágenes <% @question.question_answers.each do |answer| %> <%= answer.title %> <%= answer.description %> + + <%= link_to "Lista de imágenes", + admin_answer_images_path(answer) %> + <% end %> diff --git a/config/routes.rb b/config/routes.rb index 89691feac..387983d4b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -301,7 +301,10 @@ Rails.application.routes.draw do end resources :questions do - resources :answers, only: [:new, :create], controller: 'questions/answers' + resources :answers, only: [:new, :create], controller: 'questions/answers', shallow: true do + resources :images, controller: 'questions/answers/images' + end + end end diff --git a/spec/features/admin/poll/questions/answers/images/images_spec.rb b/spec/features/admin/poll/questions/answers/images/images_spec.rb new file mode 100644 index 000000000..54cd0544b --- /dev/null +++ b/spec/features/admin/poll/questions/answers/images/images_spec.rb @@ -0,0 +1,31 @@ +require 'rails_helper' + +feature 'Images' do + + background do + admin = create(:administrator) + login_as(admin.user) + end + + scenario "Index" do + end + + scenario "Create", :js do + question = create(:poll_question) + answer = create(:poll_question_answer, question: question) + + visit admin_question_path(question) + + within("#poll_question_answer_#{answer.id}") do + click_link "Lista de imágenes" + end + + click_link "Add image" + + imageable_attach_new_file(:poll_question_answer, "spec/fixtures/files/clippy.jpg") + click_button "Save image" + + expect(page).to have_content "Image uploaded successfully" + end + +end \ No newline at end of file diff --git a/spec/shared/features/nested_imageable.rb b/spec/shared/features/nested_imageable.rb index 242686bd6..68aab3a92 100644 --- a/spec/shared/features/nested_imageable.rb +++ b/spec/shared/features/nested_imageable.rb @@ -214,6 +214,7 @@ end def imageable_attach_new_file(imageable_factory_name, path, success = true) click_link "Add image" + within "#nested-image" do image = find(".image") image_input = image.find("input[type=file]", visible: false)