Datamapper Callback for Forum tripcode
The context: creating a tripcode implementation (http://en.wikipedia.org/wiki/Tripcode) for a forum. Essentially, a weak hash for registrationless identification.
There is one model, 'Post'. Posts are arranged in parent/child format, new post creates parent, replies create child to parent. There is one form, right now has a field that posts to the controller/model, contains a content and password field.
require 'bcrypt'
class Shout
include DataMapper::Resource
include BCrypt
property :id, Serial # unique key
property :content, Text
property :password_hash, String
property :trip, String # trip for display
belongs_to :forum
is :tree, :order => [:created_at]
attr_accessor :password
#before :save do
def password
@password ||= Password.new(password_hash)
end
def password=(new_password)
@password = Password.开发者_如何学Gocreate(new_password)
self.password_hash = @password
end
def trip
@trip = '!'<<self.password_hash.to_str[20..33]
self.trip = @trip
end
#end
end
DataMapper.finalize
The basic flow is this - post/reply, if there is a password in the password field, take that and run through bcrypt, store that result as password_hash for later comparison, create tripcode for display. But I'm getting errors I've been beating my head against
The primary error I'm getting is
undefined method `primitive?' for nil:NilClass
seemingly emanating from
lib/active_support/whiny_nil.rb:48:in `method_missing'
I don't know how to handle or work around this. I'm not doing something or checking something with the controller, but don't yet know what. The other error I'm getting stems from an invalid bcrypt hash, but not able to duplicate this immediately.
The hook methods are right off the bcrypt-ruby page.
Creating a BCryptHash field works (dm-types) -- but increases the time to process the form by a factor of 10, on a localhost server, and does it for every post so I need a way to tweak the cost of the bcrypt hash (eg. 1 instead of default 10) and only run it when there is a password present, which is why I'm doing this.
But this doesn't work right now, I've rammed my head against it enough and moving on to other problems and coming back to it if I can get some input. I'm working with rails, so I've added that tag although its not primarily a rails issue.
Feel free to review or contribute or use for errors here.
https://github.com/blueblank/Shout/tree/oneshout
精彩评论