Module: ActiveRecord::Acts::AuditedCollection::ClassMethods
- Defined in:
- lib/active_record/acts/audited_collection.rb
Instance Method Summary collapse
- #acts_as_audited_collection(options = {}) ⇒ Object
- #acts_as_audited_collection_parent(options = {}) ⇒ Object
- #define_acts_as_audited_collection(options) {|audited_collections[key] ||= {}| ... } ⇒ Object
- #without_collection_audit ⇒ Object
Instance Method Details
#acts_as_audited_collection(options = {}) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/active_record/acts/audited_collection.rb', line 5 def acts_as_audited_collection( = {}) unless self.included_modules.include?(InstanceMethods) # First time use in this class, we have some extra work to do. send :include, InstanceMethods class_attribute :audited_collections send :audited_collections=, {} attr_accessor :collection_audit_object_is_soft_deleted after_create :collection_audit_create before_update :collection_audit_update before_destroy :collection_audit_destroy has_many :child_collection_audits, :as => :child_record, :class_name => 'CollectionAudit' end = { :name => self.name.tableize.to_sym, :cascade => false, :track_modifications => false, :only => nil, :except => nil, :soft_delete => nil }.merge() [:only] &&= [[:only]].flatten.collect(&:to_s) [:except] &&= [[:except]].flatten.collect(&:to_s) unless .has_key? :parent raise ActiveRecord::ConfigurationError.new "Must specify parent for an acts_as_audited_collection (:parent => :object)" end parent_association = reflect_on_association([:parent]) unless parent_association && parent_association.belongs_to? raise ActiveRecord::ConfigurationError.new "Parent association '#{[:parent]}' must be a belongs_to relationship" end # Try explicit first, then default [:foreign_key] ||= parent_association.[:foreign_key] [:foreign_key] ||= parent_association.foreign_key # TODO Remove this when polymorphic is supported. if parent_association.[:polymorphic] raise ActiveRecord::ConfigurationError.new "Sorry, acts_as_audited_collection polymorphic associations haven't been added yet." end [:parent_type] ||= parent_association.klass.name define_acts_as_audited_collection do |config| config.merge! end end |
#acts_as_audited_collection_parent(options = {}) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/active_record/acts/audited_collection.rb', line 59 def acts_as_audited_collection_parent( = {}) unless .has_key? :for raise ActiveRecord::ConfigurationError.new "Must specify relationship for an acts_as_audited_collection_parent (:for => :objects)" end child_association = reflect_on_association([:for]) if child_association.nil? || child_association.belongs_to? raise ActiveRecord::ConfigurationError.new "Association '#{[:for]}' must be a valid parent (i.e. not belongs_to) relationship" end has_many :"#{[:for]}_audits", :as => :parent_record, :class_name => 'CollectionAudit', :conditions => ['audited_association = ?', [:for].to_s] end |
#define_acts_as_audited_collection(options) {|audited_collections[key] ||= {}| ... } ⇒ Object
74 75 76 77 |
# File 'lib/active_record/acts/audited_collection.rb', line 74 def define_acts_as_audited_collection() key = "#{[:parent_type]}##{[:name]}" yield(audited_collections[key] ||= {}) end |
#without_collection_audit ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/active_record/acts/audited_collection.rb', line 79 def without_collection_audit result = nil Thread.current[:collection_audit_enabled] = Thread.current[:collection_audit_enabled].tap do Thread.current[:collection_audit_enabled] = false result = yield if block_given? end result end |