access_logging 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/access_logging/model.rb +30 -24
- metadata +9 -9
data/lib/access_logging/model.rb
CHANGED
@@ -2,46 +2,52 @@ require 'active_support/concern'
|
|
2
2
|
|
3
3
|
module AccessLogging::Model
|
4
4
|
extend ActiveSupport::Concern
|
5
|
-
|
5
|
+
|
6
6
|
include DateRangeScopes
|
7
|
-
|
7
|
+
|
8
8
|
REDIS_ATTRIBUTES = [
|
9
9
|
:created_at,
|
10
|
-
:ip, :user_type, :user_id,
|
10
|
+
:ip, :user_type, :user_id,
|
11
11
|
:verb, :model_type, :model_id, :description,
|
12
12
|
:path
|
13
13
|
]
|
14
|
-
|
14
|
+
|
15
15
|
REDIS_SEPARATOR = "||"
|
16
|
-
|
16
|
+
|
17
17
|
included do
|
18
18
|
belongs_to :user, polymorphic: true
|
19
19
|
belongs_to :model, polymorphic: true
|
20
|
-
|
20
|
+
|
21
21
|
before_validation :set_user_name_and_email, :set_description
|
22
|
-
|
22
|
+
|
23
23
|
validates_presence_of :ip, :path, :verb,
|
24
24
|
:user_name, :user_email, :user_id, :user_type,
|
25
25
|
:created_at
|
26
|
-
|
26
|
+
|
27
27
|
default_scope order('access_logs.created_at DESC')
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
module ClassMethods
|
31
31
|
def log_request(user, request, opts={})
|
32
32
|
log = AccessLog.new
|
33
33
|
log.user = user
|
34
|
-
log.path = request.fullpath
|
35
34
|
log.ip = request.remote_ip
|
36
|
-
|
35
|
+
|
36
|
+
if [:get, :delete].include?(request.method_symbol)
|
37
|
+
log.path = request.fullpath
|
38
|
+
else
|
39
|
+
# Don't log params on POST or PUT requests
|
40
|
+
log.path = request.fullpath.split("?")[0]
|
41
|
+
end
|
42
|
+
|
37
43
|
log.model = opts[:model]
|
38
44
|
log.verb = opts[:verb] || default_verb_for_method(request.request_method)
|
39
|
-
|
45
|
+
|
40
46
|
return if log.model && log.model.new_record?
|
41
|
-
|
47
|
+
|
42
48
|
log.save!
|
43
49
|
end
|
44
|
-
|
50
|
+
|
45
51
|
def default_verb_for_method(method)
|
46
52
|
case method.to_s.upcase
|
47
53
|
when 'GET'
|
@@ -54,33 +60,33 @@ module AccessLogging::Model
|
|
54
60
|
"deleted"
|
55
61
|
end
|
56
62
|
end
|
57
|
-
|
63
|
+
|
58
64
|
def build_from_redis_string(str)
|
59
65
|
log = AccessLog.new
|
60
66
|
parts = str.split(REDIS_SEPARATOR, -1)
|
61
|
-
|
67
|
+
|
62
68
|
REDIS_ATTRIBUTES.each_with_index do |attribute, i|
|
63
69
|
log.send "#{attribute}=", parts[i]
|
64
70
|
end
|
65
|
-
|
71
|
+
|
66
72
|
log
|
67
73
|
end
|
68
74
|
end
|
69
|
-
|
75
|
+
|
70
76
|
def created_at
|
71
77
|
self[:created_at] ||= Time.zone.now
|
72
78
|
end
|
73
|
-
|
79
|
+
|
74
80
|
def redis_string
|
75
81
|
REDIS_ATTRIBUTES.map { |attribute| send(attribute) }.join(REDIS_SEPARATOR)
|
76
82
|
end
|
77
|
-
|
78
|
-
|
83
|
+
|
84
|
+
|
79
85
|
private
|
80
|
-
|
86
|
+
|
81
87
|
def set_description
|
82
88
|
self.description = "" and return unless model && !model.new_record?
|
83
|
-
|
89
|
+
|
84
90
|
if model.respond_to?(:description)
|
85
91
|
self.description = model.description
|
86
92
|
elsif model.respond_to?(:name)
|
@@ -89,7 +95,7 @@ module AccessLogging::Model
|
|
89
95
|
self.description = "#{model.class.name} #{model.id}"
|
90
96
|
end
|
91
97
|
end
|
92
|
-
|
98
|
+
|
93
99
|
def set_user_name_and_email
|
94
100
|
self.user_name = user.try :name
|
95
101
|
self.user_email = user.try :email
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: access_logging
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2012-03-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70200272177340 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70200272177340
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: date_range_scopes
|
27
|
-
requirement: &
|
27
|
+
requirement: &70200272172220 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70200272172220
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: sqlite3
|
38
|
-
requirement: &
|
38
|
+
requirement: &70200272170680 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70200272170680
|
47
47
|
description: Log access to models through your controllers.
|
48
48
|
email: nick.ragaz@gmail.com
|
49
49
|
executables: []
|
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
76
|
version: '0'
|
77
77
|
requirements: []
|
78
78
|
rubyforge_project:
|
79
|
-
rubygems_version: 1.8.
|
79
|
+
rubygems_version: 1.8.11
|
80
80
|
signing_key:
|
81
81
|
specification_version: 3
|
82
82
|
summary: Log access to models through your controllers.
|