From c10f9311678ff2183443bc03e153b30d3b68ff74 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 20 May 2014 13:09:59 +0200 Subject: Controller#flash_for instead of FlashResponder FlashResponder added a flash before responding. However at the point of responding objects have already been saved. So there is no way to test if they were changed. Now instead we can call flash_for resource before resource.save and it will add the flash messages only if the resource was actually changed. --- app/controllers/controller_extension/flash.rb | 33 +++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 app/controllers/controller_extension/flash.rb (limited to 'app/controllers') diff --git a/app/controllers/controller_extension/flash.rb b/app/controllers/controller_extension/flash.rb new file mode 100644 index 0000000..6a62351 --- /dev/null +++ b/app/controllers/controller_extension/flash.rb @@ -0,0 +1,33 @@ +module ControllerExtension::Flash + extend ActiveSupport::Concern + + protected + + def flash_for(resource, options = {}) + return unless resource.changed? + message = flash_message_for(resource) + type = flash_type(resource) + if message.present? + flash[type] = [message, flash[type]].join(' ') + end + end + + def flash_message_for(resource) + I18n.t flash_i18n_key(resource), + scope: :flash, + cascade: true, + resource: resource.class.model_name.human + end + + def flash_i18n_key(resource) + namespace = [action_name] + namespace += controller_path.split('/') + namespace << flash_type(resource) + namespace.join(".") + end + + def flash_type(resource) + resource.valid? ? :success : :error + end + +end -- cgit v1.2.3 From a337088f4d6d12d1ea26f494f4ca078cff4b4070 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 20 May 2014 13:20:25 +0200 Subject: remove unused bold helper and instead sanitize flash --- app/controllers/application_controller.rb | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 35d6cb4..a4560e2 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -23,16 +23,6 @@ class ApplicationController < ActionController::Base json: {error: "The server failed to process your request. We'll look into it."} end - # - # Allows us to pass through bold text to flash messages. See format_flash() for where this is reversed. - # - # TODO: move to core - # - def bold(str) - "[b]#{str}[/b]" - end - helper_method :bold - ## ## LOCALE ## -- cgit v1.2.3 From 560eb039f4778257559395583e1233d052d44127 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 20 May 2014 13:50:32 +0200 Subject: flash_for with_errors option displays error messages --- app/controllers/controller_extension/flash.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/controller_extension/flash.rb b/app/controllers/controller_extension/flash.rb index 6a62351..8bc9ee7 100644 --- a/app/controllers/controller_extension/flash.rb +++ b/app/controllers/controller_extension/flash.rb @@ -5,10 +5,15 @@ module ControllerExtension::Flash def flash_for(resource, options = {}) return unless resource.changed? + add_flash_message_for resource + add_flash_errors_for resource if options[:with_errors] + end + + def add_flash_message_for(resource) message = flash_message_for(resource) - type = flash_type(resource) + type = flash_type_for(resource) if message.present? - flash[type] = [message, flash[type]].join(' ') + flash[type] = message end end @@ -22,12 +27,17 @@ module ControllerExtension::Flash def flash_i18n_key(resource) namespace = [action_name] namespace += controller_path.split('/') - namespace << flash_type(resource) + namespace << flash_type_for(resource) namespace.join(".") end - def flash_type(resource) + def flash_type_for(resource) resource.valid? ? :success : :error end + def add_flash_errors_for(resource) + return if resource.valid? + flash[:error] += "
" + flash[:error] += resource.errors.full_messages.join(".
") + end end -- cgit v1.2.3 From ab49a72b52575f3b9fdf13fee47e99dfb82e2a3d Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 29 May 2014 14:57:23 +0200 Subject: html5:
instead of
--- app/controllers/controller_extension/flash.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/controllers') diff --git a/app/controllers/controller_extension/flash.rb b/app/controllers/controller_extension/flash.rb index 8bc9ee7..1642141 100644 --- a/app/controllers/controller_extension/flash.rb +++ b/app/controllers/controller_extension/flash.rb @@ -37,7 +37,7 @@ module ControllerExtension::Flash def add_flash_errors_for(resource) return if resource.valid? - flash[:error] += "
" - flash[:error] += resource.errors.full_messages.join(".
") + flash[:error] += "
" + flash[:error] += resource.errors.full_messages.join(".
") end end -- cgit v1.2.3