summaryrefslogtreecommitdiff
path: root/app/controllers/controller_extension
diff options
context:
space:
mode:
authorazul <azul@leap.se>2014-05-30 18:02:20 +0200
committerazul <azul@leap.se>2014-05-30 18:02:20 +0200
commit9f04e4c8e50f1dc5a7ff6f2e58974254731a6bc4 (patch)
tree84db71a6e52c47bc2f2f1aabce578ce367fcb0e8 /app/controllers/controller_extension
parent568a5c243f0a0ef90807c96b19643ec341994bbb (diff)
parent9e3be686ff2751707369894382293924420830d0 (diff)
Merge pull request #167 from azul/feature/i18n-for-ticket-system0.5.2-rc
Feature/i18n for ticket system
Diffstat (limited to 'app/controllers/controller_extension')
-rw-r--r--app/controllers/controller_extension/flash.rb43
1 files changed, 43 insertions, 0 deletions
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