lib/rodauth/rails/feature.rb in rodauth-rails-0.10.0 vs lib/rodauth/rails/feature.rb in rodauth-rails-0.11.0

- old
+ new

@@ -61,28 +61,37 @@ def button(*) super.html_safe end + delegate :rails_routes, :rails_request, to: :scope + private # Runs controller callbacks and rescue handlers around Rodauth actions. def _around_rodauth(&block) result = nil - rails_controller_rescue do - rails_controller_callbacks do - result = catch(:halt) { super(&block) } + rails_instrument_request do + rails_controller_rescue do + rails_controller_callbacks do + result = catch(:halt) { super(&block) } + end end + + result = handle_rails_controller_response(result) end + throw :halt, result if result + end + + # Handles controller rendering a response or setting response headers. + def handle_rails_controller_response(result) if rails_controller_instance.performed? rails_controller_response elsif result result[1].merge!(rails_controller_instance.response.headers) - throw :halt, result - else result end end # Runs any #(before|around|after)_action controller callbacks. @@ -107,19 +116,33 @@ unless rails_controller_instance.performed? raise Rodauth::Rails::Error, "rescue_from handler didn't write any response" end end + def rails_instrument_request + ActiveSupport::Notifications.instrument("start_processing.rodauth", rodauth: self) + ActiveSupport::Notifications.instrument("process_request.rodauth", rodauth: self) do |payload| + begin + status, headers, body = yield + payload[:status] = status || 404 + payload[:headers] = headers + payload[:body] = body + ensure + rails_controller_instance.send(:append_info_to_payload, payload) + end + end + end + # Returns Roda response from controller response if set. def rails_controller_response controller_response = rails_controller_instance.response response.status = controller_response.status response.headers.merge! controller_response.headers response.write controller_response.body - request.halt + response.finish end # Create emails with ActionMailer which uses configured delivery method. def create_email_to(to, subject, body) Mailer.create_email(to: to, from: email_from, subject: "#{email_subject_prefix}#{subject}", body: body) @@ -166,14 +189,11 @@ rails_controller_instance.allow_forgery_protection = value.call end # Instances of the configured controller with current request's env hash. def _rails_controller_instance - controller = rails_controller.new - rails_request = ActionDispatch::Request.new(scope.env) - + controller = rails_controller.new prepare_rails_controller(controller, rails_request) - controller end if ActionPack.version >= Gem::Version.new("5.0") def prepare_rails_controller(controller, rails_request)