espinita 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +60 -0
  4. data/Rakefile +31 -0
  5. data/app/models/espinita/audit.rb +41 -0
  6. data/config/database.yml +24 -0
  7. data/config/routes.rb +2 -0
  8. data/db/migrate/20131029200927_create_auditable_audits.rb +15 -0
  9. data/lib/espinita.rb +16 -0
  10. data/lib/espinita/auditor.rb +18 -0
  11. data/lib/espinita/auditor_behavior.rb +83 -0
  12. data/lib/espinita/auditor_request.rb +13 -0
  13. data/lib/espinita/engine.rb +18 -0
  14. data/lib/espinita/version.rb +3 -0
  15. data/lib/tasks/espinita_tasks.rake +4 -0
  16. data/spec/controllers/audits_controller_spec.rb +47 -0
  17. data/spec/dummy/README.rdoc +28 -0
  18. data/spec/dummy/Rakefile +6 -0
  19. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  20. data/spec/dummy/app/assets/javascripts/general_controller.js +2 -0
  21. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  22. data/spec/dummy/app/assets/stylesheets/general_controller.css +4 -0
  23. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  24. data/spec/dummy/app/controllers/general_controller_controller.rb +2 -0
  25. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  26. data/spec/dummy/app/helpers/general_controller_helper.rb +2 -0
  27. data/spec/dummy/app/models/general_model.rb +4 -0
  28. data/spec/dummy/app/models/user.rb +2 -0
  29. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  30. data/spec/dummy/bin/bundle +3 -0
  31. data/spec/dummy/bin/rails +4 -0
  32. data/spec/dummy/bin/rake +4 -0
  33. data/spec/dummy/config.ru +4 -0
  34. data/spec/dummy/config/application.rb +23 -0
  35. data/spec/dummy/config/boot.rb +5 -0
  36. data/spec/dummy/config/database.yml +25 -0
  37. data/spec/dummy/config/environment.rb +5 -0
  38. data/spec/dummy/config/environments/development.rb +29 -0
  39. data/spec/dummy/config/environments/production.rb +80 -0
  40. data/spec/dummy/config/environments/test.rb +36 -0
  41. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  42. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  43. data/spec/dummy/config/initializers/inflections.rb +16 -0
  44. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  45. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  46. data/spec/dummy/config/initializers/session_store.rb +3 -0
  47. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  48. data/spec/dummy/config/locales/en.yml +23 -0
  49. data/spec/dummy/config/routes.rb +4 -0
  50. data/spec/dummy/db/development.sqlite3 +0 -0
  51. data/spec/dummy/db/migrate/20131029211126_create_general_models.rb +12 -0
  52. data/spec/dummy/db/migrate/20131030014901_create_users.rb +11 -0
  53. data/spec/dummy/db/schema.rb +52 -0
  54. data/spec/dummy/log/development.log +28 -0
  55. data/spec/dummy/log/test.log +17268 -0
  56. data/spec/dummy/public/404.html +58 -0
  57. data/spec/dummy/public/422.html +58 -0
  58. data/spec/dummy/public/500.html +57 -0
  59. data/spec/dummy/public/favicon.ico +0 -0
  60. data/spec/factories/auditable_audits.rb +11 -0
  61. data/spec/factories/general_models.rb +10 -0
  62. data/spec/factories/users.rb +9 -0
  63. data/spec/integration/navigation_test.rb +10 -0
  64. data/spec/models/espinita/audit_spec.rb +8 -0
  65. data/spec/models/models/general_model_spec.rb +160 -0
  66. data/spec/models/models/user_spec.rb +5 -0
  67. data/spec/spec_helper.rb +24 -0
  68. data/spec/support/models.rb +8 -0
  69. data/spec/support/schema.rb +34 -0
  70. metadata +291 -0
@@ -0,0 +1,58 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style>
6
+ body {
7
+ background-color: #EFEFEF;
8
+ color: #2E2F30;
9
+ text-align: center;
10
+ font-family: arial, sans-serif;
11
+ }
12
+
13
+ div.dialog {
14
+ width: 25em;
15
+ margin: 4em auto 0 auto;
16
+ border: 1px solid #CCC;
17
+ border-right-color: #999;
18
+ border-left-color: #999;
19
+ border-bottom-color: #BBB;
20
+ border-top: #B00100 solid 4px;
21
+ border-top-left-radius: 9px;
22
+ border-top-right-radius: 9px;
23
+ background-color: white;
24
+ padding: 7px 4em 0 4em;
25
+ }
26
+
27
+ h1 {
28
+ font-size: 100%;
29
+ color: #730E15;
30
+ line-height: 1.5em;
31
+ }
32
+
33
+ body > p {
34
+ width: 33em;
35
+ margin: 0 auto 1em;
36
+ padding: 1em 0;
37
+ background-color: #F7F7F7;
38
+ border: 1px solid #CCC;
39
+ border-right-color: #999;
40
+ border-bottom-color: #999;
41
+ border-bottom-left-radius: 4px;
42
+ border-bottom-right-radius: 4px;
43
+ border-top-color: #DADADA;
44
+ color: #666;
45
+ box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
46
+ }
47
+ </style>
48
+ </head>
49
+
50
+ <body>
51
+ <!-- This file lives in public/404.html -->
52
+ <div class="dialog">
53
+ <h1>The page you were looking for doesn't exist.</h1>
54
+ <p>You may have mistyped the address or the page may have moved.</p>
55
+ </div>
56
+ <p>If you are the application owner check the logs for more information.</p>
57
+ </body>
58
+ </html>
@@ -0,0 +1,58 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style>
6
+ body {
7
+ background-color: #EFEFEF;
8
+ color: #2E2F30;
9
+ text-align: center;
10
+ font-family: arial, sans-serif;
11
+ }
12
+
13
+ div.dialog {
14
+ width: 25em;
15
+ margin: 4em auto 0 auto;
16
+ border: 1px solid #CCC;
17
+ border-right-color: #999;
18
+ border-left-color: #999;
19
+ border-bottom-color: #BBB;
20
+ border-top: #B00100 solid 4px;
21
+ border-top-left-radius: 9px;
22
+ border-top-right-radius: 9px;
23
+ background-color: white;
24
+ padding: 7px 4em 0 4em;
25
+ }
26
+
27
+ h1 {
28
+ font-size: 100%;
29
+ color: #730E15;
30
+ line-height: 1.5em;
31
+ }
32
+
33
+ body > p {
34
+ width: 33em;
35
+ margin: 0 auto 1em;
36
+ padding: 1em 0;
37
+ background-color: #F7F7F7;
38
+ border: 1px solid #CCC;
39
+ border-right-color: #999;
40
+ border-bottom-color: #999;
41
+ border-bottom-left-radius: 4px;
42
+ border-bottom-right-radius: 4px;
43
+ border-top-color: #DADADA;
44
+ color: #666;
45
+ box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
46
+ }
47
+ </style>
48
+ </head>
49
+
50
+ <body>
51
+ <!-- This file lives in public/422.html -->
52
+ <div class="dialog">
53
+ <h1>The change you wanted was rejected.</h1>
54
+ <p>Maybe you tried to change something you didn't have access to.</p>
55
+ </div>
56
+ <p>If you are the application owner check the logs for more information.</p>
57
+ </body>
58
+ </html>
@@ -0,0 +1,57 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style>
6
+ body {
7
+ background-color: #EFEFEF;
8
+ color: #2E2F30;
9
+ text-align: center;
10
+ font-family: arial, sans-serif;
11
+ }
12
+
13
+ div.dialog {
14
+ width: 25em;
15
+ margin: 4em auto 0 auto;
16
+ border: 1px solid #CCC;
17
+ border-right-color: #999;
18
+ border-left-color: #999;
19
+ border-bottom-color: #BBB;
20
+ border-top: #B00100 solid 4px;
21
+ border-top-left-radius: 9px;
22
+ border-top-right-radius: 9px;
23
+ background-color: white;
24
+ padding: 7px 4em 0 4em;
25
+ }
26
+
27
+ h1 {
28
+ font-size: 100%;
29
+ color: #730E15;
30
+ line-height: 1.5em;
31
+ }
32
+
33
+ body > p {
34
+ width: 33em;
35
+ margin: 0 auto 1em;
36
+ padding: 1em 0;
37
+ background-color: #F7F7F7;
38
+ border: 1px solid #CCC;
39
+ border-right-color: #999;
40
+ border-bottom-color: #999;
41
+ border-bottom-left-radius: 4px;
42
+ border-bottom-right-radius: 4px;
43
+ border-top-color: #DADADA;
44
+ color: #666;
45
+ box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
46
+ }
47
+ </style>
48
+ </head>
49
+
50
+ <body>
51
+ <!-- This file lives in public/500.html -->
52
+ <div class="dialog">
53
+ <h1>We're sorry, but something went wrong.</h1>
54
+ </div>
55
+ <p>If you are the application owner check the logs for more information.</p>
56
+ </body>
57
+ </html>
File without changes
@@ -0,0 +1,11 @@
1
+ # Read about factories at https://github.com/thoughtbot/factory_girl
2
+
3
+ FactoryGirl.define do
4
+ factory :espinita_audit, :class => 'Audit' do
5
+ auditable nil
6
+ user nil
7
+ audited_changes "MyText"
8
+ version 1
9
+ remote_address "MyString"
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ # Read about factories at https://github.com/thoughtbot/factory_girl
2
+
3
+ FactoryGirl.define do
4
+ factory :general_model do
5
+ user nil
6
+ name "MyString"
7
+ settings "MyText"
8
+ position 1
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ # Read about factories at https://github.com/thoughtbot/factory_girl
2
+
3
+ FactoryGirl.define do
4
+ factory :user do
5
+ name "John"
6
+ last_name "Afferson"
7
+ email "john@afferson.com"
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ require 'test_helper'
2
+
3
+ class NavigationTest < ActionDispatch::IntegrationTest
4
+ fixtures :all
5
+
6
+ # test "the truth" do
7
+ # assert true
8
+ # end
9
+ end
10
+
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ module Espinita
4
+ describe Audit do
5
+ it{ should belong_to(:auditable) }
6
+ it{ should belong_to(:user)}
7
+ end
8
+ end
@@ -0,0 +1,160 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ describe GeneralModel do
5
+ it{should have_many :audits}
6
+
7
+ let(:current_user) do
8
+ FactoryGirl.create(:user)
9
+ end
10
+
11
+ describe "model" do
12
+
13
+ let(:general_model) do
14
+ GeneralModel
15
+ end
16
+
17
+ it "general model checks" do
18
+ expect(subject.audits).to be_empty
19
+ end
20
+
21
+ it "general auditable only method" do
22
+ general_model.auditable only: [:name]
23
+ expect(general_model.permited_columns).to include("name")
24
+ expect(general_model.permited_columns.size).to eql 1
25
+ end
26
+
27
+ it "general auditable except method" do
28
+ general_model.auditable except: [:name]
29
+ expect(general_model.excluded_cols).to include("name")
30
+ expect(general_model.permited_columns).not_to include("name")
31
+ end
32
+ end
33
+
34
+ describe "update model with only name key" do
35
+
36
+ let(:general_model) do
37
+ FactoryGirl.create(:general_model)
38
+ end
39
+
40
+ let(:updated_model) do
41
+ general_model.class.auditable only: [:name]
42
+ general_model.update_attribute(:name , "Foo" )
43
+ general_model
44
+ end
45
+
46
+ it "model should be associated" do
47
+ expect(updated_model.audits).to have(2).audits
48
+ end
49
+ end
50
+
51
+ describe "update model with only name key" do
52
+
53
+ let(:general_model) do
54
+ FactoryGirl.create(:general_model)
55
+ end
56
+
57
+ let(:updated_model) do
58
+ general_model.class.auditable except: [:name]
59
+ general_model.update_attribute(:name , "Foo" )
60
+ general_model
61
+ end
62
+
63
+ it "model should be associated" do
64
+ expect(updated_model.audits).to have(1).audits
65
+ end
66
+ end
67
+
68
+ describe "update with audit comment" do
69
+
70
+ let(:general_model) do
71
+ FactoryGirl.create(:general_model)
72
+ end
73
+
74
+ let(:updated_model) do
75
+ general_model.class.auditable
76
+ general_model.update_attributes(name: "Foo", audit_comment: "Some comment" )
77
+ general_model
78
+ end
79
+
80
+ it "auditable should be created with comment" do
81
+ expect(updated_model).to have(2).audits
82
+ expect(updated_model.audits.last.comment).to_not be_empty
83
+ expect(updated_model.audits.last.comment).to_not be "Some comment"
84
+ end
85
+
86
+ it "auditable should be created with comment" do
87
+ expect(updated_model).to have(2).audits
88
+ expect(updated_model.audits.last.version).to_not be_blank
89
+ expect(updated_model.audits.last.version).to eql 2
90
+ end
91
+ end
92
+
93
+ describe "save with current user" do
94
+
95
+ before :each do
96
+ RequestStore.store[:audited_user] = current_user
97
+ end
98
+
99
+ let(:general_model) do
100
+ FactoryGirl.create(:general_model)
101
+ end
102
+
103
+ let(:updated_model) do
104
+ general_model.class.auditable
105
+ general_model.update_attributes(name: "Foo", audit_comment: "Some comment" )
106
+ general_model
107
+ end
108
+
109
+ it "auditable should set current user" do
110
+ expect(updated_model.audits.last.user).to_not be_blank
111
+ expect(updated_model.audits.last.user).to be_an_instance_of User
112
+ expect(updated_model.audits.last.user).to eql current_user
113
+ end
114
+ end
115
+
116
+ describe "audit only on create" do
117
+
118
+ let(:general_model) do
119
+ [:create, :update, :destroy].each do |c|
120
+ GeneralModel.reset_callbacks(c)
121
+ end
122
+ GeneralModel.auditable on: [:create]
123
+ FactoryGirl.create(:general_model)
124
+ end
125
+
126
+ let(:updated_model) do
127
+ general_model.update_attributes(name: "Foo", audit_comment: "Some comment" )
128
+ general_model
129
+ end
130
+
131
+ it "should have 1 audit" do
132
+ expect(updated_model).to have(1).audits
133
+ expect(updated_model.audits.last.version).to_not be_blank
134
+ expect(updated_model.audits.last.version).to eql 1
135
+ end
136
+ end
137
+
138
+ describe "audit only on update" do
139
+
140
+ let(:general_model) do
141
+ [:create, :update, :destroy].each do |c|
142
+ GeneralModel.reset_callbacks(c)
143
+ end
144
+ GeneralModel.auditable on: [:update]
145
+ FactoryGirl.create(:general_model)
146
+ end
147
+
148
+ let(:updated_model) do
149
+ general_model.update_attributes(name: "Foo", audit_comment: "Some comment" )
150
+ general_model
151
+ end
152
+
153
+ it "should have 1 audit" do
154
+ expect(updated_model).to have(1).audits
155
+ expect(updated_model.audits.last.version).to_not be_blank
156
+ expect(updated_model.audits.last.version).to eql 1
157
+ end
158
+ end
159
+
160
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe User do
4
+ pending "add some examples to (or delete) #{__FILE__}"
5
+ end
@@ -0,0 +1,24 @@
1
+
2
+ require 'coveralls'
3
+ Coveralls.wear!
4
+
5
+ ENV["RAILS_ENV"] ||= 'test'
6
+ require File.expand_path("../../spec/dummy/config/environment", __FILE__)
7
+ require 'rspec/rails'
8
+ require 'rspec/autorun'
9
+
10
+ require "factory_girl_rails"
11
+ require "database_cleaner"
12
+ require 'capybara'
13
+ require 'capybara/rspec'
14
+ require 'shoulda/matchers/integrations/rspec'
15
+
16
+ require "espinita"
17
+
18
+
19
+ require 'support/schema'
20
+ require 'support/models'
21
+
22
+ RSpec.configure do |config|
23
+ config.infer_base_class_for_anonymous_controllers = false
24
+ end
@@ -0,0 +1,8 @@
1
+ class GeneralModel < ActiveRecord::Base
2
+ belongs_to :user
3
+ include Espinita::Auditor
4
+ end
5
+
6
+ class User < ActiveRecord::Base
7
+ end
8
+
@@ -0,0 +1,34 @@
1
+ require 'active_record'
2
+ require 'logger'
3
+
4
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
5
+ #ActiveRecord::Base.logger = Logger.new(SPEC_ROOT.join('debug.log'))
6
+ ActiveRecord::Migration.verbose = false
7
+
8
+ ActiveRecord::Schema.define do
9
+ create_table :users do |t|
10
+ t.string :name
11
+ t.string :last_name
12
+ t.string :email
13
+ t.timestamps
14
+ end
15
+ create_table :general_models do |t|
16
+ t.references :user, index: true
17
+ t.string :name
18
+ t.text :settings
19
+ t.integer :position
20
+ t.timestamps
21
+ end
22
+
23
+ create_table :espinita_audits do |t|
24
+ t.references :auditable, polymorphic: true, index: true
25
+ t.references :user, polymorphic: true, index: true
26
+ t.text :audited_changes
27
+ t.string :comment
28
+ t.integer :version
29
+ t.string :action
30
+ t.string :remote_address
31
+ t.timestamps
32
+ end
33
+ end
34
+