Sha256: b5b555f2bcd85348b2c268e2e6a73065778a49638f5456d61674386b1eb7ed45

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 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

    def pubdraft_state_options
      options_for_select(pubdraft_states_for_select)
    end
  end

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

  if defined?(ActionView::Base)
    ActionView::Base.send(:include, Pubdraft::HelperMethods)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pubdraft-0.0.2 lib/pubdraft.rb