summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/controller_extension/flash.rb33
-rw-r--r--config/initializers/add_controller_methods.rb1
-rw-r--r--engines/support/app/controllers/tickets_controller.rb8
3 files changed, 37 insertions, 5 deletions
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
diff --git a/config/initializers/add_controller_methods.rb b/config/initializers/add_controller_methods.rb
index f572ecb..03e8393 100644
--- a/config/initializers/add_controller_methods.rb
+++ b/config/initializers/add_controller_methods.rb
@@ -1,4 +1,5 @@
ActiveSupport.on_load(:application_controller) do
include ControllerExtension::Authentication
include ControllerExtension::TokenAuthentication
+ include ControllerExtension::Flash
end
diff --git a/engines/support/app/controllers/tickets_controller.rb b/engines/support/app/controllers/tickets_controller.rb
index bb98277..7b6a7a0 100644
--- a/engines/support/app/controllers/tickets_controller.rb
+++ b/engines/support/app/controllers/tickets_controller.rb
@@ -23,10 +23,11 @@ class TicketsController < ApplicationController
@ticket.comments.last.posted_by = current_user.id
@ticket.comments.last.private = false unless admin?
@ticket.created_by = current_user.id
+ flash_for @ticket
if @ticket.save && !logged_in?
flash[:success] = t(:access_ticket_text, :full_url => ticket_url(@ticket.id))
end
- respond_with(@ticket, :location => auto_ticket_path(@ticket))
+ respond_with @ticket, :location => auto_ticket_path(@ticket)
end
def show
@@ -61,6 +62,7 @@ class TicketsController < ApplicationController
@ticket.comments.last.private = false unless admin?
end
+ flash_for @ticket
@ticket.save
respond_with @ticket, location: redirection_path
end
@@ -82,10 +84,6 @@ class TicketsController < ApplicationController
@title = t(:tickets)
end
- def self.responder
- Responders::FlashResponder
- end
-
private
#