Sha256: 0d796014f2b2fc4ffb5612e86e57eada84ac6614adc611a3d93ffca0e0d97e3e

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

begin
  require "vestal_versions"
rescue
  gem "vestal_versions"
  require "vestal_versions"
end

# Publishy
module Publishus
  
  def self.included(base)
    base.class_eval do
      extend ClassMethods
    end
  end
  
  module ClassMethods
         
    def publishable    
      versioned
      named_scope :published, :conditions => "#{name.tableize}.published_at is not null and (#{name.tableize}.deleted_at < #{name.tableize}.published_at or #{name.tableize}.deleted_at is null)" do
        def live
          collect do |publishable|
            publishable.live
          end
        end
      end
      before_destroy :choose_fait
      include InstanceMethods
    end
    
  end
  
  module InstanceMethods
    
    def current?
      self.published_at.nil? || (self.published_at >= self.updated_at)
    end
    
    def live
      returning self do |publishable|
        publishable.revert_to(publishable.published_at) unless publishable.current?
      end
    end

    attr_accessor :proper_destroy

    def real_destroy
      @proper_destroy = true
      destroy
    end

    def publish!(time=nil)
      self.update_attribute(:published_at, time||Time.now)
    end
    
    def publish_all!
      time = Time.now
      all.each { |publishable| publishable.publish!(time) }
    end
    
    def published?
      !self.published_at.nil?
    end   
    
    private
    
    def choose_fait
      unless @proper_destroy
        self.update_attribute(:deleted_at, Time.now)
        false
      end
    end
  end
  
end

ActiveRecord::Base.send(:include, Publishus)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
publishus-0.0.3 lib/publishus.rb