Skip to content
Browse files

[account] plugin-ize

  • Loading branch information...
1 parent 64745d9 commit 0222567a4708405da2f45d3f7dfe687f4717dc73 @achiu achiu committed
Showing with 83 additions and 56 deletions.
  1. +4 −56 app/models/account.rb
  2. +57 −0 app/models/plugins/authentication.rb
  3. +22 −0 app/models/plugins/karma.rb
View
60 app/models/account.rb
@@ -2,70 +2,18 @@
class Account
include MongoMapper::Document
- attr_accessor :password
+ plugin Plugins::Karma
+ plugin Plugins::Authentication
# Keys
key :first_name, String
key :last_name, String
key :username, String
- key :email, String
- key :crypted_password, String
- key :salt, String
- key :role, String
- key :karma, Integer, :default => 0
# Validations
- validates_presence_of :email, :role, :username
- validates_presence_of :password, :if => :password_required
- validates_presence_of :password_confirmation, :if => :password_required
- validates_length_of :password, :within => 4..40, :if => :password_required
- validates_confirmation_of :password, :if => :password_required
- validates_length_of :email, :within => 3..40
+ validates_presence_of :username
validates_length_of :username, :within => 3..40
validates_length_of :first_name, :within => 3..40
validates_length_of :last_name, :within => 3..40
- validates_uniqueness_of :email, :case_sensitive => false
- validates_uniqueness_of :username, :case_sensitive => false
- validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
- validates_format_of :role, :with => /[A-Za-z]/
-
- # Callbacks
- before_save :generate_password
-
-
- def add_karma(points=1)
- self.karma += points
- self.save
- end
-
- def sub_karma(points=1)
- self.karma -= points
- self.save
- end
-
- ##
- # This method is for authentication purpose
- #
- def self.authenticate(email, password)
- account = first(:email => email) if email.present?
- account && account.password_clean == password ? account : nil
- end
-
- ##
- # This method is used to retrieve the original password.
- #
- def password_clean
- crypted_password.decrypt(salt)
- end
-
- private
- def generate_password
- return if password.blank?
- self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{email}--") if new_record?
- self.crypted_password = password.encrypt(self.salt)
- end
-
- def password_required
- crypted_password.blank? || !password.blank?
- end
+ validates_uniqueness_of :username, :case_sensitive => false
end
View
57 app/models/plugins/authentication.rb
@@ -0,0 +1,57 @@
+module Plugins
+ module Authentication
+
+ def self.configure(model)
+ # fields
+ model.key :email, String
+ model.key :crypted_password, String
+ model.key :salt, String
+ model.key :role, String
+ # callbacks
+ model.before_save :generate_password
+ # validations
+ model.validates_presence_of :email, :role
+ model.validates_presence_of :password, :if => :password_required
+ model.validates_presence_of :password_confirmation, :if => :password_required
+ model.validates_length_of :password, :within => 4..40, :if => :password_required
+ model.validates_confirmation_of :password, :if => :password_required
+ model.validates_length_of :email, :within => 3..40
+ model.validates_uniqueness_of :email, :case_sensitive => false
+ model.validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
+ model.validates_format_of :role, :with => /[A-Za-z]/
+ end
+
+ module ClassMethods
+ attr_accessor :password
+ end
+
+ module InstanceMethods
+ ##
+ # This method is for authentication purpose
+ #
+ def self.authenticate(email, password)
+ account = first(:email => email) if email.present?
+ account && account.password_clean == password ? account : nil
+ end
+
+ ##
+ # This method is used to retrieve the original password.
+ #
+ def password_clean
+ crypted_password.decrypt(salt)
+ end
+
+ private
+ def generate_password
+ return if password.blank?
+ self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{email}--") if new_record?
+ self.crypted_password = password.encrypt(self.salt)
+ end
+
+ def password_required
+ crypted_password.blank? || !password.blank?
+ end
+ end
+
+ end
+end
View
22 app/models/plugins/karma.rb
@@ -0,0 +1,22 @@
+module Plugins
+ module Karma
+
+ def self.configure(model)
+ model.key :karma, Integer, :default => 0
+ end
+
+ module ClassMethods
+ end
+
+ module InstanceMethods
+ def add_karma(points=1)
+ self.update_attributes :karma => self.karma + points
+ end
+
+ def sub_karma(points=1)
+ self.update_attributes :karma => self.karma - points
+ end
+ end
+
+ end
+end

0 comments on commit 0222567

Please sign in to comment.
Something went wrong with that request. Please try again.