rails state_machine不会改变状态

时间:2012-04-29 18:40:33

标签: ruby-on-rails state-machine

我正在尝试使用state_machine来使我的用户在Rails 3.1.3应用程序中的注册过程中拥有一个状态。我试图做一个非常简单的案例,但我不能通过一个事件来改变它的状态。在重新阅读文档时,我没有发现什么是错的。

我的用户ActiveRecord模型是:

# == Schema Information
#
# Table name: users
#
#  id                 :integer         not null, primary key
#  name               :string(255)
#  email              :string(255)
#  created_at         :datetime
#  updated_at         :datetime
#  encrypted_password :string(255)
#  salt               :string(255)
#  admin              :boolean         default(FALSE)
#  notify_followers   :boolean         default(TRUE)
#  state              :string(255)
#

# MME per a utilitzar les Hash functions
require 'digest'

class User < ActiveRecord::Base

  attr_accessor :password # MME nomes dona acces a la instance var @password que no es guarda a la BBDD

  # MME si es posa, atributs (columnes) als que es podrà accedir via ActiveRecord
  attr_accessible   :name, :email, :password, :password_confirmation, :admin, :notify_followers
  # MME validacions
  validates :name, :presence => true,
                   :length=> {maximum: 50}

  validates :email, :presence => true,
                    :format => { :with => /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i },
                    :uniqueness => { :case_sensitive => false}

  validates :password, :presence => true,
                       :confirmation => true,   # crea un atribut password_confirmation i a la vegada confirma que sigui igual que password
                       :length => { :within => 6..40 }

    # validates :password_confirmation, :presence => true   # MME aixo exigigeix que al crear es passi un :password_confirmation, doncs amb nomes
                              #   l'anterior validator sol, pot crearse un usuari si no es passa :password_confirmation

  before_save :encrypt_password

  # MME a l'esborrar un User s'esborren tb els seus Micropost
  has_many :microposts, :dependent => :destroy

 # MME Afegim respostes als usuaris
  has_many :replies, :class_name => 'Micropost',
                     :foreign_key => "in_reply_to",
                     :inverse_of => :replied_user,
                     :dependent => :destroy

  # User com a seguidor (follower)

  # te molts :relationships apuntant-lo amb la clau follower_id. Si el User s'elimina tots aquests Relationship tambe seran eliminats.
  has_many :relationships, :foreign_key => "follower_id",
                           :dependent => :destroy

  # te molts seguits via :relationships als que s'apunta via :followed_id  (inferit gracies a :followed, que apunta a la vegada als User)
  has_many :following, :through => :relationships,
                       :source => :followed

  # User com a seguit (followed)

  # te molts :reverse_relationships apuntant-lo amb la clau followed_id. Si el User s'elimina tots aquests Relationship tambe seran eliminats.
  has_many :reverse_relationships, :class_name => "Relationship",
                                   :foreign_key => "followed_id",
                                   :dependent => :destroy

  # te molts seguidors via :reverse_relationships als que s'apunta via :follower_id  (inferit gracies a :follower, que apunta a la vegada als User)
  has_many :followers, :through => :reverse_relationships

  # Torna els microposts dels usuaris seguits per un user, per exemple:
  #    usr=User.find(12)
  #    usr.following_microposts
  # (no el faig anar finalment: Micropost.from_users_followed_by(user) ho he implementat sense aquests metode perque
  # em falten els microposts del propi user) 
  has_many :following_microposts, :through => :following, 
                                  :source => :microposts

  # Si n'hi ha, te un password_reminder
  has_one :password_reminder

  # Torna l'User de l'email si el password es correcte
  def self.authenticate(email, submited_pwd)
    if usr = find_by_email(email)
      usr.has_password?(submited_pwd) ? usr : nil
    else
      nil
    end
  end

  # Torna l'User del id si el salt es correcte (s'utilitza per les sessions)
  def self.authenticate_with_salt(id, salt)
    user = find_by_id(id)
    (user && user.salt == salt) ? user : nil
  end

  # verifica si el password correspon a l'User
  def has_password?(submited_pwd)
    self.encrypted_password == encrypt(submited_pwd)
  end

  def feed
    #Micropost.from_users_followed_by self
    # Microposts from
    #   self
    #   self.following
    #   self.replies
    Micropost.not_messages.from_users_followed_by_or_in_reply_to self
  end

  # Is usr being followed by self?
  def following? usr
    following.include? usr
    # MME segons el tutorial seria
    #relationships.find_by_followed_id(followed)
  end

  def follow! usr
    relationships.create! :followed_id => usr.id
  end

  def unfollow! usr
    relationships.find_by_followed_id(usr.id).destroy if following?(usr)
  end

  def replies_to(usr, content)
    microposts.create :content=>content, :in_reply_to=>usr.id, :private=>false
  end

  def sends_to(usr, content)
    microposts.create :content=>content, :in_reply_to=>usr.id, :private=>true
  end

  def messages_to usr
    microposts.messages.where(:in_reply_to => usr.id)
  end

  def messages_from usr
    usr.microposts.messages.where(:in_reply_to => self.id)
  end

  def messages_to_or_from usr
    Micropost.messages.between usr, self
  end
  alias conversation_with messages_to_or_from

  # MME generates a unique login name for a user
  def pseudo_login_name
    name.downcase.split.join("_")+"_"+ id.to_s
  end

  # MME generates a password reminder if it doesn't yet exist
  def generate_password_reminder
    #PasswordReminder.find_or_create_by_user_id_and_token :user_id=>self.id,
    #                                                     :token=>SecureRandom.hex(32)
    create_password_reminder!(:token=>SecureRandom.hex(32)) unless password_reminder
  end

  # MME removes its password reminder if exists
  def remove_password_reminder
    password_reminder.delete if password_reminder
  end

  # finds a user from a token (password reminder to change password)
  def self.find_by_token(token)
    pr=PasswordReminder.find_by_token(token, :include=>:user)
    pr.user if pr
  end

  # MME finds a user from a pseudo_login_name
  # first tries to get it from an id
  # last tries to get it from a name
  def self.find_by_pseudo_login_name(pln)
    nam=pln.split("_")
    id = nam.last.to_i
    if id>0 # First attempt: if it exists an id as the last part off the pln 
      User.find_by_id(id)
    else # Second attempt: try to generate a name from a pln
      User.find_by_name(nam.map(&:capitalize).join(" "))
    end
  end

  ## MME state_machine per a fer la inscripcio en passos
  state_machine :initial => :pending do
    event :email_confirm do
      transition :pending => :email_confirmed
    end
  end


  # FUNCIONS PRIVADES
  private

    def encrypt_password
      self.salt = make_salt unless has_password?(password)  # self.salt resets everytime user changes its password
      self.encrypted_password = encrypt(password)   # password refers to self.password
    end

    def make_salt
      Digest::SHA2.hexdigest "#{Time.now.utc}--#{password}"
    end

    def encrypt(str)
      Digest::SHA2.hexdigest "#{salt}--#{str}"
    end

end

当然,我已经完成了迁移以使用户能够编写状态机

$ rails g migration AddStateToUser state:string
$ rake db:migrate

并检查用户是否真的从rails控制台响应状态属性。

当我尝试像在此控制台会话日志中那样简单地更改机器状态时出现问题:

1.9.2-p290 :006 > u=User.find 1
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
 => #<User id: 1, name: "Marcel", email: "mme@gmail.com", created_at: "2012-04-29 10:43:42", updated_at: "2012-04-29 10:43:42", encrypted_password: "d08c12c1cfb51fe5732f5e423b94dfdcaca1a1eb67821e3e37a...", salt: "78dfbecdfd4ffdd1fbcac5a878529b91a5200d563ebe3af23cf...", admin: false, notify_followers: true, state: "pendant"> 
1.9.2-p290 :007 > u.state
 => "pendant" 
1.9.2-p290 :008 > u.email_confirm
   (0.5ms)  SELECT 1 FROM "users" WHERE (LOWER("users"."email") = LOWER('mme@gmail.com') AND "users"."id" != 1) LIMIT 1
 => false 
1.9.2-p290 :009 > u.state
 => "pendant" 

正如您可能注意到的,从最后一个命令开始,我的用户没有将其状态更改为:email_confirmed,因为它已被调整。我也不理解通过这种方式完成的SQL查询。这对我来说似乎很可疑。

更多相关内容。如果我像往常一样尝试更新用户模型,则会出现相同的奇怪SQL查询,并且不会更新模型。此会话日志显示:

1.9.2-p290 :001 > u=User.find 1
  User Load (55.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
 => #<User id: 1, name: "Marcel Massana", email: "xaxaupua@gmail.com", created_at: "2012-04-29 19:32:26", updated_at: "2012-04-29 20:44:10", encrypted_password: "2ef5fec3287e2b26600521488060f698abed387e18e395d1331...", salt: "fa4d3ebb44c00237b66c95cc75ed5d1cda3b6e1535082def2a8...", admin: true, notify_followers: true, state: "pending"> 
1.9.2-p290 :002 > u.update_attributes(:name=>"Marcel")
   (0.1ms)  SAVEPOINT active_record_1
   (0.4ms)  SELECT 1 FROM "users" WHERE (LOWER("users"."email") = LOWER('xaxaupua@gmail.com') AND "users"."id" != 1) LIMIT 1
   (0.1ms)  ROLLBACK TO SAVEPOINT active_record_1
 => false 

谁能告诉我什么是错的?任何提示?

(当然我可以改变user.state =“email_confirmed”但是为什么要使用state_machine?)

3 个答案:

答案 0 :(得分:1)

好的,似乎我发现了正在发生的事情:

每次我在state_machine中进行更改,例如:

$ u.email_confirm

state_machineUser#update_attributes(我的用户实例)内部调用ustate属性作为唯一要更新的属性。这意味着在检查之前调用User#save方法并且BTW也会被检查。如果未更新其他属性,则某些验证可能会禁止保存u,因此u.state不会明显更改。

为了克服这个问题,我简单地将所有验证都置于一个州内。总结一下:

class User < ActiveRecord::Base
    ...
    state_machine :initial => :pending do

       event :email_confirm do
         transition :pending => :email_confirmed
       end

      state :pending do

        validates :name, :presence => true,
                         :length=> {maximum: 50}

        validates :email, :presence => true,
                          :format => { :with => /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i },
                          :uniqueness => { :case_sensitive => false}

        validates :password, :presence => true,
                             :confirmation => true,
                             :length => { :within => 6..40 }
      end
    end
    ...
  end

在保存之前始终会调用验证,因此,当从:pending转换为:email_confirmed时,u.state已经有值:email_confirmed,并且不会执行任何验证。

另外,奇怪的(至少对我来说)SQL查询

SELECT 1 FROM "users" WHERE (LOWER("users"."email") = LOWER('xaxaupua@gmail.com') AND "users"."id" != 1) LIMIT 1
仅在启用验证时才执行

。如果我禁用验证,则不执行此查询。不知道为什么ActiveRecord执行该查询。尽管现在对我来说这不是问题,但我要感谢任何人对这个问题投入一点点光明,或者指出任何解释这种行为的链接。

答案 1 :(得分:1)

额外的SQL查询是验证的结果:

validates :email, :uniqueness => { :case_sensitive => false }

它检查数据库以查看是否已存在具有该(低级)电子邮件的其他用户(id != 1)。

答案 2 :(得分:0)

默认情况下,会对以下内容执行验证:创建和:更新模型上的事件。我和state_machine有类似的问题。我的解决方案是简单地删除:update事件的验证,因为电子邮件和名称attribs在创建记录后是只读的。例如:

validates(:name, :presence => true,
        :length => { :maximum => 50},
        :uniqueness =>true,
        :on => :create)
validates(:email, :presence => true,
        :format => {:with => email_regex},
        :uniqueness => { :case_sensitive => false},
        :on => :create)
validates(:password, :presence => true,
        :confirmation => true,
        :length => { :within => 6..40},
        :if => :password)

请注意,如果密码属性发生更改,则会执行密码验证。这也避免了您遇到的state_machine问题。如果您向用户提供更改名称和电子邮件的访问权限,则可以将相同的逻辑应用于这些验证器。