Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdickey/d9b79b0e19cefe1e32cb to your computer and use it in GitHub Desktop.
Save jdickey/d9b79b0e19cefe1e32cb to your computer and use it in GitHub Desktop.
Simplest possible(?) non-ActiveRecord usage of ActiveModel::SecurePassword.has_secure_password

Here's what I've (re)learned after a few hours poking at Sequel and ActiveModel::SecurePassword.has_secure_password

It's Even Simpler than I Remember It

In olden times (pre-AM 4.3?) I seem to recall that there were more than one ActiveModel module that needed to be included in a non-ActiveRecord::Base subclass to get it to meet requirements for has_secure_password. That has now been reduced to one (meta-)module.

For example, see the below user.rb file. All you need is include ActiveModel::SecurePassword, apparently.

It's Now Absurdly Easy to Set Up in the Database

I"m still using ActiveRecord for generating models, migrations and such (sequel-rails has repeatedly and consistently broken my app in ways I've chosen to defer figuring out fixes for), but the Rails 4.3 generator has a few Useful Tricks I don't remember from 4.2. The command line I used to generate the migration I wanted was

bundle exec rails g model user pen_name:string:uniq password:digest email:string:index profile:text

My notes from previous apps indicate that I'd previously used the field declaration password_digest:string rather than password:digest. Arguably easier to type, and virtually inarguably more intention-revealing. Very nice.

Right, Captain Obvious; Why the Write-Up?

Simply as a mea culpa to the folks I pestered on the Google+ Ruby community, and hopefully to make it less likely for the next sad sack to dine on his foot as energetically as I did today.

class User < Sequel::Model(:users)
include ActiveModel::SecurePassword
has_secure_password
def save
self.updated_at = Time.zone.now
self.created_at = self.updated_at unless self.created_at
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment