action_logger 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ActionLogger
2
2
 
3
- TODO: Write a gem description
3
+ Simple ActiveRecord activity logger. It allows you to log user actions to the database.
4
4
 
5
5
  ## Installation
6
6
 
@@ -10,15 +10,47 @@ Add this line to your application's Gemfile:
10
10
 
11
11
  And then execute:
12
12
 
13
- $ bundle
13
+ bundle
14
14
 
15
- Or install it yourself as:
15
+ You'll need to create the migration:
16
16
 
17
- $ gem install action_logger
17
+ rails generate action_logger:install
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Now you can call `ActionLogger.log` to logs omething to the database. It takes two required paramaters which are `action` and `source`. It also takes an optional attributes hash.
22
+
23
+ Example:
24
+
25
+ ActionLogger.log 'buy', 'search results'
26
+
27
+ This will log a buy action to the database which appeared on the search results. The optional paramters are:
28
+
29
+ :sender # Takes any object, e.g. current_user
30
+ :ip # Allows you to store an ip address
31
+ :data # Takes a hash with anything you want
32
+ :admin_id # allows you to set an extra id if for example an admin became a user through a "become" functionality
33
+
34
+ There is also a helper you can use in your controllers or views. In your `ApplicationController` add:
35
+
36
+ include ActionLogsHelper
37
+
38
+ Now you're able to call the `log_action` helper:
39
+
40
+ log_action 'buy', 'search results'
41
+
42
+ The optional attributes has will set some sane defaults:
43
+
44
+ :sender => current_user
45
+ :ip => request.remote_ip
46
+ :data => params
47
+ :admin_id => cirremt_admin_user
48
+
49
+ ## Warning
50
+
51
+ This code is still very basic and has no automated tests. Your production database might not be the best place to log these actions.
52
+ At some point we might move to Mongo or any other database system if we feel it suits our needs better.
53
+ Feel free to fork and improve the code.
22
54
 
23
55
  ## Contributing
24
56
 
@@ -16,4 +16,6 @@ Gem::Specification.new do |gem|
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'rails', '>= 3.0.0'
19
21
  end
@@ -0,0 +1,5 @@
1
+ module ActionLogsHelper
2
+ def log_action action, source, attrs = {}
3
+ ActionLogger.log action, source, attrs
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ class ActionLog < ActiveRecord::Base
2
+ serialize :data
3
+
4
+ def self.log action, source, sender = nil, ip = nil, data = {}, admin_id = nil
5
+ log = create! action: action, source: source, sender_id: sender.try(:id), sender_type: sender.try(:class).try(:name), ip: ip, data: data, admin_id: (admin_id.present? && admin_id.respond_to?(:id) ? admin_id.id : admin_id)
6
+ Rails.logger.info log.inspect
7
+ end
8
+ end
data/lib/action_logger.rb CHANGED
@@ -1,5 +1,13 @@
1
1
  require 'action_logger/version'
2
+ require 'action_logger/engine'
2
3
 
3
4
  module ActionLogger
4
- # Your code goes here...
5
+ def self.log action, source, attrs = {}
6
+ attrs[:sender] || try(:current_user)
7
+ attrs[:ip] || request.try(:remote_ip)
8
+ attrs[:data] || params
9
+ attrs[:admin_id] || try(:current_admin_user)
10
+
11
+ ActionLog.log action, source, attrs[:semder], attrs[:ip], attrs[:data], attrs[:admin_id]
12
+ end
5
13
  end
@@ -0,0 +1,5 @@
1
+ module ActionLogger
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace ActionLogger
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module ActionLogger
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -0,0 +1,21 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module ActionLogger
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+
9
+ source_root File.expand_path('../templates', __FILE__)
10
+ desc 'Generates the ActionLogger migrations'
11
+
12
+ def self.next_migration_number path
13
+ Time.now.utc.strftime '%Y%m%d%H%M%S'
14
+ end
15
+
16
+ def create_migrations
17
+ migration_template 'create_action_logs.rb', 'db/migrate/create_action_logs.rb'
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ class CreateActionLogs < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :action_logs do |t|
4
+ t.integer :sender_id
5
+ t.string :sender_type
6
+ t.string :action
7
+ t.string :source
8
+ t.string :ip
9
+ t.text :data
10
+ t.integer :admin_id
11
+ t.timestamps
12
+ end
13
+ add_index :action_logs, [:sender_type, :sender_id]
14
+ end
15
+
16
+ def self.down
17
+ drop_table :admin_notes
18
+ end
19
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-16 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-10-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
14
30
  description: Log actions in your Rails app
15
31
  email: gems@jeroenj.be
16
32
  executables: []
@@ -23,8 +39,13 @@ files:
23
39
  - README.md
24
40
  - Rakefile
25
41
  - action_logger.gemspec
42
+ - app/helpers/action_logs_helper.rb
43
+ - app/models/action_log.rb
26
44
  - lib/action_logger.rb
45
+ - lib/action_logger/engine.rb
27
46
  - lib/action_logger/version.rb
47
+ - lib/generators/action_logger/install_generator.rb
48
+ - lib/generators/action_logger/templates/create_action_logs.rb
28
49
  homepage: ''
29
50
  licenses: []
30
51
  post_install_message:
@@ -37,12 +58,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
37
58
  - - ! '>='
38
59
  - !ruby/object:Gem::Version
39
60
  version: '0'
61
+ segments:
62
+ - 0
63
+ hash: 3345375381320870001
40
64
  required_rubygems_version: !ruby/object:Gem::Requirement
41
65
  none: false
42
66
  requirements:
43
67
  - - ! '>='
44
68
  - !ruby/object:Gem::Version
45
69
  version: '0'
70
+ segments:
71
+ - 0
72
+ hash: 3345375381320870001
46
73
  requirements: []
47
74
  rubyforge_project:
48
75
  rubygems_version: 1.8.24