From d49e9fe75adfb24f1e294544911c7e2f7198978f Mon Sep 17 00:00:00 2001 From: Xavier J Date: Thu, 10 Dec 2015 17:13:39 +0100 Subject: [PATCH] admin can upload a logo for his procedure --- app/assets/stylesheets/dossiers.scss | 2 +- .../admin/procedures_controller.rb | 3 +- app/decorators/procedure_decorator.rb | 5 ++ app/models/procedure.rb | 2 + app/uploaders/procedure_logo_uploader.rb | 51 +++++++++++++++++++ .../admin/procedures/_informations.html.haml | 19 +++++-- app/views/admin/procedures/edit.html.haml | 2 +- app/views/admin/procedures/new.html.haml | 2 +- app/views/users/dossiers/new.html.haml | 3 +- .../20151210150958_add_logo_to_procedure.rb | 5 ++ db/schema.rb | 3 +- spec/models/procedure_spec.rb | 2 + .../users/dossiers/new_html.haml_spec.rb | 46 ++++++++++++----- 13 files changed, 119 insertions(+), 26 deletions(-) create mode 100644 app/uploaders/procedure_logo_uploader.rb create mode 100644 db/migrate/20151210150958_add_logo_to_procedure.rb diff --git a/app/assets/stylesheets/dossiers.scss b/app/assets/stylesheets/dossiers.scss index 85349b719..98ed7d31f 100644 --- a/app/assets/stylesheets/dossiers.scss +++ b/app/assets/stylesheets/dossiers.scss @@ -19,7 +19,7 @@ h5 span { img { max-width: 150px; - max-height: 70px; + max-height: 60px; } } diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index 2be546406..06546f8b6 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -47,6 +47,7 @@ class Admin::ProceduresController < AdminController flash.now.alert = @procedure.errors.full_messages.join('
').html_safe return render 'edit' end + flash.notice = 'Préocédure modifiée' redirect_to admin_procedures_path end @@ -66,7 +67,7 @@ class Admin::ProceduresController < AdminController private def create_procedure_params - params.require(:procedure).permit(:libelle, :description, :organisation, :direction, :lien_demarche, :euro_flag, module_api_carto_attributes: [:id, :use_api_carto, :quartiers_prioritaires, :cadastre]).merge(administrateur_id: current_administrateur.id) + params.require(:procedure).permit(:libelle, :description, :organisation, :direction, :lien_demarche, :euro_flag, :logo, module_api_carto_attributes: [:id, :use_api_carto, :quartiers_prioritaires, :cadastre]).merge(administrateur_id: current_administrateur.id) end def create_module_api_carto_params diff --git a/app/decorators/procedure_decorator.rb b/app/decorators/procedure_decorator.rb index 5f0c48513..d6a8183cd 100644 --- a/app/decorators/procedure_decorator.rb +++ b/app/decorators/procedure_decorator.rb @@ -4,4 +4,9 @@ class ProcedureDecorator < Draper::Decorator def lien h.new_users_dossiers_url(procedure_id: id) end + + def logo_img + return 'logo-tps.png' if logo.blank? + logo + end end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 1922f09bb..c9b824433 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -8,6 +8,8 @@ class Procedure < ActiveRecord::Base accepts_nested_attributes_for :module_api_carto belongs_to :administrateur + mount_uploader :logo, ProcedureLogoUploader + validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :description, presence: true, allow_blank: false, allow_nil: false diff --git a/app/uploaders/procedure_logo_uploader.rb b/app/uploaders/procedure_logo_uploader.rb new file mode 100644 index 000000000..70787d524 --- /dev/null +++ b/app/uploaders/procedure_logo_uploader.rb @@ -0,0 +1,51 @@ +# encoding: utf-8 + +class ProcedureLogoUploader < CarrierWave::Uploader::Base + + # Include RMagick or MiniMagick support: + # include CarrierWave::RMagick + # include CarrierWave::MiniMagick + + # Choose what kind of storage to use for this uploader: + storage :file + # storage :fog + + # Override the directory where uploaded files will be stored. + # This is a sensible default for uploaders that are meant to be mounted: + def store_dir + "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" + end + + # Provide a default URL as a default if there hasn't been a file uploaded: + # def default_url + # # For Rails 3.1+ asset pipeline compatibility: + # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) + # + # "/images/fallback/" + [version_name, "default.png"].compact.join('_') + # end + + # Process files as they are uploaded: + # process :scale => [200, 300] + # + # def scale(width, height) + # # do something + # end + + # Create different versions of your uploaded files: + # version :thumb do + # process :resize_to_fit => [50, 50] + # end + + # Add a white list of extensions which are allowed to be uploaded. + # For images you might use something like this: + def extension_white_list + %w(jpg jpeg png) + end + + # Override the filename of the uploaded files: + # Avoid using model.id or version_name here, see uploader/store.rb for details. + # def filename + # "something.jpg" if original_filename + # end + +end diff --git a/app/views/admin/procedures/_informations.html.haml b/app/views/admin/procedures/_informations.html.haml index 4015f52ef..142169ce4 100644 --- a/app/views/admin/procedures/_informations.html.haml +++ b/app/views/admin/procedures/_informations.html.haml @@ -7,12 +7,21 @@ =value =f.text_field key, class: 'form-control', placeholder: value -%h4 Drapeau européen -%label - =f.check_box :euro_flag - Afficher le drapeau européen +.row + .col-md-6.col-lg-6 + %h4 Logo de la procédure + = f.file_field(:logo, accept: 'image/png, image/jpg,image/jpeg') + + %div{style:'margin-top:5px'} + %i + Fichier accepté : JPG / JPEG / PNG + .col-md-6.col-lg-6 + %h4 Drapeau européen + %label + =f.check_box :euro_flag + Afficher le drapeau européen + -%br %br %h4 Cartographie diff --git a/app/views/admin/procedures/edit.html.haml b/app/views/admin/procedures/edit.html.haml index d24839ca9..9d3dd28e7 100644 --- a/app/views/admin/procedures/edit.html.haml +++ b/app/views/admin/procedures/edit.html.haml @@ -13,7 +13,7 @@ = link_to('Pièces justificatives', admin_procedure_pieces_justificatives_path(@procedure)) #procedure_new.section.section-label - = form_for @procedure, url: url_for({controller: 'admin/procedures', action: :update, id: @procedure.id}) do |f| + = form_for @procedure, url: url_for({controller: 'admin/procedures', action: :update, id: @procedure.id}), multipart: true do |f| = render partial: 'informations', locals: {f: f} = f.submit 'Editer', class: 'btn btn-success', style: 'float:right' %br diff --git a/app/views/admin/procedures/new.html.haml b/app/views/admin/procedures/new.html.haml index 43e508820..123a1b16c 100644 --- a/app/views/admin/procedures/new.html.haml +++ b/app/views/admin/procedures/new.html.haml @@ -1,6 +1,6 @@ %h2 Nouvelle procédure #procedure_new.section.section-label - = form_for @procedure, url: {controller: 'admin/procedures', action: :create} do |f| + = form_for @procedure, url: {controller: 'admin/procedures', action: :create}, multipart: true do |f| =render partial: 'informations', locals: {f: f} =f.submit 'Valider', class: 'btn btn-info', style: 'float:right' diff --git a/app/views/users/dossiers/new.html.haml b/app/views/users/dossiers/new.html.haml index 565f911f9..fd09267fb 100644 --- a/app/views/users/dossiers/new.html.haml +++ b/app/views/users/dossiers/new.html.haml @@ -9,8 +9,7 @@ =image_tag('drapeau_europe.png') #logo_procedure.flag - =image_tag('logo-tps.png') - + =image_tag( @dossier.procedure.decorate.logo_img ) %h2#titre_procedure.text-info = @dossier.procedure.libelle diff --git a/db/migrate/20151210150958_add_logo_to_procedure.rb b/db/migrate/20151210150958_add_logo_to_procedure.rb new file mode 100644 index 000000000..e7bd6c7e9 --- /dev/null +++ b/db/migrate/20151210150958_add_logo_to_procedure.rb @@ -0,0 +1,5 @@ +class AddLogoToProcedure < ActiveRecord::Migration + def change + add_column :procedures, :logo, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index cd3c6d411..4f491cc4b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151210134135) do +ActiveRecord::Schema.define(version: 20151210150958) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -161,6 +161,7 @@ ActiveRecord::Schema.define(version: 20151210134135) do t.integer "administrateur_id" t.boolean "archived", default: false t.boolean "euro_flag", default: false + t.string "logo" end create_table "quartier_prioritaires", force: :cascade do |t| diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index d36404745..506f3de36 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -15,6 +15,8 @@ describe Procedure do it { is_expected.to have_db_column(:organisation) } it { is_expected.to have_db_column(:direction) } it { is_expected.to have_db_column(:test) } + it { is_expected.to have_db_column(:euro_flag) } + it { is_expected.to have_db_column(:logo) } end describe 'validation' do diff --git a/spec/views/users/dossiers/new_html.haml_spec.rb b/spec/views/users/dossiers/new_html.haml_spec.rb index 5487ad4da..c48b0f034 100644 --- a/spec/views/users/dossiers/new_html.haml_spec.rb +++ b/spec/views/users/dossiers/new_html.haml_spec.rb @@ -3,29 +3,47 @@ require 'spec_helper' describe 'users/dossiers/new.html.haml', type: :view do let(:user) { create(:user) } let(:euro_flag) { false } - let(:procedure) { create(:procedure, euro_flag: euro_flag) } - let!(:dossier) { create(:dossier, procedure: procedure, user: user,).decorate } + let(:logo) { '' } + let(:procedure) { create(:procedure, euro_flag: euro_flag, logo: logo) } + let!(:dossier) { create(:dossier, procedure: procedure, user: user).decorate } + + before do + sign_in user + + assign(:dossier, dossier.decorate) + render + end + + subject { rendered } + + it { is_expected.to have_css('#users_siret_index') } describe 'euro flag' do - before do - sign_in user - - assign(:dossier, dossier.decorate) - render - end - - subject { rendered } - - it { is_expected.to have_css('#users_siret_index') } - context 'euro flag is not present' do it { is_expected.not_to have_css('#euro_flag.flag') } end context 'euro flag is present' do let(:euro_flag) { true } - it { is_expected.to have_css('#euro_flag.flag') } end end + + describe 'logo procedure' do + context 'procedure have no logo' do + it 'MPS logo is present' do + is_expected.to have_css("img[src='/assets#{asset_path('logo-tps.png')}']") + end + end + + context 'procedure have logo' do + let(:logo) { fixture_file_upload('app/assets/images/logo_FC_02.png', 'image/png') } + + it 'Procedure logo is present' do + p subject + + is_expected.to have_css("img[src='#{procedure.logo}']") + end + end + end end \ No newline at end of file