models_timeline 0.0.1.2 → 0.0.2

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/README.rdoc CHANGED
@@ -4,7 +4,7 @@ Track the entire life cycle of ActiveRecord models.
4
4
  ==Installation
5
5
  Add the gem to your Gemfile
6
6
 
7
- gem "timeline"
7
+ gem "models_timeline"
8
8
 
9
9
  Copy some migration stuff:
10
10
 
@@ -13,7 +13,7 @@ Copy some migration stuff:
13
13
 
14
14
  Add to your route.rb file:
15
15
 
16
- mount Timeline::Engine => "/timeline"
16
+ mount ModelsTimeline::Engine => "/timeline"
17
17
 
18
18
  Requires Ruby 1.9.2 or later.
19
19
 
@@ -37,6 +37,10 @@ Track one or more attributes:
37
37
 
38
38
  Only the changes of name and price will be tracked.
39
39
 
40
+ To see the tracked changes go to:
41
+
42
+ http://domain:port/models_timeline
43
+
40
44
  Development
41
45
  Questions or problems? Please post them on the issue tracker. You can contribute changes by forking the project and submitting a pull request. You can ensure the tests passing by running bundle and rake.
42
46
 
@@ -0,0 +1,4 @@
1
+ module ModelsTimeline
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,23 @@
1
+ require_dependency "models_timeline/application_controller"
2
+
3
+ module ModelsTimeline
4
+ class TracksController < ApplicationController
5
+ def index
6
+ @tracks = Track.all
7
+
8
+ respond_to do |format|
9
+ format.html # index.html.erb
10
+ format.json { render json: @tracks }
11
+ end
12
+ end
13
+
14
+ def show
15
+ @track = Track.find(params[:id])
16
+
17
+ respond_to do |format|
18
+ format.html # show.html.erb
19
+ format.json { render json: @track }
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,4 +1,4 @@
1
- module Timeline
1
+ module ModelsTimeline
2
2
  module ApplicationHelper
3
3
  end
4
4
  end
@@ -1,4 +1,4 @@
1
- module Timeline
1
+ module ModelsTimeline
2
2
  module TracksHelper
3
3
  end
4
4
  end
@@ -0,0 +1,7 @@
1
+ module ModelsTimeline
2
+ class Track < ActiveRecord::Base
3
+ serialize :model_changes
4
+ attr_accessible :action, :item_id, :model_changes, :model_name
5
+ default_scope order('created_at DESC')
6
+ end
7
+ end
data/config/routes.rb CHANGED
@@ -1,3 +1,3 @@
1
- Timeline::Engine.routes.draw do
1
+ ModelsTimeline::Engine.routes.draw do
2
2
  root :to => "tracks#index"
3
3
  end
@@ -0,0 +1,14 @@
1
+ class CreateModelsTimelineTracks < ActiveRecord::Migration
2
+ def change
3
+ create_table :models_timeline_tracks do |t|
4
+ t.integer :item_id
5
+ t.string :model_name
6
+ t.string :action
7
+ t.string :model_changes
8
+
9
+ t.timestamps
10
+ end
11
+ add_index(:models_timeline_tracks, :item_id)
12
+ add_index(:models_timeline_tracks, :model_name)
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ require "models_timeline/engine"
2
+ require "models_timeline/model_additions"
3
+ require "models_timeline/railtie"
4
+
5
+ module ModelsTimeline
6
+ class Engine < ::Rails::Engine
7
+ isolate_namespace ModelsTimeline
8
+ end
9
+ end
@@ -0,0 +1,60 @@
1
+ module ModelsTimeline
2
+ module ModelAdditions
3
+ def self.extended(obj) #:nodoc
4
+ obj.send :include, ClassMethods
5
+ end
6
+
7
+ # To track your model changes call <tt> timeline </tt>
8
+ # in any Active Record model class.
9
+ #
10
+ # class Product < ActiveRecord::Base
11
+ # timeline
12
+ # end
13
+ #
14
+ # To track only one or a set of attributes(columns) call <tt> timeline :attr1, :attr2 </tt>.
15
+ #
16
+ # class Product < ActiveRecord::Base
17
+ # timeline :price, :description
18
+ # end
19
+ #
20
+ def timeline(*attrs)
21
+ track(attrs)
22
+ end
23
+
24
+ private
25
+ def track(attributes)
26
+ after_create do |record|
27
+ map_attributes_and_save('created', attributes)
28
+ end
29
+
30
+ after_update do |record|
31
+ map_attributes_and_save('updated', attributes)
32
+ end
33
+
34
+ after_destroy do |record|
35
+ map_attributes_and_save('destroyed', attributes)
36
+ end
37
+ end
38
+ end
39
+
40
+ module ClassMethods
41
+ def map_attributes_and_save(action, attributes={})
42
+ attributes = attributes.map{|a| a.to_s unless a.is_a? String}
43
+
44
+ model_changes = unless attributes.empty?
45
+ HashWithIndifferentAccess.new self.changes.select{|c| attributes.include?(c)}
46
+ else
47
+ self.changes
48
+ end
49
+
50
+ model_changes.delete(:created_at)
51
+ model_changes.delete(:updated_at)
52
+
53
+ Track.create!({
54
+ item_id: self.id,
55
+ model_name: self.class.model_name,
56
+ action: action,
57
+ model_changes: model_changes })
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,9 @@
1
+ module ModelsTimeline
2
+ class Railtie < Rails::Railtie
3
+ initializer 'timeline.model_additions' do
4
+ ActiveSupport.on_load :active_record do
5
+ extend ModelAdditions
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module ModelsTimeline
2
+ VERSION = "0.0.1.2"
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'models_timeline/engine'
2
+ module ModelsTimeline
3
+ end
@@ -1,4 +1,4 @@
1
1
  # desc "Explaining what the task does"
2
- # task :timeline do
3
- # # Task goes here
4
- # end
2
+ task :timeline do
3
+ # Task goes here
4
+ end
@@ -1,3 +1,3 @@
1
- module Timeline
2
- VERSION = "0.0.1.2"
1
+ module ModelsTimeline
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/timeline.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  require 'timeline/engine'
2
- module Timeline
2
+ module ModelsTimeline
3
3
  end
File without changes
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- module Timeline
3
+ module ModelsTimeline
4
4
  class TracksControllerTest < ActionController::TestCase
5
5
  fixtures 'timeline/tracks'
6
6
  setup do
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- module Timeline
3
+ module ModelsTimeline
4
4
  class TimelinesHelperTest < ActionView::TestCase
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- module Timeline
3
+ module ModelsTimeline
4
4
  class TracksHelperTest < ActionView::TestCase
5
5
  end
6
6
  end
@@ -0,0 +1,92 @@
1
+ require 'test_helper'
2
+ require 'debugger'
3
+ module ModelsTimeline
4
+ class TrackTest < ActiveSupport::TestCase
5
+ self.use_transactional_fixtures = true
6
+
7
+ setup do
8
+ Track.destroy_all
9
+ end
10
+
11
+ def attr
12
+ {name: 'angelo', days: 2, done: true}
13
+ end
14
+
15
+ def new_project
16
+ Project.create!(attr)
17
+ end
18
+
19
+ test "test model addition" do
20
+ assert_kind_of Class, Project
21
+ assert_respond_to Project, :timeline
22
+ assert_respond_to Project, :timeline, {}
23
+ end
24
+
25
+ test "track all model attributes" do
26
+ Project.timeline
27
+ project = new_project
28
+
29
+ track = Track.first
30
+ assert_equal Track.all.count, 1
31
+ assert_equal track.model_name, 'Project'
32
+ #test if all attributes are correctly stored
33
+ attr.each_key do |k|
34
+ assert_equal track.model_changes[k], [nil, attr[k]]
35
+ end
36
+
37
+ #update
38
+ project.update_attributes({name: 'pippo'})
39
+ track = Track.first
40
+ assert_equal Track.all.count, 2
41
+ assert_equal track.model_name, 'Project'
42
+ assert_equal track.action, 'updated'
43
+ #test track added on update
44
+ assert_equal track.model_changes.size, 1
45
+ debugger
46
+ assert_equal track.model_changes[:name], ['angelo','pippo']
47
+
48
+ #destroy
49
+ project.destroy
50
+ track = Track.first
51
+ assert_equal Track.all.count, 3
52
+ assert_equal track.model_name, 'Project'
53
+ assert_equal track.action, 'destroyed'
54
+ #test track added on update
55
+ assert_equal track.model_changes.size, 0
56
+ end
57
+
58
+ test "track all some attributes" do
59
+ Project.reset_callbacks :create
60
+ Project.reset_callbacks :update
61
+
62
+ Project.timeline(:name,:days)
63
+
64
+ project = Project.create!({name: 'angelo', days: 2, done: true})
65
+ assert_equal Project.all.count, 1
66
+ track = Track.first
67
+ assert_equal track.model_name, 'Project'
68
+ #test if all attributes are correctly stored
69
+ [:name, :days].each do |k|
70
+ assert_equal track.model_changes[k], [nil, attr[k]]
71
+ end
72
+
73
+ #update
74
+ project.update_attributes({name: 'tom', days: 4})
75
+ track = Track.first
76
+ assert_equal track.model_name, 'Project'
77
+ assert_equal track.action, 'updated'
78
+ #test track added on update
79
+ assert_equal track.model_changes.size, 2
80
+ assert_equal track.model_changes[:name], ['angelo','tom']
81
+ assert_equal track.model_changes[:days], [2,4]
82
+
83
+ #destroy
84
+ project.destroy
85
+ track = Track.first
86
+ assert_equal track.model_name, 'Project'
87
+ assert_equal track.action, 'destroyed'
88
+ #test track added on update
89
+ end
90
+
91
+ end
92
+ end
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
  require 'debugger'
3
- module Timeline
3
+ module ModelsTimeline
4
4
  class TrackTest < ActiveSupport::TestCase
5
5
  self.use_transactional_fixtures = true
6
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: models_timeline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.2
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -71,17 +71,26 @@ files:
71
71
  - app/assets/stylesheets/scaffold.css
72
72
  - app/assets/stylesheets/timeline/application.css
73
73
  - app/assets/stylesheets/timeline/tracks.css
74
+ - app/controllers/models_timeline/application_controller.rb
75
+ - app/controllers/models_timeline/tracks_controller.rb
74
76
  - app/controllers/timeline/application_controller.rb
75
77
  - app/controllers/timeline/tracks_controller.rb
76
78
  - app/helpers/timeline/application_helper.rb
77
79
  - app/helpers/timeline/tracks_helper.rb
80
+ - app/models/models_timeline/track.rb
78
81
  - app/models/timeline/track.rb
79
82
  - app/views/layouts/timeline/application.html.erb
80
- - app/views/timeline/tracks/_track.erb
81
- - app/views/timeline/tracks/index.html.erb
82
- - app/views/timeline/tracks/show.html.erb
83
+ - app/views/models_timeline/tracks/_track.erb
84
+ - app/views/models_timeline/tracks/index.html.erb
85
+ - app/views/models_timeline/tracks/show.html.erb
83
86
  - config/routes.rb
87
+ - db/migrate/20121227092029_create_models_timeline_tracks.rb
84
88
  - db/migrate/20121227092029_create_timeline_tracks.rb
89
+ - lib/models_timeline/engine.rb
90
+ - lib/models_timeline/model_additions.rb
91
+ - lib/models_timeline/railtie.rb
92
+ - lib/models_timeline/version.rb
93
+ - lib/models_timeline.rb
85
94
  - lib/tasks/timeline_tasks.rake
86
95
  - lib/timeline/engine.rb
87
96
  - lib/timeline/model_additions.rb
@@ -115,6 +124,7 @@ files:
115
124
  - test/dummy/config.ru
116
125
  - test/dummy/db/migrate/20121227094706_create_projects.rb
117
126
  - test/dummy/db/schema.rb
127
+ - test/dummy/log/development.log
118
128
  - test/dummy/public/404.html
119
129
  - test/dummy/public/422.html
120
130
  - test/dummy/public/500.html
@@ -129,6 +139,7 @@ files:
129
139
  - test/integration/navigation_test.rb
130
140
  - test/model/helpers/timeline/timelines_helper_test.rb
131
141
  - test/model/helpers/tracks_helper_test.rb
142
+ - test/model/modesl_timeline/track_test.rb
132
143
  - test/model/timeline/track_test.rb
133
144
  - test/test_helper.rb
134
145
  - test/timeline_test.rb
@@ -181,6 +192,7 @@ test_files:
181
192
  - test/dummy/config.ru
182
193
  - test/dummy/db/migrate/20121227094706_create_projects.rb
183
194
  - test/dummy/db/schema.rb
195
+ - test/dummy/log/development.log
184
196
  - test/dummy/public/404.html
185
197
  - test/dummy/public/422.html
186
198
  - test/dummy/public/500.html
@@ -195,6 +207,7 @@ test_files:
195
207
  - test/integration/navigation_test.rb
196
208
  - test/model/helpers/timeline/timelines_helper_test.rb
197
209
  - test/model/helpers/tracks_helper_test.rb
210
+ - test/model/modesl_timeline/track_test.rb
198
211
  - test/model/timeline/track_test.rb
199
212
  - test/test_helper.rb
200
213
  - test/timeline_test.rb