rlog_items 0.0.1
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.
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/app/models/rlog_items/rlog_item.rb +14 -0
- data/lib/generators/rlog_items/install_generator.rb +27 -0
- data/lib/generators/rlog_items/templates/migration.rb +17 -0
- data/lib/rlog_items/active_record_logger.rb +65 -0
- data/lib/rlog_items/version.rb +3 -0
- data/lib/rlog_items.rb +33 -0
- data/rlog_items.gemspec +23 -0
- metadata +56 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class RlogItem < ActiveRecord::Base
|
2
|
+
default_scope :order => 'created_at DESC'
|
3
|
+
serialize :record_changes, ActiveSupport::HashWithIndifferentAccess
|
4
|
+
|
5
|
+
belongs_to :user
|
6
|
+
belongs_to :record, :polymorphic => true
|
7
|
+
|
8
|
+
validates :record, :user, :presence => true
|
9
|
+
|
10
|
+
def unscoped_record
|
11
|
+
record_type.constantize.unscoped.find_by_id record_id
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rails/generators/active_record'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
module RlogItems
|
5
|
+
module Generators
|
6
|
+
class InstallGenerator < Rails::Generators::Base
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
|
9
|
+
def self.source_root
|
10
|
+
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.next_migration_number(dirname)
|
14
|
+
if ActiveRecord::Base.timestamped_migrations
|
15
|
+
Time.new.utc.strftime("%Y%m%d%H%M%S")
|
16
|
+
else
|
17
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_migration_file
|
22
|
+
migration_template 'migration.rb', 'db/migrate/create_rlog_items.rb'
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class CreateRlogItems < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :rlog_items do |t|
|
4
|
+
t.integer :record_id
|
5
|
+
t.integer :user_id
|
6
|
+
t.string :record_type
|
7
|
+
t.text :record_changes
|
8
|
+
t.datetime :created_at
|
9
|
+
t.datetime :updated_at
|
10
|
+
t.string :ip
|
11
|
+
t.boolean :hack, :default => false
|
12
|
+
t.string :name
|
13
|
+
|
14
|
+
t.timestamps
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module ActiveRecordLogger
|
2
|
+
@target_models = []
|
3
|
+
|
4
|
+
def self.target_models
|
5
|
+
@target_models
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.included base
|
9
|
+
base.class_exec do
|
10
|
+
extend ClassMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
base.class.class_exec do
|
14
|
+
attr_reader :rlog_fields
|
15
|
+
attr_reader :recoverable
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module InstanceMethods
|
20
|
+
private
|
21
|
+
|
22
|
+
def prepare_rlog_item
|
23
|
+
self.log = RlogItem.new
|
24
|
+
self.log.user = ActiveRecord::Base.current_user
|
25
|
+
self.log.ip = ActiveRecord::Base.current_request ? ActiveRecord::Base.current_request.remote_ip : "rails console"
|
26
|
+
self.log.record = self
|
27
|
+
self.log.name = self.to_s
|
28
|
+
@changes = ActiveSupport::HashWithIndifferentAccess.new
|
29
|
+
self.changes.each do |k, v|
|
30
|
+
@changes[k] = [v.first] if self.class.rlog_fields.include?(k.to_sym) and v[0] != v[1]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def save_rlog_item
|
35
|
+
if @changes.present? #|| self.log.hack
|
36
|
+
@changes.each do |k, v|
|
37
|
+
@changes[k].push self.send(k)
|
38
|
+
end
|
39
|
+
self.log.record_changes = @changes
|
40
|
+
self.log.save
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
module ClassMethods
|
47
|
+
def log_changes options = {}
|
48
|
+
@recoverable = options[:recoverable].nil? ? true : options[:recoverable]
|
49
|
+
include ActiveRecordLogger::InstanceMethods
|
50
|
+
before_save :prepare_rlog_item
|
51
|
+
after_save :save_rlog_item
|
52
|
+
|
53
|
+
has_many :rlog_items, :as => :record
|
54
|
+
attr_accessor :log
|
55
|
+
|
56
|
+
ActiveRecordLogger.target_models.push self.name unless ActiveRecordLogger.target_models.include? self.name
|
57
|
+
|
58
|
+
fields = self.attribute_names.map(&:to_sym)
|
59
|
+
fields -= options[:expect].map(&:to_sym) if options[:expect]
|
60
|
+
fields &= options[:only].map(&:to_sym) if options[:only]
|
61
|
+
|
62
|
+
@rlog_fields = fields.map(&:to_sym)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/rlog_items.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "rails"
|
2
|
+
require "rlog_items/version"
|
3
|
+
require "rlog_items/active_record_logger"
|
4
|
+
require "rlog_items/view_helper"
|
5
|
+
|
6
|
+
module RlogItems
|
7
|
+
#def self.config
|
8
|
+
# yield self
|
9
|
+
#end
|
10
|
+
|
11
|
+
#def self.templates &block
|
12
|
+
# self::PageFragment.config &block
|
13
|
+
#end
|
14
|
+
|
15
|
+
class Railtie < Rails::Railtie
|
16
|
+
initializer "rlog_items.active_record_logger" do
|
17
|
+
ActiveRecord::Base.send :include, ActiveRecordLogger
|
18
|
+
ActiveRecord::Base.class_eval{cattr_accessor :current_user}
|
19
|
+
ActiveRecord::Base.class_eval{cattr_accessor :current_request}
|
20
|
+
end
|
21
|
+
|
22
|
+
initializer "rlog_items.helper" do
|
23
|
+
ActionView::Base.send :include, ViewHelper
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
class Engine < Rails::Engine
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
|
data/rlog_items.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rlog_items/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "rlog_items"
|
8
|
+
gem.version = RlogItems::VERSION
|
9
|
+
gem.authors = ["Alexandr Promakh"]
|
10
|
+
gem.email = ["s-promakh@ya.ru"]
|
11
|
+
gem.description = %q{logging record's changes in database }
|
12
|
+
gem.summary = gem.description
|
13
|
+
gem.homepage = "http://mystand.ru/"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
|
18
|
+
gem.required_ruby_version = ">= 1.9.2"
|
19
|
+
|
20
|
+
gem.license = "MIT"
|
21
|
+
|
22
|
+
#gem.add_dependency "haml", "~> 4.0.3"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rlog_items
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Alexandr Promakh
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-26 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! 'logging record''s changes in database '
|
15
|
+
email:
|
16
|
+
- s-promakh@ya.ru
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- Rakefile
|
24
|
+
- app/models/rlog_items/rlog_item.rb
|
25
|
+
- lib/generators/rlog_items/install_generator.rb
|
26
|
+
- lib/generators/rlog_items/templates/migration.rb
|
27
|
+
- lib/rlog_items.rb
|
28
|
+
- lib/rlog_items/active_record_logger.rb
|
29
|
+
- lib/rlog_items/version.rb
|
30
|
+
- rlog_items.gemspec
|
31
|
+
homepage: http://mystand.ru/
|
32
|
+
licenses:
|
33
|
+
- MIT
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.9.2
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.24
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: logging record's changes in database
|
56
|
+
test_files: []
|