adds images to anwers
This commit is contained in:
@@ -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
|
||||
12
app/models/concerns/galleryable.rb
Normal file
12
app/models/concerns/galleryable.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 %>
|
||||
|
||||
<div>
|
||||
<%= link_to "Add image", new_admin_answer_image_path(@answer) %>
|
||||
</div>
|
||||
66
app/views/admin/poll/questions/answers/images/new.html.erb
Normal file
66
app/views/admin/poll/questions/answers/images/new.html.erb
Normal file
@@ -0,0 +1,66 @@
|
||||
<%= form_for(Poll::Question::Answer.new,
|
||||
url: admin_answer_images_path(@answer),
|
||||
method: :post) do |f| %>
|
||||
<div class="images small-12 column">
|
||||
<%# render 'images/nested_image', imageable: @answer, f: f %>
|
||||
|
||||
<%= f.label :images, t("images.form.title") %>
|
||||
</div>
|
||||
|
||||
<p class="help-text"><%# imageables_note(imageable) %></p>
|
||||
|
||||
<div id="nested-image">
|
||||
<%= f.fields_for :images do |image_builder| %>
|
||||
<%# render 'images/image_fields', f: image_builder, imageable: imageable %>
|
||||
<div id="<%= dom_id(f.object) %>" class="image direct-upload nested-fields">
|
||||
<%= f.hidden_field :id %>
|
||||
<%= f.hidden_field :user_id, value: current_user.id %>
|
||||
<%= f.hidden_field :cached_attachment %>
|
||||
|
||||
<div class="small-12 column title">
|
||||
<%= f.text_field :title, placeholder: t("images.form.title_placeholder") %>
|
||||
</div>
|
||||
|
||||
<%= render_image(f.object, :thumb, false) if f.object.attachment.exists? %>
|
||||
|
||||
<div class="small-12 column attachment-actions">
|
||||
<div class="small-9 column action-add attachment-errors image-attachment">
|
||||
<%= render_image_attachment(f, @answer, f.object) %>
|
||||
</div>
|
||||
<div class="small-3 column action-remove text-right">
|
||||
<%= render_destroy_image_link(f, f.object) %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="small-6 column">
|
||||
<p class="file-name">
|
||||
<%= image_attachment_file_name(f.object) %>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="small-12 column">
|
||||
<div class="progress-bar-placeholder"><div class="loading-bar"></div></div>
|
||||
</div>
|
||||
|
||||
<hr>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<%= 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"
|
||||
} %>
|
||||
|
||||
</div>
|
||||
|
||||
<%= f.submit "Save image" %>
|
||||
<% end %>
|
||||
@@ -39,12 +39,17 @@
|
||||
<tr>
|
||||
<th scope="col"><%= t("admin.questions.show.answers.title") %></th>
|
||||
<th scope="col"><%= t("admin.questions.show.answers.description") %></th>
|
||||
<th scope="col">Imágenes</th>
|
||||
</tr>
|
||||
|
||||
<% @question.question_answers.each do |answer| %>
|
||||
<tr id="<%= dom_id(answer) %>" class="poll_question_answer">
|
||||
<th scope="col"><%= answer.title %></th>
|
||||
<th scope="col"><%= answer.description %></th>
|
||||
<th scope="col">
|
||||
<%= link_to "Lista de imágenes",
|
||||
admin_answer_images_path(answer) %>
|
||||
</th>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user