has_logs 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d0516f9656458af72a2042060e87ae6a7252fad6
4
- data.tar.gz: 1eb28b3e1cd5e84184191c4711a5df7f5466ece0
3
+ metadata.gz: 684408e9337f822cc71662e3ec453f2835bb90cc
4
+ data.tar.gz: fbb2ae959fc6a35956192512ce4173b760dfce63
5
5
  SHA512:
6
- metadata.gz: 162d25cbbf867d35ec009e726eb9b0ce7da855eceef0fba95df3338bcd058ae89e8e40ae1d62f21108c855fbeab1627b0c4e31ada17fbd7692eab8048c15107e
7
- data.tar.gz: aaffb45bcedd6103e14444d172d4dd59a336a0468ebcd33952b7f2a9c62efaa125b60b510aa9588bb1c9f35e9ae373adb90085a7320e5e3188649abd72b54ef0
6
+ metadata.gz: a5703d9075b5e7776ec7aea6908ee1f956af467ce79571f562146d60f3d9386a989552830e3ef7ccdc228a8fa923d0a8a25e2b69dd6219a0520e3a0e6077385d
7
+ data.tar.gz: c4f60a6f250708a0f8d9de94864a663c56c40219fb9746bf994c16029aad66c10f2ff009f2c0c2e2e2bfe41bc421dd200d7f421e1ece6f71b994df028485d5cb
data/.travis.yml CHANGED
@@ -1,5 +1,19 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
+ - 2.1.9
5
+ - 2.2.5
4
6
  - 2.3.1
7
+ gemfile:
8
+ - gemfiles/activerecord_4.2.5.gemfile
9
+ - gemfiles/activerecord_5.0.0.gemfile
10
+ matrix:
11
+ exclude:
12
+ - rvm: 2.1.9
13
+ gemfile: gemfiles/activerecord_5.0.0.gemfile
14
+ - rvm: 2.2.5
15
+ gemfile: gemfiles/activerecord_5.0.0.gemfile
16
+ - rvm: 2.3.1
17
+ gemfile: gemfiles/activerecord_4.2.5.gemfile
5
18
  before_install: gem install bundler -v 1.12.5
19
+ script: "bundle exec rspec"
data/Appraisals ADDED
@@ -0,0 +1,9 @@
1
+ appraise "activerecord-4.2.5" do
2
+ gem "activesupport", "4.2.5"
3
+ gem "activerecord", "4.2.5"
4
+ end
5
+
6
+ appraise "activerecord-5.0.0" do
7
+ gem "activesupport", "5.0.0"
8
+ gem "activerecord", "5.0.0"
9
+ end
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # HasLogs
2
2
 
3
- Logging your ActiveRecord model, and supply useful methods.
3
+ [![Build Status](https://travis-ci.org/isuke/has_logs.svg?branch=master)](https://travis-ci.org/isuke/has_logs)
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ Logging your ActiveRecord model, and supply useful methods.
6
6
 
7
7
  ## Installation
8
8
 
@@ -48,7 +48,7 @@ class CreateAllTables < ActiveRecord::Migration
48
48
  end
49
49
  ```
50
50
 
51
- ## Model Class
51
+ ### Model Class
52
52
 
53
53
  ```ruby
54
54
  class Article < ActiveRecord::Base
@@ -60,7 +60,7 @@ class ArticleLog < ActiveRecord::Base
60
60
  end
61
61
  ```
62
62
 
63
- ## Usage Examples
63
+ ### Usage Examples
64
64
 
65
65
  ```ruby
66
66
  article = Article.create(title: 'test1', content: 'demo1', public: false)
@@ -88,6 +88,173 @@ article.latest_log.prev
88
88
  => #<ArticleLog id: 2, article_id: 1, title: "test2", content: "demo2", created_at: "2015-01-04 03:36:51">
89
89
  ```
90
90
 
91
+ #### Originator Methods
92
+
93
+ | Methods | Describe |
94
+ | ------------- | ------------------- |
95
+ | logs | get the log list |
96
+ | latest_log | get the latest log |
97
+ | oldest_log | get the oldest log |
98
+
99
+ #### Log Methods
100
+
101
+ | Methods | Describe |
102
+ | ------------- | ------------------------ |
103
+ | originator | get the originator model |
104
+ | next | get a next log |
105
+ | prev | get a prev log |
106
+
107
+ ## Other Naming Example
108
+
109
+ ### Migration
110
+
111
+ ```ruby
112
+ class CreateAllTables < ActiveRecord::Migration
113
+ def self.up
114
+ create_table(:users) do |t|
115
+ t.timestamps
116
+ end
117
+
118
+ create_table(:user_histories) do |t|
119
+ t.integer :user_id, null: false
120
+ t.string :name, null: false
121
+ t.datetime :created_at
122
+ end
123
+ add_index :user_logs, :user_id
124
+ add_index :user_logs, [:user_id, :created_at], unique: true
125
+ end
126
+ end
127
+ ```
128
+
129
+ ### Model Class
130
+
131
+ ```ruby
132
+ class User < ActiveRecord::Base
133
+ has_logs_as 'UserHistory'
134
+ end
135
+
136
+ class UserHistory < ActiveRecord::Base
137
+ act_as_log_of 'User'
138
+ end
139
+ ```
140
+
141
+ ## Duplicate Type Example
142
+
143
+ ### Migration
144
+
145
+ ```ruby
146
+ class CreateAllTables < ActiveRecord::Migration
147
+ def self.up
148
+ create_table(:tasks) do |t|
149
+ t.string :name, null: false
150
+ t.timestamps
151
+ end
152
+ create_table(:task_logs) do |t|
153
+ t.integer :task_id, null: false
154
+ t.string :name, null: false
155
+ t.datetime :created_at
156
+ end
157
+ add_index :task_logs, :task_id
158
+ add_index :task_logs, [:task_id, :created_at], unique: true
159
+ end
160
+ end
161
+ ```
162
+
163
+ ### Model Class
164
+
165
+ ```ruby
166
+ class Task < ActiveRecord::Base
167
+ has_logs have_type: :duplicate
168
+ end
169
+ class TaskLog < ActiveRecord::Base
170
+ act_as_log
171
+ end
172
+ ```
173
+
174
+ ### Usage Examples
175
+
176
+ ```
177
+ task = Task.create(name: 'name1')
178
+ task.attributes = { name: 'name2' } ; task.save!
179
+ task.attributes = { name: 'name3' } ; task.save!
180
+
181
+ Task.all
182
+ =>
183
+ +----+-------+
184
+ | id | name |
185
+ +----+-------+
186
+ | 1 | name3 |
187
+ +----+-------+
188
+
189
+ TaskLog.all
190
+ =>
191
+ +----+---------+-------+
192
+ | id | task_id | name |
193
+ +----+---------+-------+
194
+ | 1 | 1 | name1 |
195
+ | 2 | 1 | name2 |
196
+ | 3 | 1 | name3 |
197
+ +----+---------+-------+
198
+ ```
199
+
200
+ ## Mutual Type Example
201
+
202
+ ### Migration
203
+
204
+ ```ruby
205
+ class CreateAllTables < ActiveRecord::Migration
206
+ def self.up
207
+ create_table(:posts) do |t|
208
+ t.string :name, null: false
209
+ t.timestamps
210
+ end
211
+ create_table(:post_logs) do |t|
212
+ t.integer :post_id, null: false
213
+ t.string :name, null: false
214
+ t.datetime :created_at
215
+ end
216
+ add_index :post_logs, :post_id
217
+ add_index :post_logs, [:post_id, :created_at], unique: true
218
+ end
219
+ end
220
+ ```
221
+
222
+ ### Model Class
223
+
224
+ ```ruby
225
+ class Post < ActiveRecord::Base
226
+ has_logs have_type: :mutual
227
+ end
228
+ class PostLog < ActiveRecord::Base
229
+ act_as_log
230
+ end
231
+ ```
232
+
233
+ ### Usage Examples
234
+
235
+ ```
236
+ post = Post.create(name: 'name1')
237
+ post.attributes = { name: 'name2' } ; post.save!
238
+ post.attributes = { name: 'name3' } ; post.save!
239
+
240
+ Post.all
241
+ =>
242
+ +----+-------+
243
+ | id | name |
244
+ +----+-------+
245
+ | 1 | name3 |
246
+ +----+-------+
247
+
248
+ PostLog.all
249
+ =>
250
+ +----+---------+-------+
251
+ | id | post_id | name |
252
+ +----+---------+-------+
253
+ | 1 | 1 | name1 |
254
+ | 2 | 1 | name2 |
255
+ +----+---------+-------+
256
+ ```
257
+
91
258
  ## Development
92
259
 
93
260
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activesupport", "4.2.5"
6
+ gem "activerecord", "4.2.5"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1,79 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ has_logs (0.5.0)
5
+ activerecord (> 3.0)
6
+ activesupport (> 3.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (4.2.5)
12
+ activesupport (= 4.2.5)
13
+ builder (~> 3.1)
14
+ activerecord (4.2.5)
15
+ activemodel (= 4.2.5)
16
+ activesupport (= 4.2.5)
17
+ arel (~> 6.0)
18
+ activesupport (4.2.5)
19
+ i18n (~> 0.7)
20
+ json (~> 1.7, >= 1.7.7)
21
+ minitest (~> 5.1)
22
+ thread_safe (~> 0.3, >= 0.3.4)
23
+ tzinfo (~> 1.1)
24
+ appraisal (2.1.0)
25
+ bundler
26
+ rake
27
+ thor (>= 0.14.0)
28
+ arel (6.0.3)
29
+ builder (3.2.2)
30
+ diff-lcs (1.2.5)
31
+ i18n (0.7.0)
32
+ json (1.8.3)
33
+ minitest (5.9.0)
34
+ rake (10.5.0)
35
+ rspec (3.5.0)
36
+ rspec-core (~> 3.5.0)
37
+ rspec-expectations (~> 3.5.0)
38
+ rspec-mocks (~> 3.5.0)
39
+ rspec-core (3.5.1)
40
+ rspec-support (~> 3.5.0)
41
+ rspec-expectations (3.5.0)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.5.0)
44
+ rspec-its (1.2.0)
45
+ rspec-core (>= 3.0.0)
46
+ rspec-expectations (>= 3.0.0)
47
+ rspec-mocks (3.5.0)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.5.0)
50
+ rspec-support (3.5.0)
51
+ shoulda (3.5.0)
52
+ shoulda-context (~> 1.0, >= 1.0.1)
53
+ shoulda-matchers (>= 1.4.1, < 3.0)
54
+ shoulda-context (1.2.1)
55
+ shoulda-matchers (2.8.0)
56
+ activesupport (>= 3.0.0)
57
+ sqlite3 (1.3.11)
58
+ thor (0.19.1)
59
+ thread_safe (0.3.5)
60
+ tzinfo (1.2.2)
61
+ thread_safe (~> 0.1)
62
+
63
+ PLATFORMS
64
+ ruby
65
+
66
+ DEPENDENCIES
67
+ activerecord (= 4.2.5)
68
+ activesupport (= 4.2.5)
69
+ appraisal (~> 2.1)
70
+ bundler (~> 1.12)
71
+ has_logs!
72
+ rake (~> 10.0)
73
+ rspec (~> 3.0)
74
+ rspec-its (~> 1.2)
75
+ shoulda (~> 3.5)
76
+ sqlite3 (~> 1.0)
77
+
78
+ BUNDLED WITH
79
+ 1.12.5
@@ -0,0 +1,8 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activesupport", "5.0.0"
6
+ gem "activerecord", "5.0.0"
7
+
8
+ gemspec :path => "../"
@@ -0,0 +1,76 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ has_logs (0.5.0)
5
+ activerecord (> 3.0)
6
+ activesupport (> 3.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (5.0.0)
12
+ activesupport (= 5.0.0)
13
+ activerecord (5.0.0)
14
+ activemodel (= 5.0.0)
15
+ activesupport (= 5.0.0)
16
+ arel (~> 7.0)
17
+ activesupport (5.0.0)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (~> 0.7)
20
+ minitest (~> 5.1)
21
+ tzinfo (~> 1.1)
22
+ appraisal (2.1.0)
23
+ bundler
24
+ rake
25
+ thor (>= 0.14.0)
26
+ arel (7.1.0)
27
+ concurrent-ruby (1.0.2)
28
+ diff-lcs (1.2.5)
29
+ i18n (0.7.0)
30
+ minitest (5.9.0)
31
+ rake (10.5.0)
32
+ rspec (3.5.0)
33
+ rspec-core (~> 3.5.0)
34
+ rspec-expectations (~> 3.5.0)
35
+ rspec-mocks (~> 3.5.0)
36
+ rspec-core (3.5.1)
37
+ rspec-support (~> 3.5.0)
38
+ rspec-expectations (3.5.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.5.0)
41
+ rspec-its (1.2.0)
42
+ rspec-core (>= 3.0.0)
43
+ rspec-expectations (>= 3.0.0)
44
+ rspec-mocks (3.5.0)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.5.0)
47
+ rspec-support (3.5.0)
48
+ shoulda (3.5.0)
49
+ shoulda-context (~> 1.0, >= 1.0.1)
50
+ shoulda-matchers (>= 1.4.1, < 3.0)
51
+ shoulda-context (1.2.1)
52
+ shoulda-matchers (2.8.0)
53
+ activesupport (>= 3.0.0)
54
+ sqlite3 (1.3.11)
55
+ thor (0.19.1)
56
+ thread_safe (0.3.5)
57
+ tzinfo (1.2.2)
58
+ thread_safe (~> 0.1)
59
+
60
+ PLATFORMS
61
+ ruby
62
+
63
+ DEPENDENCIES
64
+ activerecord (= 5.0.0)
65
+ activesupport (= 5.0.0)
66
+ appraisal (~> 2.1)
67
+ bundler (~> 1.12)
68
+ has_logs!
69
+ rake (~> 10.0)
70
+ rspec (~> 3.0)
71
+ rspec-its (~> 1.2)
72
+ shoulda (~> 3.5)
73
+ sqlite3 (~> 1.0)
74
+
75
+ BUNDLED WITH
76
+ 1.12.5
data/has_logs.gemspec CHANGED
@@ -24,7 +24,8 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "bundler", "~> 1.12"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
26
  spec.add_development_dependency "rspec", "~> 3.0"
27
- spec.add_development_dependency "rspec-its"
28
- spec.add_development_dependency "shoulda"
27
+ spec.add_development_dependency "rspec-its", "~> 1.2"
28
+ spec.add_development_dependency "shoulda", "~> 3.5"
29
29
  spec.add_development_dependency "sqlite3", "~> 1.0"
30
+ spec.add_development_dependency "appraisal", "~> 2.1"
30
31
  end
@@ -13,31 +13,37 @@ module HasLogs
13
13
  return if self.included_modules.include?(HasLogs::InstanceMethods)
14
14
  include HasLogs::InstanceMethods
15
15
 
16
- cattr_accessor :log_class_name, :log_foreign_key, :log_table_name, :logging_attrs
16
+ cattr_accessor :have_type, :log_class_name, :log_foreign_key, :log_table_name, :logging_attrs
17
+
18
+ self.have_type = options.delete(:have_type) || :default
17
19
 
18
20
  self.log_class_name = options[:class_name] || "#{self.name}Log"
19
21
  self.log_foreign_key = options[:foreign_key] || "#{self.name}Id".underscore
20
22
 
21
- self.logging_attrs = (log_class.column_names - [log_class.primary_key, "#{self.name}Id".underscore, 'created_at'])
23
+ self.logging_attrs = (log_class.column_names - [log_class.primary_key, "#{self.name}Id".underscore, 'created_at', 'updated_at'])
22
24
 
23
25
  class_eval do
24
26
  has_many :logs, options.merge(class_name: log_class_name, foreign_key: log_foreign_key)
25
- after_find :set_attrs
26
- after_create :create_log
27
- after_update :create_log
27
+ after_find :set_attrs if %i(default).include? have_type
28
+ before_create :create_log
29
+ before_update :create_log
28
30
 
29
- logging_attrs.each do |attr|
30
- attr_accessor attr
31
- define_method "#{attr}?" do
32
- public_send(:attr).present?
33
- end
34
- end
31
+ define_attrs(logging_attrs) if %i(default).include? have_type
35
32
  end
36
33
  end
37
34
 
38
35
  def log_class
39
36
  log_class_name.constantize
40
37
  end
38
+
39
+ def define_attrs(attrs)
40
+ attrs.each do |attr|
41
+ attr_accessor attr
42
+ define_method "#{attr}?" do
43
+ public_send(:attr).present?
44
+ end
45
+ end
46
+ end
41
47
  end
42
48
 
43
49
  module InstanceMethods
@@ -50,11 +56,14 @@ module HasLogs
50
56
  end
51
57
 
52
58
  def create_log
59
+ return if self.class.have_type == :mutual && self.new_record?
60
+
53
61
  values = {}
54
62
  self.class.logging_attrs.each do |attr|
55
- values[attr] = send(attr)
63
+ values[attr] = (self.class.have_type == :mutual ? send("#{attr}_was") : send(attr))
56
64
  end
57
65
  logs.build(values).save
66
+ return
58
67
  end
59
68
 
60
69
  def set_attrs
@@ -1,3 +1,3 @@
1
1
  module HasLogs
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_logs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - isuke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-07-25 00:00:00.000000000 Z
11
+ date: 2016-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -84,30 +84,30 @@ dependencies:
84
84
  name: rspec-its
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '0'
89
+ version: '1.2'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
96
+ version: '1.2'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: shoulda
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - ">="
101
+ - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '0'
103
+ version: '3.5'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - ">="
108
+ - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '0'
110
+ version: '3.5'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: sqlite3
113
113
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
124
  version: '1.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: appraisal
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '2.1'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '2.1'
125
139
  description: Logging your ActiveRecord model, and supply useful methods.
126
140
  email:
127
141
  - isuke770@gmail.com
@@ -132,6 +146,7 @@ files:
132
146
  - ".gitignore"
133
147
  - ".rspec"
134
148
  - ".travis.yml"
149
+ - Appraisals
135
150
  - CODE_OF_CONDUCT.md
136
151
  - Gemfile
137
152
  - LICENSE.txt
@@ -139,6 +154,10 @@ files:
139
154
  - Rakefile
140
155
  - bin/console
141
156
  - bin/setup
157
+ - gemfiles/activerecord_4.2.5.gemfile
158
+ - gemfiles/activerecord_4.2.5.gemfile.lock
159
+ - gemfiles/activerecord_5.0.0.gemfile
160
+ - gemfiles/activerecord_5.0.0.gemfile.lock
142
161
  - has_logs.gemspec
143
162
  - lib/has_logs.rb
144
163
  - lib/has_logs/act_as_log.rb