Add document upload from admin section
This commit is contained in:
@@ -0,0 +1,40 @@
|
|||||||
|
class Admin::SiteCustomization::DocumentsController < Admin::SiteCustomization::BaseController
|
||||||
|
|
||||||
|
def index
|
||||||
|
@documents = Document.admin.page(params[:page])
|
||||||
|
end
|
||||||
|
|
||||||
|
def new
|
||||||
|
@document = Document.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
@document = initialize_document
|
||||||
|
if @document.save
|
||||||
|
notice = t("admin.documents.create.success_notice")
|
||||||
|
redirect_to admin_site_customization_documents_path, notice: notice
|
||||||
|
else
|
||||||
|
notice = t("admin.documents.create.unable_notice")
|
||||||
|
redirect_to new_admin_site_customization_document_path, notice: notice
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
@document = Document.find(params[:id])
|
||||||
|
@document.destroy
|
||||||
|
|
||||||
|
notice = t("admin.documents.destroy.success_notice")
|
||||||
|
redirect_to admin_site_customization_documents_path, notice: notice
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def initialize_document
|
||||||
|
document = Document.new
|
||||||
|
document.attachment = params.dig(:document, :attachment)
|
||||||
|
document.title = document.attachment_file_name
|
||||||
|
document.user = current_user
|
||||||
|
document.admin = true
|
||||||
|
document
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -24,6 +24,8 @@ class Document < ApplicationRecord
|
|||||||
before_save :set_attachment_from_cached_attachment, if: -> { cached_attachment.present? }
|
before_save :set_attachment_from_cached_attachment, if: -> { cached_attachment.present? }
|
||||||
after_save :remove_cached_attachment, if: -> { cached_attachment.present? }
|
after_save :remove_cached_attachment, if: -> { cached_attachment.present? }
|
||||||
|
|
||||||
|
scope :admin, -> { where(admin: true) }
|
||||||
|
|
||||||
def set_cached_attachment_from_attachment
|
def set_cached_attachment_from_attachment
|
||||||
self.cached_attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem
|
self.cached_attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem
|
||||||
attachment.path
|
attachment.path
|
||||||
|
|||||||
@@ -134,6 +134,11 @@
|
|||||||
<li <%= "class=is-active" if controller_name == "information_texts" %>>
|
<li <%= "class=is-active" if controller_name == "information_texts" %>>
|
||||||
<%= link_to t("admin.menu.site_customization.information_texts"), admin_site_customization_information_texts_path %>
|
<%= link_to t("admin.menu.site_customization.information_texts"), admin_site_customization_information_texts_path %>
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
|
<li <%= "class=is-active" if controller_name == "documents" %>>
|
||||||
|
<%= link_to t("admin.menu.site_customization.documents"),
|
||||||
|
admin_site_customization_documents_path %>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
|
|||||||
45
app/views/admin/site_customization/documents/index.html.erb
Normal file
45
app/views/admin/site_customization/documents/index.html.erb
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
<h2 class="inline-block">Documentos</h2>
|
||||||
|
|
||||||
|
<%= link_to t("admin.documents.index.new_link"),
|
||||||
|
new_admin_site_customization_document_path,
|
||||||
|
class: "button float-right" %>
|
||||||
|
|
||||||
|
<h3><%= page_entries_info @documents %></h3>
|
||||||
|
|
||||||
|
<% if @documents.any? %>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th scope="col">Title</th>
|
||||||
|
<th scope="col">Format</th>
|
||||||
|
<th scope="col">Size</th>
|
||||||
|
<th scope="col">Url</th>
|
||||||
|
<th scope="col">Actions</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody id="documents">
|
||||||
|
<% @documents.each do |document| %>
|
||||||
|
<tr id="<%= dom_id(document) %>" class="document">
|
||||||
|
<td><%= document.title %></td>
|
||||||
|
<td><%= document.attachment.content_type %></td>
|
||||||
|
<td><%= number_to_human_size(document.attachment.size) %></td>
|
||||||
|
<td><%= link_to document.title, document.attachment.url, target: :blank %></td>
|
||||||
|
<td>
|
||||||
|
<div class="small-12 medium-6 column">
|
||||||
|
<%= link_to "Destroy",
|
||||||
|
admin_site_customization_document_path(document),
|
||||||
|
method: :delete,
|
||||||
|
data: { confirm: t('admin.actions.confirm') } %>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<% end %>
|
||||||
|
</tbody>
|
||||||
|
<table>
|
||||||
|
<% else %>
|
||||||
|
<div class="callout primary">
|
||||||
|
<%= t("admin.documents.index.no_documents") %>
|
||||||
|
</div>
|
||||||
|
<% end %>
|
||||||
|
|
||||||
|
<%= paginate @documents %>
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
<h2><%= t("admin.documents.new.title") %></h2>
|
||||||
|
|
||||||
|
<%= form_for @document,
|
||||||
|
url: admin_site_customization_documents_path,
|
||||||
|
multipart: true do |f| %>
|
||||||
|
|
||||||
|
<%= f.file_field 'attachment', { label: false } %>
|
||||||
|
<%= f.submit t("admin.documents.new.submit_button") %>
|
||||||
|
<% end %>
|
||||||
@@ -438,6 +438,19 @@ en:
|
|||||||
without_confirmed_hide: Pending
|
without_confirmed_hide: Pending
|
||||||
title: Hidden debates
|
title: Hidden debates
|
||||||
no_hidden_debates: There is no hidden debates.
|
no_hidden_debates: There is no hidden debates.
|
||||||
|
documents:
|
||||||
|
new:
|
||||||
|
title: "Upload a document"
|
||||||
|
file_field_text: "Choose document"
|
||||||
|
submit_button: "Upload"
|
||||||
|
index:
|
||||||
|
new_link: "Add new document"
|
||||||
|
no_documents: "There are no documents."
|
||||||
|
create:
|
||||||
|
success_notice: "Document uploaded succesfully"
|
||||||
|
unable_notice: "Invalid document"
|
||||||
|
destroy:
|
||||||
|
success_notice: "Document deleted succesfully"
|
||||||
hidden_users:
|
hidden_users:
|
||||||
index:
|
index:
|
||||||
filter: Filter
|
filter: Filter
|
||||||
@@ -678,6 +691,7 @@ en:
|
|||||||
stats: Statistics
|
stats: Statistics
|
||||||
signature_sheets: Signature Sheets
|
signature_sheets: Signature Sheets
|
||||||
site_customization:
|
site_customization:
|
||||||
|
documents: Custom documents
|
||||||
homepage: Homepage
|
homepage: Homepage
|
||||||
pages: Custom pages
|
pages: Custom pages
|
||||||
images: Custom images
|
images: Custom images
|
||||||
|
|||||||
@@ -438,6 +438,18 @@ es:
|
|||||||
without_confirmed_hide: Pendientes
|
without_confirmed_hide: Pendientes
|
||||||
title: Debates ocultos
|
title: Debates ocultos
|
||||||
no_hidden_debates: No hay debates ocultos.
|
no_hidden_debates: No hay debates ocultos.
|
||||||
|
documents:
|
||||||
|
new:
|
||||||
|
title: "Subir un documento"
|
||||||
|
submit_button: "Subir documento"
|
||||||
|
index:
|
||||||
|
new_link: "Subir un documento"
|
||||||
|
no_documents: "No hay documentos."
|
||||||
|
create:
|
||||||
|
success_notice: "Documento creado correctamente"
|
||||||
|
unable_notice: "Document inválido"
|
||||||
|
destroy:
|
||||||
|
success_notice: "Documento borrado correctamente"
|
||||||
hidden_users:
|
hidden_users:
|
||||||
index:
|
index:
|
||||||
filter: Filtro
|
filter: Filtro
|
||||||
@@ -677,6 +689,7 @@ es:
|
|||||||
stats: Estadísticas
|
stats: Estadísticas
|
||||||
signature_sheets: Hojas de firmas
|
signature_sheets: Hojas de firmas
|
||||||
site_customization:
|
site_customization:
|
||||||
|
documents: Documentos
|
||||||
homepage: Homepage
|
homepage: Homepage
|
||||||
pages: Personalizar páginas
|
pages: Personalizar páginas
|
||||||
images: Personalizar imágenes
|
images: Personalizar imágenes
|
||||||
|
|||||||
@@ -230,6 +230,7 @@ namespace :admin do
|
|||||||
resources :information_texts, only: [:index] do
|
resources :information_texts, only: [:index] do
|
||||||
post :update, on: :collection
|
post :update, on: :collection
|
||||||
end
|
end
|
||||||
|
resources :documents, only: [:index, :new, :create, :destroy]
|
||||||
end
|
end
|
||||||
|
|
||||||
resource :homepage, controller: :homepage, only: [:show]
|
resource :homepage, controller: :homepage, only: [:show]
|
||||||
|
|||||||
5
db/migrate/20181128175808_add_admin_to_documents.rb
Normal file
5
db/migrate/20181128175808_add_admin_to_documents.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
class AddAdminToDocuments < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
add_column :documents, :admin, :boolean, default: false
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -504,6 +504,7 @@ ActiveRecord::Schema.define(version: 20190411090023) do
|
|||||||
t.string "documentable_type"
|
t.string "documentable_type"
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
|
t.boolean "admin", default: false
|
||||||
t.index ["documentable_type", "documentable_id"], name: "index_documents_on_documentable_type_and_documentable_id", using: :btree
|
t.index ["documentable_type", "documentable_id"], name: "index_documents_on_documentable_type_and_documentable_id", using: :btree
|
||||||
t.index ["user_id", "documentable_type", "documentable_id"], name: "access_documents", using: :btree
|
t.index ["user_id", "documentable_type", "documentable_id"], name: "access_documents", using: :btree
|
||||||
t.index ["user_id"], name: "index_documents_on_user_id", using: :btree
|
t.index ["user_id"], name: "index_documents_on_user_id", using: :btree
|
||||||
|
|||||||
@@ -29,6 +29,10 @@ FactoryBot.define do
|
|||||||
trait :poll_question_document do
|
trait :poll_question_document do
|
||||||
association :documentable, factory: :poll_question
|
association :documentable, factory: :poll_question
|
||||||
end
|
end
|
||||||
|
|
||||||
|
trait :admin do
|
||||||
|
admin true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
factory :direct_upload do
|
factory :direct_upload do
|
||||||
|
|||||||
89
spec/features/admin/site_customization/documents_spec.rb
Normal file
89
spec/features/admin/site_customization/documents_spec.rb
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
feature 'Documents' do
|
||||||
|
|
||||||
|
before do
|
||||||
|
admin = create(:administrator)
|
||||||
|
login_as(admin.user)
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario "Navigation", :js do
|
||||||
|
visit admin_root_path
|
||||||
|
|
||||||
|
within("#side_menu") do
|
||||||
|
click_link "Site content"
|
||||||
|
click_link "Custom documents"
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(page).to have_link "Add new document",
|
||||||
|
href: new_admin_site_customization_document_path
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario "Index" do
|
||||||
|
3.times { create(:document, :admin) }
|
||||||
|
1.times { create(:document) }
|
||||||
|
|
||||||
|
visit admin_site_customization_documents_path
|
||||||
|
|
||||||
|
expect(page).to have_content "There are 3 documents"
|
||||||
|
|
||||||
|
document = Document.first
|
||||||
|
expect(page).to have_link document.title, href: document.attachment.url
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario "Index (empty)" do
|
||||||
|
visit admin_site_customization_documents_path
|
||||||
|
|
||||||
|
expect(page).to have_content "There are no documents."
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario 'Index (pagination)' do
|
||||||
|
per_page = Kaminari.config.default_per_page
|
||||||
|
(per_page + 5).times { create(:document, :admin) }
|
||||||
|
|
||||||
|
visit admin_site_customization_documents_path
|
||||||
|
|
||||||
|
expect(page).to have_selector('#documents .document', count: per_page)
|
||||||
|
|
||||||
|
within("ul.pagination") do
|
||||||
|
expect(page).to have_content("1")
|
||||||
|
expect(page).to have_link('2', href: admin_site_customization_documents_url(page: 2))
|
||||||
|
expect(page).not_to have_content("3")
|
||||||
|
click_link "Next", exact: false
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(page).to have_selector('#documents .document', count: 5)
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario "Create" do
|
||||||
|
visit new_admin_site_customization_document_path
|
||||||
|
|
||||||
|
attach_file("document_attachment", Rails.root + "spec/fixtures/files/logo.pdf")
|
||||||
|
click_button "Upload"
|
||||||
|
|
||||||
|
expect(page).to have_content "Document uploaded succesfully"
|
||||||
|
expect(page).to have_link "logo.pdf", href: Document.last.attachment.url
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario "Errors on create" do
|
||||||
|
visit new_admin_site_customization_document_path
|
||||||
|
|
||||||
|
click_button "Upload"
|
||||||
|
|
||||||
|
expect(page).to have_content "Invalid document"
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario "Destroy", :js do
|
||||||
|
document = create(:document, :admin)
|
||||||
|
|
||||||
|
visit admin_site_customization_documents_path
|
||||||
|
|
||||||
|
within("#document_#{document.id}") do
|
||||||
|
accept_confirm { click_link "Destroy" }
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(page).to have_content "Document deleted succesfully"
|
||||||
|
expect(page).to_not have_content document.title
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
@@ -5,4 +5,23 @@ describe Document do
|
|||||||
it_behaves_like "document validations", "budget_investment_document"
|
it_behaves_like "document validations", "budget_investment_document"
|
||||||
it_behaves_like "document validations", "proposal_document"
|
it_behaves_like "document validations", "proposal_document"
|
||||||
|
|
||||||
|
context "scopes" do
|
||||||
|
|
||||||
|
describe "#admin" do
|
||||||
|
|
||||||
|
it "returns admin documents" do
|
||||||
|
user_document = create(:document)
|
||||||
|
admin_document1 = create(:document, :admin)
|
||||||
|
admin_document2 = create(:document, :admin)
|
||||||
|
admin_document3 = create(:document, :admin)
|
||||||
|
|
||||||
|
expect(Document.admin.count).to eq(3)
|
||||||
|
expect(Document.admin).to include admin_document1
|
||||||
|
expect(Document.admin).to include admin_document2
|
||||||
|
expect(Document.admin).to include admin_document3
|
||||||
|
expect(Document.admin).to_not include user_document
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user