diff --git a/app/assets/stylesheets/admin.scss b/app/assets/stylesheets/admin.scss index 9c95f400f..e42bb0362 100644 --- a/app/assets/stylesheets/admin.scss +++ b/app/assets/stylesheets/admin.scss @@ -414,6 +414,15 @@ code { word-break: break-all; } +.content-type { + white-space: nowrap; + padding-right: $line-height; + + label { + margin-left: 0 !important; + } +} + // 02. Sidebar // ----------- diff --git a/app/controllers/admin/settings_controller.rb b/app/controllers/admin/settings_controller.rb index 34fa22704..f24b6c63e 100644 --- a/app/controllers/admin/settings_controller.rb +++ b/app/controllers/admin/settings_controller.rb @@ -14,6 +14,7 @@ class Admin::SettingsController < Admin::BaseController @participation_processes_settings = all_settings["process"] @map_configuration_settings = all_settings["map"] @proposals_settings = all_settings["proposals"] + @uploads_settings = all_settings["uploads"] end def update @@ -29,10 +30,24 @@ class Admin::SettingsController < Admin::BaseController redirect_to admin_settings_path, notice: t("admin.settings.index.map.flash.update") end + def update_content_types + setting = Setting.find(params[:id]) + group = setting.content_type_group + mime_type_values = content_type_params.keys.map do |content_type| + Setting.mime_types[group][content_type] + end + setting.update value: mime_type_values.join(" ") + redirect_to admin_settings_path, notice: t("admin.settings.flash.updated") + end + private def settings_params params.require(:setting).permit(:value) end + def content_type_params + params.permit(:jpg, :png, :gif, :pdf, :doc, :docx, :xls, :xlsx, :csv, :zip) + end + end diff --git a/app/models/setting.rb b/app/models/setting.rb index c3bb8bca0..12162db4e 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -8,7 +8,7 @@ class Setting < ApplicationRecord end def type - if %w[feature process proposals map html homepage].include? prefix + if %w[feature process proposals map html homepage uploads].include? prefix prefix else "configuration" @@ -19,6 +19,14 @@ class Setting < ApplicationRecord value.present? end + def content_type? + key.split(".").last == "content_types" + end + + def content_type_group + key.split(".").second + end + class << self def [](key) where(key: key).pluck(:value).first.presence @@ -44,6 +52,25 @@ class Setting < ApplicationRecord setting.destroy if setting.present? end + def mime_types + { + "images" => { + "jpg" => "image/jpeg", + "png" => "image/png", + "gif" => "image/gif" + }, + "documents" => { + "pdf" => "application/pdf", + "doc" => "application/msword", + "docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + "xls" => "application/x-ole-storage", + "xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + "csv" => "text/plain", + "zip" => "application/zip" + } + } + end + def defaults { "feature.featured_proposals": nil, diff --git a/app/views/admin/settings/_content_types_settings_form.html.erb b/app/views/admin/settings/_content_types_settings_form.html.erb new file mode 100644 index 000000000..67262c155 --- /dev/null +++ b/app/views/admin/settings/_content_types_settings_form.html.erb @@ -0,0 +1,19 @@ +<%= form_tag admin_update_content_types_path, method: :put, id: "edit_#{dom_id(setting)}" do %> + <%= hidden_field_tag "id", setting.id %> + +
+ <% group = setting.content_type_group %> + <% Setting.mime_types[group].each do |content_type, mime_type_value| %> + + <%= check_box_tag content_type, + setting.value.split(" ").include?(mime_type_value), + setting.value.split(" ").include?(mime_type_value) %> + <%= label_tag content_type, content_type.upcase %> + + <% end %> +
+ +
+ <%= submit_tag t("admin.settings.index.update_setting"), class: "button hollow expanded" %> +
+<% end %> diff --git a/app/views/admin/settings/_filter_subnav.html.erb b/app/views/admin/settings/_filter_subnav.html.erb index d4e4a00d8..355e7a1c5 100644 --- a/app/views/admin/settings/_filter_subnav.html.erb +++ b/app/views/admin/settings/_filter_subnav.html.erb @@ -29,6 +29,12 @@ <% end %> +
  • + <%= link_to "#tab-images-and-documents" do %> + <%= t("admin.settings.index.images_and_documents") %> + <% end %> +
  • +
  • <%= link_to "#tab-proposals" do %> <%= t("admin.settings.index.dashboard.title") %> diff --git a/app/views/admin/settings/_images_and_documents_tab.html.erb b/app/views/admin/settings/_images_and_documents_tab.html.erb new file mode 100644 index 000000000..740f55f23 --- /dev/null +++ b/app/views/admin/settings/_images_and_documents_tab.html.erb @@ -0,0 +1,3 @@ +

    <%= t("admin.settings.index.images_and_documents") %>

    + +<%= render "settings_table", settings: @uploads_settings %> diff --git a/app/views/admin/settings/_settings_table.html.erb b/app/views/admin/settings/_settings_table.html.erb index 76f567afc..ce99ffe4e 100644 --- a/app/views/admin/settings/_settings_table.html.erb +++ b/app/views/admin/settings/_settings_table.html.erb @@ -16,7 +16,11 @@ - <%= render "admin/settings/settings_form", setting: setting %> + <% if setting.content_type? %> + <%= render "admin/settings/content_types_settings_form", setting: setting %> + <% else %> + <%= render "admin/settings/settings_form", setting: setting %> + <% end %> <% end %> diff --git a/app/views/admin/settings/index.html.erb b/app/views/admin/settings/index.html.erb index 1de5b66be..a000cffc5 100644 --- a/app/views/admin/settings/index.html.erb +++ b/app/views/admin/settings/index.html.erb @@ -18,6 +18,10 @@ <%= render "map_configuration_tab" %> +
    + <%= render "images_and_documents_tab" %> +
    +
    <%= render "proposals_dashboard" %>
    diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index e3c5c597b..c860315ab 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -1261,6 +1261,7 @@ en: title: Configuration settings update_setting: Update participation_processes: "Participation processes" + images_and_documents: "Images and documents" feature_flags: Features features: enabled: "Feature enabled" diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 34fc7510b..f783e0777 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -1260,6 +1260,7 @@ es: title: Configuración global update_setting: Actualizar participation_processes: "Procesos de participación" + images_and_documents: "Imágenes y documentos" feature_flags: Funcionalidades features: enabled: "Funcionalidad activada" diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 39ed9b44b..ca162b9ca 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -90,6 +90,7 @@ namespace :admin do resources :settings, only: [:index, :update] put :update_map, to: "settings#update_map" + put :update_content_types, to: "settings#update_content_types" resources :moderators, only: [:index, :create, :destroy] do get :search, on: :collection diff --git a/spec/features/admin/settings_spec.rb b/spec/features/admin/settings_spec.rb index 4800b8c3e..ef72b5d0c 100644 --- a/spec/features/admin/settings_spec.rb +++ b/spec/features/admin/settings_spec.rb @@ -98,6 +98,40 @@ describe "Admin settings" do end + describe "Update content types" do + + scenario "stores the correct mime types" do + setting = Setting.create(key: "upload.images.content_types", value: "image/png") + admin = create(:administrator).user + login_as(admin) + visit admin_settings_path + find("#images-and-documents-tab").click + + within "#edit_setting_#{setting.id}" do + expect(find("#png")).to be_checked + expect(find("#jpg")).not_to be_checked + expect(find("#gif")).not_to be_checked + + check "gif" + + click_button "Update" + end + + expect(page).to have_content "Value updated" + expect(Setting["upload.images.content_types"]).to include "image/png" + expect(Setting["upload.images.content_types"]).to include "image/gif" + + visit admin_settings_path(anchor: "tab-images-and-documents") + + within "#edit_setting_#{setting.id}" do + expect(find("#png")).to be_checked + expect(find("#gif")).to be_checked + expect(find("#jpg")).not_to be_checked + end + end + + end + describe "Skip verification" do scenario "deactivate skip verification", :js do diff --git a/spec/models/setting_spec.rb b/spec/models/setting_spec.rb index 971fbe3df..df764b7ca 100644 --- a/spec/models/setting_spec.rb +++ b/spec/models/setting_spec.rb @@ -80,6 +80,26 @@ describe Setting do end end + describe "#content_type?" do + it "returns true if the last part of the key is content_types" do + expect(Setting.create(key: "key_name.content_types").content_type?).to be true + end + + it "returns false if the last part of the key is not content_types" do + expect(Setting.create(key: "key_name.whatever").content_type?).to be false + end + end + + describe "#content_type_group" do + it "returns the group for content_types settings" do + images = Setting.create(key: "update.images.content_types") + documents = Setting.create(key: "update.documents.content_types") + + expect(images.content_type_group).to eq "images" + expect(documents.content_type_group).to eq "documents" + end + end + describe ".rename_key" do it "renames the setting keeping the original value and deletes the old setting" do Setting["old_key"] = "old_value"