Module: ActsAsTemporary

Defined in:
lib/acts_as_temporary/engine.rb,
lib/acts_as_temporary/version.rb,
lib/acts_as_temporary/acts_as_temporary.rb

Defined Under Namespace

Modules: InstanceMethods Classes: Engine

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Instance Method Details

#can_be_temporaryObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/acts_as_temporary/acts_as_temporary.rb', line 2

def can_be_temporary
  #has_many :reviews, :as=>:reviewable, :dependent=>:destroy
  include InstanceMethods
  attr_reader :temporary_id
  after_save :drop_temporary
  
  def recall(temp_id=nil)
    raise ArgumentError, "Please provide a temporary object ID" if temp_id.blank?
    temporary_object = TemporaryObject.find(temp_id)
    if temporary_object
      raise TypeError, "Temporary object and calling object must be the same class" unless temporary_object.permanent_class.eql?(self.name)
      new_object = self.new(temporary_object.definition)
      new_object.instance_variable_set("@temporary_id",temp_id)
      new_object
    end
  end
  
  def clear_stale_objects
    expiry_date = Time.now - Rails.application.config.acts_as_temporary_shelf_life
    TemporaryObject.delete_all(["permanent_class = ? AND created_at < ?", self.name, expiry_date])
  end
end

#clear_stale_objectsObject



19
20
21
22
# File 'lib/acts_as_temporary/acts_as_temporary.rb', line 19

def clear_stale_objects
  expiry_date = Time.now - Rails.application.config.acts_as_temporary_shelf_life
  TemporaryObject.delete_all(["permanent_class = ? AND created_at < ?", self.name, expiry_date])
end

#recall(temp_id = nil) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
# File 'lib/acts_as_temporary/acts_as_temporary.rb', line 8

def recall(temp_id=nil)
  raise ArgumentError, "Please provide a temporary object ID" if temp_id.blank?
  temporary_object = TemporaryObject.find(temp_id)
  if temporary_object
    raise TypeError, "Temporary object and calling object must be the same class" unless temporary_object.permanent_class.eql?(self.name)
    new_object = self.new(temporary_object.definition)
    new_object.instance_variable_set("@temporary_id",temp_id)
    new_object
  end
end