I need to create reusable Devise extension. This is my first experience of creating gems. So, according sources of other Devise extensions, i’ve started with creating file devise_sms_authenticatable.rb in ‘lib’ folder with following code:
# encoding: utf-8
unless defined?(Devise)
require 'devise'
end
Devise.add_module :sms_authenticatable, :model => 'devise_sms_authenticatable/model'
Also, i’ve created folder devise_sms_authenticatable in the ‘lib’ folder, and added model.rb with code:
# encoding: utf-8
module Devise
module Models
module SmsAuthenticatable
# ...
end
end
end
After all i’ve added my new extension to the User model.
class User << ActiveRecord::Base
devise ..., :sms_authenticatable
end
But application falls with error uninitialized constant Devise::Models::SmsAuthenticatable (NameError). What i’m doing wrong?
If you’re putting the SmsAuthenticatable module already on
Devise::Models, there is no need to add it throughadd_module, though you may need to require it onconfig/initializers/devise(if you’re writing a gem, you may avoid this, since you will not touch devise.rb on initializers).You can also write its code without Devise::Models in your gem lib directory and only add this line
And Devise should add the module.