Sha256: b79327c1ec516465c17b1b69fe9c814385cfc6e2670b2f1583a57abe9754d135

Contents?: true

Size: 1.41 KB

Versions: 8

Compression:

Stored size: 1.41 KB

Contents

module ActsAsPublished
  module ActiveRecord
    def self.included(klass)
      klass.class_eval do
        extend ClassMethods
        include InstanceMethods
      end  
    end  
  
    module ClassMethods
      def acts_as_published
        attr_accessible :published # , :published_at
        
        def published
          t = self.arel_table
          where(t[:published_at].not_eq(nil))
        end
        
        def unpublished
          t = self.arel_table
          where(t[:published_at].eq(nil))
        end
      end
    end
    
    module InstanceMethods
      def published
        !!self.published_at
      end  
      alias :published? :published
      
      def published=(published)
        if ::ActiveRecord::ConnectionAdapters::Column.value_to_boolean(published)
          self.published_at = Time.zone.now # .to_date
          # write_attribute(:published_at, Time.zone.now) # .to_date
        else
          self.published_at = nil
          # write_attribute(:published_at, nil)
        end
      end
      
      def toggle_published
        if self.published_at.nil?
          self.published_at = Time.zone.now # .to_date
          # write_attribute(:published_at, Time.zone.now) # .to_date
        else
          self.published_at = nil
          # write_attribute(:published_at, nil)
        end 
      end
      
      def toggle_published!
        toggle_published
        save!
      end 
    end
  end
end  

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
acts_as_published-0.0.9.pre lib/acts_as_published/active_record.rb
acts_as_published-0.0.8.pre lib/acts_as_published/active_record.rb
acts_as_published-0.0.7.pre lib/acts_as_published/active_record.rb
acts_as_published-0.0.6.pre lib/acts_as_published/active_record.rb
acts_as_published-0.0.5.pre lib/acts_as_published/active_record.rb
acts_as_published-0.0.4.pre lib/acts_as_published/active_record.rb
acts_as_published-0.0.3.pre lib/acts_as_published/active_record.rb
acts_as_published-0.0.2.pre lib/acts_as_published/active_record.rb