activitylog 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZGU5Mjc4N2FhZTU3YWFiY2E5M2JmNDM0YTQ3MjRiZTlmOGExMDRmYg==
4
+ YjVmMmJjZWJjNzkyZTkzNzYzMzYzM2I5MjRmNDczYWZhNDBmZTFjYw==
5
5
  data.tar.gz: !binary |-
6
- NzlmYTYyNjJhZTA5MzNkN2MyNDA2YWM1MGZjMWM5M2QwYzA1N2NjZA==
6
+ YzEwZjVjYzU3NDg4ZjJkZGYxZGZkZjk1MTBiY2M0ZjJjYjM0MGYzYg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YjdkMTU5M2ExZGJhMDExZGM2M2MzNWExOGU3MzQ0ZTA3OTNlYmU0OGJhMDYw
10
- OTkyNDAyMjkxZTdiZTNiMmM1ZWRjMDYwNzNmMzk5NjJhNDUyMmI5OGUxNjg2
11
- ZDgwZDFhZWE2MjdiYzEwOTZmZTRhMTI2N2M0ZDIyYWQyNWQwNTc=
9
+ ODQ5YjFkMjhhOGIxMWU5ZTNmM2VlNGI5NzM2YzAxMjBhYTQ1YzIwZWZlOTU5
10
+ ZDk4ZDU0MGQ4MDhhYWNkNjMwMzU4MTcwMTQ0YzNkYmRkMzIyYTY5OTNkYjky
11
+ YzQ1OTE5ZGM4MmUxOTFkODEzZjQ2MDRhMDJhMjBhNWRlYWIzYzY=
12
12
  data.tar.gz: !binary |-
13
- YWMwNmI0MTk4N2M3ZWVjMzViNmYxN2IzODhiYWZhMDFiNmYwNDUwZGE0NTU1
14
- NTY3NjRiMzNlMWM4OGMyZmVmMmExMzgyMDk0MzFkNTM3MWQzYWU4YmI5MDBm
15
- Y2NjZThkZWY5ZDRlYTI1MjgwZmNmN2Q0ZTk0YmU4YWYyYjY5NjU=
13
+ MzRjNDA5ZWU5OGM0YjNjNzk3MmNhNTQwZTc1ZjBlNzBmM2EzNTI4NDdhMzY0
14
+ ODAxMTNhNWE3NjJjZmEzMzgzZjUwZWI0OTdjYzVjOTI2ZjQxMTgyYzE4YzJk
15
+ NGY1MjkzMmNiMjg4ZGM5YjkzOGZjYWE5NWFmOTllYmU2MDA0YTU=
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.2
1
+ 1.2.0
data/activitylog.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "activitylog"
8
- s.version = "1.1.2"
8
+ s.version = "1.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jon Erik Suero", "Elan Dubrofsky"]
@@ -2,26 +2,10 @@ class ActivityLog < ActiveRecord::Base
2
2
 
3
3
  serialize :data, Hash
4
4
 
5
- belongs_to :parent, :class_name => 'ActivityLog', :foreign_key => 'parent_activity_id'
6
- has_many :children, :class_name => 'ActivityLog', :foreign_key => 'parent_activity_id'
5
+ belongs_to :parent, class_name: 'ActivityLog', foreign_key: 'parent_activity_id'
6
+ has_many :children, class_name: 'ActivityLog', foreign_key: 'parent_activity_id'
7
+ belongs_to :loggable, polymorphic: true
8
+ belongs_to :user
7
9
 
8
- def self.create_new(user, action, object, data={}, parent_activity_id=nil)
9
-
10
- options = {
11
- "user_id" => user.id,
12
- "object_id" => object.id,
13
- "object_type" => object.class.name,
14
- "action" => action,
15
- "data" => data,
16
- "parent_activity_id" => parent_activity_id
17
- }
18
- log = ActivityLog.create(options)
19
-
20
- if log.save
21
- log
22
- else
23
- raise "ActivityLog failed to be saved" # TODO: this is just temporary
24
- end
25
-
26
- end
10
+ validates :type, inclusion: {in: %w(model controller)}
27
11
  end
@@ -9,25 +9,26 @@ module ActivityLogger
9
9
  self.stored_parent_activity_id = nil
10
10
  end
11
11
 
12
- def record_model_activity_log(action_to_store)
12
+ def record_model_activity_log(action)
13
13
  # if current_user_id == nil, then the User is in the process of Logging In
14
- current_id = defined?(current_user_id) ? (current_user_id || id) : id
15
- ActivityLog.create_new(User.find(current_id), action_to_store, self, changes, self.stored_parent_activity_id)
14
+ current_id = defined?(current_user_id) ? current_user_id : nil
15
+ activity_logs.create user_id: current_id, action: action, data: changes, type: 'model', stored_parent_activity_id: self.stored_parent_activity_id
16
16
  end
17
17
 
18
18
  def record_controller_activity_log
19
19
  return unless params[:id].present?
20
20
  return if request.get? #exit if it's a get
21
- object = controller_name.classify.constantize.find_by_id(params[:id]) #TODO: mk-- change find for LoanStep if searching by step name and loan_id
21
+ object = controller_name.classify.constantize.find_by_id(params[:id])
22
22
  return unless object.present?
23
- activity = ActivityLog.create_new(User.find(current_user.try(:id) || id), params[:controller]+"/"+params[:action], object, params)
23
+ return unless object.respond_to? :activity_logs
24
+ object.activity_logs.create user_id: current_user.try(:id), action: "#{params[:controller]}/#{params[:action]}", data: params, type: 'controller'
24
25
  self.stored_parent_activity_id = activity.id
25
26
  end
26
27
 
27
28
  # CLASS METHODS (self.)
28
29
  module ClassMethods
29
30
  def log_model_activity
30
- self.has_many :object_activity_logs, :foreign_key => 'object_id', :class_name => 'ActivityLog', :conditions => {:object_type => self.class.name}
31
+ self.has_many :activity_logs, as: :loggable
31
32
  self.after_create {record_model_activity_log("create")}
32
33
  self.after_update {record_model_activity_log("update")}
33
34
  self.before_destroy {record_model_activity_log("destroy")}
data/lib/activitylog.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'activity_log/activity_log'
2
2
  require 'activity_log/activity_logger.rb'
3
3
  ActiveRecord::Base.send(:include, ActivityLogger)
4
+ ActionController::Base.send(:include, ActivityLogger)
@@ -2,12 +2,18 @@ class CreateActivityLogs < ActiveRecord::Migration
2
2
  def change
3
3
  create_table :activity_logs do |t|
4
4
  t.integer :user_id
5
- t.integer :object_id
6
- t.string :object_type
5
+ t.integer :loggable_id
6
+ t.integer :loggable_type
7
7
  t.string :action
8
8
  t.integer :parent_activity_id
9
+ t.string :type
9
10
  t.binary :data, :limit => 10.megabyte
10
11
  t.timestamps
11
12
  end
13
+
14
+ add_index :activity_logs, [:loggable_id, :loggable_type]
15
+ add_index :activity_logs, :user_id
16
+ add_index :activity_logs, :parent_activity_id
17
+ add_index :activity_logs, :type
12
18
  end
13
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activitylog
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Erik Suero