model_auditor 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +39 -0
- data/.travis.yml +5 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +95 -0
- data/Guardfile +70 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/model_auditor.rb +30 -0
- data/lib/model_auditor/attribute_reader.rb +26 -0
- data/lib/model_auditor/attribute_readers/array_field.rb +20 -0
- data/lib/model_auditor/attribute_readers/associated.rb +35 -0
- data/lib/model_auditor/changes.rb +61 -0
- data/lib/model_auditor/normalizers/array_diff.rb +38 -0
- data/lib/model_auditor/normalizers/string.rb +34 -0
- data/lib/model_auditor/version.rb +5 -0
- data/model_auditor.gemspec +32 -0
- metadata +175 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7d523e4b799f2522a0a3170c90cf59498d9ccd67
|
4
|
+
data.tar.gz: a5b042e8e92b9f6fecc9a8af0b4b4cebc7ca081b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b927e3763db5382d09847dd25bb723216646ef11711bf1391ee6516295b3393c73409a70fdcf24b36dd4759d3026cdcc9d391e14c0b6586b46e88af0dda88873
|
7
|
+
data.tar.gz: 9cff07e7560b7ae94fffe04ab40846459937fb8d6a0c829ba8e24845bec65959739d9b139889bf5a3f8733ebb2eaad64a61b835774bf5c1a917befd8517b3c0f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.4.4
|
3
|
+
Exclude:
|
4
|
+
- "bin/*"
|
5
|
+
- "Guardfile"
|
6
|
+
|
7
|
+
##################### Styles ##################################
|
8
|
+
|
9
|
+
Style/Documentation:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Style/SymbolArray:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
##################### Metrics ##################################
|
16
|
+
|
17
|
+
Metrics/LineLength:
|
18
|
+
Max: 110
|
19
|
+
|
20
|
+
Metrics/ClassLength:
|
21
|
+
Max: 200
|
22
|
+
|
23
|
+
Metrics/ModuleLength:
|
24
|
+
Max: 200
|
25
|
+
Exclude:
|
26
|
+
- "**/*_spec.rb"
|
27
|
+
|
28
|
+
Metrics/BlockLength:
|
29
|
+
Max: 50
|
30
|
+
Exclude:
|
31
|
+
- "**/*_spec.rb"
|
32
|
+
|
33
|
+
Metrics/MethodLength:
|
34
|
+
Max: 15
|
35
|
+
|
36
|
+
##################### Rails ##################################
|
37
|
+
|
38
|
+
Rails:
|
39
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
model_auditor (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (5.2.1)
|
10
|
+
activesupport (= 5.2.1)
|
11
|
+
activerecord (5.2.1)
|
12
|
+
activemodel (= 5.2.1)
|
13
|
+
activesupport (= 5.2.1)
|
14
|
+
arel (>= 9.0)
|
15
|
+
activesupport (5.2.1)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 0.7, < 2)
|
18
|
+
minitest (~> 5.1)
|
19
|
+
tzinfo (~> 1.1)
|
20
|
+
arel (9.0.0)
|
21
|
+
coderay (1.1.2)
|
22
|
+
concurrent-ruby (1.0.5)
|
23
|
+
diff-lcs (1.3)
|
24
|
+
ffi (1.9.25)
|
25
|
+
formatador (0.2.5)
|
26
|
+
guard (2.14.2)
|
27
|
+
formatador (>= 0.2.4)
|
28
|
+
listen (>= 2.7, < 4.0)
|
29
|
+
lumberjack (>= 1.0.12, < 2.0)
|
30
|
+
nenv (~> 0.1)
|
31
|
+
notiffany (~> 0.0)
|
32
|
+
pry (>= 0.9.12)
|
33
|
+
shellany (~> 0.0)
|
34
|
+
thor (>= 0.18.1)
|
35
|
+
guard-compat (1.2.1)
|
36
|
+
guard-rspec (4.7.3)
|
37
|
+
guard (~> 2.1)
|
38
|
+
guard-compat (~> 1.1)
|
39
|
+
rspec (>= 2.99.0, < 4.0)
|
40
|
+
i18n (1.1.1)
|
41
|
+
concurrent-ruby (~> 1.0)
|
42
|
+
listen (3.1.5)
|
43
|
+
rb-fsevent (~> 0.9, >= 0.9.4)
|
44
|
+
rb-inotify (~> 0.9, >= 0.9.7)
|
45
|
+
ruby_dep (~> 1.2)
|
46
|
+
lumberjack (1.0.13)
|
47
|
+
method_source (0.9.0)
|
48
|
+
minitest (5.11.3)
|
49
|
+
nenv (0.3.0)
|
50
|
+
notiffany (0.1.1)
|
51
|
+
nenv (~> 0.1)
|
52
|
+
shellany (~> 0.0)
|
53
|
+
pry (0.11.3)
|
54
|
+
coderay (~> 1.1.0)
|
55
|
+
method_source (~> 0.9.0)
|
56
|
+
rake (10.5.0)
|
57
|
+
rb-fsevent (0.9.8)
|
58
|
+
rb-inotify (0.9.10)
|
59
|
+
ffi (>= 0.5.0, < 2)
|
60
|
+
rspec (3.8.0)
|
61
|
+
rspec-core (~> 3.8.0)
|
62
|
+
rspec-expectations (~> 3.8.0)
|
63
|
+
rspec-mocks (~> 3.8.0)
|
64
|
+
rspec-core (3.8.0)
|
65
|
+
rspec-support (~> 3.8.0)
|
66
|
+
rspec-expectations (3.8.2)
|
67
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
68
|
+
rspec-support (~> 3.8.0)
|
69
|
+
rspec-mocks (3.8.0)
|
70
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
71
|
+
rspec-support (~> 3.8.0)
|
72
|
+
rspec-support (3.8.0)
|
73
|
+
ruby_dep (1.5.0)
|
74
|
+
shellany (0.0.1)
|
75
|
+
sqlite3 (1.3.13)
|
76
|
+
thor (0.20.0)
|
77
|
+
thread_safe (0.3.6)
|
78
|
+
tzinfo (1.2.5)
|
79
|
+
thread_safe (~> 0.1)
|
80
|
+
|
81
|
+
PLATFORMS
|
82
|
+
ruby
|
83
|
+
|
84
|
+
DEPENDENCIES
|
85
|
+
activerecord (>= 4.0)
|
86
|
+
bundler (~> 1.15)
|
87
|
+
guard-rspec (~> 4.7, >= 4.7.3)
|
88
|
+
model_auditor!
|
89
|
+
rake (~> 10.0)
|
90
|
+
rb-fsevent (= 0.9.8)
|
91
|
+
rspec (~> 3.0)
|
92
|
+
sqlite3 (~> 1.3, >= 1.3.13)
|
93
|
+
|
94
|
+
BUNDLED WITH
|
95
|
+
1.16.1
|
data/Guardfile
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
5
|
+
# directories %w(app lib config test spec features) \
|
6
|
+
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
7
|
+
|
8
|
+
## Note: if you are using the `directories` clause above and you are not
|
9
|
+
## watching the project directory ('.'), then you will want to move
|
10
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
11
|
+
#
|
12
|
+
# $ mkdir config
|
13
|
+
# $ mv Guardfile config/
|
14
|
+
# $ ln -s config/Guardfile .
|
15
|
+
#
|
16
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
17
|
+
|
18
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
19
|
+
# rspec may be run, below are examples of the most common uses.
|
20
|
+
# * bundler: 'bundle exec rspec'
|
21
|
+
# * bundler binstubs: 'bin/rspec'
|
22
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
23
|
+
# installed the spring binstubs per the docs)
|
24
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
25
|
+
# * 'just' rspec: 'rspec'
|
26
|
+
|
27
|
+
guard :rspec, cmd: "bundle exec rspec" do
|
28
|
+
require "guard/rspec/dsl"
|
29
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
30
|
+
|
31
|
+
# Feel free to open issues for suggestions and improvements
|
32
|
+
|
33
|
+
# RSpec files
|
34
|
+
rspec = dsl.rspec
|
35
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
36
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
37
|
+
watch(rspec.spec_files)
|
38
|
+
|
39
|
+
# Ruby files
|
40
|
+
ruby = dsl.ruby
|
41
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
42
|
+
|
43
|
+
# Rails files
|
44
|
+
rails = dsl.rails(view_extensions: %w(erb haml slim))
|
45
|
+
dsl.watch_spec_files_for(rails.app_files)
|
46
|
+
dsl.watch_spec_files_for(rails.views)
|
47
|
+
|
48
|
+
watch(rails.controllers) do |m|
|
49
|
+
[
|
50
|
+
rspec.spec.call("routing/#{m[1]}_routing"),
|
51
|
+
rspec.spec.call("controllers/#{m[1]}_controller"),
|
52
|
+
rspec.spec.call("acceptance/#{m[1]}")
|
53
|
+
]
|
54
|
+
end
|
55
|
+
|
56
|
+
# Rails config changes
|
57
|
+
watch(rails.spec_helper) { rspec.spec_dir }
|
58
|
+
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
|
59
|
+
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
|
60
|
+
|
61
|
+
# Capybara features specs
|
62
|
+
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
|
63
|
+
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
|
64
|
+
|
65
|
+
# Turnip features and steps
|
66
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
67
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
68
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
|
69
|
+
end
|
70
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Igor Malinovskiy
|
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,39 @@
|
|
1
|
+
# ModelAuditor
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/model_auditor`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'model_auditor'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install model_auditor
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/model_auditor.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "model_auditor"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'model_auditor/version'
|
4
|
+
|
5
|
+
module ModelAuditor
|
6
|
+
autoload :AttributeReader, 'model_auditor/attribute_reader'
|
7
|
+
autoload :Changes, 'model_auditor/changes'
|
8
|
+
|
9
|
+
module AttributeReaders
|
10
|
+
autoload :Associated, 'model_auditor/attribute_readers/associated'
|
11
|
+
autoload :ArrayField, 'model_auditor/attribute_readers/array_field'
|
12
|
+
autoload :String, 'model_auditor/attribute_readers/string'
|
13
|
+
end
|
14
|
+
|
15
|
+
module Normalizers
|
16
|
+
autoload :ArrayDiff, 'model_auditor/normalizers/array_diff'
|
17
|
+
autoload :String, 'model_auditor/normalizers/string'
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.normalize_value(value)
|
21
|
+
case value
|
22
|
+
when Date then value.strftime('%d.%m.%Y')
|
23
|
+
when DateTime, Time then value.strftime('%d.%m.%Y %H:%M')
|
24
|
+
when String then Normalizers::String.new(value).normalize
|
25
|
+
when BigDecimal then value.to_f
|
26
|
+
when Hash, Array then value.inspect.gsub(/[\"\r\n\t]/, '')
|
27
|
+
else value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ModelAuditor
|
4
|
+
class AttributeReader
|
5
|
+
attr_reader :model, :key
|
6
|
+
|
7
|
+
def initialize(model, key)
|
8
|
+
@model = model
|
9
|
+
@key = key
|
10
|
+
end
|
11
|
+
|
12
|
+
def value
|
13
|
+
associated_attr_value || array_attr_value || model.send(key)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def associated_attr_value
|
19
|
+
ModelAuditor::AttributeReaders::Associated.new(model, key).value
|
20
|
+
end
|
21
|
+
|
22
|
+
def array_attr_value
|
23
|
+
ModelAuditor::AttributeReaders::ArrayField.new(model, key).value
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ModelAuditor
|
4
|
+
module AttributeReaders
|
5
|
+
class ArrayField < Associated
|
6
|
+
def value
|
7
|
+
return unless method_name
|
8
|
+
|
9
|
+
values = model.send(method_name).map { |i| i.try(:title) }
|
10
|
+
values.join(', ') if values.try(:all?)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def method_name
|
16
|
+
attr_name(/[s]?_ids$/, 's')
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ModelAuditor
|
4
|
+
module AttributeReaders
|
5
|
+
class Associated
|
6
|
+
attr_reader :model, :key
|
7
|
+
|
8
|
+
def initialize(model, key)
|
9
|
+
@model = model
|
10
|
+
@key = key
|
11
|
+
end
|
12
|
+
|
13
|
+
def value
|
14
|
+
return unless method_name
|
15
|
+
|
16
|
+
model.send(method_name)&.title
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def attr_name(pattern, replacement = '')
|
22
|
+
return unless key.to_s =~ pattern
|
23
|
+
attribute_name = key.to_s.gsub(pattern, replacement)
|
24
|
+
|
25
|
+
attribute_name if model.respond_to?(attribute_name)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def method_name
|
31
|
+
attr_name(/_id$/)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ModelAuditor
|
4
|
+
class Changes
|
5
|
+
TRUE_ARRAY = [true, 1, '1', 't', 'T', 'true', 'TRUE'].freeze
|
6
|
+
IGNORED_ATTRIBUTES = [:created_at, :updated_at].freeze
|
7
|
+
|
8
|
+
def initialize(model, changes = nil, options = {})
|
9
|
+
@model = model
|
10
|
+
@changes = changes || @model.previous_changes
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
|
14
|
+
def filter(filtered = nil)
|
15
|
+
filtered ||= IGNORED_ATTRIBUTES
|
16
|
+
|
17
|
+
@changes = @changes.reject do |key, _value|
|
18
|
+
Array(filtered).include?(key.to_sym)
|
19
|
+
end
|
20
|
+
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def audit
|
25
|
+
return unless changed?
|
26
|
+
|
27
|
+
changes = @changes.keys.inject({}) do |hash, key|
|
28
|
+
hash.merge!(human_name(key) => human_value(key))
|
29
|
+
end
|
30
|
+
|
31
|
+
changes.presence
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def changed?
|
37
|
+
return true if force_changed?
|
38
|
+
|
39
|
+
@model.created_at != @model.updated_at && @model.persisted? && !@model.destroyed?
|
40
|
+
end
|
41
|
+
|
42
|
+
def force_changed?
|
43
|
+
TRUE_ARRAY.include?(@options[:changed])
|
44
|
+
end
|
45
|
+
|
46
|
+
def human_name(key)
|
47
|
+
@model.class.human_attribute_name(key)
|
48
|
+
end
|
49
|
+
|
50
|
+
def human_value(key)
|
51
|
+
value = ModelAuditor::AttributeReader.new(@model, key).value
|
52
|
+
return array_diff(key) if value.is_a? Array
|
53
|
+
|
54
|
+
ModelAuditor.normalize_value(value)
|
55
|
+
end
|
56
|
+
|
57
|
+
def array_diff(key)
|
58
|
+
Normalizers::ArrayDiff.new(@changes, key).normalize
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ModelAuditor
|
4
|
+
module Normalizers
|
5
|
+
class ArrayDiff
|
6
|
+
attr_reader :changes, :key
|
7
|
+
|
8
|
+
def initialize(changes, key)
|
9
|
+
@changes = changes
|
10
|
+
@key = key
|
11
|
+
end
|
12
|
+
|
13
|
+
def normalize
|
14
|
+
diff.map do |title, value|
|
15
|
+
humanize(title, value)
|
16
|
+
end.join(', ')
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def diff
|
22
|
+
{ 'Inserted' => inserted, 'Removed' => removed }.reject { |_key, value| value.empty? }
|
23
|
+
end
|
24
|
+
|
25
|
+
def inserted
|
26
|
+
changes[key].last - changes[key].first
|
27
|
+
end
|
28
|
+
|
29
|
+
def removed
|
30
|
+
changes[key].first - changes[key].last
|
31
|
+
end
|
32
|
+
|
33
|
+
def humanize(title, value)
|
34
|
+
[title, ModelAuditor.normalize_value(value)].join(': ')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ModelAuditor
|
4
|
+
module Normalizers
|
5
|
+
class String
|
6
|
+
attr_reader :value, :options
|
7
|
+
|
8
|
+
DEFAULTS = {
|
9
|
+
limit: 97
|
10
|
+
}.freeze
|
11
|
+
|
12
|
+
def initialize(value, options = {})
|
13
|
+
@value = value
|
14
|
+
@options = DEFAULTS.merge options
|
15
|
+
end
|
16
|
+
|
17
|
+
def normalize
|
18
|
+
return value unless valid?
|
19
|
+
|
20
|
+
value.truncate(limit)
|
21
|
+
end
|
22
|
+
|
23
|
+
def valid?
|
24
|
+
value.present? && value.respond_to?(:truncate)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def limit
|
30
|
+
options[:limit]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'model_auditor/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'model_auditor'
|
9
|
+
spec.version = ModelAuditor::VERSION
|
10
|
+
spec.authors = ['Igor Malinovskiy']
|
11
|
+
spec.email = ['igor.malinovskiy@netfix.xyz']
|
12
|
+
|
13
|
+
spec.summary = 'Show changed attributes of ActiveRecord model'
|
14
|
+
spec.homepage = 'https://github.com/psyipm/model_auditor'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
25
|
+
spec.add_development_dependency 'guard-rspec', '~> 4.7', '>= 4.7.3'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rb-fsevent', '0.9.8'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
|
+
|
30
|
+
spec.add_development_dependency 'activerecord', '>= 4.0'
|
31
|
+
spec.add_development_dependency 'sqlite3', '~> 1.3', '>= 1.3.13'
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: model_auditor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Igor Malinovskiy
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-02 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.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: guard-rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.7'
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 4.7.3
|
37
|
+
type: :development
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '4.7'
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 4.7.3
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '10.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '10.0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rb-fsevent
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - '='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 0.9.8
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 0.9.8
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rspec
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '3.0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '3.0'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: activerecord
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '4.0'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '4.0'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: sqlite3
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '1.3'
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: 1.3.13
|
113
|
+
type: :development
|
114
|
+
prerelease: false
|
115
|
+
version_requirements: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - "~>"
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '1.3'
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: 1.3.13
|
123
|
+
description:
|
124
|
+
email:
|
125
|
+
- igor.malinovskiy@netfix.xyz
|
126
|
+
executables: []
|
127
|
+
extensions: []
|
128
|
+
extra_rdoc_files: []
|
129
|
+
files:
|
130
|
+
- ".gitignore"
|
131
|
+
- ".rspec"
|
132
|
+
- ".rubocop.yml"
|
133
|
+
- ".travis.yml"
|
134
|
+
- Gemfile
|
135
|
+
- Gemfile.lock
|
136
|
+
- Guardfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- bin/console
|
141
|
+
- bin/setup
|
142
|
+
- lib/model_auditor.rb
|
143
|
+
- lib/model_auditor/attribute_reader.rb
|
144
|
+
- lib/model_auditor/attribute_readers/array_field.rb
|
145
|
+
- lib/model_auditor/attribute_readers/associated.rb
|
146
|
+
- lib/model_auditor/changes.rb
|
147
|
+
- lib/model_auditor/normalizers/array_diff.rb
|
148
|
+
- lib/model_auditor/normalizers/string.rb
|
149
|
+
- lib/model_auditor/version.rb
|
150
|
+
- model_auditor.gemspec
|
151
|
+
homepage: https://github.com/psyipm/model_auditor
|
152
|
+
licenses:
|
153
|
+
- MIT
|
154
|
+
metadata: {}
|
155
|
+
post_install_message:
|
156
|
+
rdoc_options: []
|
157
|
+
require_paths:
|
158
|
+
- lib
|
159
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
|
+
requirements:
|
166
|
+
- - ">="
|
167
|
+
- !ruby/object:Gem::Version
|
168
|
+
version: '0'
|
169
|
+
requirements: []
|
170
|
+
rubyforge_project:
|
171
|
+
rubygems_version: 2.6.14.1
|
172
|
+
signing_key:
|
173
|
+
specification_version: 4
|
174
|
+
summary: Show changed attributes of ActiveRecord model
|
175
|
+
test_files: []
|