Permalink
Browse files

Separate out modules

  • Loading branch information...
John Crepezzi
John Crepezzi committed Jan 11, 2011
1 parent 0e6cac7 commit 4e326e86d4e09cb717fdf3bac74624f03870d4d5
Showing with 58 additions and 58 deletions.
  1. +0 −1 TODO
  2. +4 −57 lib/track_history.rb
  3. +17 −0 lib/track_history/historical_relation_helpers.rb
  4. +37 −0 lib/track_history/history_methods.rb
View
1 TODO
@@ -4,7 +4,6 @@
needed to announce
------------------
-* separate modules into separate files and autoload them
* separate inference before / after need
* option to disable inference
* track all used historical fields and report conflicts via 'raise'
View
@@ -1,9 +1,12 @@
module TrackHistory
- autoload :VERSION, File.join(File.dirname(__FILE__), 'track_history', 'version')
require 'rubygems'
require 'active_record'
+ autoload :VERSION, 'track_history/version'
+ autoload :HistoryMethods, 'track_history/history_methods'
+ autoload :HistoricalRelationHelpers, 'track_history/historical_relation_helpers'
+
def self.install
ActiveRecord::Base.send(:include, self)
end
@@ -85,62 +88,6 @@ def define_historical_model(base, model_name, table_name, track_reference, &bloc
end
- module HistoryMethods
-
- attr_reader :historical_fields, :historical_action_field
-
- def track_historical_reference?
- @track_historical_reference
- end
-
- def historical_fields
- @historical_fields ||= {}
- end
-
- def historical_tracks
- @historical_tracks ||= {}
- end
-
- def field(field, options = {})
- field_s = field.is_a?(String) ? field : field.to_s
- historical_fields[field_s] = {
- :before => options[:before] || "#{field}_before".to_sym,
- :after => options[:after] || "#{field}_after".to_sym
- }
- nil
- end
-
- def annotate(field, options = {}, &block) # haha
- options.assert_valid_keys(:as)
- save_as = options.has_key?(:as) ? options[:as] : field
-
- unless columns_hash.has_key?(save_as.to_s)
- raise ActiveRecord::StatementInvalid.new("No such attribute '#{field}' on #{@klass_reference.name}")
- end
-
- historical_tracks[save_as] = block.nil? ? field : block
- end
-
- end
-
- module HistoricalRelationHelpers
-
- # Get a list of the modifications in a given history
- def modifications
- self.class.historical_fields.reject do |field, options|
- send(options[:before]) == send(options[:after])
- end.keys
- end
-
- def to_s
- return 'modified nothing' if modifications.empty?
- str = 'modified ' + modifications.sort.join(', ')
- str += " on #{historical_relation}" if self.class.instance_variable_get(:@track_historical_reference)
- str
- end
-
- end
-
module InstanceMethods
private
@@ -0,0 +1,17 @@
+module HistoricalRelationHelpers
+
+ # Get a list of the modifications in a given history
+ def modifications
+ self.class.historical_fields.reject do |field, options|
+ send(options[:before]) == send(options[:after])
+ end.keys
+ end
+
+ def to_s
+ return 'modified nothing' if modifications.empty?
+ str = 'modified ' + modifications.sort.join(', ')
+ str += " on #{historical_relation}" if self.class.instance_variable_get(:@track_historical_reference)
+ str
+ end
+
+end
@@ -0,0 +1,37 @@
+module HistoryMethods
+
+ attr_reader :historical_fields, :historical_action_field
+
+ def track_historical_reference?
+ @track_historical_reference
+ end
+
+ def historical_fields
+ @historical_fields ||= {}
+ end
+
+ def historical_tracks
+ @historical_tracks ||= {}
+ end
+
+ def field(field, options = {})
+ field_s = field.is_a?(String) ? field : field.to_s
+ historical_fields[field_s] = {
+ :before => options[:before] || "#{field}_before".to_sym,
+ :after => options[:after] || "#{field}_after".to_sym
+ }
+ nil
+ end
+
+ def annotate(field, options = {}, &block) # haha
+ options.assert_valid_keys(:as)
+ save_as = options.has_key?(:as) ? options[:as] : field
+
+ unless columns_hash.has_key?(save_as.to_s)
+ raise ActiveRecord::StatementInvalid.new("No such attribute '#{field}' on #{@klass_reference.name}")
+ end
+
+ historical_tracks[save_as] = block.nil? ? field : block
+ end
+
+end

0 comments on commit 4e326e8

Please sign in to comment.