Sha256: 2093b98233c76cf4740e23624592511a0775fbdf76a027f06c179dd60b604c46

Contents?: true

Size: 1.24 KB

Versions: 7

Compression:

Stored size: 1.24 KB

Contents

module Statesman
  module Adapters
    module ActiveRecordModel
      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        def in_state(*states)
          joins(transition_name)
            .joins(transition_join)
            .where("#{transition_name}.to_state" => states.map(&:to_s))
            .where("transition2.id" => nil)
        end

        def not_in_state(*states)
          joins(transition_name)
            .joins(transition_join)
            .where("#{transition_name}.to_state NOT IN (?)", states.map(&:to_s))
            .where("transition2.id" => nil)
        end

        private

        def transition_class
          raise NotImplementedError, "A transition_class method should be " +
                                     "defined on the model"
        end

        def transition_name
          transition_class.table_name.to_sym
        end

        def model_foreign_key
          reflections[transition_name].foreign_key
        end

        def transition_join
          "LEFT OUTER JOIN #{transition_name} transition2
             ON transition2.#{model_foreign_key} = #{table_name}.id
             AND transition2.sort_key > #{transition_name}.sort_key"
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
statesman-0.8.3 lib/statesman/adapters/active_record_model.rb
statesman-0.8.2 lib/statesman/adapters/active_record_model.rb
statesman-0.8.1 lib/statesman/adapters/active_record_model.rb
statesman-0.8.0 lib/statesman/adapters/active_record_model.rb
statesman-0.7.0 lib/statesman/adapters/active_record_model.rb
statesman-0.6.1 lib/statesman/adapters/active_record_model.rb
statesman-0.6.0 lib/statesman/adapters/active_record_model.rb