summaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/application_controller.rb10
-rw-r--r--app/controllers/controller_extension/flash.rb43
2 files changed, 43 insertions, 10 deletions
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
##
diff --git a/app/controllers/controller_extension/flash.rb b/app/controllers/controller_extension/flash.rb
new file mode 100644
index 0000000..1642141
--- /dev/null
+++ b/app/controllers/controller_extension/flash.rb
@@ -0,0 +1,43 @@
+module ControllerExtension::Flash
+ extend ActiveSupport::Concern
+
+ protected
+
+ 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_for(resource)
+ if message.present?
+ flash[type] = message
+ 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_for(resource)
+ namespace.join(".")
+ end
+
+ def flash_type_for(resource)
+ resource.valid? ? :success : :error
+ end
+
+ def add_flash_errors_for(resource)
+ return if resource.valid?
+ flash[:error] += "<br>"
+ flash[:error] += resource.errors.full_messages.join(". <br>")
+ end
+end