Module: IdHash
- Defined in:
- lib/drafter/id_hash.rb
Instance Method Summary collapse
-
#id_hash ⇒ Hash
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).
-
#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.
Instance Method Details
#id_hash ⇒ Hash
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
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.
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 |