Sha256: 7037ee2e04bda4adb9c70cf75760d222e5e6c99c528747b70937c99c33ec4ea5

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

module ActiveRecord
  module DraftRecords
    def self.included(klass)
      klass.extend(ClassMethods)
      # Define default_scope to not include drafts
      klass.send(:default_scope, :conditions => {:draft => false})
    end
    
    module ClassMethods
      # Same as ActiveRecord::Base#create, but sets the record as a draft and save
      # ignoring validations.
      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
      
      # Attempt to create the record, if it fails, save it as a draft
      def create_or_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_or_draft
           object
         end        
      end
      
      # Returns a ActiveRecord::Scope that fetchs all the drafts
      def find_drafts(*args)
        with_exclusive_scope { self.scoped(:conditions => {:draft => true}).find(*args) }
      end
    end
    
    # Save the record and, if it is a draft, attempt to transform it in a normal (non-draft) record.
    def save_and_attempt_to_undraft
      self.draft, is_draft = false, self.draft
      
      if self.valid?
        save
      elsif self.draft = is_draft
        save(false)
      else
        false
      end
    end
    
    # Save the record as a draft, setting the record attribute 'draft' to true
    # and saving the record ignoring the validations.
    def save_as_draft
      self.draft = true
      save(false)
    end
    
    # Attempt to save the record, if any validation fails, save it as a draft.
    def save_or_draft
      save || save_as_draft
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-draft_records-0.1.1 lib/active_record/draft_records.rb