rails_admin_histeroid 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rails_admin_histeroid.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ Rails Admin Histeroid
2
+ ===============
3
+
4
+ a basic implementation of the history auditing of Rails Admin 0.1.1 for Mongoid 3.1
5
+
6
+ Installation
7
+ -------------------
8
+
9
+ in your Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rails_admin_histeroid', git: 'https://github.com/franc/rails_admin_histeroid.git'
13
+ ```
14
+
15
+ then run
16
+
17
+ ```
18
+ bundle install
19
+ ```
20
+
21
+ in your rails_admin initializer:
22
+
23
+ ```ruby
24
+ config.audit_with :histeroid, User
25
+ ```
26
+
27
+ your rails app should now have a working RailsAdmin with history support.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,37 @@
1
+ module RailsAdmin
2
+ module Extensions
3
+ module Histeroid
4
+ class AuditingAdapter
5
+ def initialize(controller, user_class = User)
6
+ @controller = controller
7
+ @user_class = user_class.to_s.constantize
8
+ require 'rails_admin_histeroid/histeroid'
9
+ end
10
+
11
+ def latest
12
+ ::RailsAdmin::Histeroid.latest
13
+ end
14
+
15
+ def delete_object(message, object, model, user)
16
+ ::RailsAdmin::Histeroid.create_history_item(message, object, model, user)
17
+ end
18
+
19
+ def update_object(model, object, associations_before, associations_after, modified_associations, old_object, user)
20
+ ::RailsAdmin::Histeroid.create_update_history(model, object, associations_before, associations_after, modified_associations, old_object, user)
21
+ end
22
+
23
+ def create_object(message, object, abstract_model, user)
24
+ ::RailsAdmin::Histeroid.create_history_item(message, object, abstract_model, user)
25
+ end
26
+
27
+ def listing_for_model(model, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
28
+ ::RailsAdmin::Histeroid.history_for_model(model, query, sort, sort_reverse, all, page, per_page)
29
+ end
30
+
31
+ def listing_for_object(model, object, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
32
+ ::RailsAdmin::Histeroid.history_for_object(model, object, query, sort, sort_reverse, all, page, per_page)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,85 @@
1
+ class RailsAdmin::Histeroid
2
+ include Mongoid::Document
3
+ store_in :collection => "rails_admin_histories"
4
+
5
+ field :message, type: String
6
+ field :item, type: String
7
+ field :table, type: String
8
+ field :username, type: String
9
+
10
+ include Mongoid::Timestamps
11
+
12
+ IGNORED_ATTRS = Set[:_id, :created_at, :created_on, :deleted_at, :updated_at, :updated_on, :deleted_on]
13
+
14
+ attr_accessible :message, :item, :table, :username
15
+
16
+ default_scope order_by(_id: :desc)
17
+
18
+ def self.latest
19
+ self.limit(100)
20
+ end
21
+
22
+ def self.create_update_history(model, object, associations_before, associations_after, modified_associations, old_object, user)
23
+ messages = []
24
+ changed_property_list = []
25
+ properties = model.properties.reject{|p| IGNORED_ATTRS.include?(p[:name])}
26
+
27
+ properties.each do |p|
28
+ property_name = p[:name].to_param
29
+ if old_object.safe_send(property_name) != object.safe_send(property_name)
30
+ changed_property_list << " #{property_name} : #{old_object.safe_send(property_name)} -> #{object.safe_send(property_name)} "
31
+ end
32
+ end
33
+
34
+ model.associations.each do |t|
35
+ assoc = changed_property_list.index(t[:foreign_key].to_param)
36
+ changed_property_list[assoc] = "associated #{t[:pretty_name]} #{t[:foreign_key].to_param}" if assoc
37
+ end
38
+
39
+ associations_after.each do |key, current|
40
+ removed_ids = (associations_before[key] - current).map{|m| '#' + m.to_s}
41
+ added_ids = (current - associations_before[key]).map{|m| '#' + m.to_s}
42
+ messages << "Removed #{key.to_s.capitalize} #{removed_ids.join(', ')} associations" if removed_ids.any?
43
+ messages << "Added #{key.to_s.capitalize} #{added_ids.join(', ')} associations" if added_ids.any?
44
+ end
45
+
46
+ modified_associations.uniq.each { |t| changed_property_list << "associated #{t}" }
47
+
48
+ unless changed_property_list.empty?
49
+ if object.respond_to? 'name'
50
+ messages << object.name
51
+ end
52
+ messages << "Changed #{changed_property_list.join(", ")}"
53
+ end
54
+ create_history_item(messages, object, model, user) unless messages.empty?
55
+ end
56
+
57
+ def self.create_history_item(message, object, abstract_model, user)
58
+ create(
59
+ :message => [message].flatten.join(', '),
60
+ :item => object.id,
61
+ :table => abstract_model.pretty_name,
62
+ :username => user.try(:email)
63
+ )
64
+ end
65
+
66
+ def self.history_for_model(model, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
67
+ history = where(table: model.pretty_name)
68
+ history = history.any_of(message: /.*#{query}.*/, username: /.*#{query}.*/) if query
69
+ if sort
70
+ order = sort_reverse == "true" ? :desc : :asc
71
+ history = history.order_by(sort.to_sym => order)
72
+ end
73
+ all ? history.entries : history.send(Kaminari.config.page_method_name, page.presence || "1").per(per_page)
74
+ end
75
+
76
+ def self.history_for_object(model, object, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
77
+ history = where(:table => model.pretty_name, :item => object.id)
78
+ history = history.any_of(message: /.*#{query}.*/, username: /.*#{query}.*/) if query
79
+ if sort
80
+ order = sort_reverse == "true" ? :desc : :asc
81
+ history = history.order_by(sort.to_sym => order)
82
+ end
83
+ all ? history.entries : history.send(Kaminari.config.page_method_name, page.presence || "1").per(per_page)
84
+ end
85
+ end
@@ -0,0 +1,3 @@
1
+ module RailsAdminHisteroid
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "rails_admin_histeroid/version"
2
+ require 'rails_admin_histeroid/auditing_adapter'
3
+
4
+ module RailsAdminHisteroid
5
+ RailsAdmin.add_extension(:histeroid, RailsAdmin::Extensions::Histeroid, {
6
+ :auditing => true
7
+ })
8
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rails_admin_histeroid/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rails_admin_histeroid"
7
+ s.version = RailsAdminHisteroid::VERSION
8
+ s.authors = ["Francois Paul"]
9
+ s.email = ["francois@rails.co.za"]
10
+ s.homepage = "https://github.com/franc/rails_admin_histeroid"
11
+ s.summary = %q{a basic implementation of the history auditing of Rails Admin for Mongoid 3.1}
12
+ s.description = %q{add support for Mongoid as a history auditing store to Rails Admin}
13
+
14
+ s.rubyforge_project = "rails_admin_histeroid"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_runtime_dependency 'rails_admin', '>= 0.1.1'
22
+ s.add_runtime_dependency 'mongoid', '>= 3.1.0'
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_admin_histeroid
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Francois Paul
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-10-08 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails_admin
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 25
30
+ segments:
31
+ - 0
32
+ - 1
33
+ - 1
34
+ version: 0.1.1
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: mongoid
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 3
48
+ - 1
49
+ - 0
50
+ version: 3.1.0
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ description: add support for Mongoid as a history auditing store to Rails Admin
54
+ email:
55
+ - francois@rails.co.za
56
+ executables: []
57
+
58
+ extensions: []
59
+
60
+ extra_rdoc_files: []
61
+
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - lib/rails_admin_histeroid.rb
68
+ - lib/rails_admin_histeroid/auditing_adapter.rb
69
+ - lib/rails_admin_histeroid/histeroid.rb
70
+ - lib/rails_admin_histeroid/version.rb
71
+ - rails_admin_histeroid.gemspec
72
+ has_rdoc: true
73
+ homepage: https://github.com/franc/rails_admin_histeroid
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options: []
78
+
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project: rails_admin_histeroid
102
+ rubygems_version: 1.3.7
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: a basic implementation of the history auditing of Rails Admin for Mongoid 3.1
106
+ test_files: []
107
+