gregfitz23-historic 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Greg Fitzgerald
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,32 @@
1
+ = historic
2
+
3
+ A gem for tracking changes of a model in a history table.
4
+ Allows fields to be marked as trigger fields that will initiate a copy of the record back to the history table.
5
+ The history table rows will be created with the data as it existed before the current modifications were made.
6
+
7
+ Usage (on the object to create history for):
8
+ =begin
9
+ acts_as_historic :on_change_of=>:field_1, :after => :some_method
10
+ =end
11
+
12
+ Usage (on the history object):
13
+ =begin
14
+ acts_as_historic_record
15
+ =end
16
+
17
+ By default, the History Record object is assumed to be the name of the main object plus History so:
18
+ For an object Foo, the History Record would be in a table called FooHistory.
19
+ This can be overridden using the class_name option:
20
+ =begin
21
+ acts_as_historic :class_name=>"BarHistory"
22
+ =end
23
+
24
+ Options:
25
+ * :on_change_of - A list of fields to monitor for change. When any of these fields changes, a history row is created.
26
+ * :on_initialize - Create a History Record indicating the initial creation of the main object.
27
+ * :class_name - Specify a different History Record class name.
28
+ * :after - A symbol or proc to be called on completion of the history row being created.
29
+
30
+ == Copyright
31
+
32
+ Copyright (c) 2009 Greg Fitzgerald. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "historic"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "greg_fitz@yahoo.com"
10
+ gem.homepage = "http://github.com/gregfitz23/historic"
11
+ gem.authors = ["Greg Fitzgerald"]
12
+
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test' << 'active_record'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "historic #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 0
4
+ :patch: 1
data/lib/historic.rb ADDED
@@ -0,0 +1,3 @@
1
+ require File.join(File.dirname(__FILE__), 'historic', 'historic')
2
+ require File.join(File.dirname(__FILE__), 'historic', 'historic_record')
3
+ require "acts_as_list"
@@ -0,0 +1,85 @@
1
+ module ActiveRecord
2
+ module Acts
3
+ module Historic
4
+
5
+ def self.append_features(base) #:nodoc:
6
+ super
7
+ base.extend(Definition)
8
+ end
9
+
10
+ module Definition
11
+ # Sets up before_save and before_destroy to create HistoricRecord rows when appropriate.
12
+ def acts_as_historic(options={})
13
+ extend ActiveRecord::Acts::Historic::ClassMethods
14
+ include ActiveRecord::Acts::Historic::InstanceMethods
15
+
16
+ historic_extract_options(options)
17
+
18
+ alias_method_chain :before_save, :historic
19
+ alias_method_chain :before_destroy, :historic
20
+ end
21
+ end
22
+
23
+ module ClassMethods
24
+
25
+ def historic_table_class
26
+ @historic_table_class
27
+ end
28
+
29
+ def historic_options
30
+ @historic_options
31
+ end
32
+
33
+
34
+ private
35
+ # Extract the table class to be used or default to this class name + History.
36
+ # Ensure that on_change_of is an array.
37
+ #
38
+ def historic_extract_options(options)
39
+ @historic_table_class = (options.delete(:class_name) || "#{self.to_s}History").constantize
40
+
41
+ options[:on_change_of] = [options[:on_change_of]] if options[:on_change_of] && !options[:on_change_of].is_a?(Array)
42
+ @historic_options = options
43
+ end
44
+
45
+ end #ClassMethods
46
+
47
+ module InstanceMethods
48
+ # Chain historic_copy_to_history into before_save.
49
+ # This will only be triggered if:
50
+ # a. The current record is a new record OR the on_initialize flag is true
51
+ # b. AND on_change_of is not nil AND one of the fields specified in on_change_of has changed
52
+ def before_save_with_historic
53
+ before_save_without_historic
54
+
55
+ if historic_should_move_to_history?(historic_options)
56
+ historic_copy_to_history
57
+ end
58
+ end
59
+
60
+ # Chain historic_copy_to_history into before_destroy.
61
+ #
62
+ def before_destroy_with_historic
63
+ before_destroy_without_historic
64
+ historic_copy_to_history
65
+ end
66
+
67
+ private
68
+ def historic_should_move_to_history?(options)
69
+ (options[:on_initialize] || !new_record?) && (options[:on_change_of] && options[:on_change_of].any? {|field| self.__send__("#{field}_changed?")})
70
+ end
71
+
72
+ # Copy the current item to the HistoryRecord object and execute the :after hook, if it exists
73
+ #
74
+ def historic_copy_to_history
75
+ self.class.historic_table_class.__send__("create_history_from_current!", self)
76
+ historic_options[:after].to_proc.call(self) if historic_options[:after]
77
+ end
78
+
79
+ def historic_options
80
+ self.class.historic_options
81
+ end
82
+ end #InstanceMethods
83
+ end #Historic
84
+ end
85
+ end
@@ -0,0 +1,47 @@
1
+ module ActiveRecord
2
+ module Acts
3
+ module HistoricRecord
4
+ def self.append_features(base)
5
+ super
6
+ base.extend(Definition)
7
+ end
8
+
9
+ module Definition
10
+ def acts_as_historic_record
11
+ extend ClassMethods
12
+ acts_as_list
13
+ end
14
+ end
15
+
16
+ module ClassMethods
17
+
18
+ # Same as create_history_from_current! but swallows exceptions and returns false.
19
+ def create_history_from_current(current)
20
+ create_history_from_current!(current) rescue false
21
+ end
22
+
23
+ # Create an HistoricRecord record based off of the passed in Object.
24
+ # Copy any fields that are on both the Object and the HistoricRecord object, preserving any changed values.
25
+ #
26
+ def create_history_from_current!(current)
27
+ history_obj = self.new
28
+ attrs_to_copy = current.attributes.reject {|key, value| !history_obj.attribute_names.include?(key)}
29
+
30
+ changed_attrs_with_old_values = {}
31
+ current.changes.each_pair {|field, value_ary| changed_attrs_with_old_values[field] = value_ary[0]}
32
+
33
+ attrs_to_copy.merge!(changed_attrs_with_old_values)
34
+ self.create(attrs_to_copy)
35
+ end
36
+ end
37
+
38
+ end #HistoricRecord
39
+ end #Acts
40
+ end #ActiveRecord
41
+
42
+ ActiveRecord::Base.class_eval do
43
+ include ActiveRecord::Acts::Historic
44
+ include ActiveRecord::Acts::HistoricRecord
45
+ end
46
+
47
+
@@ -0,0 +1,8 @@
1
+ class HistoricItem < ActiveRecord::Base
2
+
3
+ acts_as_historic :on_change_of=>:field_1, :after => :clear_field_3
4
+
5
+ def clear_field_3
6
+ self.field_3 = nil
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ class HistoricItemHistory < ActiveRecord::Base
2
+
3
+ acts_as_historic_record
4
+
5
+ end
@@ -0,0 +1,88 @@
1
+ require 'test_helper'
2
+
3
+ class HistoricTest < Test::Unit::TestCase
4
+ context "A HistoryItem, @item" do
5
+ setup do
6
+ HistoricItemHistory.delete_all
7
+ HistoricItem.delete_all
8
+
9
+ @item = HistoricItem.new
10
+ @item.field_1, @item.field_2, @item.field_3 = 1, 2, 3
11
+ @item.save!
12
+ end
13
+
14
+ should "not create a HistoryRecord when newly created" do
15
+ assert !HistoricItemHistory.exists?(:field_1=>"1", :field_2=>"2", :field_3=>"3")
16
+ end
17
+
18
+ context "and a new value for :field_1" do
19
+ setup do
20
+ @new_field_1 = "4"
21
+ end
22
+
23
+ context "when changing the value of :field_1 to @new_field_1" do
24
+ setup do
25
+ @old_item = @item.clone
26
+ @item.update_attributes(:field_1 => @new_field_1)
27
+ end
28
+
29
+ should "create a new ItemGuideHistory for the old item guide and item" do
30
+ assert HistoricItemHistory.exists?({:field_1=>@old_item.field_1, :field_2=>@old_item.field_2, :field_3=>@old_item.field_3})
31
+ end
32
+
33
+ context ", the HistoricItemHistory" do
34
+ setup do
35
+ @item_history = HistoricItemHistory.find_by_field_1_and_field_2_and_field_3(@old_item.field_1, @old_item.field_2, @old_item.field_3)
36
+ end
37
+
38
+ should "copy a from the HistoricItem" do
39
+ assert_equal @old_item.field_1, @item_history.field_1.to_i
40
+ end
41
+
42
+ should "copy b from the HistoricItem" do
43
+ assert_equal @old_item.field_2, @item_history.field_2.to_i
44
+ end
45
+
46
+ should "copy c from the HistoricItem" do
47
+ assert_equal @old_item.field_3, @item_history.field_3.to_i
48
+ end
49
+
50
+ should "clear c on the original HistoryItem" do
51
+ assert_equal nil, @item.field_3
52
+ end
53
+ end #for the HistoricItemHistory
54
+ end #when changing field_1 of HistoricIte
55
+ end #when changing the value of :field_1 to @new_field_1
56
+
57
+ context "passed to create_from_history_item" do
58
+ setup do
59
+ @item_history = HistoricItemHistory.create_history_from_current(@item)
60
+ end
61
+
62
+ should "copy a from the HistoricItem" do
63
+ assert_equal @item.field_1, @item_history.field_1
64
+ end
65
+
66
+ should "copy b from the HistoricItem" do
67
+ assert_equal @item.field_2, @item_history.field_2
68
+ end
69
+
70
+ should "copy c from the HistoricItem" do
71
+ assert_equal @item.field_3, @item_history.field_3
72
+ end
73
+
74
+ end #passed to create_history_from_current
75
+
76
+ context "when destroyed" do
77
+ setup do
78
+ assert_equal 0, HistoricItemHistory.count
79
+ @item.destroy
80
+ end
81
+
82
+ should "create an HistoricItemHistory row" do
83
+ assert_equal 1, HistoricItemHistory.count
84
+ # assert HistoricItemHistory.exists?({:field_1=>@item.field_1, :field_2=>@item.field_2, :field_3=>@item.field_3})
85
+ end
86
+ end #when destroyed
87
+ end # An item guide
88
+ end
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require "active_record"
8
+ require 'historic'
9
+
10
+ class Test::Unit::TestCase
11
+
12
+
13
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
14
+
15
+ ActiveRecord::Schema.define(:version => 1) do
16
+ columns = [:field_1, :field_2, :field_3]
17
+ create_table :historic_items do |t|
18
+ t.string *columns
19
+ end
20
+
21
+ create_table :historic_item_histories do |t|
22
+ t.string *columns
23
+ t.integer :position
24
+ end
25
+ end
26
+
27
+ require "historic_item_history"
28
+ require "historic_item"
29
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gregfitz23-historic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Greg Fitzgerald
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-11 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: greg_fitz@yahoo.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - lib/historic.rb
31
+ - lib/historic/historic.rb
32
+ - lib/historic/historic_record.rb
33
+ - test/historic_item.rb
34
+ - test/historic_item_history.rb
35
+ - test/historic_test.rb
36
+ - test/test_helper.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/gregfitz23/historic
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --charset=UTF-8
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ requirements: []
57
+
58
+ rubyforge_project:
59
+ rubygems_version: 1.2.0
60
+ signing_key:
61
+ specification_version: 2
62
+ summary: TODO
63
+ test_files:
64
+ - test/historic_item.rb
65
+ - test/historic_item_history.rb
66
+ - test/historic_test.rb
67
+ - test/test_helper.rb