audit_model 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4fbf268b640ba3489c07ed6ff20a40f454a44ac2
4
- data.tar.gz: 137cbed98f5cb719d63b38abce211db531b2ec0b
3
+ metadata.gz: 0ecfdf66cff271315a58dde4a53b3699c26cd513
4
+ data.tar.gz: e8f1672bce78f28ccabe6bd41a0f0451cdd285b1
5
5
  SHA512:
6
- metadata.gz: 7168fbf66da9d93b435e64458e8d48f16e088bda655a2d9a4ddf28ca0f220e32355cb8096d9bca92bfaacbb4a4cc13a4d1e17fc09504da624e49cc4f3f42c7f0
7
- data.tar.gz: e11de75b2c0c50d62c4dd972efd52fb06af0a1b0ae4ca0bcd85e3c6d7db1a93cde291bee8fed53ca9d549c7561959d585837906e5551d66b6cca36477ccdac0a
6
+ metadata.gz: 9dd4a6f127ad3dd0b21ad950b0f2b075ecac5b349ff1d763a30aca684ac0230043530ee37ba60e67997980db403575bd6a2ae96dd4e64858fd374d91aff2254a
7
+ data.tar.gz: 151fe8095d9822825490bc931654c0d317f7f362c4b8ef6dffe7d2c2495ec798b00779dce21bd6e1708b19bc9e3f5c5d196fb92e083dc46d20897e6b6865df44
data/.DS_Store ADDED
Binary file
data/lib/.DS_Store ADDED
Binary file
data/lib/audit_model.rb CHANGED
@@ -1,5 +1,27 @@
1
+ require 'active_support'
2
+ require 'active_support/rails'
1
3
  require "audit_model/version"
2
4
 
3
5
  module AuditModel
4
- # Your code goes here...
6
+ extend ActiveSupport::Autoload
7
+
8
+ autoload :Config, 'audit_model/config'
9
+
10
+ module Controllers
11
+ autoload :Helpers, 'audit_model/controllers/helpers'
12
+ end
13
+
14
+ module Models
15
+ autoload :Base, 'audit_model/models/base'
16
+ autoload :Revision, 'audit_model/models/revision'
17
+ autoload :Helpers, 'audit_model/models/helpers'
18
+ end
19
+
20
+ class << self
21
+ def setup
22
+ yield Config
23
+ end
24
+ end
5
25
  end
26
+
27
+ require "audit_model/railtie" if defined?(Rails::Railtie)
Binary file
@@ -0,0 +1,5 @@
1
+ module AuditModel
2
+ module Config
3
+ mattr_accessor :user_method
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ module AuditModel
2
+ module Controllers
3
+ module Helpers
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ send :include, InstanceMethods
7
+ before_filter :set_current_user
8
+ after_action :clear_current_user
9
+ end
10
+
11
+ module InstanceMethods
12
+ def set_current_user
13
+ Thread.current[:user_id] = get_user.try(:id)
14
+ end
15
+
16
+ def clear_current_user
17
+ Thread.current[:user_id] = nil
18
+ end
19
+
20
+ private
21
+ def get_user
22
+ send(Config.user_method) if respond_to? Config.user_method
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,42 @@
1
+ module AuditModel
2
+ module Models
3
+ class Base < ActiveRecord::Base
4
+ self.abstract_class = true
5
+
6
+ BLACK_LIST = [
7
+ "id",
8
+ "created_at",
9
+ "updated_at"
10
+ ]
11
+
12
+ def who
13
+ revision.user
14
+ end
15
+
16
+ class << self
17
+ def audit(model, type)
18
+ revision = Revision.new({
19
+ model: model,
20
+ user_id: Thread.current[:user_id],
21
+ revison_date: Time.now
22
+ })
23
+ revision.audit = new sanitizer_args(model, type)
24
+ revision.save!
25
+ end
26
+
27
+ private
28
+
29
+ def sanitizer_args(model, type)
30
+ args = model.attributes.merge(model.changed_attributes)
31
+ args.except!(*BLACK_LIST).select! {|k,v| copy?(k) }
32
+ args[:rev_type] = type
33
+ args
34
+ end
35
+
36
+ def copy?(attribute)
37
+ attribute_names.include?(attribute)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,63 @@
1
+ module AuditModel
2
+ module Models
3
+ module Helpers
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ send :include, InstanceMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ def auditable(options = {})
12
+ options[:on] ||= [:update, :destroy]
13
+ setup_callbacks options[:on]
14
+ setup_model
15
+ has_many :revisions, as: :model, class_name: "AuditModel::Models::Revision"
16
+ has_many :audits, through: :revisions, source: :audit, source_type: audit_class
17
+ end
18
+
19
+ private
20
+ def setup_callbacks(options_on = [])
21
+ options_on.each do |option|
22
+ send "callback_on_#{option}"
23
+ end
24
+ end
25
+
26
+ def setup_model
27
+ Object.const_set audit_class, Class.new(Models::Base)
28
+ end
29
+
30
+ def callback_on_create
31
+ after_create :audit_on_create
32
+ end
33
+
34
+ def callback_on_update
35
+ before_update :audit_on_update
36
+ end
37
+
38
+ def callback_on_destroy
39
+ before_destroy :audit_on_destroy
40
+ end
41
+
42
+ def audit_class
43
+ "#{name}Audit"
44
+ end
45
+ end
46
+
47
+ module InstanceMethods
48
+ def audit_on_create
49
+ "#{self.class}Audit".constantize.audit(self, :create)
50
+ end
51
+
52
+ def audit_on_update
53
+ "#{self.class}Audit".constantize.audit(self, :update)
54
+ end
55
+
56
+ def audit_on_destroy
57
+ "#{self.class}Audit".constantize.audit(self, :destroy)
58
+ end
59
+
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,8 @@
1
+ module AuditModel
2
+ module Models
3
+ class Revision < ActiveRecord::Base
4
+ belongs_to :model, polymorphic: true
5
+ belongs_to :audit, polymorphic: true
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,16 @@
1
+ module AuditModel
2
+ class Railtie < Rails::Railtie
3
+ config.audit_model = Config
4
+ initializer "audit_model.controllers.include_helpers" do
5
+ ActiveSupport.on_load(:action_controller) do
6
+ include Controllers::Helpers
7
+ end
8
+ end
9
+
10
+ initializer "audit_model.models.include_helpers" do
11
+ ActiveSupport.on_load(:active_record) do
12
+ include Models::Helpers
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module AuditModel
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
Binary file
@@ -0,0 +1,40 @@
1
+ require 'rails/generators/active_record'
2
+ require 'generators/audit_model/column_not_found_error'
3
+ module AuditModel
4
+ module Generators
5
+ class AuditGenerator < ActiveRecord::Generators::Base
6
+ source_root File.expand_path("../templates", __FILE__)
7
+ argument :properties, type: :array, default: []
8
+
9
+ def klass
10
+ begin
11
+ class_name.constantize
12
+ rescue
13
+ raise "#{class_name} not defined"
14
+ end
15
+ end
16
+
17
+ def valid_properties?
18
+ properties.each do |propertie|
19
+ raise AuditModel::ColumnNotFoundError.new(klass: class_name, column: propertie) unless klass.attribute_names.include? propertie
20
+ end
21
+ end
22
+
23
+ def create_audit_migration
24
+ migration_template "audit_migration.rb", "db/migrate/create_#{table_name}.rb"
25
+ end
26
+
27
+ def audited_properties
28
+ klass.columns.select {|column| properties.include? column.name }
29
+ end
30
+
31
+ def table_name
32
+ "#{class_name}_audits".downcase
33
+ end
34
+
35
+ def migration_class_name
36
+ "Create#{table_name.camelize}"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,10 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def change
3
+ create_table(:<%= table_name %>) do |t|
4
+ <% audited_properties.each do |column| -%>
5
+ t.<%= column.type %> :<%= column.name %>
6
+ <% end -%>
7
+ t.string :rev_type
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module AuditModel
2
+ class ColumnNotFoundError < StandardError
3
+ def initialize(info)
4
+ super("The column #{info[:column]} doesn't exist in #{info[:klass]}!")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ require 'rails/generators/active_record'
2
+ module AuditModel
3
+ module Generators
4
+ class InstallGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ def copy_initializer
9
+ copy_file "audit_model.rb", "config/initializers/audit_model.rb"
10
+ end
11
+ def copy_migration
12
+ migration_template "revision.rb", "db/migrate/create_revisions.rb"
13
+ end
14
+
15
+ def self.next_migration_number(dirname)
16
+ ActiveRecord::Generators::Base.next_migration_number(dirname)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ AuditModel.setup do |config|
2
+ config.user_method = "current_user"
3
+ end
@@ -0,0 +1,13 @@
1
+ class CreateRevisions < ActiveRecord::Migration
2
+ create_table :revisions do |t|
3
+ t.integer :user_id, index: true
4
+ t.integer :model_id
5
+ t.string :model_type
6
+ t.integer :audit_id
7
+ t.string :audit_type
8
+
9
+ t.timestamp :revison_date
10
+ end
11
+ add_index :revisions, [:model_id, :model_type], :name => 'model_index'
12
+ add_index :revisions, [:audit_id, :audit_type], :name => 'audit_index'
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: audit_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alisson Bruno
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-30 00:00:00.000000000 Z
11
+ date: 2016-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -79,6 +79,7 @@ executables: []
79
79
  extensions: []
80
80
  extra_rdoc_files: []
81
81
  files:
82
+ - ".DS_Store"
82
83
  - ".gitignore"
83
84
  - ".rspec"
84
85
  - ".travis.yml"
@@ -86,8 +87,24 @@ files:
86
87
  - README.md
87
88
  - Rakefile
88
89
  - audit_model.gemspec
90
+ - lib/.DS_Store
89
91
  - lib/audit_model.rb
92
+ - lib/audit_model/.DS_Store
93
+ - lib/audit_model/config.rb
94
+ - lib/audit_model/controllers/helpers.rb
95
+ - lib/audit_model/models/base.rb
96
+ - lib/audit_model/models/helpers.rb
97
+ - lib/audit_model/models/revision.rb
98
+ - lib/audit_model/railtie.rb
90
99
  - lib/audit_model/version.rb
100
+ - lib/generators/.DS_Store
101
+ - lib/generators/audit_model/.DS_Store
102
+ - lib/generators/audit_model/audit/audit_generator.rb
103
+ - lib/generators/audit_model/audit/templates/audit_migration.rb
104
+ - lib/generators/audit_model/column_not_found_error.rb
105
+ - lib/generators/audit_model/install/install_generator.rb
106
+ - lib/generators/audit_model/install/templates/audit_model.rb
107
+ - lib/generators/audit_model/install/templates/revision.rb
91
108
  homepage: https://bitbucket.org/alissonbruno/audit_model
92
109
  licenses:
93
110
  - MIT