私はDevise in Rails 3を使用しており、レールにUserモデルを持っていて、ちょっと混雑しています。私はモジュールの中にすべてのログインメソッドを入れて、それらをモデルから取り込みたいと思います。私のような何かをしようとしている:Deviseを外部モジュールにロードすることは可能ですか?
アプリ/モデル/ user.rb
class User < ActiveRecord::Base
include UserImageable
extend Loginable
has_one :profile, :dependent => :destroy
has_many :items, :dependent => :destroy
has_many :products, :through => :items
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :phone_number, :location, :photo, :profile_attributes, :access_token
delegate :first_name, :last_name, :phone_number, :phone_number=, :location, :location=, :photo, :to => :profile
accepts_nested_attributes_for :profile
end
と
のlib /オートロード/ loginable.rb
module Loginable
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable
def password_require?
new_record?
end
end
をサーバーがありませんNoMeethodErrorがロードされるので、それは好きです。
loginable.rb:4:in `<module:Loginable>': undefined method `devise' for Loginable:Module (NoMethodError)
私が撮影しているものを行う方法はありますか、それとも本当ですか?
おかげ
こんにちは、私はこれでかなり新しいので、コードを整理する上であなたが提供しなければならないアドバイスや専門知識がありがとうと思います。あなたは私があなたがレイアウトした設定に行くことを提案しました。あるいは、「私はUserモデルにそのすべてを入れてはいけません」と言いましたが、別の方法がありますか?ありがとうございました! – noli
私も同様の機能を多くのモジュールにグループ分けする方向に向かっています。私のモデルはますます激しくなってきています。あなたはどこでも入手可能なこのスタイルの書き込みを持っていますか? – noli
Eduardoの答えと私の大きな違いは、私がRubyの最小限のソリューションであることです.Eduardo'sはRails固有のActiveSupport :: Concernモジュールを使用しています。これは一般的なモジュールのリファクタリングを規定しています。 – gtd