Module: ActiveRecord::DraftRecords::ClassMethods

Defined in:
lib/active_record/draft_records.rb

Instance Method Summary collapse

Instance Method Details

#create_as_draft(attributes = {}, &block) ⇒ Object

Same as ActiveRecord::Base#create, but sets the record as a draft and save ignoring validations.



12
13
14
15
16
17
18
19
20
21
# File 'lib/active_record/draft_records.rb', line 12

def create_as_draft(attributes = {}, &block)
  if attributes.is_a?(Array)
     attributes.collect { |attr| create_as_draft(attr, &block) }
   else
     object = new(attributes)
     yield(object) if block_given?
     object.save_as_draft
     object
   end
end

#create_or_draft(attributes = {}, options = {}, &block) ⇒ Object

Attempt to create the record, if it fails, save it as a draft



24
25
26
27
28
29
30
31
32
33
# File 'lib/active_record/draft_records.rb', line 24

def create_or_draft(attributes = {}, options = {}, &block)
  if attributes.is_a?(Array)
     attributes.collect { |attr| create_or_draft(attr, &block) }
   else
     object = new(attributes)
     yield(object) if block_given?
     object.save_or_draft(options)
     object
   end        
end

#find_drafts(*args) ⇒ Object

Returns a ActiveRecord::Scope that fetchs all the drafts



36
37
38
# File 'lib/active_record/draft_records.rb', line 36

def find_drafts(*args)
  with_exclusive_scope { self.scoped(:conditions => {:draft => true}).find(*args) }
end