module Drafting::InstanceMethods

Public Instance Methods

dump_to_draft() click to toggle source

Override this two methods if you want to change the way to dump/load data

# File lib/drafting/instance_methods.rb, line 27
def dump_to_draft
  Marshal.dump(instance_values)
end
load_from_draft(string) click to toggle source
# File lib/drafting/instance_methods.rb, line 31
def load_from_draft(string)
  values = Marshal.load(string)

  values.each do |name, value|
    instance_variable_set("@#{name}", value)
  end
end
save_draft(user=nil) click to toggle source
# File lib/drafting/instance_methods.rb, line 3
def save_draft(user=nil)
  return false unless self.new_record?

  draft = Draft.find_by_id(self.draft_id) || Draft.new

  draft.data = dump_to_draft
  draft.target_type = self.class.name
  draft.user_id = user.try(:id)
  draft.user_type = user.try(:class).try(:name)
  draft.parent = self.send(self.class.draft_parent) if self.class.draft_parent

  result = draft.save
  self.draft_id = draft.id if result
  result
end
update_draft(user, attributes) click to toggle source
# File lib/drafting/instance_methods.rb, line 19
def update_draft(user, attributes)
  with_transaction_returning_status do
    assign_attributes(attributes)
    save_draft(user)
  end
end

Private Instance Methods

clear_draft() click to toggle source
# File lib/drafting/instance_methods.rb, line 41
def clear_draft
  if draft = Draft.find_by_id(self.draft_id)
    self.draft_id = nil if draft.destroy
  end
end