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

'{namespace}_{status}?' helper not working, e.g. 'status_active?', if value is a symbol #184

Open
fruwe opened this issue May 16, 2012 · 4 comments
Assignees

Comments

@fruwe
Copy link

fruwe commented May 16, 2012

Hi, great plugin, thanks.

I am using state_machine with the enum_column3 gem.
It seems, that the enum_column3 gem sets the attribute values in the model instance to symbols, rather than strings

code:

class XXX < ActiveRecord::Base
  state_machine :status, :initial => :register, :namespace => :status do
    state :register, :active, :inactive
    event :inactive do
      transition [:active] => :inactive
    end
  end
end

status_active? and can_inactive_status? should be true in this example, but only the "can" helper is:

irb(main):014:0> c = XXX.first
irb(main):014:0> c.status
=> :active
irb(main):015:0> c.status_active?
=> false
irb(main):015:0> c.can_inactive_status?
=> true

The code responsible for this is

module StateMachine
  class State
    [...]

    def initialize(machine, name, options = {}) #:nodoc:
      [...]
      @value = options.include?(:value) ? options[:value] : name && name.to_s
      [...]
    end

    [...]

    def matches?(other_value)
      matcher ? matcher.call(other_value) : other_value == value
    end

    [...]
  end
end

using either
@value = options.include?(:value) ? options[:value] : name
or
matcher ? matcher.call(other_value) : other_value.to_s == value.to_s
would fix the problem.

I know, that probably using e.g. "state :active, :value => :active" could work, but rather would like to avoid doing that, since I would have to fix many places, and I think that the gem should be more specific about it, since the "can_{status}{namespace}?" works, but not the "{namespace}{status}?" helper.

Let me know, what you think.

Thanks

@ghost ghost assigned obrie May 31, 2012
@glongman
Copy link

I'm having the same problem. Anyone looked at this?

@pmor
Copy link

pmor commented Mar 22, 2013

Same problem here, I'm having to use 'state_name == ' to get around this issue.

Using mongoid 3, and defining states as numbers (i.e. 'state :open, value: 0'). The helper methods are defined, but always return false.

@Blue-Dog-Archolite
Copy link

Also having the same issue. Only with some states however. Things like prospective? and prospective_public?

@mattconnolly
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants