tracker_jacker 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 840c6c43cfb9f27c32cd7487eda3adcfb5ac2df5
4
+ data.tar.gz: 6fd3f01466d48867e7d381bd16e0d18d986e2617
5
+ SHA512:
6
+ metadata.gz: 40e43e92d5b704a09668aa85a3bec30b7642a6c7899ade9fd9b22ae19a7fbf30c73610926c96869ecb7b7a06e4fc708ca246023da4baef181e4e107d42790159
7
+ data.tar.gz: c38e3ec0bac64b2520ac131d1cc5b140d3b4dd6f65d3ec7d4e96dffdb64ae6cf19e0f5df7fd63708f1abbdc0650a261d7b266668e2318bd82d5720f8224d8432
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2015 John Gesimondo and PlateJoy, Inc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'TrackerJacker'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
@@ -0,0 +1,16 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module TrackerJacker
4
+ module Generators
5
+ class MigrationGenerator < ActiveRecord::Generators::Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ argument :name, :type => :string, :default => 'create_trackable_events'
9
+ # Create migration in project's folder
10
+ def generate_files
11
+ migration_template 'migration.rb', "db/migrate/#{name}.rb"
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ # Migration responsible for creating trackable events
2
+ class CreateTrackableEvents < ActiveRecord::Migration
3
+ def change
4
+ create_table :trackable_events do |t|
5
+ t.belongs_to :trackable, :polymorphic => true
6
+ t.belongs_to :owner, :polymorphic => true
7
+
8
+ t.string :category
9
+ t.string :event
10
+ t.text :new_value
11
+ t.text :old_value
12
+
13
+ t.timestamps null: false
14
+ end
15
+
16
+ add_index :trackable_events, [:trackable_id, :trackable_type]
17
+ add_index :trackable_events, [:owner_id, :owner_type]
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :tracker_jacker do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,86 @@
1
+ module TrackerJacker
2
+ module Trackable
3
+ EventTrackingCallback = Struct.new(:event, :if_proc, :owner_method, :category)
4
+ AttributeTrackingCallback = Struct.new(:attribute, :owner_method, :category)
5
+
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ base.class_eval do
9
+ after_save :run_track_event_callbacks
10
+ after_save :run_track_attribute_callbacks
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+ def analytic_event_tracking_callbacks
16
+ @analytic_event_tracking_callbacks ||= {}
17
+ end
18
+
19
+ def analytic_attribute_tracking_callbacks
20
+ @analytic_attribute_tracking_callbacks ||= {}
21
+ end
22
+
23
+ def track_event(event, options = {})
24
+ if_proc = options.fetch(:if)
25
+ owner_method = options.fetch(:owner)
26
+ category = options.fetch(:category)
27
+
28
+ analytic_event_tracking_callbacks[event] = EventTrackingCallback.new(event, if_proc, owner_method, category)
29
+ end
30
+
31
+ def track_attribute(attribute, options = {})
32
+ owner_method = options.fetch(:owner)
33
+ category = options.fetch(:category)
34
+
35
+ analytic_attribute_tracking_callbacks[attribute] = AttributeTrackingCallback.new(attribute, owner_method, category)
36
+ end
37
+
38
+ def track_attributes(attributes, options = {})
39
+ attributes.each {|attr| track_attribute(attr, options)}
40
+ end
41
+ end
42
+
43
+ def run_track_attribute_callbacks
44
+ return if @ignore_tracking
45
+
46
+ object = self
47
+ object.class.analytic_attribute_tracking_callbacks.values.each do |event_callback|
48
+ dirty_method = event_callback.attribute.to_s + "_changed?"
49
+ if object.public_send(dirty_method)
50
+ owner = object.public_send(event_callback.owner_method)
51
+ TrackerJacker::TrackableEvent.create(
52
+ category: event_callback.category,
53
+ owner: owner,
54
+ trackable: object,
55
+ event: event_callback.attribute.to_s + "_changed",
56
+ old_value: object.public_send(event_callback.attribute.to_s + "_was"),
57
+ new_value: object.public_send(event_callback.attribute)
58
+ )
59
+ end
60
+ end
61
+ true
62
+ end
63
+
64
+ def run_track_event_callbacks
65
+ return if @ignore_tracking
66
+
67
+ object = self
68
+ object.class.analytic_event_tracking_callbacks.values.each do |event_callback|
69
+ if event_callback.if_proc.call(object)
70
+ owner = object.public_send(event_callback.owner_method)
71
+ TrackerJacker::TrackableEvent.create(
72
+ category: event_callback.category,
73
+ owner: owner,
74
+ trackable: object,
75
+ event: event_callback.event
76
+ )
77
+ end
78
+ end
79
+ true
80
+ end
81
+
82
+ def ignore_tracking!
83
+ @ignore_tracking = true
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,14 @@
1
+ module TrackerJacker
2
+ class TrackableEvent < ActiveRecord::Base
3
+ belongs_to :trackable, polymorphic: true
4
+ belongs_to :owner, polymorphic: true
5
+
6
+ def week_created
7
+ created_at.at_beginning_of_week
8
+ end
9
+
10
+ def month_created
11
+ created_at.at_beginning_of_month
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module TrackerJacker
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'tracker_jacker/trackable'
2
+ require 'tracker_jacker/trackable_event'
3
+
4
+ module TrackerJacker
5
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tracker_jacker
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - John Gesimondo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.14.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.14.0
55
+ description: Track events and ActiveRecord attribute changes in a timeline
56
+ email:
57
+ - john@jmondo.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - Rakefile
64
+ - lib/generators/tracker_jacker/migration/migration_generator.rb
65
+ - lib/generators/tracker_jacker/migration/templates/migration.rb
66
+ - lib/tasks/tracker_jacker_tasks.rake
67
+ - lib/tracker_jacker.rb
68
+ - lib/tracker_jacker/trackable.rb
69
+ - lib/tracker_jacker/trackable_event.rb
70
+ - lib/tracker_jacker/version.rb
71
+ homepage: https://github.com/HealthyDelivery/tracker_jacker
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.4.5
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Track events and ActiveRecord attribute changes
95
+ test_files: []