From 19b75264213823d684221bec4ef8f54469acbbb1 Mon Sep 17 00:00:00 2001 From: voodoorai2000 Date: Thu, 29 Nov 2018 12:30:47 +0100 Subject: [PATCH 1/7] 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 From 4193bb193c20a919a275d1916182e1c647fcf628 Mon Sep 17 00:00:00 2001 From: decabeza Date: Fri, 30 Nov 2018 15:25:50 +0100 Subject: [PATCH 2/7] Adds documents to menu customization admin helper --- app/helpers/admin_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/admin_helper.rb b/app/helpers/admin_helper.rb index 842f7725f..0446aaf73 100644 --- a/app/helpers/admin_helper.rb +++ b/app/helpers/admin_helper.rb @@ -56,7 +56,7 @@ module AdminHelper end def menu_customization? - ["pages", "banners", "information_texts"].include?(controller_name) || + ["pages", "banners", "information_texts", "documents"].include?(controller_name) || menu_homepage? || menu_pages? end From 62a29e2cf969498ec57d49fbe9da73acdf27ee5c Mon Sep 17 00:00:00 2001 From: decabeza Date: Fri, 30 Nov 2018 15:26:16 +0100 Subject: [PATCH 3/7] Adds missing i18n --- .../documents/index.html.erb | 22 +++++++++---------- config/locales/en/activerecord.yml | 3 +++ config/locales/en/admin.yml | 6 ++++- config/locales/es/activerecord.yml | 3 +++ config/locales/es/admin.yml | 7 +++++- .../site_customization/documents_spec.rb | 2 +- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/app/views/admin/site_customization/documents/index.html.erb b/app/views/admin/site_customization/documents/index.html.erb index e122cc891..ee792f63f 100644 --- a/app/views/admin/site_customization/documents/index.html.erb +++ b/app/views/admin/site_customization/documents/index.html.erb @@ -1,20 +1,20 @@ -

    Documentos

    +

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

    <%= 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? %> +

    <%= page_entries_info @documents %>

    +
    - - - - - + + + + + @@ -26,10 +26,10 @@ diff --git a/config/locales/en/activerecord.yml b/config/locales/en/activerecord.yml index 9396fbc85..450dcb797 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -79,6 +79,9 @@ en: site_customization/content_block: one: Custom content block other: Custom content blocks + document: + one: Document + other: Documents legislation/process: one: "Process" other: "Processes" diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index b881adfc9..ad66ad884 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -441,11 +441,15 @@ en: documents: new: title: "Upload a document" - file_field_text: "Choose document" + label_attachment: "Choose document" submit_button: "Upload" index: new_link: "Add new document" no_documents: "There are no documents." + title: "Documents" + format: "Format" + size: "Size" + url: "URL" create: success_notice: "Document uploaded succesfully" unable_notice: "Invalid document" diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index f564f4e11..c8ef9247d 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -79,6 +79,9 @@ es: site_customization/content_block: one: Bloque other: Bloques + document: + one: Documento + other: Documentos legislation/process: one: "Proceso" other: "Procesos" diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index bc3eb9314..b55e1dae8 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -441,13 +441,18 @@ es: documents: new: title: "Subir un documento" + label_attachment: "Selecciona un documento" submit_button: "Subir documento" index: + title: "Documentos" new_link: "Subir un documento" no_documents: "No hay documentos." + format: "Formato" + size: "Tamaño" + url: "URL" create: success_notice: "Documento creado correctamente" - unable_notice: "Document inválido" + unable_notice: "Documento inválido" destroy: success_notice: "Documento borrado correctamente" hidden_users: diff --git a/spec/features/admin/site_customization/documents_spec.rb b/spec/features/admin/site_customization/documents_spec.rb index c7bbe446a..d373993ce 100644 --- a/spec/features/admin/site_customization/documents_spec.rb +++ b/spec/features/admin/site_customization/documents_spec.rb @@ -79,7 +79,7 @@ feature 'Documents' do visit admin_site_customization_documents_path within("#document_#{document.id}") do - accept_confirm { click_link "Destroy" } + accept_confirm { click_link "Delete" } end expect(page).to have_content "Document deleted succesfully" From b023ceabddfe01902efcd647c2e2bf406c68c9a4 Mon Sep 17 00:00:00 2001 From: decabeza Date: Fri, 30 Nov 2018 17:53:08 +0100 Subject: [PATCH 4/7] Fixes not_to hound warnings --- spec/features/admin/site_customization/documents_spec.rb | 2 +- spec/models/document_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/features/admin/site_customization/documents_spec.rb b/spec/features/admin/site_customization/documents_spec.rb index d373993ce..a154ff7fd 100644 --- a/spec/features/admin/site_customization/documents_spec.rb +++ b/spec/features/admin/site_customization/documents_spec.rb @@ -83,7 +83,7 @@ feature 'Documents' do end expect(page).to have_content "Document deleted succesfully" - expect(page).to_not have_content document.title + expect(page).not_to have_content document.title end end diff --git a/spec/models/document_spec.rb b/spec/models/document_spec.rb index ac1ee0e8c..13b4d4f70 100644 --- a/spec/models/document_spec.rb +++ b/spec/models/document_spec.rb @@ -19,7 +19,7 @@ describe Document do 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 + expect(Document.admin).not_to include user_document end end From 1719a8ed28395ffd2777301f6083bc959a361345 Mon Sep 17 00:00:00 2001 From: decabeza Date: Fri, 30 Nov 2018 18:46:12 +0100 Subject: [PATCH 5/7] Adds styles to admin site customization documents --- .../site_customization/documents_controller.rb | 4 ++-- .../site_customization/documents/index.html.erb | 1 + .../admin/site_customization/documents/new.html.erb | 13 +++++++------ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/controllers/admin/site_customization/documents_controller.rb b/app/controllers/admin/site_customization/documents_controller.rb index 20f9581f6..51cee4227 100644 --- a/app/controllers/admin/site_customization/documents_controller.rb +++ b/app/controllers/admin/site_customization/documents_controller.rb @@ -14,8 +14,8 @@ class Admin::SiteCustomization::DocumentsController < Admin::SiteCustomization:: 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 + flash.now[:error] = t("admin.documents.create.unable_notice") + render :new end end diff --git a/app/views/admin/site_customization/documents/index.html.erb b/app/views/admin/site_customization/documents/index.html.erb index ee792f63f..a4bc3de65 100644 --- a/app/views/admin/site_customization/documents/index.html.erb +++ b/app/views/admin/site_customization/documents/index.html.erb @@ -29,6 +29,7 @@ <%= link_to t("admin.shared.delete"), admin_site_customization_document_path(document), method: :delete, + class: "button hollow alert", data: { confirm: t("admin.actions.confirm") } %> diff --git a/app/views/admin/site_customization/documents/new.html.erb b/app/views/admin/site_customization/documents/new.html.erb index 0c53b9cdf..778345750 100644 --- a/app/views/admin/site_customization/documents/new.html.erb +++ b/app/views/admin/site_customization/documents/new.html.erb @@ -1,9 +1,10 @@

    <%= 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") %> +<%= form_for @document, + url: admin_site_customization_documents_path do |f| %> + <%= f.file_field 'attachment', label: t("admin.documents.new.label_attachment") %> + +
    + <%= f.submit t("admin.documents.new.submit_button"), class: "button success" %> +
    <% end %> From 367c2e7b7869f2297eee873778ff971780a5430c Mon Sep 17 00:00:00 2001 From: decabeza Date: Tue, 30 Apr 2019 12:06:56 +0200 Subject: [PATCH 6/7] Fix hound warnings --- .../admin/site_customization/documents/new.html.erb | 2 +- .../admin/site_customization/documents_spec.rb | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/views/admin/site_customization/documents/new.html.erb b/app/views/admin/site_customization/documents/new.html.erb index 778345750..9a4971564 100644 --- a/app/views/admin/site_customization/documents/new.html.erb +++ b/app/views/admin/site_customization/documents/new.html.erb @@ -5,6 +5,6 @@ <%= f.file_field 'attachment', label: t("admin.documents.new.label_attachment") %>
    - <%= f.submit t("admin.documents.new.submit_button"), class: "button success" %> + <%= f.submit t("admin.documents.new.submit_button"), class: "button success" %>
    <% end %> diff --git a/spec/features/admin/site_customization/documents_spec.rb b/spec/features/admin/site_customization/documents_spec.rb index a154ff7fd..0ff667c2b 100644 --- a/spec/features/admin/site_customization/documents_spec.rb +++ b/spec/features/admin/site_customization/documents_spec.rb @@ -1,6 +1,6 @@ -require 'rails_helper' +require "rails_helper" -feature 'Documents' do +feature "Documents" do before do admin = create(:administrator) @@ -37,22 +37,22 @@ feature 'Documents' do expect(page).to have_content "There are no documents." end - scenario 'Index (pagination)' do + 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) + 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).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) + expect(page).to have_selector("#documents .document", count: 5) end scenario "Create" do From 88852544fdae3acb403874ae7772f52f09ed8249 Mon Sep 17 00:00:00 2001 From: decabeza Date: Fri, 10 May 2019 17:17:39 +0200 Subject: [PATCH 7/7] Fix indentation on schema --- db/schema.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index d60546259..e5bf875af 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -502,8 +502,8 @@ ActiveRecord::Schema.define(version: 20190411090023) do t.integer "user_id" t.integer "documentable_id" t.string "documentable_type" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + 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
    TitleFormatSizeUrlActions<%= t("admin.shared.title") %><%= t("admin.documents.index.format") %><%= t("admin.documents.index.size") %><%= t("admin.documents.index.url") %><%= t("admin.shared.actions") %>
    <%= link_to document.title, document.attachment.url, target: :blank %>
    - <%= link_to "Destroy", - admin_site_customization_document_path(document), + <%= link_to t("admin.shared.delete"), + admin_site_customization_document_path(document), method: :delete, - data: { confirm: t('admin.actions.confirm') } %> + data: { confirm: t("admin.actions.confirm") } %>