preact 0.8.2 → 0.8.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/generators/preact_generator.rb +1 -1
- data/lib/preact/configuration.rb +1 -3
- data/lib/preact/objects/person.rb +3 -3
- data/lib/preact/rails/controllers/helpers.rb +28 -9
- data/lib/preact/version.rb +1 -1
- data/readme.md +73 -20
- metadata +1 -1
@@ -17,7 +17,7 @@ Preact.configure do |config|
|
|
17
17
|
|
18
18
|
# specify controller#action items that you want to ignore and not log to Preact.
|
19
19
|
# default is to not log sessions#create beacuse if you're using Devise, we get that already
|
20
|
-
config.autolog_ignored_actions = ["sessions#create"]
|
20
|
+
config.autolog_ignored_actions = ["sessions#create", "devise/sessions#create"]
|
21
21
|
|
22
22
|
# disable in Rails non-production environments
|
23
23
|
# uncomment this if you don't want to log development activities
|
data/lib/preact/configuration.rb
CHANGED
@@ -113,9 +113,7 @@ module Preact
|
|
113
113
|
:name => user.name,
|
114
114
|
:email => user.email,
|
115
115
|
:uid => user.id,
|
116
|
-
:
|
117
|
-
:created_at => (user.created_at.to_i if user.respond_to?(:created_at))
|
118
|
-
}
|
116
|
+
:created_at => (user.created_at.to_i if user.respond_to?(:created_at))
|
119
117
|
}
|
120
118
|
end
|
121
119
|
|
@@ -3,12 +3,12 @@ class Preact::Person < Preact::ApiObject
|
|
3
3
|
attr_accessor :name, :email, :external_identifier, :properties, :uid, :created_at
|
4
4
|
|
5
5
|
def as_json(options={})
|
6
|
-
{
|
6
|
+
d = {
|
7
7
|
:name => self.name,
|
8
8
|
:email => self.email,
|
9
9
|
:uid => self.uid || self.external_identifier,
|
10
|
-
:created_at => self.created_at.to_i,
|
11
|
-
:properties => self.properties
|
10
|
+
:created_at => self.created_at ? self.created_at.to_i : nil,
|
11
|
+
:properties => self.properties || {}
|
12
12
|
}
|
13
13
|
end
|
14
14
|
|
@@ -1,16 +1,18 @@
|
|
1
1
|
module Preact
|
2
2
|
module Controllers
|
3
3
|
module Helpers
|
4
|
-
extend ActiveSupport::Concern
|
5
4
|
|
6
5
|
def preact_autolog_controller_action
|
6
|
+
|
7
7
|
# only track logged-in users
|
8
8
|
return true unless current_user
|
9
|
+
|
10
|
+
# don't autolog if we logged something already on this controller execution
|
11
|
+
# this allows you to add better preact logging in your controller and not get duplicate logging
|
12
|
+
return true if defined?(@preact_logged_event)
|
9
13
|
|
10
14
|
controller = params[:controller]
|
11
15
|
action = params[:action]
|
12
|
-
target_id = params[:id]
|
13
|
-
note = nil
|
14
16
|
|
15
17
|
return true unless controller && action
|
16
18
|
|
@@ -18,15 +20,11 @@ module Preact
|
|
18
20
|
|
19
21
|
event_name = "#{controller}##{action}"
|
20
22
|
|
21
|
-
|
22
|
-
note = "NOT FOUND"
|
23
|
-
elsif response.status == 500
|
24
|
-
event_name = "!#{event_name}--error"
|
25
|
-
end
|
23
|
+
note = self.guess_target_item_name(controller)
|
26
24
|
|
27
25
|
event = {
|
28
26
|
:name => event_name,
|
29
|
-
:target_id =>
|
27
|
+
:target_id => params[:id],
|
30
28
|
:medium => "autolog-rails-v1.0",
|
31
29
|
:note => note,
|
32
30
|
:extras => {
|
@@ -39,6 +37,9 @@ module Preact
|
|
39
37
|
preact_log(event)
|
40
38
|
|
41
39
|
true
|
40
|
+
rescue => ex
|
41
|
+
Preact.logger.warn "[Preact] Autolog failed: #{ex.message}"
|
42
|
+
true # always returns strue no matter what...
|
42
43
|
end
|
43
44
|
|
44
45
|
# helper method on the controller to make logging events easy
|
@@ -48,6 +49,9 @@ module Preact
|
|
48
49
|
else
|
49
50
|
Preact.log_event(current_user, event)
|
50
51
|
end
|
52
|
+
|
53
|
+
# make a note that we've logged an event on this controller
|
54
|
+
@preact_logged_event = event
|
51
55
|
end
|
52
56
|
|
53
57
|
# attach the after_filter to all controllers if we've enabled autologging
|
@@ -57,6 +61,21 @@ module Preact
|
|
57
61
|
end
|
58
62
|
end
|
59
63
|
|
64
|
+
|
65
|
+
protected
|
66
|
+
|
67
|
+
def guess_target_item_name(controller)
|
68
|
+
# get a little too clever and try to see if we've loaded an item
|
69
|
+
guessed_target_variable = controller.split("/").last.singularize rescue nil
|
70
|
+
if guessed_target_variable
|
71
|
+
if guessed_target_item = self.instance_variable_get("@#{guessed_target_variable}")
|
72
|
+
return guessed_target_item.name if guessed_target_item.respond_to?(:name)
|
73
|
+
return guessed_target_item.title if guessed_target_item.respond_to?(:title)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
nil
|
77
|
+
end
|
78
|
+
|
60
79
|
end
|
61
80
|
end
|
62
81
|
end
|
data/lib/preact/version.rb
CHANGED
data/readme.md
CHANGED
@@ -8,28 +8,87 @@ Installation
|
|
8
8
|
In your Gemfile:
|
9
9
|
|
10
10
|
```ruby
|
11
|
-
gem 'preact'
|
11
|
+
gem 'preact', "~> 0.8.0"
|
12
12
|
```
|
13
13
|
|
14
|
+
Then do a `bundle install` to get the gem.
|
15
|
+
|
14
16
|
Configuration
|
15
17
|
---
|
16
18
|
|
17
|
-
|
19
|
+
In version 0.8.1 we added a rails generator to make it really easy to add the initializer and get you up and running.
|
20
|
+
|
21
|
+
First, obtain your Preact Project Code and API Secret from the [API settings page](https://secure.preact.io/settings/api). Then, in your application directory, run the generator:
|
22
|
+
|
23
|
+
```bash
|
24
|
+
rails g preact your-code-1234 api-secret-xyzw
|
25
|
+
```
|
26
|
+
|
27
|
+
That will generate a preact.rb initializer that looks something like this:
|
18
28
|
|
19
29
|
```ruby
|
20
30
|
Preact.configure do |config|
|
21
|
-
config.code = '
|
22
|
-
config.secret = '
|
31
|
+
config.code = 'your-code-1234' # required
|
32
|
+
config.secret = 'api-secret-xyzw' # required
|
23
33
|
|
34
|
+
config.autolog = true
|
35
|
+
config.autolog_ignored_actions = ["sessions#create"]
|
36
|
+
|
24
37
|
# Disable in Rails development environments
|
25
38
|
# config.disabled = Rails.env.development?
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
Now when you launch your app and do something as an authenticated user, you should see the activity in Preact.
|
43
|
+
|
44
|
+
Autolog
|
45
|
+
---
|
46
|
+
|
47
|
+
New in version 0.8.1 is the ability to automatically log all controller-based actions that your authenticated users are performing.
|
48
|
+
|
49
|
+
In many cases, you won't need to do anything other than put the gem in your Gemfile, bundle, and run the generator.
|
50
|
+
|
51
|
+
This works by creating an method on the ActiveController::Base class and setting it as an after_filter for all controllers. We assume that you're using Devise/Warden, because everyone does, and can directly access that to identify who the current_user is. With Autolog enabled, what you will see is rails routes-style events in Preact for each user.
|
52
|
+
|
53
|
+
We turn Autolog on by default when you use the generator to build the preact.rb initializer, but if you're upgrading from an existing preact-ruby install you will need to manually update your config.
|
54
|
+
|
55
|
+
You can configure controllers and routes to ignore by adding them in the initializer as well. If you want to ignore a specific action, you can include it like so:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
config.autolog_ignored_actions = [
|
59
|
+
"documents#new", # ignores the new action on the documents_controller
|
60
|
+
"people#new", # ignores the new action on the people_controller
|
61
|
+
"secret_pages#*" # ignores ALL actions on the secret_pages_controller
|
62
|
+
]
|
63
|
+
```
|
64
|
+
|
65
|
+
Rails Controller Helper
|
66
|
+
---
|
67
|
+
|
68
|
+
Since version 0.8.1, we include a helper method on the base controller called `preact_log` to make it convenient for you to log events directly.
|
69
|
+
|
70
|
+
The helper is aware of the current_user and so only requires you to pass the event information as things occur. So for instance, you may log a simple event from one of yoru controllers like so:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
class DocumentsController < ApplicationController
|
74
|
+
def show
|
75
|
+
# YOUR CODE STUFF HERE
|
76
|
+
|
77
|
+
preact_log("did-something-cool")
|
78
|
+
end
|
26
79
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
80
|
+
def search
|
81
|
+
# YOUR CODE STUFF HERE
|
82
|
+
|
83
|
+
preact_log({
|
84
|
+
name: "searched-documents",
|
85
|
+
note: @search_term,
|
86
|
+
extras: {
|
87
|
+
term: @search_term,
|
88
|
+
result_count: @results.count
|
89
|
+
}
|
90
|
+
})
|
91
|
+
end
|
33
92
|
end
|
34
93
|
```
|
35
94
|
|
@@ -138,19 +197,13 @@ All you need to do is add `require 'preact/sidekiq'` at the top of your `preact.
|
|
138
197
|
|
139
198
|
Devise / Warden Integration
|
140
199
|
--
|
141
|
-
|
200
|
+
If you are using Warden, Preact will automatically log your login/logout events.
|
201
|
+
If when Preact loads, it notices that a ::Warden class is defined, it will require the preact/warden module which adds the appropriate hooks into Warden.
|
142
202
|
|
143
|
-
```ruby
|
144
|
-
# after-auth hook to log the login
|
145
|
-
Warden::Manager.after_authentication do |user,auth,opts|
|
146
|
-
Preact.log_event(user, "logged-in")
|
147
|
-
end
|
148
|
-
Warden::Manager.before_logout do |user,auth,opts|
|
149
|
-
Preact.log_event(user, "logged-out")
|
150
|
-
end
|
151
|
-
```
|
152
203
|
|
153
204
|
|
205
|
+
License
|
206
|
+
--
|
154
207
|
Copyright (c) 2011-2013 Christopher Gooley, Preact / Less Neglect, Inc. See LICENSE.txt for further details.
|
155
208
|
|
156
209
|
Thanks to [Zach Millman](https://github.com/zmillman) for many contributions.
|