train_track 0.2.0
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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +9 -0
- data/CODE_OF_CONDUCT.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +154 -0
- data/Rakefile +4 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/generators/train_track/install/USAGE +2 -0
- data/lib/generators/train_track/install/install_generator.rb +11 -0
- data/lib/generators/train_track/install/templates/application_tracker.rb +26 -0
- data/lib/generators/train_track/tracker/USAGE +9 -0
- data/lib/generators/train_track/tracker/templates/tracker.rb +5 -0
- data/lib/generators/train_track/tracker/tracker_generator.rb +11 -0
- data/lib/train_track.rb +48 -0
- data/lib/train_track/version.rb +3 -0
- data/train_track.gemspec +27 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 13b665f35dbf21fdcfa2734d36ef9d5fe700b205
|
4
|
+
data.tar.gz: bbc8b3dbf6bd2fbca004734cae3229161745a08a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 87b2f3e6619d27eaf033711af4e30f3429afe6b705dad7b38c478bedb89efb2a794b550ce577412fde8cea1af5864cd5ee96f44c50b16adb142f49ec7d2f08f8
|
7
|
+
data.tar.gz: fc7b60c170c51556f2f3d64a7ba741cff9e1f3bb6e70dd9424a8ddfc448664053fed524e26e6782b3ad2ffef25acc5f8ff9899e00b1b9ad95de75368249e03d8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Anthony Super
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
# :steam_locomotive: TrainTrack :steam_locomotive:
|
2
|
+
|
3
|
+
[](https://codeclimate.com/github/AnthonySuper/train_track)
|
4
|
+
[](https://codeclimate.com/github/AnthonySuper/train_track/coverage)
|
5
|
+
[](https://travis-ci.org/AnthonySuper/train_track)
|
6
|
+
|
7
|
+
TrainTrack is a gem that makes it easier to track changes in your rails projects.
|
8
|
+
It doesn't make any decisions on how to do that.
|
9
|
+
That part is up to you.
|
10
|
+
All it does is provide some nice helpers to make that task easier.
|
11
|
+
|
12
|
+
TrainTrack may gain features in the future that also manage the actual tracking side.
|
13
|
+
If you want to help develop that, shoot us a pull request!
|
14
|
+
### Why not just use ActiveRecord callbacks?
|
15
|
+
|
16
|
+
Well, you could use those.
|
17
|
+
In fact, for very simple tracking, those are probably better.
|
18
|
+
However, that has some limitations:
|
19
|
+
|
20
|
+
1. Doesn't fully work with collections: a `has_many` is going to cause severe trouble for your code, since some methods will trigger `before_update` and `after_update`, while others won't.
|
21
|
+
|
22
|
+
2. Will run on records updated by automated tasks, when you probably care more about what users are doing
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
|
26
|
+
Add this line to your application's Gemfile:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
gem 'train_track'
|
30
|
+
```
|
31
|
+
|
32
|
+
And then execute:
|
33
|
+
|
34
|
+
$ bundle
|
35
|
+
|
36
|
+
Or install it yourself as:
|
37
|
+
|
38
|
+
$ gem install train_track
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
Include TrainTrack in your controller:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
class ImagesController < ApplicationController
|
46
|
+
include TrainTrack
|
47
|
+
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
Call the `record` method in the controller when there's a state you wish to track.
|
52
|
+
What do I mean by this?
|
53
|
+
Well, in the create method, you probably only want to track that it has been created
|
54
|
+
```ruby
|
55
|
+
def create
|
56
|
+
i = Image.new(image_params)
|
57
|
+
if i.save
|
58
|
+
track i
|
59
|
+
redirect_to i
|
60
|
+
else
|
61
|
+
# handle error
|
62
|
+
end
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
Whereas in an update action, you probably want to manage multiple states:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
def update
|
70
|
+
i = Image.find(params[:id])
|
71
|
+
track i
|
72
|
+
if i.update(image_params)
|
73
|
+
track i
|
74
|
+
redirect_to i
|
75
|
+
else
|
76
|
+
# handle error
|
77
|
+
end
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
### Trackers
|
82
|
+
Now, all those calls to track are pretty useless right now.
|
83
|
+
In order to make them do something, you have to specify a tracker.
|
84
|
+
We recommend you put those in `/app/trackers/`
|
85
|
+
|
86
|
+
A tracker looks like this:
|
87
|
+
```ruby
|
88
|
+
class ImageTracker < ApplicationTracker
|
89
|
+
# user is taken from the `current_user` method on your controller
|
90
|
+
def initialize(user, record)
|
91
|
+
@user = user
|
92
|
+
@record = record
|
93
|
+
end
|
94
|
+
# Called on the create action
|
95
|
+
def create
|
96
|
+
# assuming we have a model called "ImageEdit" that tracks modifications of Images
|
97
|
+
# also assume that this uses Postgres' arrays
|
98
|
+
ImageEdit.create(user: @user,
|
99
|
+
image: @record,
|
100
|
+
action: :created,
|
101
|
+
old_tags: [],
|
102
|
+
new_tags: @record.tags.pluck(:id))
|
103
|
+
end
|
104
|
+
|
105
|
+
# Called on the first call to "track" in the edit action
|
106
|
+
def update_before
|
107
|
+
@old_tags = @record.tags.pluck(:id)
|
108
|
+
end
|
109
|
+
|
110
|
+
def update_after
|
111
|
+
@new_tags = @record.tags.reload.pluck(:id)
|
112
|
+
# Assuming ImageEdit makes use of Postgres' arrays
|
113
|
+
ImageEdit.create(user: @user,
|
114
|
+
image: @record,
|
115
|
+
action: :edited,
|
116
|
+
old_tags: @old_tags,
|
117
|
+
new_tags: @new_tags)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
```
|
121
|
+
|
122
|
+
Much like in [Pundit](https://github.com/elabs/pundit), a tracker is a PORO.
|
123
|
+
This gives TrainTracks a higher level of flexibility.
|
124
|
+
|
125
|
+
Also like in pundit, we infer the name of the tracker from the class name.
|
126
|
+
If you want to over-ride this behavior, simple include a method called `train_tracker_class` on the class.
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
class Noided < ActiveRecord::Base
|
130
|
+
def self.train_tracker_class
|
131
|
+
ParanoidTracker
|
132
|
+
end
|
133
|
+
end
|
134
|
+
```
|
135
|
+
You can also define it as an instance method.
|
136
|
+
This lets you do fancy things:
|
137
|
+
```ruby
|
138
|
+
class ElfmanSong < ActiveRecord::Base
|
139
|
+
def train_tracker_class
|
140
|
+
if band == "Oingo Boingo"
|
141
|
+
BoingoTracker
|
142
|
+
else
|
143
|
+
SoloTracker
|
144
|
+
end
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
## Contributing
|
149
|
+
|
150
|
+
1. Fork it ( https://github.com/AnthonySuper/train_track/fork )
|
151
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
152
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
153
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
154
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "train_track"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
module TrainTrack
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < ::Rails::Generators::Base
|
4
|
+
source_root File.expand_path(File.join(File.dirname(__FILE__), "templates"))
|
5
|
+
def copy_application_tracker
|
6
|
+
template 'application_tracker.rb', 'app/trackers/application_tracker.rb'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class ApplicationTracker
|
2
|
+
attr_reader :user, :record
|
3
|
+
|
4
|
+
def initialize(user, record)
|
5
|
+
@user = user
|
6
|
+
@record = record
|
7
|
+
end
|
8
|
+
|
9
|
+
def create
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
def update_before
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
def update_after
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
def destroy
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module TrainTrack
|
2
|
+
module Generators
|
3
|
+
class TrackerGenerator < ::Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
|
5
|
+
def create_tracker
|
6
|
+
template 'tracker.rb', File.join('app/trackers', class_path, "#{file_name}_tracker.rb")
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
data/lib/train_track.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require "train_track/version"
|
2
|
+
require 'active_support/concern'
|
3
|
+
require 'active_support/core_ext/string/inflections'
|
4
|
+
module TrainTrack
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
##
|
7
|
+
# Find the tracker class for a given model
|
8
|
+
def self.tracker_class(model)
|
9
|
+
if model.respond_to? :train_tracker_class
|
10
|
+
model.train_tracker_class
|
11
|
+
elsif model.class.respond_to? :train_tracker_class
|
12
|
+
model.class.train_tracker_class
|
13
|
+
else
|
14
|
+
(model.class.name.to_s + "Tracker").constantize
|
15
|
+
end
|
16
|
+
end
|
17
|
+
##
|
18
|
+
# Methods to add to a controller when included
|
19
|
+
included do
|
20
|
+
##
|
21
|
+
# Track a model
|
22
|
+
#
|
23
|
+
def track(model)
|
24
|
+
@_tracker ||= TrainTrack.tracker_class(model).new(user_method, model)
|
25
|
+
if params[:action] == :update && _tracker_times_called == 0
|
26
|
+
@_tracker.update_before
|
27
|
+
@_tracker_times_called += 1
|
28
|
+
elsif params[:action] == :update && _tracker_times_called == 1
|
29
|
+
@_tracker.update_after
|
30
|
+
else
|
31
|
+
@_tracker.send(params[:action])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
##
|
37
|
+
# Find the current_user of the controller
|
38
|
+
# Done by using current_user
|
39
|
+
def user_method
|
40
|
+
current_user
|
41
|
+
end
|
42
|
+
|
43
|
+
def _tracker_times_called
|
44
|
+
@_tracker_times_called ||= 0
|
45
|
+
@_tracker_times_called
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/train_track.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'train_track/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "train_track"
|
8
|
+
spec.version = TrainTrack::VERSION
|
9
|
+
spec.authors = ["Anthony Super"]
|
10
|
+
spec.email = ["anthony@noided.media"]
|
11
|
+
|
12
|
+
|
13
|
+
spec.summary = %q{Track changes to rails models in a generic way}
|
14
|
+
spec.homepage = "https://github.com/ANthonySUper/train_track"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
spec.add_development_dependency "codeclimate-test-reporter"
|
26
|
+
spec.add_dependency "activesupport", ">= 4.0.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: train_track
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anthony Super
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: codeclimate-test-reporter
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 4.0.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 4.0.0
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- anthony@noided.media
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- lib/generators/train_track/install/USAGE
|
101
|
+
- lib/generators/train_track/install/install_generator.rb
|
102
|
+
- lib/generators/train_track/install/templates/application_tracker.rb
|
103
|
+
- lib/generators/train_track/tracker/USAGE
|
104
|
+
- lib/generators/train_track/tracker/templates/tracker.rb
|
105
|
+
- lib/generators/train_track/tracker/tracker_generator.rb
|
106
|
+
- lib/train_track.rb
|
107
|
+
- lib/train_track/version.rb
|
108
|
+
- train_track.gemspec
|
109
|
+
homepage: https://github.com/ANthonySUper/train_track
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.2.2
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Track changes to rails models in a generic way
|
133
|
+
test_files: []
|
134
|
+
has_rdoc:
|