1+ # frozen_string_literal: true
2+
3+ module Valued ::Rails
4+ class ProcessAction
5+ # Called by ActiveSupport::Notifications when a process_action.action_controller event is published.
6+ # @api private
7+ def self . call ( name , start , finish , id , payload )
8+ return unless name == "process_action.action_controller"
9+ Valued ::Rails . process_action ( new ( start : start , finish : finish , **payload ) )
10+ end
11+
12+ # @return [Time] the time the controller action started
13+ attr_reader :start
14+
15+ # @return [Time] the time the controller action finished
16+ attr_reader :finish
17+
18+ # @return [String] the controller class name
19+ attr_reader :controller
20+
21+ # @return [String] the controller action name
22+ attr_reader :action
23+
24+ # @return [Hash] the controller action parameters without any filtered parameter
25+ attr_reader :params
26+
27+ # @return [ActionDispatch::Http::Headers] the request headers
28+ attr_reader :headers
29+
30+ # @return [Symbol] the response format (:html, :json, :xml, etc.)
31+ attr_reader :format
32+
33+ # @return [String] the controller action method ("GET", "POST", etc.)
34+ attr_reader :method
35+
36+ # @return [String] the controller action path
37+ attr_reader :path
38+
39+ # @return [ActionDispatch::Request] the request object
40+ attr_reader :request
41+
42+ # @return [ActionDispatch::Response] the response object
43+ attr_reader :response
44+
45+ # @return [Integer] the response status code
46+ attr_reader :status
47+
48+ # @api private
49+ def initialize ( start :, finish :, controller :, action :, params :, headers :, format :, method :, path :, request :, response :, status :)
50+ @start = start
51+ @finish = finish
52+ @controller = controller
53+ @action = action
54+ @params = params
55+ @headers = headers
56+ @format = format
57+ @method = method
58+ @path = path
59+ @request = request
60+ @response = response
61+ @status = status
62+ end
63+
64+ # @return [ActionDispatch::Session::AbstractSecureStore] the session object
65+ def session = request . session
66+
67+ # @return [Warden::Proxy, nil] the warden object if Warden/Devise is set up
68+ def warden = request . env [ 'warden' ]
69+
70+ # @return [Object, nil] the current user if Warden/Devise is set up and the user is authenticated
71+ # @todo Needs to be overridable with custom logic.
72+ def user = @user ||= find_user
73+
74+ # @return [true, false] whether {#user} is set
75+ def user? = !!user
76+
77+ # @return [Object, nil] Currently active customer if available.
78+ # @todo Needs to be overridable with custom logic.
79+ def customer = @customer ||= find_customer
80+
81+ private
82+
83+ def find_user
84+ return unless warden
85+ warden . user ( :user ) || warden . user
86+ end
87+
88+ def find_customer
89+ return ActsAsTenant . current_tenant if defined? ActsAsTenant and ActsAsTenant . current_tenant
90+ return unless user
91+ return user . customer if user . respond_to? :customer
92+ return user . organization if user . respond_to? :organization
93+ return user . account if user . respond_to? :account
94+ end
95+ end
96+ end
0 commit comments