Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transparent scope #67

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) }
->(model, values) { values.present? ? model.where(scope.call(values)) : model.all }
end

# Generates the results for the given scope based on one or more states to filter by
def run_scope(scope, machine, klass, states)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this method added?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I add it to override this method from state machine (see state-machines/state_machines#63).
I don't know how to change gem dependency to do that in a good way. Can you give me some information, so that I can to it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@diegotoral Are you up to rebase this PR ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not updated with the pull request but I can give it a try some day this week. 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any update ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the branch rebased but I'm not allowed to push it here. 😬

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you open a new pre ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks to continuing my work 👍

values = states.flatten.compact.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
54 changes: 49 additions & 5 deletions test/machine_with_scopes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,21 @@

class MachineWithScopesTest < BaseTestCase
def setup
@model = new_model
@model = new_model do
connection.add_column table_name, :name, :string
end
@machine = StateMachines::Machine.new(@model)
@machine.state :parked, :first_gear
@machine.state :idling, :value => -> { 'idling' }
end

def test_should_allow_chaining_scopes_with_queries
named = @model.create :state => 'parked', :name => 'a_name'
@model.create :state => 'parked'

assert_equal [named], @model.where(:name => 'a_name').with_state(:parked)
end

def test_should_create_singular_with_scope
assert @model.respond_to?(:with_state)
end
Expand All @@ -19,6 +28,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 +46,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 +66,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