log_table 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +33 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/log_table/version.rb +3 -0
- data/lib/log_table.rb +47 -0
- data/lib/tasks/db/generate_log_table.rake +57 -0
- data/lib/tasks/db/generate_log_trigger_migration.rake +22 -0
- data/lib/tasks/db/templates/create_log_table.erb +13 -0
- data/log_table.gemspec +28 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5a3bac03cb5e151d6994bfbcddcf876d6d1ff4bb
|
4
|
+
data.tar.gz: d8197dba004608c3f58e82f2ad2254dd33f9666e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8533007d4d2f356397df004ddfadca193bf83112878d4f889f5e374ba0dcd09d6cc87aa5c2b0ad08b9822d0d2d25eddce640cef12df126ee4a63a13dad55b6a9
|
7
|
+
data.tar.gz: e1b55ac03f7fa8d1353e10bc4cf3957777b034d4a3e17bf26997b271d9daff8a7bd469a28430d7c6e725c40686b429df326e593006c86663c4410e8ca4d965c5
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Daisuke Shimamoto
|
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,33 @@
|
|
1
|
+
# LogTable
|
2
|
+
|
3
|
+
The LogTable gem will read your Active Record model and generate a table to store logs to.
|
4
|
+
It will additionally create triggers which will do the actual saving into the log table.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'log_table'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install log_table
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
WIP
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/diskshima/log_table. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
29
|
+
|
30
|
+
|
31
|
+
## License
|
32
|
+
|
33
|
+
The gem is available as open source under the terms of the [MIT License](http://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 "log_table"
|
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
|
data/bin/setup
ADDED
data/lib/log_table.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
#
|
2
|
+
# log_table.rb
|
3
|
+
# Copyright (C) 2015 Daisuke Shimamoto <diskshima@gmail.com>
|
4
|
+
#
|
5
|
+
# Distributed under terms of the MIT license.
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'log_table/version'
|
9
|
+
require 'hairtrigger'
|
10
|
+
|
11
|
+
module LogTable
|
12
|
+
def self.included(base)
|
13
|
+
base.send :extend, ClassMethods
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
##
|
18
|
+
# add_log_trigger will add hairtrigger triggers to the model.
|
19
|
+
# It will generate triggers for :insert and :update.
|
20
|
+
#
|
21
|
+
# Options:
|
22
|
+
#
|
23
|
+
# - :table_name - Specify table name of the log table.
|
24
|
+
#
|
25
|
+
def add_log_trigger(options = {})
|
26
|
+
col_names = column_names
|
27
|
+
|
28
|
+
db_columns = col_names.map { |col_name|
|
29
|
+
col_name == 'id' ? "#{name.underscore}_id" : "#{col_name}"
|
30
|
+
}.join(', ')
|
31
|
+
|
32
|
+
values = col_names.map { |col| "NEW.#{col}" }.join(', ')
|
33
|
+
|
34
|
+
log_table_name = options[:table_name] || "#{table_name}_log"
|
35
|
+
|
36
|
+
insert_sql = "INSERT INTO #{log_table_name}(#{db_columns}) VALUES (#{values})"
|
37
|
+
|
38
|
+
trigger.after(:insert) do
|
39
|
+
insert_sql
|
40
|
+
end
|
41
|
+
|
42
|
+
trigger.after(:update) do
|
43
|
+
insert_sql
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require 'erb'
|
3
|
+
|
4
|
+
namespace :db do
|
5
|
+
desc 'Create a database migration for log tables'
|
6
|
+
task generate_log_table: :environment do
|
7
|
+
args = ENV['MODEL']
|
8
|
+
|
9
|
+
unless args
|
10
|
+
$stderr.puts 'Please specify at the model with MODEL=CamelCaseModelName'
|
11
|
+
next
|
12
|
+
end
|
13
|
+
|
14
|
+
models = args.split(',')
|
15
|
+
clazzes = models.map { |model_name| Object.const_get(model_name) }
|
16
|
+
|
17
|
+
if clazzes.any? { |clazz| !(clazz < ActiveRecord::Base) }
|
18
|
+
$stderr.puts 'The models must be an ActiveRecord model.'
|
19
|
+
next
|
20
|
+
end
|
21
|
+
|
22
|
+
migration_with_action = 'create_log_tables'
|
23
|
+
file_name = ActiveRecord::Migration.next_migration_number(0) + '_' + migration_with_action + '.rb'
|
24
|
+
|
25
|
+
ns = OpenStruct.new
|
26
|
+
ns.tables = []
|
27
|
+
ns.migration_class_name = migration_with_action.camelize
|
28
|
+
|
29
|
+
clazzes.each do |clazz|
|
30
|
+
tables = ns.tables
|
31
|
+
table = OpenStruct.new
|
32
|
+
table.model_id_col_name = "#{clazz.name.underscore}_id"
|
33
|
+
|
34
|
+
table.attributes = clazz.columns.map do |col|
|
35
|
+
if col.name == 'id'
|
36
|
+
OpenStruct.new(name: table.model_id_col_name, type: :integer)
|
37
|
+
else
|
38
|
+
col
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
table.name = "#{clazz.table_name}_log"
|
43
|
+
tables << table
|
44
|
+
end
|
45
|
+
|
46
|
+
file_path = File.join('db', 'migrate', file_name)
|
47
|
+
template_file_path = File.join(File.dirname(__FILE__), 'templates',
|
48
|
+
'create_log_table.erb')
|
49
|
+
template_file = File.read(template_file_path)
|
50
|
+
|
51
|
+
File.open(file_path, 'w') do |f|
|
52
|
+
erb_binding = ns.instance_eval { binding }
|
53
|
+
f.write ERB.new(template_file, nil, '-').result(erb_binding)
|
54
|
+
end
|
55
|
+
puts "Written to #{file_path}"
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
namespace :db do
|
2
|
+
desc 'Create a migration to generate log triggers'
|
3
|
+
task generate_log_trigger_migration: :environment do
|
4
|
+
args = ENV['MODEL']
|
5
|
+
|
6
|
+
models = args.split(',')
|
7
|
+
clazzes = models.map { |model_name| Object.const_get(model_name) }
|
8
|
+
|
9
|
+
clazzes.each do |clazz|
|
10
|
+
include_log_triggers(clazz)
|
11
|
+
end
|
12
|
+
|
13
|
+
Rake::Task['db:generate_trigger_migration'].invoke
|
14
|
+
end
|
15
|
+
|
16
|
+
def include_log_triggers(clazz)
|
17
|
+
clazz.class_eval do
|
18
|
+
include LogTable
|
19
|
+
add_log_trigger
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
<% tables.each do |table| %>
|
4
|
+
create_table :<%= table.name %> do |t|
|
5
|
+
<% table.attributes.each do |attribute| -%>
|
6
|
+
t.<%= attribute.type %> :<%= attribute.name %>
|
7
|
+
<% end -%>
|
8
|
+
end
|
9
|
+
|
10
|
+
add_index :<%= table.name %>, :<%= table.model_id_col_name %>
|
11
|
+
<% end %>
|
12
|
+
end
|
13
|
+
end
|
data/log_table.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'log_table/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'log_table'
|
8
|
+
spec.version = LogTable::VERSION
|
9
|
+
spec.authors = ['Daisuke Shimamoto']
|
10
|
+
spec.email = ['diskshima@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Creates a migration for a log table and triggers}
|
13
|
+
spec.description = %q{log_table creates a log table for the given Active Record model and triggers to write into it.}
|
14
|
+
spec.homepage = 'https://github.com/diskshima/log_table'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_dependency 'hairtrigger', '~> 0.2.16'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'minitest', '~> 5.8'
|
27
|
+
spec.add_development_dependency 'm', '~> 1.4'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: log_table
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daisuke Shimamoto
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: hairtrigger
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.2.16
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.2.16
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.8'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.8'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: m
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.4'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.4'
|
83
|
+
description: log_table creates a log table for the given Active Record model and triggers
|
84
|
+
to write into it.
|
85
|
+
email:
|
86
|
+
- diskshima@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- lib/log_table.rb
|
101
|
+
- lib/log_table/version.rb
|
102
|
+
- lib/tasks/db/generate_log_table.rake
|
103
|
+
- lib/tasks/db/generate_log_trigger_migration.rake
|
104
|
+
- lib/tasks/db/templates/create_log_table.erb
|
105
|
+
- log_table.gemspec
|
106
|
+
homepage: https://github.com/diskshima/log_table
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.4.8
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Creates a migration for a log table and triggers
|
130
|
+
test_files: []
|
131
|
+
has_rdoc:
|