Sha256: 08087cf50c8d1b31d3e8adb5a85691f3b33a705c14c5551a62785f2ef36c1dae

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

require "affairs_of_state/version"

module AffairsOfState

  def self.included(base)
    base.extend ClassMethods
  end

  module ClassMethods
    def affairs_of_state(*args)
      @_status_options = ({:column => :status, :allow_blank => false, :scopes => true, :if => nil}).merge(args.extract_options!)
      @_statuses = args.flatten.map(&:to_s)

      const_set("STATUSES", @_statuses)

      validates(@_status_options[:column], :inclusion => {:in => @_statuses, :allow_blank => @_status_options[:allow_blank]}, :if => @_status_options[:if])

      if @_status_options[:scopes]
        @_statuses.each do |status|
          raise "Affairs of State: '#{ status }' is not a valid status" unless valid_status?(status)
          self.scope status.to_sym, -> { where(@_status_options[:column] => status.to_s) }
        end
      end

      include InstanceMethods
      extend SingletonMethods
      true
    end

    private

    def valid_status?(status)
      ![:new].include?(status.to_sym)
    end
  end

  module InstanceMethods
    def method_missing(method, *args)
      if self.class::STATUSES.map{|s| "#{s}?".to_sym }.include?(method)
        self.class.send(:define_method, method) do
          self.status == method.to_s.gsub(/\?$/, "")
        end

        send method

      elsif self.class::STATUSES.map{|s| "#{s}!".to_sym }.include?(method)
        self.class.send(:define_method, method) do
          self.send("#{self.class.instance_variable_get('@_status_options')[:column]}=", method.to_s.gsub(/\!$/, ""))
          self.save
        end

        send method
      else
        super
      end
    end
  end

  module SingletonMethods
    def statuses_for_select
      @_statuses.map{|s| [s.humanize, s]}
    end
  end

end

ActiveRecord::Base.send :include, AffairsOfState

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
affairs_of_state-0.1.4 lib/affairs_of_state.rb