footprintable 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5acf753ef41fff92e1b4c38a032fba4ecd4b98b6
4
+ data.tar.gz: 13c9482803c0617dcd0d083f0fc1e7bbc51b2603
5
+ SHA512:
6
+ metadata.gz: 3991b2e6e2077c77e22c9988ef3e977feeecb3d1dadd3865ccf176b2d802ad205936d4b609907e575174e2372bc77c51aa6864f65edf1ea6bb6e24223029423c
7
+ data.tar.gz: 4c6dc7a43b695040e8ad403bdf6fa848854dd0761d7ebd356469a0addc63e5edc346856129b167ad98896b126778e6c62c85fbf53827f60d629c0e05e3b8fadf
@@ -0,0 +1,20 @@
1
+ Copyright 2018 mars
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,72 @@
1
+ # Footprintable
2
+ track model columns change
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'footprintable', github: 'rails-gems/footprintable'
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ ```
15
+
16
+ ## Usage
17
+ 创建`model`
18
+ ```
19
+ rails g model footprint before:text after:text action:string trackable:references{polymorphic} actorable:references{polymorphic}
20
+ ```
21
+
22
+ 添加代码到`footprint.rb`
23
+ ```
24
+ serialize :before, JSON
25
+ serialize :after, JSON
26
+ ```
27
+
28
+ * trackable, 追踪的对象(改变的model)
29
+ * actorable,导致对象改变的对象,比如操作者, 如果为空,默认为trackable本身
30
+
31
+ actorable 赋值当前操作者举例
32
+ ```
33
+ # in controler
34
+ around_action :set_thread_footprint_actor
35
+ def set_thread_footprint_actor
36
+ Footprintable::Current.actor = current_user
37
+ yield
38
+ ensure
39
+ # to address the thread variable leak issues in Puma/Thin webserver
40
+ Footprintable::Current.actor = nil
41
+ end
42
+ ```
43
+
44
+ 需要追踪的model添加如下代码
45
+ ```
46
+ include Footprintable
47
+ has_footprints(options)
48
+ ```
49
+ options, default: {}
50
+ * :on, after_commit actions, default: [:create, :update, :destroy]
51
+ * :except, 不需要记录的字段, default: [:updated_at, :created_at]
52
+ * :extract, 只需要记录的字段, default: [], 空指记录所有字段
53
+
54
+ `except`,`extract`的关系: `(changed_columns & extract) - except`
55
+
56
+ ### fragment
57
+ ```
58
+ def has_footprints options = {}
59
+ # 触发时间
60
+ options[:on] ||= [:create, :update]
61
+ # 摘取字段 [] => 所有字段
62
+ options[:extract] ||= []
63
+ # 排除字段
64
+ options[:except] ||= [:updated_at, :created_at]
65
+ end
66
+ ```
67
+
68
+ ## Contributing
69
+ Contribution directions go here.
70
+
71
+ ## License
72
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,33 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Footprintable'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+
33
+ task default: :test
@@ -0,0 +1,15 @@
1
+ require 'footprintable/class_methods'
2
+ require 'footprintable/instance_methods'
3
+ require 'footprintable/current'
4
+ module Footprintable
5
+
6
+ def self.included(klass)
7
+ klass.class_eval do
8
+ has_many :footprints, as: :trackable
9
+ class_attribute :footprint_options
10
+ end
11
+ klass.send :extend, ClassMethods
12
+ klass.send :include, InstanceMethods
13
+ end
14
+
15
+ end
@@ -0,0 +1,21 @@
1
+ # except,extract的关系: (changed_columns & extract) - except
2
+ module Footprintable
3
+ module ClassMethods
4
+ def has_footprints options = {}
5
+ # 触发时间
6
+ options[:on] ||= [:create, :update, :destroy]
7
+ # 摘取字段 [] => 所有字段
8
+ options[:extract] ||= []
9
+ # 排除字段
10
+ options[:except] ||= [:updated_at, :created_at]
11
+ self.footprint_options = options
12
+ setup_callbacks_from_options(options[:on])
13
+ end
14
+
15
+ def setup_callbacks_from_options options_on = []
16
+ options_on.each do |action|
17
+ after_commit ->(obj) { obj.create_footprint action }, on: action
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,8 @@
1
+ require 'active_support/core_ext/module/attribute_accessors_per_thread'
2
+ module Footprintable
3
+ # rails 5
4
+ # 当前线程导致数据改变的触发对象
5
+ module Current
6
+ thread_mattr_accessor :actor
7
+ end
8
+ end
@@ -0,0 +1,20 @@
1
+ require 'footprintable/current'
2
+ module Footprintable
3
+ module InstanceMethods
4
+ def create_footprint action
5
+ attrs_changed = previous_changes
6
+ return if attrs_changed.empty?
7
+ attrs_changed = attrs_changed.except *self.class.footprint_options[:except]
8
+ attrs_changed = attrs_changed.extract! *self.class.footprint_options[:extract] if self.class.footprint_options[:extract].present?
9
+
10
+ attrs_before = {}
11
+ attrs_after = {}
12
+
13
+ attrs_changed.each do |k, v|
14
+ attrs_before.store(k, v.first) unless v.first.nil?
15
+ attrs_after.store(k, v.last) unless v.last.nil?
16
+ end
17
+ self.footprints.model.new(before: attrs_before, after: attrs_after, action: action, trackable: self, actorable: Current.actor).save(validate: false)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Footprintable
2
+ VERSION = '0.1.2'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :footprintable do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: footprintable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - mars
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "<"
18
+ - !ruby/object:Gem::Version
19
+ version: '6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "<"
25
+ - !ruby/object:Gem::Version
26
+ version: '6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: track model column change.
42
+ email:
43
+ - 578595193@qq.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/footprintable.rb
52
+ - lib/footprintable/class_methods.rb
53
+ - lib/footprintable/current.rb
54
+ - lib/footprintable/instance_methods.rb
55
+ - lib/footprintable/version.rb
56
+ - lib/tasks/footprintable_tasks.rake
57
+ homepage: https://github.com/wuyuedefeng/footprintable
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.6.13
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: track model column change.
81
+ test_files: []