From 19b75264213823d684221bec4ef8f54469acbbb1 Mon Sep 17 00:00:00 2001 From: voodoorai2000 Date: Thu, 29 Nov 2018 12:30:47 +0100 Subject: [PATCH] Add document upload from admin section --- .../documents_controller.rb | 40 +++++++++ app/models/document.rb | 2 + app/views/admin/_menu.html.erb | 5 ++ .../documents/index.html.erb | 45 ++++++++++ .../site_customization/documents/new.html.erb | 9 ++ config/locales/en/admin.yml | 14 +++ config/locales/es/admin.yml | 13 +++ config/routes/admin.rb | 1 + .../20181128175808_add_admin_to_documents.rb | 5 ++ db/schema.rb | 1 + spec/factories/files.rb | 4 + .../site_customization/documents_spec.rb | 89 +++++++++++++++++++ spec/models/document_spec.rb | 19 ++++ 13 files changed, 247 insertions(+) create mode 100644 app/controllers/admin/site_customization/documents_controller.rb create mode 100644 app/views/admin/site_customization/documents/index.html.erb create mode 100644 app/views/admin/site_customization/documents/new.html.erb create mode 100644 db/migrate/20181128175808_add_admin_to_documents.rb create mode 100644 spec/features/admin/site_customization/documents_spec.rb diff --git a/app/controllers/admin/site_customization/documents_controller.rb b/app/controllers/admin/site_customization/documents_controller.rb new file mode 100644 index 000000000..20f9581f6 --- /dev/null +++ b/app/controllers/admin/site_customization/documents_controller.rb @@ -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 diff --git a/app/models/document.rb b/app/models/document.rb index 3a78231f1..3edf203d6 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -24,6 +24,8 @@ class Document < ApplicationRecord before_save :set_attachment_from_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 self.cached_attachment = if Paperclip::Attachment.default_options[:storage] == :filesystem attachment.path diff --git a/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb index 9fe52bcd4..cb509cf15 100644 --- a/app/views/admin/_menu.html.erb +++ b/app/views/admin/_menu.html.erb @@ -134,6 +134,11 @@
  • > <%= link_to t("admin.menu.site_customization.information_texts"), admin_site_customization_information_texts_path %>
  • + +
  • > + <%= link_to t("admin.menu.site_customization.documents"), + admin_site_customization_documents_path %> +
  • diff --git a/app/views/admin/site_customization/documents/index.html.erb b/app/views/admin/site_customization/documents/index.html.erb new file mode 100644 index 000000000..e122cc891 --- /dev/null +++ b/app/views/admin/site_customization/documents/index.html.erb @@ -0,0 +1,45 @@ +

    Documentos

    + +<%= link_to t("admin.documents.index.new_link"), + new_admin_site_customization_document_path, + class: "button float-right" %> + +

    <%= page_entries_info @documents %>

    + +<% if @documents.any? %> + + + + + + + + + + + + <% @documents.each do |document| %> + + + + + + + + <% end %> + +
    TitleFormatSizeUrlActions
    <%= document.title %><%= document.attachment.content_type %><%= number_to_human_size(document.attachment.size) %><%= link_to document.title, document.attachment.url, target: :blank %> +
    + <%= link_to "Destroy", + admin_site_customization_document_path(document), + method: :delete, + data: { confirm: t('admin.actions.confirm') } %> +
    +
    +<% else %> +
    + <%= t("admin.documents.index.no_documents") %> +
    +<% end %> + +<%= paginate @documents %> diff --git a/app/views/admin/site_customization/documents/new.html.erb b/app/views/admin/site_customization/documents/new.html.erb new file mode 100644 index 000000000..0c53b9cdf --- /dev/null +++ b/app/views/admin/site_customization/documents/new.html.erb @@ -0,0 +1,9 @@ +

    <%= t("admin.documents.new.title") %>

    + +<%= 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 %> diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index 55c7142fc..b881adfc9 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -438,6 +438,19 @@ en: without_confirmed_hide: Pending title: 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: index: filter: Filter @@ -678,6 +691,7 @@ en: stats: Statistics signature_sheets: Signature Sheets site_customization: + documents: Custom documents homepage: Homepage pages: Custom pages images: Custom images diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 54e7e9806..bc3eb9314 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -438,6 +438,18 @@ es: without_confirmed_hide: Pendientes title: 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: index: filter: Filtro @@ -677,6 +689,7 @@ es: stats: Estadísticas signature_sheets: Hojas de firmas site_customization: + documents: Documentos homepage: Homepage pages: Personalizar páginas images: Personalizar imágenes diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 220500f89..c5b350528 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -230,6 +230,7 @@ namespace :admin do resources :information_texts, only: [:index] do post :update, on: :collection end + resources :documents, only: [:index, :new, :create, :destroy] end resource :homepage, controller: :homepage, only: [:show] diff --git a/db/migrate/20181128175808_add_admin_to_documents.rb b/db/migrate/20181128175808_add_admin_to_documents.rb new file mode 100644 index 000000000..d90a570e4 --- /dev/null +++ b/db/migrate/20181128175808_add_admin_to_documents.rb @@ -0,0 +1,5 @@ +class AddAdminToDocuments < ActiveRecord::Migration + def change + add_column :documents, :admin, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 780a0cea6..d60546259 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -504,6 +504,7 @@ ActiveRecord::Schema.define(version: 20190411090023) do t.string "documentable_type" t.datetime "created_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 ["user_id", "documentable_type", "documentable_id"], name: "access_documents", using: :btree t.index ["user_id"], name: "index_documents_on_user_id", using: :btree diff --git a/spec/factories/files.rb b/spec/factories/files.rb index 90c4875ce..45d1d6e7e 100644 --- a/spec/factories/files.rb +++ b/spec/factories/files.rb @@ -29,6 +29,10 @@ FactoryBot.define do trait :poll_question_document do association :documentable, factory: :poll_question end + + trait :admin do + admin true + end end factory :direct_upload do diff --git a/spec/features/admin/site_customization/documents_spec.rb b/spec/features/admin/site_customization/documents_spec.rb new file mode 100644 index 000000000..c7bbe446a --- /dev/null +++ b/spec/features/admin/site_customization/documents_spec.rb @@ -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 diff --git a/spec/models/document_spec.rb b/spec/models/document_spec.rb index b65a6f318..ac1ee0e8c 100644 --- a/spec/models/document_spec.rb +++ b/spec/models/document_spec.rb @@ -5,4 +5,23 @@ describe Document do it_behaves_like "document validations", "budget_investment_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