samsara 0.0.2
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 +15 -0
- data/.gitignore +16 -0
- data/.travis.yml +12 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +8 -0
- data/lib/samsara/context.rb +7 -0
- data/lib/samsara/context_concern.rb +31 -0
- data/lib/samsara/controller.rb +37 -0
- data/lib/samsara/http_request.rb +32 -0
- data/lib/samsara/model.rb +43 -0
- data/lib/samsara/railtie.rb +13 -0
- data/lib/samsara/revision.rb +7 -0
- data/lib/samsara/revision_concern.rb +30 -0
- data/lib/samsara/serializer.rb +29 -0
- data/lib/samsara/version.rb +3 -0
- data/lib/samsara.rb +90 -0
- data/samsara.gemspec +32 -0
- data/spec/context_spec.rb +42 -0
- data/spec/controller_spec.rb +122 -0
- data/spec/dummy/.rspec +2 -0
- data/spec/dummy/README.rdoc +28 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/images/.keep +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/controllers/application_controller.rb +5 -0
- data/spec/dummy/app/controllers/celebrities_controller.rb +30 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.keep +0 -0
- data/spec/dummy/app/models/.keep +0 -0
- data/spec/dummy/app/models/celebrity.rb +5 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +29 -0
- data/spec/dummy/config/application.rb +32 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +41 -0
- data/spec/dummy/config/environments/production.rb +79 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/routes.rb +5 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/migrate/20150223145759_create_test_tables.rb +30 -0
- data/spec/dummy/db/schema.rb +45 -0
- data/spec/dummy/lib/assets/.keep +0 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/log/development.log +0 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/model_spec.rb +219 -0
- data/spec/rails_helper.rb +51 -0
- data/spec/revision_spec.rb +64 -0
- data/spec/serializer_spec.rb +41 -0
- data/spec/spec_helper.rb +90 -0
- metadata +257 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
---
|
|
2
|
+
!binary "U0hBMQ==":
|
|
3
|
+
metadata.gz: !binary |-
|
|
4
|
+
ZTg2NTcwOGUzYzc2ZGVlNjM3MmMyZTM3NmNmY2Y3YzVmNWVjMzk2NA==
|
|
5
|
+
data.tar.gz: !binary |-
|
|
6
|
+
YWFhMGZhY2RiYzJjODhhMWU3MTEwNWNjNmQ2OTNlNmEyYmVmNTVhNQ==
|
|
7
|
+
SHA512:
|
|
8
|
+
metadata.gz: !binary |-
|
|
9
|
+
N2QyNzMwZjIxMTU2YTY0YjNhZDI3MGZjMTJhNTY2NTA1MDU0ZDc0Y2MxZmE0
|
|
10
|
+
OGI4ZTJlYzMyODg2NjU5MzdjZGQ4Y2UyNzZkODA4NDM5YmViMjE2MTYyNTdm
|
|
11
|
+
ZDU3NTQyNzFjNDgwODIxMjFmYzZkNGJmZjdlM2YxZDY5ZjBjMmE=
|
|
12
|
+
data.tar.gz: !binary |-
|
|
13
|
+
OGEyZWQ2Njc0MjY5OGY5MmZmOTc4YzBhM2E5OGRiMDJmZWUyNTdlNTM1MjZi
|
|
14
|
+
ZDRmOWNjYjViYzEwZWIzNTk5MzhjZWE2OTUyODM0NGI0OTg5NzMzZjIwOGNj
|
|
15
|
+
OGY4NTc3NWE1YmM1OThlZjBjODcyYmM1ZjZmM2ZiMGIzYzk2M2Y=
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2015 Tom Nijmeijer
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# Samsara
|
|
2
|
+
[](http://travis-ci.org/nedap/samsara)
|
|
3
|
+
[](https://codeclimate.com/repos/55377484e30ba0452a000e20/feed)
|
|
4
|
+
[](https://codeclimate.com/repos/55377484e30ba0452a000e20/feed)
|
|
5
|
+
|
|
6
|
+
A simple auditing gem that does things a little differently. Rather than just storing the domain changes made
|
|
7
|
+
this gem is also capable of storing the context in which the changes were made such as a http request. This
|
|
8
|
+
context is stored in a separate database table and is shared by multiple revisions in case a single request
|
|
9
|
+
causes changes to multiple domain models.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Add this line to your application's Gemfile:
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
gem 'samsara'
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
And then execute:
|
|
20
|
+
|
|
21
|
+
$ bundle
|
|
22
|
+
|
|
23
|
+
Or install it yourself as:
|
|
24
|
+
|
|
25
|
+
$ gem install samsara
|
|
26
|
+
|
|
27
|
+
## Contributing
|
|
28
|
+
|
|
29
|
+
1. Fork it ( https://github.com/nedap/samsara/fork )
|
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
31
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
33
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
require 'active_support/concern'
|
|
2
|
+
|
|
3
|
+
module Samsara::ContextConcern
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
# t.string :event_type
|
|
7
|
+
# t.text :event_attributes
|
|
8
|
+
# t.string :environment_name
|
|
9
|
+
# t.string :application_name
|
|
10
|
+
# t.timestamps
|
|
11
|
+
|
|
12
|
+
included do
|
|
13
|
+
has_many :revisions, class_name: Samsara.revision_class_name
|
|
14
|
+
|
|
15
|
+
serialize :event_attributes, Samsara::Serializer
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def event_class
|
|
19
|
+
event_type.constantize
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def event
|
|
23
|
+
@event ||= event_class.new(event_attributes)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def event=(event)
|
|
27
|
+
self.event_type = event.class.name
|
|
28
|
+
self.event_attributes = event.attributes
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module Samsara::Controller
|
|
2
|
+
extend ActiveSupport::Concern
|
|
3
|
+
|
|
4
|
+
included do
|
|
5
|
+
before_filter :set_samsara_context
|
|
6
|
+
after_filter :unset_samsara_context
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def set_samsara_context
|
|
10
|
+
return unless Samsara.active?
|
|
11
|
+
Samsara.current_context = build_samsara_context
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def unset_samsara_context
|
|
15
|
+
Samsara.current_context = nil
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def build_samsara_context
|
|
19
|
+
Samsara.context_class.new.tap do |c|
|
|
20
|
+
c.event = build_samsara_http_request
|
|
21
|
+
c.application_name = application_name_for_samsara
|
|
22
|
+
c.environment_name = environment_name_for_samsara
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def build_samsara_http_request
|
|
27
|
+
Samsara.request_class.build_from(self)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def environment_name_for_samsara
|
|
31
|
+
Rails.env
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def application_name_for_samsara
|
|
35
|
+
Rails.application.class.parent.name
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
class Samsara::HttpRequest
|
|
2
|
+
attr_accessor :attributes
|
|
3
|
+
|
|
4
|
+
def self.build_from(controller)
|
|
5
|
+
self.new(extract_attributes_from controller)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def self.extract_attributes_from(controller)
|
|
9
|
+
{
|
|
10
|
+
url: controller.request.filtered_path,
|
|
11
|
+
params: controller.request.filtered_parameters,
|
|
12
|
+
method: controller.request.method
|
|
13
|
+
}
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def initialize(attributes = {})
|
|
17
|
+
@attributes = attributes
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def method
|
|
21
|
+
attributes[:method]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def url
|
|
25
|
+
attributes[:url]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def params
|
|
29
|
+
attributes[:params]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
module Samsara::Model
|
|
2
|
+
extend ActiveSupport::Concern
|
|
3
|
+
|
|
4
|
+
included do
|
|
5
|
+
def self.is_audited?
|
|
6
|
+
@is_audited || false
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def self.is_audited
|
|
10
|
+
@is_audited = true
|
|
11
|
+
|
|
12
|
+
has_many :auditing_revisions, class_name: Samsara.revision_class_name, as: :subject
|
|
13
|
+
|
|
14
|
+
after_create :audit_create
|
|
15
|
+
after_update :audit_update, if: :changed?
|
|
16
|
+
after_destroy :audit_destroy, if: :destroyed?
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def audit_create
|
|
21
|
+
create_audit :create
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def audit_update
|
|
25
|
+
create_audit :update
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def audit_destroy
|
|
29
|
+
create_audit :destroy
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def create_audit(action)
|
|
33
|
+
return unless Samsara.active?
|
|
34
|
+
Samsara.revision_class.new do |a|
|
|
35
|
+
a.action = action
|
|
36
|
+
a.subject = self
|
|
37
|
+
a.context = Samsara.current_context
|
|
38
|
+
a.modified_attributes = self.attributes
|
|
39
|
+
a.original_attributes = self.changed_attributes
|
|
40
|
+
end.save
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
class Samsara::Railtie < Rails::Railtie
|
|
2
|
+
initializer 'samsara.initialize' do
|
|
3
|
+
|
|
4
|
+
ActiveSupport.on_load(:active_record) do
|
|
5
|
+
include Samsara::Model
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
ActiveSupport.on_load(:action_controller) do
|
|
9
|
+
include Samsara::Controller
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require 'active_support/concern'
|
|
2
|
+
|
|
3
|
+
module Samsara::RevisionConcern
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
# t.string :action
|
|
7
|
+
# t.integer :subject_id
|
|
8
|
+
# t.string :subject_type
|
|
9
|
+
# t.text :modified_attributes
|
|
10
|
+
# t.text :original_attributes
|
|
11
|
+
# t.references :context
|
|
12
|
+
# t.timestamps
|
|
13
|
+
|
|
14
|
+
included do
|
|
15
|
+
belongs_to :subject, polymorphic: true
|
|
16
|
+
belongs_to :context, class_name: Samsara.context_class_name
|
|
17
|
+
|
|
18
|
+
default_scope{ order(:created_at) }
|
|
19
|
+
|
|
20
|
+
serialize :modified_attributes, Samsara::Serializer
|
|
21
|
+
serialize :original_attributes, Samsara::Serializer
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def attribute_changes
|
|
25
|
+
original_attributes.each_with_object({}) do |(attribute, value), changes|
|
|
26
|
+
changes[attribute] = [value, modified_attributes[attribute]]
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
class Samsara::Serializer
|
|
2
|
+
|
|
3
|
+
def self.dump(arg)
|
|
4
|
+
case arg
|
|
5
|
+
when Hash
|
|
6
|
+
arg.to_json
|
|
7
|
+
else
|
|
8
|
+
arg
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def self.load(arg)
|
|
13
|
+
case arg
|
|
14
|
+
when String
|
|
15
|
+
load JSON.parse(arg)
|
|
16
|
+
when Hash
|
|
17
|
+
symbolize_keys! arg
|
|
18
|
+
else
|
|
19
|
+
arg
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.symbolize_keys!(arg)
|
|
24
|
+
return arg unless arg.is_a? Hash
|
|
25
|
+
arg.values.each{|val| symbolize_keys! val }
|
|
26
|
+
arg.symbolize_keys!
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
end
|
data/lib/samsara.rb
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
require "samsara/version"
|
|
2
|
+
|
|
3
|
+
module Samsara
|
|
4
|
+
|
|
5
|
+
def self.table_name_prefix
|
|
6
|
+
'samsara_'
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def self.active?
|
|
10
|
+
active != :disabled
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def self.active
|
|
14
|
+
@@auditing_active ||= :disabled
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.active=(active)
|
|
18
|
+
@@auditing_active = active
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.enable!
|
|
22
|
+
@@auditing_active = :enabled
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def self.disable!
|
|
26
|
+
@@auditing_active = :disabled
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.current_context=(val)
|
|
30
|
+
Thread.current[:samsara_context] = val
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.current_context
|
|
34
|
+
Thread.current[:samsara_context]
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.revision_class_name
|
|
38
|
+
@@revision_class_name ||= "Samsara::Revision"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def self.revision_class_name=(name)
|
|
42
|
+
Samsara::Revision.abstract_class = (name != "Samsara::Revision")
|
|
43
|
+
@@revision_class_name = name
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.revision_class
|
|
47
|
+
revision_class_name.constantize
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def self.context_class_name
|
|
51
|
+
@@context_class_name ||= "Samsara::Context"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.context_class_name=(name)
|
|
55
|
+
Samsara::Context.abstract_class = (name != "Samsara::Context")
|
|
56
|
+
@@context_class_name = name
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def self.context_class
|
|
60
|
+
context_class_name.constantize
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def self.request_class_name
|
|
64
|
+
@@request_class_name ||= "Samsara::HttpRequest"
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def self.request_class_name=(name)
|
|
68
|
+
Samsara::Context.abstract_class = (name != "Samsara::HttpRequest")
|
|
69
|
+
@@request_class_name = name
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def self.request_class
|
|
73
|
+
request_class_name.constantize
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def self.configure(&block)
|
|
77
|
+
yield self
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
require "samsara/serializer"
|
|
83
|
+
require "samsara/http_request"
|
|
84
|
+
require "samsara/revision_concern"
|
|
85
|
+
require "samsara/revision"
|
|
86
|
+
require "samsara/context_concern"
|
|
87
|
+
require "samsara/context"
|
|
88
|
+
require "samsara/model"
|
|
89
|
+
require "samsara/controller"
|
|
90
|
+
require "samsara/railtie" if defined?(Rails)
|
data/samsara.gemspec
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'samsara/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "samsara"
|
|
8
|
+
spec.version = Samsara::VERSION
|
|
9
|
+
spec.authors = ["Tom Nijmeijer"]
|
|
10
|
+
spec.email = ["tom.nijmeijer@nedap.com"]
|
|
11
|
+
spec.summary = "Custom auditing gem made for and by PEP"
|
|
12
|
+
spec.description = <<-DESC
|
|
13
|
+
Custom auditing gem made for and by PEP. Rather than just saving the modifications made,
|
|
14
|
+
this gem puts the changes in context by storing a separate context object. Multiple
|
|
15
|
+
revisions can be attached to a single context such as a single http request.
|
|
16
|
+
DESC
|
|
17
|
+
spec.homepage = ""
|
|
18
|
+
spec.license = "MIT"
|
|
19
|
+
|
|
20
|
+
spec.files = `git ls-files -z`.split("\x0")
|
|
21
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
22
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
23
|
+
spec.require_paths = ["lib"]
|
|
24
|
+
|
|
25
|
+
spec.add_dependency "activerecord", ">= 3.0"
|
|
26
|
+
spec.add_dependency "activesupport", ">= 3.0"
|
|
27
|
+
|
|
28
|
+
spec.add_development_dependency "bundler", ">= 1.6"
|
|
29
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
30
|
+
spec.add_development_dependency "sqlite3", "~> 1.2"
|
|
31
|
+
spec.add_development_dependency "rspec-rails", "~> 3.2"
|
|
32
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
require 'rails_helper'
|
|
2
|
+
|
|
3
|
+
describe Samsara::Context do
|
|
4
|
+
|
|
5
|
+
let(:celebrity_params) do
|
|
6
|
+
{ first_name: "Neil", last_name: "Harris" }
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
let(:params) do
|
|
10
|
+
{ celebrity: celebrity_params, controller: "celebrities", action: "create" }
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
let(:event_attributes) do
|
|
14
|
+
{ method: "POST", url: "/celebrities", params: params }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
let(:event) do
|
|
18
|
+
Samsara::HttpRequest.new(event_attributes)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
let(:context_attributes) do
|
|
22
|
+
{ environment_name: "test", application_name: "Dummy", event: event }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
before { Samsara::Context.create context_attributes }
|
|
26
|
+
subject { Samsara::Context.first }
|
|
27
|
+
|
|
28
|
+
describe "#event_attributes" do
|
|
29
|
+
it "is serialized using the Samsara::Serializer" do
|
|
30
|
+
allow(Samsara::Serializer).to receive(:dump).with(any_args)
|
|
31
|
+
expect(Samsara::Serializer).to receive(:dump).with(event_attributes)
|
|
32
|
+
subject.event_attributes = event_attributes
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "is deserialized using the Samsara::Serializer" do
|
|
36
|
+
allow(Samsara::Serializer).to receive(:load).with(any_args)
|
|
37
|
+
expect(Samsara::Serializer).to receive(:load).with(event_attributes.to_json)
|
|
38
|
+
subject.event_attributes
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
require 'rails_helper'
|
|
2
|
+
|
|
3
|
+
describe CelebritiesController, type: :request do
|
|
4
|
+
let(:context_class) { Samsara.context_class }
|
|
5
|
+
let(:revision_class) { Samsara.revision_class }
|
|
6
|
+
|
|
7
|
+
let(:celebrity_params) do
|
|
8
|
+
{ first_name: "Brigitte", last_name: "Bardot", gender: "2", birth_date: "1934-9-28" }
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
context "while samsara is not active" do
|
|
12
|
+
before do
|
|
13
|
+
Samsara.disable!
|
|
14
|
+
post(celebrities_path, celebrity: celebrity_params)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "does not create a context" do
|
|
18
|
+
expect(context_class.count).to eq 0
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "does not create a revision" do
|
|
22
|
+
expect(revision_class.count).to eq 0
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
context "while samsara is active" do
|
|
27
|
+
before do
|
|
28
|
+
Samsara.enable!
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
context "requesting the index" do
|
|
32
|
+
before do
|
|
33
|
+
get(celebrities_path)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "does not create a context" do
|
|
37
|
+
expect(context_class.count).to eq 0
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "does not create a revision" do
|
|
41
|
+
expect(revision_class.count).to eq 0
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
context "creating an invalid celebrity" do
|
|
46
|
+
before do
|
|
47
|
+
post(celebrities_path, celebrity: celebrity_params.merge(gender: 3))
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it "does not create a context" do
|
|
51
|
+
expect(context_class.count).to eq 0
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it "does not create a revision" do
|
|
55
|
+
expect(revision_class.count).to eq 0
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
context "creating a single celebrity" do
|
|
60
|
+
before do
|
|
61
|
+
post(celebrities_path, celebrity: celebrity_params, password: "supersecret")
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it "creates a context" do
|
|
65
|
+
expect(context_class.count).to eq 1
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it "creates a revision" do
|
|
69
|
+
expect(revision_class.count).to eq 1
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
context "the created context" do
|
|
73
|
+
let(:context) { context_class.first }
|
|
74
|
+
|
|
75
|
+
let(:expected_params) do
|
|
76
|
+
{ celebrity: celebrity_params, password: "[FILTERED]", controller: "celebrities", action: "create" }
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it "has set the application name" do
|
|
80
|
+
expect(context.application_name).to eq "Dummy"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it "has set the environment name" do
|
|
84
|
+
expect(context.environment_name).to eq "test"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it "has set the http request as the event" do
|
|
88
|
+
expect(context.event).to be_a Samsara::HttpRequest
|
|
89
|
+
expect(context.event.method).to eq "POST"
|
|
90
|
+
expect(context.event.url).to eq celebrities_path
|
|
91
|
+
expect(context.event.params).to eq(expected_params)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
it "has been linked to the revision" do
|
|
95
|
+
expect(context.revisions.count).to eq 1
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
context "creating multiple celebrities" do
|
|
101
|
+
before do
|
|
102
|
+
post(multiply_celebrities_path, celebrity: celebrity_params, password: "supersecret")
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
it "creates a context" do
|
|
106
|
+
expect(context_class.count).to eq 1
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it "creates multiple revisions" do
|
|
110
|
+
expect(revision_class.count).to eq 2
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
context "the created context" do
|
|
114
|
+
let(:context) { context_class.first }
|
|
115
|
+
|
|
116
|
+
it "has been linked to all the revisions" do
|
|
117
|
+
expect(context.revisions.count).to eq 2
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
data/spec/dummy/.rspec
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
== README
|
|
2
|
+
|
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
|
4
|
+
application up and running.
|
|
5
|
+
|
|
6
|
+
Things you may want to cover:
|
|
7
|
+
|
|
8
|
+
* Ruby version
|
|
9
|
+
|
|
10
|
+
* System dependencies
|
|
11
|
+
|
|
12
|
+
* Configuration
|
|
13
|
+
|
|
14
|
+
* Database creation
|
|
15
|
+
|
|
16
|
+
* Database initialization
|
|
17
|
+
|
|
18
|
+
* How to run the test suite
|
|
19
|
+
|
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
|
21
|
+
|
|
22
|
+
* Deployment instructions
|
|
23
|
+
|
|
24
|
+
* ...
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
|
28
|
+
<tt>rake doc:app</tt>.
|