Module: Drafting::InstanceMethods

Defined in:
lib/drafting/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#dump_to_draftObject

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



27
28
29
# File 'lib/drafting/instance_methods.rb', line 27

def dump_to_draft
  Marshal.dump(instance_values)
end

#load_from_draft(string) ⇒ Object



31
32
33
34
35
36
37
# 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) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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) ⇒ Object



19
20
21
22
23
24
# 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