khoa nguyen

coder by day, an hero by night

Page 2


Devise Authentication unscoped

At Shop2 we do the Rails 3.2, Mongoid 2.4, and Devise 2.0. We found that Devise by default does not play nice with default scoping.

 Our User model with default_scope of active users,
 and a :vip scope

class User
  include Mongoid::Document

  field :status

   now only active users can be authenticated
  default_scope where(status: 'active')

  scope :vip, where(status: 'vip')
end

So how would we authenticate VIPs?

Devise uses Warden::Strategies to authenticate, and loads back user from a session. This is how I plugged into Devise:

 lib/vip_authenticatable.rb

 We introduce :vip_authenticatable strategy to Warden.
 With this strategy your login form might look something like

 <form action="/path/to/create/session">
   <input name="vip[:id]" placeholder="Enter your VIP No.">
   <input type="submit">
 </form>

Warden::Strategies.add(:vip_authenticatable) do
  def valid?
...

Continue reading →


Claim back Snow Leopard speed from Lion

At Shop2, we use the Mac platform heavily for development. But the vim on Mac OSX Lion is just so darn slow, especially autocomplete. One of our engineers has this clever idea of disabling Spotlight. BAM! The vim feels speedier once again.

The only thing I’m using spotlight is to quickly launch applications. So this is how I configured my Spotlight:

  1. System Preferences…
  2. Spotlight
  3. Untick everything except for Applications
  4. All done. Now go and enjoy a speedier/more productive vim experience

Cheers.

P.S. Spotlight can be launched by pressing cmd+space

View →