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..51cee4227
--- /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
+ flash.now[:error] = t("admin.documents.create.unable_notice")
+ render :new
+ 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/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
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..a4bc3de65
--- /dev/null
+++ b/app/views/admin/site_customization/documents/index.html.erb
@@ -0,0 +1,46 @@
+<%= t("admin.documents.index.title") %>
+
+<%= link_to t("admin.documents.index.new_link"),
+ new_admin_site_customization_document_path,
+ class: "button float-right" %>
+
+<% if @documents.any? %>
+ <%= page_entries_info @documents %>
+
+
+
+
+ | <%= t("admin.shared.title") %> |
+ <%= t("admin.documents.index.format") %> |
+ <%= t("admin.documents.index.size") %> |
+ <%= t("admin.documents.index.url") %> |
+ <%= t("admin.shared.actions") %> |
+
+
+
+ <% @documents.each do |document| %>
+
+ | <%= document.title %> |
+ <%= document.attachment.content_type %> |
+ <%= number_to_human_size(document.attachment.size) %> |
+ <%= link_to document.title, document.attachment.url, target: :blank %> |
+
+
+ <%= link_to t("admin.shared.delete"),
+ admin_site_customization_document_path(document),
+ method: :delete,
+ class: "button hollow alert",
+ data: { confirm: t("admin.actions.confirm") } %>
+
+ |
+
+ <% end %>
+
+
+<% 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..9a4971564
--- /dev/null
+++ b/app/views/admin/site_customization/documents/new.html.erb
@@ -0,0 +1,10 @@
+<%= t("admin.documents.new.title") %>
+
+<%= 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 %>
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 55c7142fc..ad66ad884 100644
--- a/config/locales/en/admin.yml
+++ b/config/locales/en/admin.yml
@@ -438,6 +438,23 @@ en:
without_confirmed_hide: Pending
title: Hidden debates
no_hidden_debates: There is no hidden debates.
+ documents:
+ new:
+ title: "Upload a 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"
+ destroy:
+ success_notice: "Document deleted succesfully"
hidden_users:
index:
filter: Filter
@@ -678,6 +695,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/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 54e7e9806..b55e1dae8 100644
--- a/config/locales/es/admin.yml
+++ b/config/locales/es/admin.yml
@@ -438,6 +438,23 @@ es:
without_confirmed_hide: Pendientes
title: Debates ocultos
no_hidden_debates: No hay debates ocultos.
+ 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: "Documento inválido"
+ destroy:
+ success_notice: "Documento borrado correctamente"
hidden_users:
index:
filter: Filtro
@@ -677,6 +694,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..e5bf875af 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -502,8 +502,9 @@ 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
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..0ff667c2b
--- /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 "Delete" }
+ end
+
+ expect(page).to have_content "Document deleted succesfully"
+ 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 b65a6f318..13b4d4f70 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).not_to include user_document
+ end
+
+ end
+ end
end