Sha256: 509ddfa1453f88ee2a0757d5577d8432974fabe5c028c34e31e05e29af938ab2

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

require "pubdraft/version"

module Pubdraft
  module InstanceMethods
    def self.included(base)
      base.send :before_create, :set_pubdraft_state
      base.attr_accessible :state

      base.scope :published, base.where(:state => 'published')
      base.scope :drafted,   base.where(:state => 'drafted')
    end

    def published?
      state == 'published'
    end

    def drafted?
      state == 'drafted'
    end

    def publish!
      publish && save
    end

    def publish
      self.state = 'published'
    end

    def draft!
      draft && save
    end

    def draft
      self.state = 'drafted'
    end

    private
    def set_pubdraft_state
      return unless state.blank?
      publish
    end
  end

  module ClassMethods
    def pubdraft
      send(:include, InstanceMethods)
    end
  end

  module HelperMethods
    def pubdraft_states_for_select
      [['Published', 'published'], ['Drafted', 'drafted']]
    end
  end

  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.extend Pubdraft::ClassMethods
  end

  if defined?(ApplicationHelper)
    ApplicationHelper.extend Pubdraft::HelperMethods
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pubdraft-0.0.1 lib/pubdraft.rb