aasm_history 0.0.1 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d753fc692e9678444789c357d0abcde9a976409
4
- data.tar.gz: 3279159e3145247d579781d6318326e1cf5eb518
3
+ metadata.gz: 403df6b0e2e4c41c78a9c9f99f22729a54ec1d78
4
+ data.tar.gz: 59594bd1ddcaf37ae6370f9a4009e4dcadafa80a
5
5
  SHA512:
6
- metadata.gz: 198b91dd1785f2d6b5cd91ae9a7ddac077493a7f01ea629122417fcd3fbaf9120e0ac8565be566e2e76937ec0016ec78106e82dd8186228d4a6ba52b1cdf644d
7
- data.tar.gz: 635b3a36f2367456e7f037f249c1bbbde62f22d512ea66a57df3d391e6e02f0a47b7fbe16d02c04824f1ca4a3ce5452b386ac776e92955270699fa500653bcb6
6
+ metadata.gz: ca3e98942fefe4ac69eea1e42e92fc9a1a73f87bb78c22ffaabd06f8ed43768dcff9ccbb3775debea227a3be9fcb6a5eb980a090980db0233d72adbe3532ddf1
7
+ data.tar.gz: 203d472f453728ceb7a5189989ca92ae4f26afd168727589c8ec469138a73e1c17b1c92aa5e038fca5709e48001dba210c616ec69440b45273c0d7fba63f0971
data/.gitignore CHANGED
@@ -1,22 +1,4 @@
1
1
  *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
2
  Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
18
3
  *.bundle
19
- *.so
20
- *.o
21
- *.a
22
- mkmf.log
4
+
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 2.1.0
3
+ - 2.0.0
4
+ script:
5
+ - bundle exec rake
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
+ [![Build Status](https://travis-ci.org/gogiel/aasm_history.svg?branch=master)](https://travis-ci.org/gogiel/aasm_history)
2
+
1
3
  # AasmHistory
2
4
 
3
5
  Track and persist AASM state history.
4
6
 
5
- Currently only ActiveRecord is supported.
7
+ This gem depends on Ruby >= 2.0.0.
6
8
 
7
9
  ## Installation
8
10
 
@@ -10,7 +12,9 @@ Add this line to your application's Gemfile:
10
12
 
11
13
  gem 'aasm_history'
12
14
 
13
- ## Usage
15
+ ## Installation
16
+
17
+ ### Active Record
14
18
 
15
19
  Add migration:
16
20
 
@@ -22,15 +26,52 @@ Add migration:
22
26
  t.datetime :created_at
23
27
  t.datetime :updated_at
24
28
  end
25
-
29
+
26
30
  It will create state history table - common for all classes.
27
31
 
28
- Inside aasm block insert
32
+ Create StateHistory class
33
+
34
+ class StateHistory < ActiveRecord::Base
35
+ belongs_to :stateable, polymorphic: true
36
+ end
37
+
38
+ ### Sequel, Mongoid and others
39
+
40
+ Not supported.
41
+
42
+ ## Usage
43
+
44
+ Inside `aasm` block call `has_history`.
45
+
46
+ aasm do
47
+ has_history
48
+ # ...
49
+ end
50
+
51
+ You can also call `has_state_history` in your model to create `state_histories` relation.
52
+
53
+ ## Example:
54
+
55
+ ### Definition
56
+
57
+ class Order
58
+ aasm do
59
+ has_history
60
+ # ...
61
+ end
62
+
63
+ has_state_history
64
+ end
65
+
66
+ ### Usage
67
+
68
+ #> Order.last.state_histories
69
+ [#<StateHistory id: 2, state: "in_production", previous_state: "new", stateable_id: 2, stateable_type: "Order", created_at: "2014-09-27 22:34:00", updated_at: "2014-09-27 22:34:00">]
29
70
 
30
- has_history
71
+ ## Extending
31
72
 
32
- And that's all!
73
+ Gem is highly extendable and configurable.
33
74
 
34
75
  ## Contributing
35
76
 
36
- You are welcome to contribute with Github's Pull Requests!
77
+ You are welcome to contribute with Pull Requests and new Issues!
data/Rakefile CHANGED
@@ -1,2 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
2
3
 
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ['Jan Jedrychowski']
10
10
  spec.email = ['jan@jedrychowski.org']
11
11
  spec.summary = %q{Track and persist AASM state history}
12
- spec.homepage = ''
12
+ spec.homepage = 'https://github.com/gogiel/aasm_history'
13
13
  spec.license = 'MIT'
14
14
 
15
15
  spec.files = `git ls-files -z`.split("\x0")
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.required_ruby_version = '>= 2.0.0' # due to Module#prepend
19
19
 
20
20
  spec.add_dependency 'aasm', '~> 3.3'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
21
22
  spec.add_development_dependency 'bundler', '~> 1.6'
22
23
  spec.add_development_dependency 'rspec', '~> 3.1'
23
24
  spec.add_development_dependency 'activerecord', '~> 4.1'
@@ -1,6 +1,20 @@
1
- require "aasm_history/version"
2
- require 'aasm_history/aasm_ext/base'
1
+ require 'aasm'
3
2
 
4
3
  module AasmHistory
5
- autoload :HistoryCreator, 'aasm_history/history_creator'
4
+ autoload :PersistanceDeterminator, 'aasm_history/persistance_determinator'
5
+ autoload :UnknownPersistanceLayer, 'aasm_history/persistance_determinator'
6
+
7
+ module Persistance
8
+ autoload :ActiveRecord, 'aasm_history/persistance/active_record'
9
+ autoload :ActiveRecordCreator, 'aasm_history/persistance/active_record_creator'
10
+ end
11
+
12
+ module Ext
13
+ autoload :ActiveRecord, 'aasm_history/ext/active_record'
14
+ end
15
+
6
16
  end
17
+
18
+ require 'aasm_history/version'
19
+ require 'aasm_history/aasm_ext/base'
20
+ require 'aasm_history/load_extensions'
@@ -1,24 +1,14 @@
1
1
  module AASM
2
2
  class Base
3
- module HistoryMethods
4
- def aasm_write_state state
5
- previous_state = read_attribute(self.class.aasm_column)
6
- success = super state
7
- store_aasm_history state, previous_state if success
8
- success
9
- end
10
-
11
- private
12
-
13
- def store_aasm_history state, previous_state
14
- AasmHistory::HistoryCreator.new(self, state, previous_state).create
15
- end
16
- end
17
-
18
3
  def has_history
19
- configure 'history_class', 'StateHistory'
20
- @klass.send :prepend, HistoryMethods
4
+ case AasmHistory::PersistanceDeterminator.determine(@klass)
5
+ when :active_record
6
+ configure :history_class, 'StateHistory'
7
+ configure :creator_class, 'AasmHistory::Persistance::ActiveRecordCreator'
8
+ @klass.send :prepend, AasmHistory::Persistance::ActiveRecord
9
+ else
10
+ raise AasmHistory::UnknownPersistanceLayer
11
+ end
21
12
  end
22
-
23
13
  end
24
14
  end
@@ -0,0 +1,15 @@
1
+ module AasmHistory
2
+ module Ext
3
+ module ActiveRecord
4
+ def self.included base
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def has_state_history
10
+ has_many :state_histories, as: :stateable
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ if defined? ActiveRecord::Base
2
+ ActiveRecord::Base.include AasmHistory::Ext::ActiveRecord
3
+ end
@@ -0,0 +1,19 @@
1
+ module AasmHistory
2
+ module Persistance
3
+ module ActiveRecord
4
+
5
+ def aasm_write_state state
6
+ previous_state = read_attribute(self.class.aasm_column)
7
+ success = super state
8
+ store_aasm_history state, previous_state if success
9
+ success
10
+ end
11
+
12
+ private
13
+
14
+ def store_aasm_history state, previous_state
15
+ AASM::StateMachine[self.class].config.creator_class.constantize.new(self, state, previous_state).create
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ module AasmHistory
2
+ module Persistance
3
+ class ActiveRecordCreator
4
+ def initialize object, state, previous_state
5
+ @object = object
6
+ @state = state
7
+ @previous_state = previous_state
8
+ end
9
+
10
+ def create
11
+ klass.create! attributes
12
+ end
13
+
14
+ def klass
15
+ @klass ||= AASM::StateMachine[@object.class].config.history_class.constantize
16
+ end
17
+
18
+ def attributes
19
+ {state: @state, previous_state: @previous_state, stateable: @object}
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,26 @@
1
+ module AasmHistory
2
+ class UnknownPersistanceLayer < StandardError
3
+ def message
4
+ 'Persistance layer is not supported.'
5
+ end
6
+ end
7
+
8
+ class PersistanceDeterminator
9
+ def initialize base
10
+ @base = base
11
+ end
12
+
13
+ def determine
14
+ hierarchy = @base.ancestors.map {|klass| klass.to_s}
15
+
16
+ if hierarchy.include?('ActiveRecord::Base')
17
+ :active_record
18
+ end
19
+ end
20
+
21
+ def self.determine base
22
+ new(base).determine
23
+ end
24
+
25
+ end
26
+ end
@@ -1,3 +1,3 @@
1
1
  module AasmHistory
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1"
3
3
  end
@@ -1,13 +1,37 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe 'AASM History' do
4
- it 'creates history entry on state change' do
5
- object = Dummy.create!
6
- object.a_to_b!
7
- expect(StateHistory.count).to eq 1
8
- history = StateHistory.first
9
- expect(history.stateable).to eq object
10
- expect(history.state).to eq 'b'
11
- expect(history.previous_state).to eq 'a'
3
+ describe 'determining persistance layer' do
4
+ class NoPersistence
5
+ include AASM
6
+ end
7
+
8
+ it 'fails if persistance layer is unknown' do
9
+ expect {
10
+ NoPersistence.aasm do
11
+ has_history
12
+ end
13
+ }.to raise_exception AasmHistory::UnknownPersistanceLayer
14
+ end
15
+ end
16
+
17
+ describe 'saving history' do
18
+ describe 'active record' do
19
+ subject! do
20
+ object = Dummy.create!
21
+ object.a_to_b!
22
+ object
23
+ end
24
+
25
+ it 'creates history entry on state change' do
26
+ expect(StateHistory.count).to eq 1
27
+ history = StateHistory.first
28
+ expect(history.stateable).to eq subject
29
+ expect(history.state).to eq 'b'
30
+ expect(history.previous_state).to eq 'a'
31
+ end
32
+
33
+ it 'is possible to acess history using relation from helper' do
34
+ expect(subject.state_histories.count).to eq 1
35
+ end
12
36
  end
13
37
  end
@@ -1,5 +1,5 @@
1
- require 'aasm_history'
2
1
  require 'active_record'
2
+ require 'aasm_history'
3
3
 
4
4
  Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each { |f| require_relative f }
5
5
 
@@ -1,5 +1,3 @@
1
- require 'aasm'
2
-
3
1
  class Dummy < ActiveRecord::Base
4
2
  include AASM
5
3
 
@@ -14,6 +12,8 @@ class Dummy < ActiveRecord::Base
14
12
  transitions from: :a, to: :b
15
13
  end
16
14
  end
15
+
16
+ has_state_history
17
17
  end
18
18
 
19
19
  class StateHistory < ActiveRecord::Base
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aasm_history
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: '0.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Jedrychowski
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +108,7 @@ extensions: []
94
108
  extra_rdoc_files: []
95
109
  files:
96
110
  - ".gitignore"
111
+ - ".travis.yml"
97
112
  - Gemfile
98
113
  - LICENSE.txt
99
114
  - README.md
@@ -101,13 +116,17 @@ files:
101
116
  - aasm_history.gemspec
102
117
  - lib/aasm_history.rb
103
118
  - lib/aasm_history/aasm_ext/base.rb
104
- - lib/aasm_history/history_creator.rb
119
+ - lib/aasm_history/ext/active_record.rb
120
+ - lib/aasm_history/load_extensions.rb
121
+ - lib/aasm_history/persistance/active_record.rb
122
+ - lib/aasm_history/persistance/active_record_creator.rb
123
+ - lib/aasm_history/persistance_determinator.rb
105
124
  - lib/aasm_history/version.rb
106
125
  - spec/aasm_history_spec.rb
107
126
  - spec/spec_helper.rb
108
127
  - spec/support/active_record/models.rb
109
128
  - spec/support/active_record/schema.rb
110
- homepage: ''
129
+ homepage: https://github.com/gogiel/aasm_history
111
130
  licenses:
112
131
  - MIT
113
132
  metadata: {}
@@ -1,21 +0,0 @@
1
- module AasmHistory
2
- class HistoryCreator
3
- def initialize object, state, previous_state
4
- @object = object
5
- @state = state
6
- @previous_state = previous_state
7
- end
8
-
9
- def create
10
- klass.create! attributes
11
- end
12
-
13
- def klass
14
- @klass ||= AASM::StateMachine[@object.class].config.history_class.constantize
15
- end
16
-
17
- def attributes
18
- {state: @state, previous_state: @previous_state, stateable: @object}
19
- end
20
- end
21
- end