From 2bc5ee88f789bdbb29ff17ba0ca8eb3145595852 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Tue, 24 Jan 2017 14:36:43 +0100 Subject: [PATCH] Change controller name according to model name, add simple_form --- .gitignore | 1 + Gemfile | 2 + Gemfile.lock | 4 + ...roller.rb => mail_templates_controller.rb} | 8 +- app/decorators/mail_template_decorator.rb | 2 +- app/views/admin/mail_templates/edit.html.haml | 30 ++++ .../admin/mail_templates/index.html.haml | 15 ++ app/views/admin/mails/edit.html.haml | 27 --- app/views/admin/mails/index.html.haml | 13 -- ...in_mailtemplatescontroller_edit.html.haml} | 0 ...in_mailtemplatescontroller_index.html.haml | 1 + ...dmin_procedurescontroller_navbar.html.haml | 2 +- config/initializers/simple_form.rb | 169 ++++++++++++++++++ config/initializers/simple_form_bootstrap.rb | 154 ++++++++++++++++ config/locales/simple_form.en.yml | 31 ++++ config/locales/simple_form.fr.yml | 31 ++++ config/routes.rb | 2 +- lib/templates/haml/scaffold/_form.html.haml | 10 ++ .../admin/mail_templates_controller_spec.rb | 51 ++++++ .../admin/mails_controller_spec.rb | 46 ----- 20 files changed, 506 insertions(+), 93 deletions(-) rename app/controllers/admin/{mails_controller.rb => mail_templates_controller.rb} (58%) create mode 100644 app/views/admin/mail_templates/edit.html.haml create mode 100644 app/views/admin/mail_templates/index.html.haml delete mode 100644 app/views/admin/mails/edit.html.haml delete mode 100644 app/views/admin/mails/index.html.haml rename app/views/layouts/left_panels/{_left_panel_admin_mailscontroller_index.html.haml => _left_panel_admin_mailtemplatescontroller_edit.html.haml} (100%) create mode 100644 app/views/layouts/left_panels/_left_panel_admin_mailtemplatescontroller_index.html.haml create mode 100644 config/initializers/simple_form.rb create mode 100644 config/initializers/simple_form_bootstrap.rb create mode 100644 config/locales/simple_form.en.yml create mode 100644 config/locales/simple_form.fr.yml create mode 100644 lib/templates/haml/scaffold/_form.html.haml create mode 100644 spec/controllers/admin/mail_templates_controller_spec.rb delete mode 100644 spec/controllers/admin/mails_controller_spec.rb diff --git a/.gitignore b/.gitignore index 298f5f1bc..d7587c6c6 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ config/fog_credentials.yml uploads/* coverage/**/* .DS_Store +.byebug_history diff --git a/Gemfile b/Gemfile index ac6c7a849..e38f8d656 100644 --- a/Gemfile +++ b/Gemfile @@ -97,6 +97,8 @@ gem 'openstack' gem 'browser' +gem 'simple_form' + group :test do gem 'capybara' gem 'launchy' diff --git a/Gemfile.lock b/Gemfile.lock index b30c53477..3cbdc6776 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -540,6 +540,9 @@ GEM shellany (0.0.1) shoulda-matchers (3.1.1) activesupport (>= 4.0.0) + simple_form (3.4.0) + actionpack (> 4, < 5.1) + activemodel (> 4, < 5.1) simplecov (0.12.0) docile (~> 1.1.0) json (>= 1.8, < 3) @@ -688,6 +691,7 @@ DEPENDENCIES selenium-webdriver sentry-raven shoulda-matchers + simple_form simplecov smart_listing spreadsheet_architect diff --git a/app/controllers/admin/mails_controller.rb b/app/controllers/admin/mail_templates_controller.rb similarity index 58% rename from app/controllers/admin/mails_controller.rb rename to app/controllers/admin/mail_templates_controller.rb index cd1627371..aa158e48f 100644 --- a/app/controllers/admin/mails_controller.rb +++ b/app/controllers/admin/mail_templates_controller.rb @@ -1,4 +1,4 @@ -class Admin::MailsController < AdminController +class Admin::MailTemplatesController < AdminController before_action :retrieve_procedure def index @@ -10,10 +10,10 @@ class Admin::MailsController < AdminController end def update - mail = @procedure.mail_templates.find(params[:id]) - mail.update_attributes(update_params) + mail_template = @procedure.mail_templates.find(params[:id]) + mail_template.update_attributes(update_params) - redirect_to admin_procedure_mails_path + redirect_to admin_procedure_mail_templates_path end private diff --git a/app/decorators/mail_template_decorator.rb b/app/decorators/mail_template_decorator.rb index b6f1e64dc..b9c153553 100644 --- a/app/decorators/mail_template_decorator.rb +++ b/app/decorators/mail_template_decorator.rb @@ -4,7 +4,7 @@ class MailTemplateDecorator < Draper::Decorator def name case object.type when "MailReceived" - "Email d'accusé de réception" + "E-mail d'accusé de réception" else object.type end diff --git a/app/views/admin/mail_templates/edit.html.haml b/app/views/admin/mail_templates/edit.html.haml new file mode 100644 index 000000000..08110877f --- /dev/null +++ b/app/views/admin/mail_templates/edit.html.haml @@ -0,0 +1,30 @@ +.white-back + %h3 + = @mail_template.decorate.name + + = simple_form_for @mail_template, url: admin_procedure_mail_template_path(@procedure, @mail_template) do |f| + .row + .col-md-6 + = f.input :object, label: "Objet de l'email" + = f.input :body, label: "Corps de l'email", input_html: {class: 'wysihtml5'} + .text-right + = link_to "Annuler", admin_procedure_mail_templates_path(@procedure), class: "btn btn-default" + = f.button :submit, 'Mettre à jour', class: "btn-success" + + .row + .col-md-6 + %table.table + %tr + %th + Balise + %th + Description + - MailTemplate.tags.each do |balise| + %tr + %td.center + %b.text-success + \-- + = balise.first + \-- + %td + =balise.second[:description] diff --git a/app/views/admin/mail_templates/index.html.haml b/app/views/admin/mail_templates/index.html.haml new file mode 100644 index 000000000..8f7df9420 --- /dev/null +++ b/app/views/admin/mail_templates/index.html.haml @@ -0,0 +1,15 @@ +.row.white-back + %h1 E-mails personnalisables + + .row + .col-md-6 + %table.table + %tr + %th{ colspan: 2 } + Type d'email + - @procedure.mail_templates.each do |mt| + %tr + %td + = mt.decorate.name + %td.text-right + = link_to "Personnaliser l'e-mail", edit_admin_procedure_mail_template_path(@procedure, mt) diff --git a/app/views/admin/mails/edit.html.haml b/app/views/admin/mails/edit.html.haml deleted file mode 100644 index 5456994e4..000000000 --- a/app/views/admin/mails/edit.html.haml +++ /dev/null @@ -1,27 +0,0 @@ -.white-back - %h3 - E-mail d'accusé de réception - = @mail_template.type - - = form_for @mail_template, url: {controller: 'admin/mails', action: 'update', id: @procedure.mail_received.id} do |f| - =f.text_field :object, {class:'form-control', style:'width: 40%'} - %br - =f.text_area :body, {class: 'form-control wysihtml5'} - %br - =f.submit 'Mettre à jour', {class:'btn btn-success', style:'float: right'} - - %table.table{style:'width: 50%'} - %tr - %th - Balise - %th - Description - - MailTemplate.tags.each do |balise| - %tr - %td.center - %b.text-success - \-- - = balise.first - \-- - %td - =balise.second[:description] diff --git a/app/views/admin/mails/index.html.haml b/app/views/admin/mails/index.html.haml deleted file mode 100644 index 7f2f7a222..000000000 --- a/app/views/admin/mails/index.html.haml +++ /dev/null @@ -1,13 +0,0 @@ -.row.white-back - %h1 Emails personnalisables - - %table.table - %tr - %th{ colspan: 2 } - Type d'email - - @procedure.mail_templates.each do |mt| - %tr - %td - = mt.type - %td.text-right - = link_to "Personnaliser l'email", edit_admin_procedure_mail_path(@procedure, mt) diff --git a/app/views/layouts/left_panels/_left_panel_admin_mailscontroller_index.html.haml b/app/views/layouts/left_panels/_left_panel_admin_mailtemplatescontroller_edit.html.haml similarity index 100% rename from app/views/layouts/left_panels/_left_panel_admin_mailscontroller_index.html.haml rename to app/views/layouts/left_panels/_left_panel_admin_mailtemplatescontroller_edit.html.haml diff --git a/app/views/layouts/left_panels/_left_panel_admin_mailtemplatescontroller_index.html.haml b/app/views/layouts/left_panels/_left_panel_admin_mailtemplatescontroller_index.html.haml new file mode 100644 index 000000000..1bdf2e3b1 --- /dev/null +++ b/app/views/layouts/left_panels/_left_panel_admin_mailtemplatescontroller_index.html.haml @@ -0,0 +1 @@ += render partial: 'layouts/left_panels/left_panel_admin_procedurescontroller_navbar', locals: {active: 'E-mails'} diff --git a/app/views/layouts/left_panels/_left_panel_admin_procedurescontroller_navbar.html.haml b/app/views/layouts/left_panels/_left_panel_admin_procedurescontroller_navbar.html.haml index 01774dbe0..66ab55a3f 100644 --- a/app/views/layouts/left_panels/_left_panel_admin_procedurescontroller_navbar.html.haml +++ b/app/views/layouts/left_panels/_left_panel_admin_procedurescontroller_navbar.html.haml @@ -39,7 +39,7 @@ %div.procedure_list_element{class: ('active' if active == 'Champs privés')} Champs privés - %a{:href => "#{url_for admin_procedure_mails_path(@procedure)}", id: 'onglet-inemailsfos'} + %a{:href => "#{url_for admin_procedure_mail_templates_path(@procedure)}", id: 'onglet-inemailsfos'} %div.procedure_list_element{class: ('active' if active == 'E-mails')} E-mails diff --git a/config/initializers/simple_form.rb b/config/initializers/simple_form.rb new file mode 100644 index 000000000..f306c195a --- /dev/null +++ b/config/initializers/simple_form.rb @@ -0,0 +1,169 @@ +# Use this setup block to configure all options available in SimpleForm. +SimpleForm.setup do |config| + # Wrappers are used by the form builder to generate a + # complete input. You can remove any component from the + # wrapper, change the order or even add your own to the + # stack. The options given below are used to wrap the + # whole input. + config.wrappers :default, class: :input, + hint_class: :field_with_hint, error_class: :field_with_errors do |b| + ## Extensions enabled by default + # Any of these extensions can be disabled for a + # given input by passing: `f.input EXTENSION_NAME => false`. + # You can make any of these extensions optional by + # renaming `b.use` to `b.optional`. + + # Determines whether to use HTML5 (:email, :url, ...) + # and required attributes + b.use :html5 + + # Calculates placeholders automatically from I18n + # You can also pass a string as f.input placeholder: "Placeholder" + b.use :placeholder + + ## Optional extensions + # They are disabled unless you pass `f.input EXTENSION_NAME => true` + # to the input. If so, they will retrieve the values from the model + # if any exists. If you want to enable any of those + # extensions by default, you can change `b.optional` to `b.use`. + + # Calculates maxlength from length validations for string inputs + # and/or database column lengths + b.optional :maxlength + + # Calculate minlength from length validations for string inputs + b.optional :minlength + + # Calculates pattern from format validations for string inputs + b.optional :pattern + + # Calculates min and max from length validations for numeric inputs + b.optional :min_max + + # Calculates readonly automatically from readonly attributes + b.optional :readonly + + ## Inputs + b.use :label_input + b.use :hint, wrap_with: { tag: :span, class: :hint } + b.use :error, wrap_with: { tag: :span, class: :error } + + ## full_messages_for + # If you want to display the full error message for the attribute, you can + # use the component :full_error, like: + # + # b.use :full_error, wrap_with: { tag: :span, class: :error } + end + + # The default wrapper to be used by the FormBuilder. + config.default_wrapper = :default + + # Define the way to render check boxes / radio buttons with labels. + # Defaults to :nested for bootstrap config. + # inline: input + label + # nested: label > input + config.boolean_style = :nested + + # Default class for buttons + config.button_class = 'btn' + + # Method used to tidy up errors. Specify any Rails Array method. + # :first lists the first message for each field. + # Use :to_sentence to list all errors for each field. + # config.error_method = :first + + # Default tag used for error notification helper. + config.error_notification_tag = :div + + # CSS class to add for error notification helper. + config.error_notification_class = 'error_notification' + + # ID to add for error notification helper. + # config.error_notification_id = nil + + # Series of attempts to detect a default label method for collection. + # config.collection_label_methods = [ :to_label, :name, :title, :to_s ] + + # Series of attempts to detect a default value method for collection. + # config.collection_value_methods = [ :id, :to_s ] + + # You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none. + # config.collection_wrapper_tag = nil + + # You can define the class to use on all collection wrappers. Defaulting to none. + # config.collection_wrapper_class = nil + + # You can wrap each item in a collection of radio/check boxes with a tag, + # defaulting to :span. + # config.item_wrapper_tag = :span + + # You can define a class to use in all item wrappers. Defaulting to none. + # config.item_wrapper_class = nil + + # How the label text should be generated altogether with the required text. + # config.label_text = lambda { |label, required, explicit_label| "#{required} #{label}" } + + # You can define the class to use on all labels. Default is nil. + # config.label_class = nil + + # You can define the default class to be used on forms. Can be overriden + # with `html: { :class }`. Defaulting to none. + # config.default_form_class = nil + + # You can define which elements should obtain additional classes + # config.generate_additional_classes_for = [:wrapper, :label, :input] + + # Whether attributes are required by default (or not). Default is true. + # config.required_by_default = true + + # Tell browsers whether to use the native HTML5 validations (novalidate form option). + # These validations are enabled in SimpleForm's internal config but disabled by default + # in this configuration, which is recommended due to some quirks from different browsers. + # To stop SimpleForm from generating the novalidate option, enabling the HTML5 validations, + # change this configuration to true. + config.browser_validations = false + + # Collection of methods to detect if a file type was given. + # config.file_methods = [ :mounted_as, :file?, :public_filename ] + + # Custom mappings for input types. This should be a hash containing a regexp + # to match as key, and the input type that will be used when the field name + # matches the regexp as value. + # config.input_mappings = { /count/ => :integer } + + # Custom wrappers for input types. This should be a hash containing an input + # type as key and the wrapper that will be used for all inputs with specified type. + # config.wrapper_mappings = { string: :prepend } + + # Namespaces where SimpleForm should look for custom input classes that + # override default inputs. + # config.custom_inputs_namespaces << "CustomInputs" + + # Default priority for time_zone inputs. + # config.time_zone_priority = nil + + # Default priority for country inputs. + # config.country_priority = nil + + # When false, do not use translations for labels. + # config.translate_labels = true + + # Automatically discover new inputs in Rails' autoload path. + # config.inputs_discovery = true + + # Cache SimpleForm inputs discovery + # config.cache_discovery = !Rails.env.development? + + # Default class for inputs + # config.input_class = nil + + # Define the default class of the input wrapper of the boolean input. + config.boolean_label_class = 'checkbox' + + # Defines if the default input wrapper class should be included in radio + # collection wrappers. + # config.include_default_input_wrapper_class = true + + # Defines which i18n scope will be used in Simple Form. + # config.i18n_scope = 'simple_form' +end diff --git a/config/initializers/simple_form_bootstrap.rb b/config/initializers/simple_form_bootstrap.rb new file mode 100644 index 000000000..c7705e8e7 --- /dev/null +++ b/config/initializers/simple_form_bootstrap.rb @@ -0,0 +1,154 @@ +# Use this setup block to configure all options available in SimpleForm. +SimpleForm.setup do |config| + config.error_notification_class = 'alert alert-danger' + config.button_class = 'btn btn-default' + config.boolean_label_class = nil + + config.wrappers :vertical_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + b.use :html5 + b.use :placeholder + b.optional :maxlength + b.optional :minlength + b.optional :pattern + b.optional :min_max + b.optional :readonly + b.use :label, class: 'control-label' + + b.use :input, class: 'form-control' + b.use :error, wrap_with: { tag: 'span', class: 'help-block' } + b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + + config.wrappers :vertical_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + b.use :html5 + b.use :placeholder + b.optional :maxlength + b.optional :minlength + b.optional :readonly + b.use :label, class: 'control-label' + + b.use :input + b.use :error, wrap_with: { tag: 'span', class: 'help-block' } + b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + + config.wrappers :vertical_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + b.use :html5 + b.optional :readonly + + b.wrapper tag: 'div', class: 'checkbox' do |ba| + ba.use :label_input + end + + b.use :error, wrap_with: { tag: 'span', class: 'help-block' } + b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + + config.wrappers :vertical_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + b.use :html5 + b.optional :readonly + b.use :label, class: 'control-label' + b.use :input + b.use :error, wrap_with: { tag: 'span', class: 'help-block' } + b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + + config.wrappers :horizontal_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + b.use :html5 + b.use :placeholder + b.optional :maxlength + b.optional :minlength + b.optional :pattern + b.optional :min_max + b.optional :readonly + b.use :label, class: 'col-sm-3 control-label' + + b.wrapper tag: 'div', class: 'col-sm-9' do |ba| + ba.use :input, class: 'form-control' + ba.use :error, wrap_with: { tag: 'span', class: 'help-block' } + ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + end + + config.wrappers :horizontal_file_input, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + b.use :html5 + b.use :placeholder + b.optional :maxlength + b.optional :minlength + b.optional :readonly + b.use :label, class: 'col-sm-3 control-label' + + b.wrapper tag: 'div', class: 'col-sm-9' do |ba| + ba.use :input + ba.use :error, wrap_with: { tag: 'span', class: 'help-block' } + ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + end + + config.wrappers :horizontal_boolean, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + b.use :html5 + b.optional :readonly + + b.wrapper tag: 'div', class: 'col-sm-offset-3 col-sm-9' do |wr| + wr.wrapper tag: 'div', class: 'checkbox' do |ba| + ba.use :label_input + end + + wr.use :error, wrap_with: { tag: 'span', class: 'help-block' } + wr.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + end + + config.wrappers :horizontal_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + b.use :html5 + b.optional :readonly + + b.use :label, class: 'col-sm-3 control-label' + + b.wrapper tag: 'div', class: 'col-sm-9' do |ba| + ba.use :input + ba.use :error, wrap_with: { tag: 'span', class: 'help-block' } + ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + end + + config.wrappers :inline_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + b.use :html5 + b.use :placeholder + b.optional :maxlength + b.optional :minlength + b.optional :pattern + b.optional :min_max + b.optional :readonly + b.use :label, class: 'sr-only' + + b.use :input, class: 'form-control' + b.use :error, wrap_with: { tag: 'span', class: 'help-block' } + b.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + + config.wrappers :multi_select, tag: 'div', class: 'form-group', error_class: 'has-error' do |b| + b.use :html5 + b.optional :readonly + b.use :label, class: 'control-label' + b.wrapper tag: 'div', class: 'form-inline' do |ba| + ba.use :input, class: 'form-control' + ba.use :error, wrap_with: { tag: 'span', class: 'help-block' } + ba.use :hint, wrap_with: { tag: 'p', class: 'help-block' } + end + end + # Wrappers for forms and inputs using the Bootstrap toolkit. + # Check the Bootstrap docs (http://getbootstrap.com) + # to learn about the different styles for forms and inputs, + # buttons and other elements. + config.default_wrapper = :vertical_form + config.wrapper_mappings = { + check_boxes: :vertical_radio_and_checkboxes, + radio_buttons: :vertical_radio_and_checkboxes, + file: :vertical_file_input, + boolean: :vertical_boolean, + datetime: :multi_select, + date: :multi_select, + time: :multi_select + } +end diff --git a/config/locales/simple_form.en.yml b/config/locales/simple_form.en.yml new file mode 100644 index 000000000..237438334 --- /dev/null +++ b/config/locales/simple_form.en.yml @@ -0,0 +1,31 @@ +en: + simple_form: + "yes": 'Yes' + "no": 'No' + required: + text: 'required' + mark: '*' + # You can uncomment the line below if you need to overwrite the whole required html. + # When using html, text and mark won't be used. + # html: '*' + error_notification: + default_message: "Please review the problems below:" + # Examples + # labels: + # defaults: + # password: 'Password' + # user: + # new: + # email: 'E-mail to sign in.' + # edit: + # email: 'E-mail.' + # hints: + # defaults: + # username: 'User name to sign in.' + # password: 'No special characters, please.' + # include_blanks: + # defaults: + # age: 'Rather not say' + # prompts: + # defaults: + # age: 'Select your age' diff --git a/config/locales/simple_form.fr.yml b/config/locales/simple_form.fr.yml new file mode 100644 index 000000000..73dbc13fc --- /dev/null +++ b/config/locales/simple_form.fr.yml @@ -0,0 +1,31 @@ +fr: + simple_form: + "yes": 'Oui' + "no": 'Non' + required: + text: 'obligatoire' + mark: '*' + # You can uncomment the line below if you need to overwrite the whole required html. + # When using html, text and mark won't be used. + # html: '*' + error_notification: + default_message: "Erreur, veuillez vérifier vos réponses:" + # Examples + # labels: + # defaults: + # password: 'Password' + # user: + # new: + # email: 'E-mail to sign in.' + # edit: + # email: 'E-mail.' + # hints: + # defaults: + # username: 'User name to sign in.' + # password: 'No special characters, please.' + # include_blanks: + # defaults: + # age: 'Rather not say' + # prompts: + # defaults: + # age: 'Select your age' diff --git a/config/routes.rb b/config/routes.rb index abe924522..0e38a41ed 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -130,7 +130,7 @@ Rails.application.routes.draw do post '/:index/move_down' => 'pieces_justificatives#move_down', as: :move_down end - resources 'mails' + resources :mail_templates, only: [:index, :edit, :update] put 'archive' => 'procedures#archive', as: :archive put 'publish' => 'procedures#publish', as: :publish diff --git a/lib/templates/haml/scaffold/_form.html.haml b/lib/templates/haml/scaffold/_form.html.haml new file mode 100644 index 000000000..ac3aa7bc1 --- /dev/null +++ b/lib/templates/haml/scaffold/_form.html.haml @@ -0,0 +1,10 @@ += simple_form_for(@<%= singular_table_name %>) do |f| + = f.error_notification + + .form-inputs + <%- attributes.each do |attribute| -%> + = f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> + <%- end -%> + + .form-actions + = f.button :submit diff --git a/spec/controllers/admin/mail_templates_controller_spec.rb b/spec/controllers/admin/mail_templates_controller_spec.rb new file mode 100644 index 000000000..4b02d1cf3 --- /dev/null +++ b/spec/controllers/admin/mail_templates_controller_spec.rb @@ -0,0 +1,51 @@ +require 'spec_helper' + +describe Admin::MailTemplatesController, type: :controller do + let(:mail_template) { create :mail_template, :dossier_received } + let(:procedure) { create :procedure, mail_templates: [mail_template]} + + before do + sign_in procedure.administrateur + end + + describe 'GET index' do + render_views + + subject { get :index, params: {procedure_id: procedure.id} } + + it { expect(subject.status).to eq 200 } + it { expect(subject.body).to include("E-mails personnalisables") } + it { expect(subject.body).to include(*procedure.mail_templates.map{ |mt| mt.decorate.name }) } + end + + describe 'PATCH update' do + let(:object) { 'plop modif' } + let(:body) { 'plip modif' } + + context 'when is mail_received id' do + subject { patch :update, + params: {procedure_id: mail_template.procedure.id, + id: mail_template.id, + mail_received: { + object: object, + body: body + }} } + + it { expect(subject).to redirect_to admin_procedure_mail_templates_path } + + it { + expect { + subject + mail_template.reload + }.to change(mail_template, :object).from("Object, voila voila").to(object) + } + + it { + expect { + subject + mail_template.reload + }.to change(mail_template, :body).from("Blabla ceci est mon body").to(body) + } + end + end +end diff --git a/spec/controllers/admin/mails_controller_spec.rb b/spec/controllers/admin/mails_controller_spec.rb deleted file mode 100644 index ed800a0d6..000000000 --- a/spec/controllers/admin/mails_controller_spec.rb +++ /dev/null @@ -1,46 +0,0 @@ -require 'spec_helper' - -describe Admin::MailsController, type: :controller do - let(:procedure) { create :procedure } - - before do - sign_in procedure.administrateur - end - - describe 'GET index' do - render_views - - subject { get :index, params: {procedure_id: procedure.id} } - - it { expect(subject.status).to eq 200 } - it { expect(subject.body).to include("Emails personnalisables") } - it { expect(subject.body).to include(*procedure.mail_templates.pluck(:type)) } - end - - describe 'PATCH update' do - let(:object) { 'plop modif' } - let(:body) { 'plip modif' } - - context 'when is mail_received id' do - subject { patch :update, - params: {procedure_id: procedure.id, - id: procedure.mail_received.id, - mail_received: { - object: object, - body: body - }} } - - it { expect(subject).to redirect_to admin_procedure_mails_path } - - describe 'values in database for mail received' do - before do - subject - procedure.reload - end - - it { expect(procedure.mail_received.object).to eq object } - it { expect(procedure.mail_received.body).to eq body } - end - end - end -end