Module: IdHash

Defined in:
lib/drafter/id_hash.rb

Instance Method Summary collapse

Instance Method Details

#id_hashHash

Checks the state of the object to see whether it’s an unapproved draft (in which case it has no :id), or whether it’s an approved object which is being edited or updated (in which case we can address it by :id).

This is primarily useful in your controllers. Sometimes, you might want to identify the object by its :id; other times, you’re dealing with an object that only has a Draft but no actual object identity, in which case you’ll want to retrieve the Draft object by its :draft_id, and call

Returns:

  • (Hash)

    a hash with either :draft_id => this object’s draft’s id, or :id => this object’s id.



16
17
18
19
20
21
22
23
24
# File 'lib/drafter/id_hash.rb', line 16

def id_hash
  if !new_record?
    { :id => self.to_param }
  elsif new_record? && draft
    { :draft_id => draft.to_param }
  else
    raise "It's not clear what kind of id you're looking for."
  end
end

#id_hash_as(sym) ⇒ Hash

When you’re attaching sub-objects to a parent object, you’ll sometimes need to send the :id and :draft_id in another context, e.g. as :article_id and/or :draft_article_id.

Parameters:

  • sym (Symbol)

    the symbol you want to inject into your id to provide context.

Returns:

  • (Hash)

    a hash with e.g. :article_id or :draft_article_id, with the appropriate values set (as per id_hash above).



35
36
37
38
39
40
41
42
43
# File 'lib/drafter/id_hash.rb', line 35

def id_hash_as(sym)
  if !new_record?
    { "#{sym}_id".to_sym => self.to_param }
  elsif new_record? && draft
    { "draft_#{sym}_id".to_sym => draft.to_param }
  else
    raise "It's not clear what kind of id you're looking for."
  end
end