Skip to content

Commit

Permalink
add test for transparent scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
gagalago committed Dec 28, 2017
1 parent 049ef73 commit fad6f6a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
8 changes: 7 additions & 1 deletion lib/state_machines/integrations/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,13 @@ def locale_path

# Defines a new named scope with the given name
def create_scope(name, scope)
lambda { |model, values| model.where(scope.call(values)) if values.present? }
->(model, values) { values.present? ? model.where(scope.call(values)) : model }
end

# Generates the results for the given scope based on one or more states to filter by
def run_scope(scope, machine, klass, states)
values = states.compact.flatten.map { |state| machine.states.fetch(state).value }
scope.call(klass, values)
end

# ActiveModel's use of method_missing / respond_to for attribute methods
Expand Down
43 changes: 39 additions & 4 deletions test/machine_with_scopes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def test_should_only_include_records_with_state_in_singular_with_scope
assert_equal [parked], @model.with_state(:parked).all
end

def test_should_allow_transparent_with_state_in_singular_with_scope
@model.create :state => 'parked'
@model.create :state => 'idling'

assert_equal @model.all, @model.with_state(nil).all
end

def test_should_create_plural_with_scope
assert @model.respond_to?(:with_states)
end
Expand All @@ -30,6 +37,13 @@ def test_should_only_include_records_with_states_in_plural_with_scope
assert_equal [parked, idling], @model.with_states(:parked, :idling).all
end

def test_should_allow_transparent_with_states_in_plural_with_scope
@model.create :state => 'parked'
@model.create :state => 'idling'

assert_equal @model.all, @model.with_states(nil).all
end

def test_should_allow_lookup_by_string_name
parked = @model.create :state => 'parked'
idling = @model.create :state => 'idling'
Expand All @@ -43,28 +57,49 @@ def test_should_create_singular_without_scope

def test_should_only_include_records_without_state_in_singular_without_scope
parked = @model.create :state => 'parked'
idling = @model.create :state => 'idling'
@model.create :state => 'idling'

assert_equal [parked], @model.without_state(:idling).all
end

def test_allow_transparent_without_state_in_singular_without_scope
@model.create :state => 'parked'
@model.create :state => 'idling'

assert_equal @model.all, @model.without_state(nil).all
end

def test_should_create_plural_without_scope
assert @model.respond_to?(:without_states)
end

def test_should_only_include_records_without_states_in_plural_without_scope
parked = @model.create :state => 'parked'
idling = @model.create :state => 'idling'
first_gear = @model.create :state => 'first_gear'
@model.create :state => 'first_gear'

assert_equal [parked, idling], @model.without_states(:first_gear).all
end

def test_allow_transparent_without_states_in_plural_without_scope
@model.create :state => 'parked'
@model.create :state => 'idling'
@model.create :state => 'first_gear'

assert_equal @model.all, @model.without_states(nil).all
end

def test_should_allow_chaining_scopes
parked = @model.create :state => 'parked'
@model.create :state => 'parked'
idling = @model.create :state => 'idling'

assert_equal [idling], @model.without_state(:parked).with_state(:idling).all
end
end

def test_should_allow_chaining_transparent_scopes
@model.create :state => 'parked'
idling = @model.create :state => 'idling'

assert_equal [idling], @model.with_state(nil).with_state(:idling).all
end
end

0 comments on commit fad6f6a

Please sign in to comment.