Module: Paranoid::Base
- Defined in:
- lib/paranoid/base.rb
Instance Method Summary collapse
-
#paranoid(opts = {}) ⇒ Object
Call this in your model to enable paranoid.
-
#paranoid? ⇒ Boolean
Returns true if the model is paranoid and paranoid is enabled.
Instance Method Details
#paranoid(opts = {}) ⇒ Object
Call this in your model to enable paranoid.
Examples
Post < ActiveRecord::Base
paranoid
end
Item < ActiveRecord::Base
paranoid :field => [:available, false, true]
end
Options
- :field
-
The field used to recognize a record as destroyed. Default: :deleted_at IsParanoid Compatibility: Also accepts an Array of form
- field_name, destroyed_value, not_destroyed_value
-
however :destroyed_value and :not_destroyed_value will be ignored
- :destroyed_value
-
The value to set the paranoid field to on destroy. Can be either a static value or a Proc which will be evaluated when destroy is called. Default: Proc.newTime.now.utc
- :not_destroyed_value
-
The value used to recognize a record as not destroyed. Default: nil
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/paranoid/base.rb', line 34 def paranoid(opts = {}) return if paranoid? @paranoid = true opts[:field] ||= [:deleted_at, Proc.new{Time.now.utc}, nil] class_attribute :destroyed_field, :field_destroyed, :field_not_destroyed if opts[:field].is_a?(Array) self.destroyed_field, self.field_destroyed, self.field_not_destroyed = opts[:field] else self.destroyed_field = opts.key?(:field) ? opts[:field] : :deleted_at self.field_destroyed = opts.key?(:destroyed_value) ? opts[:destroyed_value] : Proc.new{Time.now.utc} self.field_not_destroyed = opts.key?(:not_destroyed_value) ? opts[:not_destroyed_value] : nil end include Paranoid::ParanoidMethods class_eval do class << self delegate :with_destroyed, :with_destroyed_only, :to => :scoped end end end |
#paranoid? ⇒ Boolean
Returns true if the model is paranoid and paranoid is enabled
58 59 60 61 |
# File 'lib/paranoid/base.rb', line 58 def paranoid? @paranoid = (self != ActiveRecord::Base && self.superclass.paranoid?) unless defined?(@paranoid) @paranoid end |