Skip to content

Commit

Permalink
remove unused methods find_in_state, count_in_state, with_state_scope…
Browse files Browse the repository at this point in the history
… for Mongoid
  • Loading branch information
alto committed Jul 10, 2015
1 parent 85905c9 commit ebf8109
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 96 deletions.
23 changes: 0 additions & 23 deletions lib/aasm/persistence/mongoid_persistence.rb
Expand Up @@ -32,34 +32,11 @@ module MongoidPersistence
#
def self.included(base)
base.send(:include, AASM::Persistence::Base)
base.extend AASM::Persistence::MongoidPersistence::ClassMethods
base.send(:include, AASM::Persistence::MongoidPersistence::InstanceMethods)

base.after_initialize :aasm_ensure_initial_state
end

module ClassMethods

def find_in_state(number, state, *args)
with_state_scope state do
find(number, *args)
end
end

def count_in_state(state, *args)
with_state_scope state do
count(*args)
end
end

def with_state_scope(state)
with_scope where(aasm.attribute_name.to_sym => state.to_s) do
yield if block_given?
end
end

end

module InstanceMethods

# Writes <tt>state</tt> to the state column and persists it to the database
Expand Down
77 changes: 4 additions & 73 deletions spec/unit/persistence/mongoid_persistance_spec.rb
Expand Up @@ -5,15 +5,15 @@
require 'spec_helper'

before(:all) do
Dir[File.dirname(__FILE__) + "/../../models/mongoid/*.rb"].sort.each { |f| require File.expand_path(f) }
Dir[File.dirname(__FILE__) + "/../../models/mongoid/*.rb"].sort.each do |f|
require File.expand_path(f)
end

# if you want to see the statements while running the spec enable the following line
# Mongoid.logger = Logger.new(STDERR)

DATABASE_NAME = "mongoid_#{Process.pid}"

Mongoid.configure do |config|
config.connect_to DATABASE_NAME
config.connect_to "mongoid_#{Process.pid}"
end
end

Expand Down Expand Up @@ -61,75 +61,6 @@

end

describe "#find_in_state" do

let!(:model) { SimpleNewDslMongoid.create!(:status => :unknown_scope) }
let!(:model_id) { model._id }

it "should respond to method" do
expect(SimpleNewDslMongoid).to respond_to(:find_in_state)
end

it "should find the model when given the correct scope and model id" do
expect(SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope').class).to eq(SimpleNewDslMongoid)
expect(SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope')).to eq(model)
end

it "should raise DocumentNotFound error when given incorrect scope" do
expect {SimpleNewDslMongoid.find_in_state(model_id, 'new')}.to raise_error Mongoid::Errors::DocumentNotFound
end

it "should raise DocumentNotFound error when given incorrect model id" do
expect {SimpleNewDslMongoid.find_in_state('bad_id', 'unknown_scope')}.to raise_error Mongoid::Errors::DocumentNotFound
end

end

describe "#count_in_state" do

before do
3.times { SimpleNewDslMongoid.create!(:status => :unknown_scope) }
end

it "should respond to method" do
expect(SimpleNewDslMongoid).to respond_to(:count_in_state)
end

it "should return n for a scope with n records persisted" do
expect(SimpleNewDslMongoid.count_in_state('unknown_scope').class).to eq(Fixnum)
expect(SimpleNewDslMongoid.count_in_state('unknown_scope')).to eq(3)
end

it "should return zero for a scope without records persisted" do
expect(SimpleNewDslMongoid.count_in_state('new').class).to eq(Fixnum)
expect(SimpleNewDslMongoid.count_in_state('new')).to eq(0)
end

end

describe "#with_state_scope" do

before do
3.times { SimpleNewDslMongoid.create!(:status => :unknown_scope) }
2.times { SimpleNewDslMongoid.create!(:status => :new) }
end

it "should respond to method" do
expect(SimpleNewDslMongoid).to respond_to(:with_state_scope)
end

it "should correctly process block" do
expect(SimpleNewDslMongoid.with_state_scope('unknown_scope') do
SimpleNewDslMongoid.count
end).to eq(3)
expect(SimpleNewDslMongoid.with_state_scope('new') do
SimpleNewDslMongoid.count
end).to eq(2)
end

end


describe "instance methods" do
let(:simple) {SimpleNewDslMongoid.new}

Expand Down

0 comments on commit ebf8109

Please sign in to comment.