summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzul <azul@leap.se>2014-05-20 13:50:32 +0200
committerAzul <azul@leap.se>2014-05-26 12:59:26 +0200
commit560eb039f4778257559395583e1233d052d44127 (patch)
tree400ff92e4e4d64a58a57794170923cd83a696f26
parenta337088f4d6d12d1ea26f494f4ca078cff4b4070 (diff)
flash_for with_errors option displays error messages
-rw-r--r--app/controllers/controller_extension/flash.rb18
-rw-r--r--engines/support/app/controllers/tickets_controller.rb2
-rw-r--r--engines/support/app/views/tickets/edit.html.haml1
3 files changed, 16 insertions, 5 deletions
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] += "<br/>"
+ flash[:error] += resource.errors.full_messages.join(". <br/>")
+ end
end
diff --git a/engines/support/app/controllers/tickets_controller.rb b/engines/support/app/controllers/tickets_controller.rb
index 7b6a7a0..9c1a741 100644
--- a/engines/support/app/controllers/tickets_controller.rb
+++ b/engines/support/app/controllers/tickets_controller.rb
@@ -62,7 +62,7 @@ class TicketsController < ApplicationController
@ticket.comments.last.private = false unless admin?
end
- flash_for @ticket
+ flash_for @ticket, with_errors: true
@ticket.save
respond_with @ticket, location: redirection_path
end
diff --git a/engines/support/app/views/tickets/edit.html.haml b/engines/support/app/views/tickets/edit.html.haml
index 99afa2a..03bda7d 100644
--- a/engines/support/app/views/tickets/edit.html.haml
+++ b/engines/support/app/views/tickets/edit.html.haml
@@ -1,4 +1,5 @@
- @show_navigation = params[:user_id].present?
+- @comment = TicketComment.new
.ticket
= render 'tickets/edit_form'