log_book 0.0.1 → 0.6.3

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
+ SHA256:
3
+ metadata.gz: eeea49aa6e73b24e651d354848d74e5cab562bc9fd4cfa41179824249ee3bdba
4
+ data.tar.gz: e2ba899bdb4ec534d3f97ce6164c2c7e62ff0b9667f1396917f09803e8f74fa3
5
+ SHA512:
6
+ metadata.gz: 6313ed2fa0546d058d45930675dd0c77b11602a5beb5365d024c90adfd96eda90ee80887ec3ea1d1eb975cb1eb2948e544d8b869f28ab576779b2a5a9fe7bb9e
7
+ data.tar.gz: f3d02963f53897fcd2347054f4ea29ccd48a9319839921ccbb5c51b190db72ee1155fb7cf78eee471a93621f3a1dc805ea08d8fc19e3699d0a8796981fb33961
data/.gitignore CHANGED
@@ -3,7 +3,6 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
- Gemfile.lock
7
6
  InstalledFiles
8
7
  _yardoc
9
8
  coverage
@@ -15,4 +14,4 @@ spec/reports
15
14
  test/tmp
16
15
  test/version_tmp
17
16
  tmp
18
- db/*sqlite
17
+ test/db/*sqlite
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.1
4
+ - 2.5.1
5
+
6
+ matrix:
7
+ fast_finish: true
8
+ allow_failures:
9
+ - rvm: 2.5.1
data/Gemfile CHANGED
@@ -1,4 +1,2 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in log_book.gemspec
1
+ source "https://rubygems.org"
4
2
  gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,155 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ log_book (0.5.2)
5
+ acts-as-taggable-on (>= 8.0)
6
+ rails (~> 6.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (6.1.4.1)
12
+ actionpack (= 6.1.4.1)
13
+ activesupport (= 6.1.4.1)
14
+ nio4r (~> 2.0)
15
+ websocket-driver (>= 0.6.1)
16
+ actionmailbox (6.1.4.1)
17
+ actionpack (= 6.1.4.1)
18
+ activejob (= 6.1.4.1)
19
+ activerecord (= 6.1.4.1)
20
+ activestorage (= 6.1.4.1)
21
+ activesupport (= 6.1.4.1)
22
+ mail (>= 2.7.1)
23
+ actionmailer (6.1.4.1)
24
+ actionpack (= 6.1.4.1)
25
+ actionview (= 6.1.4.1)
26
+ activejob (= 6.1.4.1)
27
+ activesupport (= 6.1.4.1)
28
+ mail (~> 2.5, >= 2.5.4)
29
+ rails-dom-testing (~> 2.0)
30
+ actionpack (6.1.4.1)
31
+ actionview (= 6.1.4.1)
32
+ activesupport (= 6.1.4.1)
33
+ rack (~> 2.0, >= 2.0.9)
34
+ rack-test (>= 0.6.3)
35
+ rails-dom-testing (~> 2.0)
36
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
37
+ actiontext (6.1.4.1)
38
+ actionpack (= 6.1.4.1)
39
+ activerecord (= 6.1.4.1)
40
+ activestorage (= 6.1.4.1)
41
+ activesupport (= 6.1.4.1)
42
+ nokogiri (>= 1.8.5)
43
+ actionview (6.1.4.1)
44
+ activesupport (= 6.1.4.1)
45
+ builder (~> 3.1)
46
+ erubi (~> 1.4)
47
+ rails-dom-testing (~> 2.0)
48
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
49
+ activejob (6.1.4.1)
50
+ activesupport (= 6.1.4.1)
51
+ globalid (>= 0.3.6)
52
+ activemodel (6.1.4.1)
53
+ activesupport (= 6.1.4.1)
54
+ activerecord (6.1.4.1)
55
+ activemodel (= 6.1.4.1)
56
+ activesupport (= 6.1.4.1)
57
+ activestorage (6.1.4.1)
58
+ actionpack (= 6.1.4.1)
59
+ activejob (= 6.1.4.1)
60
+ activerecord (= 6.1.4.1)
61
+ activesupport (= 6.1.4.1)
62
+ marcel (~> 1.0.0)
63
+ mini_mime (>= 1.1.0)
64
+ activesupport (6.1.4.1)
65
+ concurrent-ruby (~> 1.0, >= 1.0.2)
66
+ i18n (>= 1.6, < 2)
67
+ minitest (>= 5.1)
68
+ tzinfo (~> 2.0)
69
+ zeitwerk (~> 2.3)
70
+ acts-as-taggable-on (8.1.0)
71
+ activerecord (>= 5.0, < 6.2)
72
+ assert_difference (1.0.0)
73
+ activesupport (>= 3.0.0)
74
+ builder (3.2.4)
75
+ concurrent-ruby (1.1.9)
76
+ crass (1.0.6)
77
+ erubi (1.10.0)
78
+ globalid (0.6.0)
79
+ activesupport (>= 5.0)
80
+ i18n (1.8.11)
81
+ concurrent-ruby (~> 1.0)
82
+ loofah (2.12.0)
83
+ crass (~> 1.0.2)
84
+ nokogiri (>= 1.5.9)
85
+ mail (2.7.1)
86
+ mini_mime (>= 0.1.1)
87
+ marcel (1.0.2)
88
+ method_source (1.0.0)
89
+ mini_mime (1.1.2)
90
+ minitest (5.14.4)
91
+ mocha (1.13.0)
92
+ nio4r (2.5.8)
93
+ nokogiri (1.12.5-x86_64-darwin)
94
+ racc (~> 1.4)
95
+ racc (1.6.0)
96
+ rack (2.2.3)
97
+ rack-test (1.1.0)
98
+ rack (>= 1.0, < 3)
99
+ rails (6.1.4.1)
100
+ actioncable (= 6.1.4.1)
101
+ actionmailbox (= 6.1.4.1)
102
+ actionmailer (= 6.1.4.1)
103
+ actionpack (= 6.1.4.1)
104
+ actiontext (= 6.1.4.1)
105
+ actionview (= 6.1.4.1)
106
+ activejob (= 6.1.4.1)
107
+ activemodel (= 6.1.4.1)
108
+ activerecord (= 6.1.4.1)
109
+ activestorage (= 6.1.4.1)
110
+ activesupport (= 6.1.4.1)
111
+ bundler (>= 1.15.0)
112
+ railties (= 6.1.4.1)
113
+ sprockets-rails (>= 2.0.0)
114
+ rails-dom-testing (2.0.3)
115
+ activesupport (>= 4.2.0)
116
+ nokogiri (>= 1.6)
117
+ rails-html-sanitizer (1.4.2)
118
+ loofah (~> 2.3)
119
+ railties (6.1.4.1)
120
+ actionpack (= 6.1.4.1)
121
+ activesupport (= 6.1.4.1)
122
+ method_source
123
+ rake (>= 0.13)
124
+ thor (~> 1.0)
125
+ rake (13.0.6)
126
+ sprockets (4.0.2)
127
+ concurrent-ruby (~> 1.0)
128
+ rack (> 1, < 3)
129
+ sprockets-rails (3.4.1)
130
+ actionpack (>= 5.2)
131
+ activesupport (>= 5.2)
132
+ sprockets (>= 3.0.0)
133
+ sqlite3 (1.4.2)
134
+ thor (1.1.0)
135
+ tzinfo (2.0.4)
136
+ concurrent-ruby (~> 1.0)
137
+ websocket-driver (0.7.5)
138
+ websocket-extensions (>= 0.1.0)
139
+ websocket-extensions (0.1.5)
140
+ zeitwerk (2.5.1)
141
+
142
+ PLATFORMS
143
+ x86_64-darwin-19
144
+
145
+ DEPENDENCIES
146
+ assert_difference (~> 1.0)
147
+ bundler (>= 2.0)
148
+ log_book!
149
+ minitest (>= 5.1)
150
+ mocha (~> 1.13)
151
+ rake (>= 0.13)
152
+ sqlite3 (~> 1.0)
153
+
154
+ BUNDLED WITH
155
+ 2.2.32
data/README.md CHANGED
@@ -8,14 +8,22 @@ Add this line to your application's Gemfile:
8
8
 
9
9
  gem "log_book"
10
10
 
11
+ As the Model should be loaded after ActiveReccord has established the connection you have to add this:
12
+
13
+ # config/application.rb
14
+ config.after_initialize do
15
+ require "log_book/event"
16
+ end
17
+
11
18
  ### Create the table
12
19
 
13
- rails generate guinea_pig:migration
20
+ rails generate log_book:migration
14
21
  rake db:migrate
15
22
 
16
23
  ### ActsOnTaggableOn dependency
17
24
 
18
25
  rails generate acts_as_taggable_on:migration
26
+ # rake acts_as_taggable_on_engine:install:migrations # for version '~> 4.0' or superior
19
27
  rake db:migrate
20
28
 
21
29
  ## Usage
@@ -31,7 +39,7 @@ For example:
31
39
  ## ActiveRecord integration
32
40
 
33
41
  class MyModel < ActiveRecord::Base
34
- log_book_log_book
42
+ log_book
35
43
  end
36
44
 
37
45
  MyModel.create! # => LogBook created
@@ -43,6 +51,47 @@ If you want to include _who executes the action_ use the special attribute `log_
43
51
  my_model.log_book_historian = current_user
44
52
  my_model.save!
45
53
 
54
+ If you want to _mute_ a model change:
55
+
56
+ my_model.log_book_mute = true
57
+ my_model.save! # No LogBook::Event will be generated
58
+
59
+ If you want to _mute_ LogBook globally:
60
+
61
+ LogBook.mute = true
62
+ my_model.save! # No LogBook::Event will be generated
63
+
64
+ If you want to _ignore_ some fields from the changes Event:
65
+
66
+ class MyModel < ActiveRecord::Base
67
+ log_book :ignore => [:my_counter]
68
+ end
69
+
70
+ my_model.update_atttibtes!(:my_counter => 9) # No LogBook::Event will be generated
71
+
72
+ If you want _LogBook::Events_ to be destroyed on _Model_ destroy:
73
+
74
+ class MyModel < ActiveRecord::Base
75
+ log_book :dependent => :destroy
76
+ end
77
+
78
+ In other case the _LogBook::Events_ will remain after _Model_ destroyed.
79
+
80
+ ## Rails Integration
81
+
82
+ Check this example project to see how LogBook is integrated:
83
+
84
+ - [Skeleton](https://github.com/fguillen/Skeleton)
85
+
86
+ ## TODO
87
+
88
+ Use block configuration instead of `model.log_book_historian` do something like:
89
+
90
+ LogBook.conf(:log_book_historian => user) do
91
+ model.save!
92
+ end
93
+
94
+
46
95
  ## Sate of the art
47
96
 
48
97
  Beta version but already used in production environments
@@ -1,9 +1,9 @@
1
1
  require "rails/generators"
2
2
 
3
- class GuineaPig::MigrationGenerator < Rails::Generators::Base
3
+ class LogBook::MigrationGenerator < Rails::Generators::Base
4
4
  include Rails::Generators::Migration
5
5
 
6
- desc "Generates migration for ABTest model"
6
+ desc "Generates migration for LogBook::Event model"
7
7
 
8
8
  def self.source_root
9
9
  File.join(File.dirname(__FILE__), "templates")
@@ -20,6 +20,6 @@ class GuineaPig::MigrationGenerator < Rails::Generators::Base
20
20
  end
21
21
 
22
22
  def copy_migration
23
- migration_template "create_log_books.rb", "db/migrate/create_log_books.rb"
23
+ migration_template "create_log_book_events.rb", "db/migrate/create_log_book_events.rb"
24
24
  end
25
25
  end
@@ -1,17 +1,20 @@
1
- class CreateLogBookEvents < ActiveRecord::Migration
1
+ class CreateLogBookEvents < ActiveRecord::Migration[4.2]
2
2
  def self.up
3
3
  create_table :log_book_events do |t|
4
4
  t.integer :historian_id
5
5
  t.string :historian_type
6
6
  t.integer :historizable_id
7
7
  t.string :historizable_type
8
- t.string :text, :null => false
8
+ t.text :differences, :limit => 16777215 # mediumtext
9
9
 
10
- t.timestamps
10
+ t.timestamps :null => false
11
11
  end
12
+
13
+ add_index :log_book_events, [:historizable_id, :historizable_type, :created_at], :name => "index_log_book_events_on_historizable_and_created_at"
14
+ add_index :log_book_events, [:created_at]
12
15
  end
13
16
 
14
17
  def self.down
15
- drop_table :log_books
18
+ drop_table :log_book_events
16
19
  end
17
- end
20
+ end
@@ -1,12 +1,12 @@
1
1
  class LogBook::Event < ::ActiveRecord::Base
2
2
  self.table_name = "log_book_events"
3
3
 
4
- attr_accessible :historian, :historizable, :text, :tag_list
5
-
6
4
  acts_as_taggable
7
5
 
8
- belongs_to :historian, :polymorphic => true
6
+ belongs_to :historian, :polymorphic => true, :optional => true
9
7
  belongs_to :historizable, :polymorphic => true
10
8
 
11
- validates :text, :presence => true
9
+ scope :by_recent, -> { order("id desc") }
10
+
11
+ serialize :differences, JSON
12
12
  end
@@ -5,41 +5,44 @@ module LogBook::Plugin
5
5
  end
6
6
 
7
7
  module ClassMethods
8
- def log_book
8
+ def log_book(opts = {})
9
9
  after_create :log_book_event_on_create
10
10
  after_update :log_book_event_on_update
11
- after_destroy :log_book_event_on_destroy
11
+ before_destroy :log_book_event_on_destroy
12
+
13
+ has_many :log_book_events, :class_name => "LogBook::Event", :as => :historizable, :dependent => (opts[:dependent] || :nullify)
12
14
 
13
15
  attr_accessor :log_book_historian
16
+ attr_accessor :log_book_mute
17
+ cattr_accessor :log_book_options
18
+
19
+ self.log_book_options = opts
20
+ self.log_book_options[:ignore] ||= []
21
+ self.log_book_options[:ignore] << :updated_at # ignoring noisy field
14
22
  end
15
23
  end
16
24
 
17
25
  module InstanceMethods
18
26
  def log_book_event_on_create
19
- LogBook.created(self.log_book_historian, self)
27
+ LogBook.created(self.log_book_historian, self) if !self.log_book_mute
20
28
  end
21
29
 
22
30
  def log_book_event_on_update
23
- LogBook.updated(self.log_book_historian, self)
31
+ # TODO: this line of code is duplicated
32
+ if ActiveRecord::VERSION::STRING.to_f >= 5.1
33
+ clean_changes = saved_changes.select { |k,v| !self.log_book_options[:ignore].include? k.to_sym }
34
+ else
35
+ clean_changes = changes.select { |k,v| !self.log_book_options[:ignore].include? k.to_sym }
36
+ end
37
+ LogBook.updated(self.log_book_historian, self) if !clean_changes.empty? and !self.log_book_mute
24
38
  end
25
39
 
26
40
  def log_book_event_on_destroy
27
- LogBook.destroyed(self.log_book_historian, self)
28
- end
29
-
30
- def pretty_changes
31
- result =
32
- self.previous_changes.reject { |k,v| k == "updated_at" || k =~ /password/ || k == "perishable_token" || k == "persistence_token" }.map do |k,v|
33
- old_value = v[0]
34
- new_value = v[1]
35
-
36
- old_value = old_value.to_s( :localdb ) if old_value.instance_of? ActiveSupport::TimeWithZone
37
- new_value = new_value.to_s( :localdb ) if new_value.instance_of? ActiveSupport::TimeWithZone
38
-
39
- "#{k}[#{old_value} -> #{new_value}]"
40
- end.join( ", " )
41
-
42
- result
41
+ LogBook.destroyed(self.log_book_historian, self) if !self.log_book_mute
43
42
  end
44
43
  end
45
- end
44
+ end
45
+
46
+ ActiveSupport.on_load(:active_record) do
47
+ include LogBook::Plugin
48
+ end
@@ -0,0 +1,27 @@
1
+ module LogBook::Utils
2
+ def self.pretty_changes(model)
3
+ # TODO: this line of code is duplicated
4
+ if ActiveRecord::VERSION::STRING.to_f >= 5.1
5
+ clean_changes = model.saved_changes.select { |k,v| !model.log_book_options[:ignore].include? k.to_sym }
6
+ else
7
+ clean_changes = model.changes.select { |k,v| !model.log_book_options[:ignore].include? k.to_sym }
8
+ end
9
+
10
+ result =
11
+ clean_changes.map do |k,v|
12
+ old_value = v[0]
13
+ new_value = v[1]
14
+
15
+ old_value = old_value.to_s( :localdb ) if old_value.instance_of? ActiveSupport::TimeWithZone
16
+ new_value = new_value.to_s( :localdb ) if new_value.instance_of? ActiveSupport::TimeWithZone
17
+
18
+ {
19
+ "key" => k,
20
+ "before" => old_value,
21
+ "after" => new_value
22
+ }
23
+ end
24
+
25
+ result
26
+ end
27
+ end
@@ -1,3 +1,3 @@
1
1
  module LogBook
2
- VERSION = "0.0.1"
2
+ VERSION = "0.6.3"
3
3
  end
data/lib/log_book.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  require "active_record"
2
+ require "active_support/core_ext/module"
2
3
  require "acts-as-taggable-on"
3
4
  require_relative "log_book/version"
4
- require_relative "log_book/event"
5
5
  require_relative "log_book/plugin"
6
-
6
+ require_relative "log_book/utils"
7
7
 
8
8
  module LogBook
9
9
  OPERATIONS = {
@@ -12,7 +12,11 @@ module LogBook
12
12
  :destroy => "destroy"
13
13
  }
14
14
 
15
- def self.event(historian, historizable, text, tag_list)
15
+ @@muted = false
16
+
17
+ def self.event(historian, historizable, differences, tag_list)
18
+ return if @@muted
19
+
16
20
  tag_list_composed = []
17
21
  tag_list_composed << scope_tag(historian) if historian
18
22
  tag_list_composed << kind_tag(historizable) if historizable
@@ -21,25 +25,33 @@ module LogBook
21
25
  LogBook::Event.create!(
22
26
  :historian => historian,
23
27
  :historizable => historizable,
24
- :text => text,
28
+ :differences => differences,
25
29
  :tag_list => tag_list_composed
26
30
  )
27
31
  end
28
32
 
33
+ def self.muted=(value)
34
+ @@muted = value
35
+ end
36
+
37
+ def self.muted
38
+ @@muted
39
+ end
40
+
41
+ private
42
+
29
43
  def self.created(historian, historizable)
30
- LogBook.event(historian, historizable, "#{historizable.class.name} created", LogBook::OPERATIONS[:create])
44
+ LogBook.event(historian, historizable, nil, LogBook::OPERATIONS[:create])
31
45
  end
32
46
 
33
47
  def self.updated(historian, historizable)
34
- LogBook.event(historian, historizable, "#{historizable.class.name} updated [#{historizable.pretty_changes}]", LogBook::OPERATIONS[:update])
48
+ LogBook.event(historian, historizable, LogBook::Utils.pretty_changes(historizable), LogBook::OPERATIONS[:update])
35
49
  end
36
50
 
37
51
  def self.destroyed(historian, historizable)
38
- LogBook.event(historian, historizable, "#{historizable.class.name} destroyed", LogBook::OPERATIONS[:destroy])
52
+ LogBook.event(historian, historizable, nil, LogBook::OPERATIONS[:destroy])
39
53
  end
40
54
 
41
- private
42
-
43
55
  def self.scope_tag(historian)
44
56
  historian.class.name.underscore
45
57
  end
@@ -48,8 +60,3 @@ module LogBook
48
60
  historizable.class.name.underscore
49
61
  end
50
62
  end
51
-
52
- ActiveSupport.on_load(:active_record) do
53
- include LogBook::Plugin
54
- end
55
-
data/log_book.gemspec CHANGED
@@ -18,12 +18,13 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_runtime_dependency "rails", "~> 3.0"
22
- spec.add_runtime_dependency "acts-as-taggable-on"
21
+ spec.add_runtime_dependency "rails", "~> 6.0"
22
+ spec.add_runtime_dependency "acts-as-taggable-on", ">= 8.0"
23
23
 
24
- spec.add_development_dependency "bundler", "~> 1.3"
25
- spec.add_development_dependency "rake"
26
- spec.add_development_dependency "mocha"
27
- spec.add_development_dependency "sqlite3"
28
- spec.add_development_dependency "assert_difference"
24
+ spec.add_development_dependency "bundler", ">= 2.0"
25
+ spec.add_development_dependency "rake", ">= 0.13"
26
+ spec.add_development_dependency "mocha", "~> 1.13"
27
+ spec.add_development_dependency "sqlite3", "~> 1.0"
28
+ spec.add_development_dependency "assert_difference", "~> 1.0"
29
+ spec.add_development_dependency "minitest", ">= 5.1"
29
30
  end
@@ -1,31 +1,149 @@
1
1
  require_relative "test_helper"
2
2
 
3
- class LogBookTest < MiniTest::Unit::TestCase
3
+ class LogBookTest < MiniTest::Test
4
4
  def setup
5
5
  LogBook::Event.destroy_all
6
6
  User.destroy_all
7
7
  Item.destroy_all
8
8
 
9
9
  @user = User.create!(:name => "User Name")
10
- @item = Item.create!(:title => "Item Title")
10
+ @item = Item.create!(:title => "Item Title", :log_book_historian => @user)
11
11
  end
12
12
 
13
- def test_event_on_create
14
- item = Item.new(:log_book_historian => @user)
15
- LogBook.expects(:created).with(@user, item)
13
+ def test_event
14
+ assert_difference "LogBook::Event.count", 1 do
15
+ LogBook.event(@user, @item, ["DIFFERENCE"], LogBook::OPERATIONS[:create])
16
+ end
17
+
18
+ log_book_event = LogBook::Event.last
19
+ assert_equal(@user, log_book_event.historian)
20
+ assert_equal(@item, log_book_event.historizable)
21
+ assert_equal(["DIFFERENCE"], log_book_event.differences)
22
+ assert_equal(["user", "item", "create"].sort, log_book_event.tag_list.sort)
23
+ end
24
+
25
+ def test_event_with_nils
26
+ assert_difference "LogBook::Event.count", 1 do
27
+ LogBook.event(nil, nil, ["DIFFERENCE"], nil)
28
+ end
29
+
30
+ log_book_event = LogBook::Event.last
31
+ assert_nil(log_book_event.historian)
32
+ assert_nil(log_book_event.historizable)
33
+ assert_equal(["DIFFERENCE"], log_book_event.differences)
34
+ assert_equal([], log_book_event.tag_list)
35
+ end
36
+
37
+ def test_created
38
+ item = Item.new(:title => "Item Title")
39
+ item.log_book_historian = @user
40
+
41
+ LogBook.expects(:event).with(@user, item, nil, LogBook::OPERATIONS[:create])
42
+
16
43
  item.save!
17
44
  end
18
45
 
19
- def test_event_on_update
20
- @item.log_book_historian = @user
21
- LogBook.expects(:updated).with(@user, @item)
22
- @item.update_attributes(:title => "Other Title")
46
+ def test_created_when_muted
47
+ item = Item.new(:title => "Item Title")
48
+ item.log_book_mute = true
49
+
50
+ LogBook.expects(:event).never
51
+
52
+ item.save!
23
53
  end
24
54
 
25
- def test_event_on_destroy
26
- @item.log_book_historian = @user
27
- LogBook.expects(:destroyed).with(@user, @item)
55
+ def test_updated
56
+ differences = [
57
+ {
58
+ "key" => "title",
59
+ "before" => "Item Title",
60
+ "after" => "Other Title"
61
+ }
62
+ ]
63
+
64
+ LogBook.expects(:event).with(@user, @item, differences, LogBook::OPERATIONS[:update])
65
+
66
+ @item.update!(:title => "Other Title")
67
+ end
68
+
69
+ def test_updated_when_muted
70
+ LogBook.expects(:event).never
71
+
72
+ @item.log_book_mute = true
73
+ @item.update!(:title => "Other Title")
74
+ end
75
+
76
+ def test_updated_with_ignore_fields
77
+ differences = [
78
+ {
79
+ "key" => "title",
80
+ "before" => "Item Title",
81
+ "after" => "Other Title"
82
+ }
83
+ ]
84
+
85
+ item_with_opts = ItemWithOpts.create!(:title => "Item Title", :my_counter => 0)
86
+ LogBook.expects(:event).with(@user, item_with_opts, differences, LogBook::OPERATIONS[:update])
87
+
88
+ item_with_opts.log_book_historian = @user
89
+ item_with_opts.update!(:title => "Other Title", :my_counter => 10)
90
+ end
91
+
92
+ def test_item_destroyed
93
+ LogBook.expects(:event).with(@user, @item, nil, LogBook::OPERATIONS[:destroy])
94
+
28
95
  @item.destroy
29
96
  end
30
- end
31
97
 
98
+ def test_item_destroyed_when_muted
99
+ LogBook.expects(:event).never
100
+
101
+ @item.log_book_mute = true
102
+ @item.destroy
103
+ end
104
+
105
+ def test_has_many_log_book_events
106
+ LogBook::Event.destroy_all
107
+
108
+ log_book_event_1 = LogBook.event(@user, @item, nil, LogBook::OPERATIONS[:create])
109
+ log_book_event_2 = LogBook.event(@user, @item, nil, LogBook::OPERATIONS[:update])
110
+
111
+ @item.reload
112
+
113
+ assert_equal(2, @item.log_book_events.count)
114
+ assert_equal(log_book_event_1, @item.log_book_events.first)
115
+ assert_equal(log_book_event_2, @item.log_book_events.last)
116
+ end
117
+
118
+ def test_log_book_events_nullify_on_historizable_destroy
119
+ log_book_event = LogBook.event(@user, @item, nil, LogBook::OPERATIONS[:create])
120
+
121
+ @item.destroy
122
+
123
+ log_book_event.reload
124
+
125
+ assert_equal(true, log_book_event.historizable_id.nil?)
126
+ assert_equal(true, log_book_event.historizable.nil?)
127
+ assert_equal(true, log_book_event.historizable_type.nil?)
128
+ end
129
+
130
+ def test_log_book_events_destroy_on_historizable_destroy
131
+ item_with_opts = ItemWithOpts.new
132
+ log_book_event = LogBook.event(@user, item_with_opts, nil, LogBook::OPERATIONS[:create])
133
+
134
+ item_with_opts.destroy
135
+
136
+ assert_equal(false, LogBook::Event.exists?(log_book_event.id))
137
+ end
138
+
139
+ def test_created_when_global_muted
140
+ item = Item.new(:title => "Item Title")
141
+ LogBook.muted = true
142
+
143
+ LogBook::Event.expects(:create!).never
144
+
145
+ item.save!
146
+
147
+ LogBook.muted = false
148
+ end
149
+ end
data/test/models.rb CHANGED
@@ -3,4 +3,8 @@ end
3
3
 
4
4
  class Item < ActiveRecord::Base
5
5
  log_book
6
+ end
7
+
8
+ class ItemWithOpts < Item
9
+ log_book(:dependent => :destroy, :ignore => [:my_counter])
6
10
  end
@@ -0,0 +1,30 @@
1
+ require_relative "test_helper"
2
+
3
+ class PluginTest < MiniTest::Test
4
+ def setup
5
+ LogBook::Event.destroy_all
6
+ User.destroy_all
7
+ Item.destroy_all
8
+
9
+ @user = User.create!(:name => "User Name")
10
+ @item = Item.create!(:title => "Item Title")
11
+ end
12
+
13
+ def test_event_on_create
14
+ item = Item.new(:log_book_historian => @user)
15
+ LogBook.expects(:created).with(@user, item)
16
+ item.save!
17
+ end
18
+
19
+ def test_event_on_update
20
+ @item.log_book_historian = @user
21
+ LogBook.expects(:updated).with(@user, @item)
22
+ @item.update!(:title => "Other Title")
23
+ end
24
+
25
+ def test_event_on_destroy
26
+ @item.log_book_historian = @user
27
+ LogBook.expects(:destroyed).with(@user, @item)
28
+ @item.destroy
29
+ end
30
+ end
data/test/schema.rb CHANGED
@@ -5,20 +5,28 @@ ActiveRecord::Schema.define :version => 0 do
5
5
 
6
6
  create_table :items, :force => true do |t|
7
7
  t.string :title
8
+ t.integer :my_counter
8
9
  end
9
10
 
10
11
  # acts-as-taggable-on
11
- # https://github.com/mbleigh/acts-as-taggable-on/blob/master/lib/generators/acts_as_taggable_on/migration/templates/active_record/migration.rb
12
- create_table :tags do |t|
12
+ # https://github.com/mbleigh/acts-as-taggable-on/blob/6e1837762f0d60edc9b6c2d92b5a9435f404173f/spec/internal/db/schema.rb
13
+ create_table :tags, force: true do |t|
13
14
  t.string :name
15
+ t.integer :taggings_count, default: 0
16
+ t.string :type
14
17
  end
18
+ add_index 'tags', ['name'], name: 'index_tags_on_name', unique: true
15
19
 
16
- create_table :taggings do |t|
20
+ create_table :taggings, force: true do |t|
17
21
  t.references :tag
18
- t.references :taggable, :polymorphic => true
19
- t.references :tagger, :polymorphic => true
20
- t.string :context, :limit => 128
22
+ t.references :taggable, polymorphic: true
23
+ t.references :tagger, polymorphic: true
24
+ t.string :context, limit: 128
21
25
 
22
26
  t.datetime :created_at
23
27
  end
28
+
29
+ add_index 'taggings',
30
+ ['tag_id', 'taggable_id', 'taggable_type', 'context', 'tagger_id', 'tagger_type'],
31
+ unique: true, name: 'taggings_idx'
24
32
  end
data/test/test_helper.rb CHANGED
@@ -1,10 +1,15 @@
1
- require "minitest/unit"
2
- require "minitest/autorun"
3
- require "mocha/setup"
4
- require "assert_difference"
1
+
5
2
  require "active_record"
3
+ require "active_support/core_ext/module"
4
+ require "assert_difference"
6
5
  require "acts-as-taggable-on"
7
6
 
7
+ # require "minitest/unit"
8
+ require "minitest/autorun"
9
+ require "mocha/mini_test"
10
+
11
+ require_relative "../lib/log_book"
12
+
8
13
  FileUtils.rm("#{File.dirname(__FILE__)}/db/log_book.sqlite", :force => true)
9
14
 
10
15
  ActiveRecord::Base.establish_connection(
@@ -12,15 +17,15 @@ ActiveRecord::Base.establish_connection(
12
17
  :database => "#{File.dirname(__FILE__)}/db/log_book.sqlite"
13
18
  )
14
19
 
20
+ require_relative "../lib/log_book/event"
21
+
15
22
  require_relative "../lib/generators/log_book/templates/create_log_book_events"
16
23
  CreateLogBookEvents.up
17
24
 
18
- require_relative "../lib/log_book"
19
-
20
25
  load("#{File.dirname(__FILE__)}/schema.rb")
21
26
  load("#{File.dirname(__FILE__)}/models.rb")
22
27
 
23
- class MiniTest::Unit::TestCase
28
+ class MiniTest::Test
24
29
  include AssertDifference
25
30
  FIXTURES = File.expand_path("#{File.dirname(__FILE__)}/fixtures")
26
31
  end
metadata CHANGED
@@ -1,93 +1,127 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log_book
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.6.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Fernando Guillen
9
- autorequire:
8
+ autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-03-11 00:00:00.000000000 Z
11
+ date: 2021-11-25 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rails
16
- requirement: &70266156350280 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
- version: '3.0'
19
+ version: '6.0'
22
20
  type: :runtime
23
21
  prerelease: false
24
- version_requirements: *70266156350280
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: acts-as-taggable-on
27
- requirement: &70266156349820 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
- - - ! '>='
31
+ - - ">="
31
32
  - !ruby/object:Gem::Version
32
- version: '0'
33
+ version: '8.0'
33
34
  type: :runtime
34
35
  prerelease: false
35
- version_requirements: *70266156349820
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '8.0'
36
41
  - !ruby/object:Gem::Dependency
37
42
  name: bundler
38
- requirement: &70266156349260 !ruby/object:Gem::Requirement
39
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
40
44
  requirements:
41
- - - ~>
45
+ - - ">="
42
46
  - !ruby/object:Gem::Version
43
- version: '1.3'
47
+ version: '2.0'
44
48
  type: :development
45
49
  prerelease: false
46
- version_requirements: *70266156349260
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
47
55
  - !ruby/object:Gem::Dependency
48
56
  name: rake
49
- requirement: &70266156348840 !ruby/object:Gem::Requirement
50
- none: false
57
+ requirement: !ruby/object:Gem::Requirement
51
58
  requirements:
52
- - - ! '>='
59
+ - - ">="
53
60
  - !ruby/object:Gem::Version
54
- version: '0'
61
+ version: '0.13'
55
62
  type: :development
56
63
  prerelease: false
57
- version_requirements: *70266156348840
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0.13'
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: mocha
60
- requirement: &70266156348380 !ruby/object:Gem::Requirement
61
- none: false
71
+ requirement: !ruby/object:Gem::Requirement
62
72
  requirements:
63
- - - ! '>='
73
+ - - "~>"
64
74
  - !ruby/object:Gem::Version
65
- version: '0'
75
+ version: '1.13'
66
76
  type: :development
67
77
  prerelease: false
68
- version_requirements: *70266156348380
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.13'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: sqlite3
71
- requirement: &70266156347940 !ruby/object:Gem::Requirement
72
- none: false
85
+ requirement: !ruby/object:Gem::Requirement
73
86
  requirements:
74
- - - ! '>='
87
+ - - "~>"
75
88
  - !ruby/object:Gem::Version
76
- version: '0'
89
+ version: '1.0'
77
90
  type: :development
78
91
  prerelease: false
79
- version_requirements: *70266156347940
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
80
97
  - !ruby/object:Gem::Dependency
81
98
  name: assert_difference
82
- requirement: &70266156347520 !ruby/object:Gem::Requirement
83
- none: false
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
84
107
  requirements:
85
- - - ! '>='
108
+ - - "~>"
86
109
  - !ruby/object:Gem::Version
87
- version: '0'
110
+ version: '1.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: minitest
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '5.1'
88
118
  type: :development
89
119
  prerelease: false
90
- version_requirements: *70266156347520
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '5.1'
91
125
  description: Storing an events log book
92
126
  email:
93
127
  - fguillen.mail@gmail.com
@@ -95,10 +129,10 @@ executables: []
95
129
  extensions: []
96
130
  extra_rdoc_files: []
97
131
  files:
98
- - .gitignore
99
- - .rvmrc
100
- - .rvmrc.example
132
+ - ".gitignore"
133
+ - ".travis.yml"
101
134
  - Gemfile
135
+ - Gemfile.lock
102
136
  - LICENSE.txt
103
137
  - README.md
104
138
  - Rakefile
@@ -107,45 +141,42 @@ files:
107
141
  - lib/log_book.rb
108
142
  - lib/log_book/event.rb
109
143
  - lib/log_book/plugin.rb
144
+ - lib/log_book/utils.rb
110
145
  - lib/log_book/version.rb
111
146
  - log_book.gemspec
112
147
  - test/db/.keep
113
- - test/db/log_book.sqlite
114
- - test/history_event_test.rb
115
148
  - test/log_book_test.rb
116
149
  - test/models.rb
150
+ - test/plugin_test.rb
117
151
  - test/schema.rb
118
152
  - test/test_helper.rb
119
153
  homepage: https://github.com/fguillen/LogBook
120
154
  licenses:
121
155
  - MIT
122
- post_install_message:
156
+ metadata: {}
157
+ post_install_message:
123
158
  rdoc_options: []
124
159
  require_paths:
125
160
  - lib
126
161
  required_ruby_version: !ruby/object:Gem::Requirement
127
- none: false
128
162
  requirements:
129
- - - ! '>='
163
+ - - ">="
130
164
  - !ruby/object:Gem::Version
131
165
  version: '0'
132
166
  required_rubygems_version: !ruby/object:Gem::Requirement
133
- none: false
134
167
  requirements:
135
- - - ! '>='
168
+ - - ">="
136
169
  - !ruby/object:Gem::Version
137
170
  version: '0'
138
171
  requirements: []
139
- rubyforge_project:
140
- rubygems_version: 1.8.15
141
- signing_key:
142
- specification_version: 3
172
+ rubygems_version: 3.2.22
173
+ signing_key:
174
+ specification_version: 4
143
175
  summary: Storing an events log book
144
176
  test_files:
145
177
  - test/db/.keep
146
- - test/db/log_book.sqlite
147
- - test/history_event_test.rb
148
178
  - test/log_book_test.rb
149
179
  - test/models.rb
180
+ - test/plugin_test.rb
150
181
  - test/schema.rb
151
182
  - test/test_helper.rb
data/.rvmrc DELETED
@@ -1 +0,0 @@
1
- rvm use --create 1.9.3-p286@log_book
data/.rvmrc.example DELETED
@@ -1 +0,0 @@
1
- rvm use --create 1.9.3-p286@log_book
Binary file
@@ -1,54 +0,0 @@
1
- require_relative "test_helper"
2
-
3
- class LogBookTest < MiniTest::Unit::TestCase
4
- def setup
5
- LogBook::Event.destroy_all
6
- User.destroy_all
7
- Item.destroy_all
8
-
9
- @user = User.create!(:name => "User Name")
10
- @item = Item.create!(:title => "Item Title")
11
- end
12
-
13
- def test_event
14
- assert_difference "LogBook::Event.count", 1 do
15
- LogBook.event(@user, @item, "Item wadus", LogBook::OPERATIONS[:create])
16
- end
17
-
18
- log_book = LogBook::Event.last
19
- assert_equal(@user, log_book.historian)
20
- assert_equal(@item, log_book.historizable)
21
- assert_equal("Item wadus", log_book.text)
22
- assert_equal(["user", "item", "create"].sort, log_book.tag_list.sort)
23
- end
24
-
25
- def test_event_with_nils
26
- assert_difference "LogBook::Event.count", 1 do
27
- LogBook.event(nil, nil, "Item wadus", nil)
28
- end
29
-
30
- log_book = LogBook::Event.last
31
- assert_equal(nil, log_book.historian)
32
- assert_equal(nil, log_book.historizable)
33
- assert_equal("Item wadus", log_book.text)
34
- assert_equal([], log_book.tag_list)
35
- end
36
-
37
- def test_created
38
- LogBook.expects(:event).with("historian", @item, "Item created", LogBook::OPERATIONS[:create])
39
- LogBook.created("historian", @item)
40
- end
41
-
42
- def test_updated
43
- @item.update_attributes!(:title => "Other Title")
44
-
45
- LogBook.expects(:event).with(@user, @item, "Item updated [title[Item Title -> Other Title]]", LogBook::OPERATIONS[:update])
46
- LogBook.updated(@user, @item)
47
- end
48
-
49
- def test_item_destroyed
50
- LogBook.expects(:event).with(@user, @item, "Item destroyed", LogBook::OPERATIONS[:destroy])
51
- LogBook.destroyed(@user, @item)
52
- end
53
- end
54
-