Class: Draft

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/drafter/draft.rb

Overview

A model to store serialized draft data. Drafts attach themselves polymorphically to any other ActiveRecord class, loop through the target draftable classes’s attributes, and store them as a Hash in the :data text field in the database.

Drafts can also keep track of the target draftable’s CarrierWave uploads, if any exist.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

Set things up so we can use dot notation to access draft data, e.g. we can do either @foo.draft.data or (more neatly) we can do @foo.draft.title



56
57
58
59
60
61
62
# File 'lib/drafter/draft.rb', line 56

def method_missing(meth, *args, &block)
  if self.data.keys.include?(meth.to_s)
    self.data[meth.to_s]
  else
    super
  end
end

Instance Method Details

#approve!Object

Approve a draft, setting the attributes of the draftable object to contain the draft content, saving the draftable, and destroying the draft.



38
39
40
41
42
43
# File 'lib/drafter/draft.rb', line 38

def approve!
	draftable = inflate
	draftable.save!
	self.destroy
	draftable
end

#approverDraft

It’s possible to use the :delegate_approval_to => :foo option in the draftable class method, so that this model can be approved as part of its parent model. This method finds the ‘approver’, i.e. the Draft object which is the parent object of this one and for which calling the ‘approve’ method will approve this subdraft too.

This is currently quite weak, in the sense that it’ll screw up as soon as you depart from standard AR class naming conventions. It’s a good target for future development, but I think at the moment, IAGNI.

At the moment, we just return the parent if a delegate exists. This should do the trick for us right now, as we’re not using deeply nested object graphs that need approvals. I’ve got the start of an alternate, somewhat stronger implementation, underneath that, but we don’t have time to do up the general case right now.

Returns:

  • (Draft)

    approver the draft object which serves as the approval context for this one. Returns self or a delegated draft object.



93
94
95
96
97
98
99
100
# File 'lib/drafter/draft.rb', line 93

def approver
  delegate = self.inflate.class.delegate_approval_to
  if delegate
    parent
  else
    self
  end
end

#inflateObject

Returns the existing draftable object, or a new one of the proper type, with attributes and files hanging off it.

Returns:

  • the existing draftable object, or a new one of the proper type, with attributes and files hanging off it.



68
69
70
71
72
73
# File 'lib/drafter/draft.rb', line 68

def inflate
   draftabl = draftable.nil? ? self.draftable_type.constantize.new(:draft => self) : draftable
   draftabl.draft = self # this should absolutely *not* be necessary, there's an STI bug in AR.
   draftabl.apply_draft
   draftabl
end

#reject!Object

Reject the draft, basically destroying the draft object and leaving the draftable unchanged.



48
49
50
# File 'lib/drafter/draft.rb', line 48

def reject!
  self.destroy
end