sequel-audit_by_day 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sequel-audit_by_day.gemspec
4
+ gemspec
5
+
6
+ gem "rake"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jonathan Tron
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,117 @@
1
+ # Sequel::AuditByDay
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sequel-audit_by_day'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sequel-audit_by_day
18
+
19
+ ## Usage
20
+
21
+ Given following models:
22
+
23
+ ```ruby
24
+ #
25
+ # Sequel.migration do
26
+ # change do
27
+ # create_table :users do
28
+ # primary_key :id
29
+ # FalseClass :admin, default: false, null: false
30
+ # end
31
+ # end
32
+ # end
33
+ #
34
+ class User < Sequel::Model
35
+ def admin_in_audit?
36
+ admin
37
+ end
38
+ end
39
+ #
40
+ # Sequel.migration do
41
+ # change do
42
+ # create_table :posts do
43
+ # primary_key :id
44
+ # end
45
+ # create_table :post_versions do
46
+ # primary_key :id
47
+ # foreign_key :master_id, :posts, on_delete: :cascade, deferrable: true
48
+ # Time :created_at
49
+ # Time :expired_at
50
+ # Date :valid_from
51
+ # Date :valid_to
52
+ # String :title
53
+ # index [:master_id, :created_at, :valid_from, :valid_to]
54
+ # end
55
+ # create_table :post_audits do
56
+ # primary_key :id
57
+ # Date :for
58
+ # foreign_key :post_id, :posts, on_delete: :cascade, deferrable: true
59
+ # index [:post_id, :for], unique: true
60
+ # end
61
+ # create_table :post_audit_versions do
62
+ # primary_key :id
63
+ # foreign_key :master_id, :post_audits, on_delete: :cascade, deferrable: true
64
+ # Time :created_at
65
+ # Time :expired_at
66
+ # Date :valid_from
67
+ # Date :valid_to
68
+ # foreign_key :title_updated_by_id, :users, on_delete: :set_null
69
+ # foreign_key :admin_user_id, :users, on_delete: :set_null
70
+ # index [:master_id, :created_at, :valid_from, :valid_to]
71
+ # end
72
+ # end
73
+ # end
74
+ #
75
+ class Post < Sequel::Model
76
+ plugin :bitemporal, version_class: PostVersion,
77
+ audit_class: PostAudit,
78
+ audit_updated_by_method: :updated_by
79
+
80
+ one_to_many :audits, class: "PostAudit"
81
+ delegate :updated_by, to: :pending_or_current_version, allow_nil: true
82
+ end
83
+ class PostVersion < Sequel::Model
84
+ attr_accessor :updated_by
85
+ end
86
+ class PostAudit < Sequel::Model
87
+ plugin :bitemporal, version_class: PostAuditVersion
88
+ plugin :audit_by_day, foreign_key: :post_id
89
+ end
90
+ class PostAuditVersion < Sequel::Model
91
+ end
92
+ ```
93
+
94
+ Then you can record changes like this:
95
+
96
+ ```
97
+ user = User.create
98
+ admin = User.create admin: true
99
+
100
+ post = Post.new.update_attributes({
101
+ title: "First post",
102
+ updated_by: admin,
103
+ })
104
+
105
+ post.update_attributes({
106
+ title: "First post updated",
107
+ updated_by: user
108
+ })
109
+ ```
110
+
111
+ ## Contributing
112
+
113
+ 1. Fork it
114
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
115
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
116
+ 4. Push to the branch (`git push origin my-new-feature`)
117
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,51 @@
1
+ module Sequel
2
+ module Plugins
3
+ module AuditByDay
4
+ require "set"
5
+
6
+ def self.configure(master, opts={})
7
+ audit_foreign_key = opts[:foreign_key]
8
+ default_valid_from = opts.fetch(:default_valid_from){ Time.utc(1000) }
9
+ raise Error, ":foreign_key options is required for audit" unless audit_foreign_key
10
+ raise Error, ":foreign_key column does not exists for audit" unless master.columns.include? audit_foreign_key
11
+ updated_by_suffix = "_updated_by_id"
12
+ version_columns = master.version_class.columns.collect do |column|
13
+ column_str = column.to_s
14
+ if column_str.end_with? updated_by_suffix
15
+ column_str = column_str.gsub(updated_by_suffix, "").to_sym
16
+ end
17
+ end.compact
18
+ master.instance_eval do
19
+ @audit_foreign_key = audit_foreign_key
20
+ @audit_checked_columns = Set.new version_columns
21
+ end
22
+ end
23
+
24
+ module ClassMethods
25
+ attr_reader :audit_foreign_key, :audit_checked_columns, :default_valid_from
26
+
27
+ def find_for(audited_id, at)
28
+ where(audit_foreign_key => audited_id, :for => at).with_current_version.first
29
+ end
30
+
31
+ def audit(master, previous_values, updated_values, update_time, updated_by)
32
+ changed_values = updated_values.select do |column, updated_value|
33
+ audit_checked_columns.include?(column) &&
34
+ previous_values[column]!=updated_value
35
+ end
36
+ audit_for_day = find_for master.id, update_time
37
+ audit_for_day ||= new({audit_foreign_key => master.id, :for => update_time})
38
+ if updated_by.respond_to?(:admin_in_audit?) && updated_by.admin_in_audit?
39
+ attrs = {admin_user_id: updated_by.id}
40
+ else
41
+ attrs = Hash[changed_values.collect{|column, _| ["#{column}_updated_by_id", updated_by]}]
42
+ attrs[:admin_user_id] = nil
43
+ end
44
+ audit_for_day.update_attributes attrs.merge({
45
+ partial_update: true, valid_from: self.class.default_valid_from
46
+ })
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ module Sequel
2
+ module AuditByDay
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ require "sequel"
2
+ require "sequel-audit_by_day/version"
3
+ require "sequel/plugins/audit_by_day"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sequel-audit_by_day/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "sequel-audit_by_day"
8
+ gem.version = Sequel::AuditByDay::VERSION
9
+ gem.authors = ["Jonathan Tron", "Joseph Halter"]
10
+ gem.email = ["jonathan.tron@metrilio.com", "joseph.halter@metrilio.com"]
11
+ gem.description = "Audit by day for sequel_bitemporal"
12
+ gem.summary = gem.description
13
+ gem.homepage = "https://github.com/TalentBox/sequel-audit_by_day"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency "sequel_bitemporal", ">= 0.4.7"
21
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel-audit_by_day
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jonathan Tron
9
+ - Joseph Halter
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-11-21 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sequel_bitemporal
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 0.4.7
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: 0.4.7
31
+ description: Audit by day for sequel_bitemporal
32
+ email:
33
+ - jonathan.tron@metrilio.com
34
+ - joseph.halter@metrilio.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - .gitignore
40
+ - Gemfile
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - lib/sequel-audit_by_day.rb
45
+ - lib/sequel-audit_by_day/version.rb
46
+ - lib/sequel/plugins/audit_by_day.rb
47
+ - sequel-audit_by_day.gemspec
48
+ homepage: https://github.com/TalentBox/sequel-audit_by_day
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ segments:
61
+ - 0
62
+ hash: -3583529659193523704
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ segments:
70
+ - 0
71
+ hash: -3583529659193523704
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.24
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Audit by day for sequel_bitemporal
78
+ test_files: []