2011-07-24 10 views
0

私の問題は、テスト環境で古いエイリアスメソッドを呼び出すことです。おそらく唯一のRuby、Railsとエイリアシング - スタックが深すぎる問題

module Authentication 
    module ByPassword 
    # Stuff directives into including module 
    def self.included(recipient) 
     recipient.class_eval do 
     include ModelInstanceMethods 
     attr_accessor :password 
     validates_presence_of :password, :if => :password_required? 
     end 
    end # #included directives 

    module ModelInstanceMethods 
     def password_required? 
     crypted_password.blank? || !password.blank? 
     end 
    end # instance methods 
    end 
end 

仕様/モデル/ user_spec.rb

by_password.rbファクトリー・ガール

アプリ/モデル/ user.rb

class User < ActiveRecord::Base 
    ... 
    include Authentication 
    include Authentication::ByPassword 
    ... 

    attr_accessor :skip_password_validations 

    alias old_password_required? password_required? 
    # alias_method :old_password_required?, :password_required? 
    def password_required? 
    return false if !!skip_password_validations 
    old_password_required? 
    end 

    # def password_required_with_skip_validations? 
    # return false if !!skip_password_validations 
    # password_required_without_skip_validations? 
    # end 
    # alias_method_chain :password_required?, :skip_validations 
end 

ベンダー/プラグイン/../と

describe User do 
    it 'test' do 
     @user = Factory(:user) 
     @user.should be_valid 
    end 
end 

spec/factories/user_factories.rb

FactoryGirl.define do 
    factory :user do |u| 
    u.sequence(:email) { |n| "login#{n}@example.com" } 
    u.password 'qwertz123' 
    u.password_confirmation 'qwertz123' 
    end 
end 

結果が

1) User test 
    Failure/Error: Unable to find matching line from backtrace 
    SystemStackError: 
     stack level too deep 
    # /Users/schovi/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.1.0.rc4/lib/active_record/connection_adapters/abstract/database_statements.rb:197 

私は、デバッガの宝石でそれをデバッグするとき、私はuser.rbに新しい方法password_required?にその方法old_password_required?ポイントを発見し、vendor/plugins/../by_password.rb

alias, alias_method or alias_method_chain

と同じ結果を取得できません

アイデア?

答えて

1

私の意見は:Rubyでaliasを使用すると、しばしばお尻の痛みです。だから、どのようにそれ自身のモジュールにあなたのpassword_required?を移動し、その中superを呼び出すことについて、そのようAuthentication::ByPassword::ModelInstanceMethodsからpassword_required?を呼び出すために:回避策の

class User < ActiveRecord::Base 
    module PasswordRequired 
    def password_required? 
     return false if !!skip_password_validations 
     super # Is going to call Authentication's password_required? 
    end 
    end 

    include Authentication 
    include Authentication::ByPassword 
    include PasswordRequired 

    ... 

end 
+0

素晴らしいアイデア。私はおそらくそれを使用します。しかし、私はまだ興味があります、なぜ私はそのエラーを取得しています。 – Schovi

関連する問題