Skip to content

Commit 0fb6580

Browse files
committedSep 7, 2022
[valued-rails] start listening to controller events
1 parent 2c72123 commit 0fb6580

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed
 

‎valued-rails/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* Notification based – allows you to keep Valued logic outside of your business logic
2+
* Automatic user detection when using [Devise](https://github.com/heartcombo/devise) or [Warden](https://github.com/wardencommunity/warden)
3+
* Automatic customer detection when using [Acts As Tenant](https://github.com/ErwinM/acts_as_tenant) or for common one-to-many relations between users and customers.

‎valued-rails/lib/valued/rails.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "rails"
2+
require "valued"
3+
4+
module Valued::Rails
5+
require "valued/rails/process_action"
6+
require "valued/rails/railtie"
7+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# frozen_string_literal: true
2+
3+
module Valued::Rails
4+
class Railtie < ::Rails::Railtie
5+
initializer "valued.initialize" do
6+
ActiveSupport::Notifications.subscribe "process_action.action_controller", ProcessAction
7+
end
8+
end
9+
end

‎valued-rails/valued-rails.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ require_relative "../lib/gemspec"
22

33
Gemspec.new do |spec|
44
spec.summary = "Rails plugin for event tracking with Valued"
5+
spec.add_dependency "rails", "~> 7.0"
56
end

0 commit comments

Comments
 (0)
Please sign in to comment.