Skip to content

Instantly share code, notes, and snippets.

Created March 9, 2010 07:53
Show Gist options
  • Select an option

  • Save anonymous/326364 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/326364 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
hobo_user_model # Don't put anything above this
fields do
username :string, :login => true
name :string, :required, :name => true
email_address :email_address, :required
administrator :boolean, :default => false
company_name :string, :required
website :string, :required
address_line_1 :string, :required
address_line_2 :string
towncity :string, :required
countystate :string, :required
postcodezip :string
country :string, :required
work_address_line_1 :string
work_address_line_2 :string
work_towncity :string
work_countystate :string
work_postcodezip :string
work_country :string
contract_company_name :string
contract_website :string
contract_address_line_1 :string
contract_address_line_2 :string
contract_towncity :string
contract_countystate :string
contract_postcodezip :string
contract_country :string
accepted_tnc :boolean, :default => false
accepted_at :datetime
email_optout :boolean, :default => false
timestamps
end
#validates_acceptance_of :accepted_tnc, :accept => true, :if => :validate_tnc
validates_presence_of :work_address_line_1, :if => :validate_work_address
validates_presence_of :work_towncity, :if => :validate_work_address
validates_presence_of :work_countystate, :if => :validate_work_address
validates_presence_of :work_country, :if => :validate_work_address
validates_presence_of :contract_company_name, :if => :validate_contract_address
validates_presence_of :contract_address_line_1, :if => :validate_contract_address
validates_presence_of :contract_towncity, :if => :validate_contract_address
validates_presence_of :contract_countystate, :if => :validate_contract_address
validates_presence_of :contract_country, :if => :validate_contract_address
has_many :versions, :class_name => "Version", :foreign_key => "tester_id"
has_many :version_change_logs, :class_name => "VersionChangeLog", :foreign_key => "tester_id"
has_many :applications, :class_name => "Application", :foreign_key => "owner_id"
has_many :application_change_logs, :class_name => "ApplicationChangeLog", :foreign_key => "owner_id"
has_many :comments, :class_name => "Comment", :foreign_key => "comment_id"
# Change Logs
has_many :version_change_logs, :class_name => "VersionChangeLog", :foreign_key => "updated_by_id"
has_many :application_change_logs, :class_name => "ApplicationChangeLog", :foreign_key => "updated_by_id"
has_many :user_change_logs, :class_name => "UserChangeLog", :foreign_key => "updated_by_id"
# This gives admin rights to the first sign-up.
# Just remove it if you don't want that
before_create { |user| user.administrator = true if !Rails.env.test? && count == 0 }
after_create { |user| user.state = "active" if !Rails.env.test? && count == 1 }
attr_reader :just_accepted_tnc
before_save :set_accepted_at
def validate_work_address
if work_address_line_1 == "" && work_address_line_2 == "" && work_towncity == "" && work_countystate == "" && work_postcodezip == "" && work_country == ""
return false
end
return true
end
def validate_contract_address
if contract_company_name == "" && contract_website == "" && contract_address_line_1 == "" && contract_address_line_2 == "" && contract_towncity == "" && contract_countystate == "" && contract_postcodezip == "" && contract_country == ""
return false
end
return true
end
def validate_tnc
if self.state == "active"
return true
else
return false
end
end
# --- Signup lifecycle --- #
lifecycle do\
#state :active, :default => true
state :inactive, :default => true
state :active
state :pending
state :rejected
create :signup, :available_to => "Guest", :creator => true,
:params => [:username, :name, :email_address, :password, :password_confirmation, :company_name,
:website, :address_line_1, :address_line_2, :towncity, :countystate, :postcodezip, :country,
:work_address_line_1, :work_address_line_2, :work_towncity, :work_countystate,
:work_postcodezip, :work_country, :contract_company_name, :contract_website, :contract_address_line_1,
:contract_address_line_2, :contract_towncity, :contract_countystate, :contract_postcodezip, :contract_country],
#:become => :active
:become => :inactive, :new_key => true do
UserMailer.deliver_activation(self, lifecycle.key) unless email_address.blank?
end
transition :activate, { :inactive => :pending }, :available_to => :key_holder do
UserMailer.deliver_approval(self, lifecycle.key)
end
transition :approve, { :pending => :active }, :available_to => :key_holder do
UserMailer.deliver_approved(self, lifecycle.key)
end
transition :reject, { :pending => :rejected }, :available_to => :key_holder do
UserMailer.deliver_rejected(self, lifecycle.key)
end
transition :request_password_reset, { :active => :active }, :new_key => true do
UserMailer.deliver_forgot_password(self, lifecycle.key)
end
transition :request_password_reset, { :inactive => :inactive }, :new_key => true do
UserMailer.deliver_activation(self, lifecycle.key)
end
transition :reset_password, { :active => :active }, :available_to => :key_holder,
:params => [ :password, :password_confirmation ]
end
def logChange
result = UserChangeLog.create( :user => self,
:username => self.username,
:name => self.name,
:email_address => self.email_address,
:administrator => self.administrator,
:company_name => self.company_name,
:website => self.website,
:address_line_1 => self.address_line_1,
:address_line_2 => self.address_line_2,
:towncity => self.towncity,
:countystate => self.countystate,
:work_address_line_1 => self.work_address_line_1,
:work_address_line_2 => self.work_address_line_1,
:work_towncity => self.work_towncity,
:work_countystate => self.work_countystate,
:work_postcodezip => self.work_postcodezip,
:work_country => self.work_country,
:contract_company_name => self.contract_company_name,
:contract_website => self.contract_website,
:contract_address_line_1 => self.contract_address_line_1,
:contract_address_line_2 => self.contract_address_line_2,
:contract_towncity => self.contract_towncity,
:contract_countystate => self.contract_countystate,
:contract_postcodezip => self.contract_postcodezip,
:contract_country => self.contract_country,
:accepted_tnc => self.accepted_tnc,
:email_optout => self.email_optout,
:updated_by => changedActor )
end
def changedActor
unless self
if self.acting_user.guest?
return nil
else
return self.acting_user
end
end
return nil
end
def set_accepted_at
@just_accepted_tnc = false
if accepted_tnc_changed? && accepted_tnc?
self.accepted_at = Time.now
@just_accepted_tnc = true
end
end
# --- Permissions --- #
def create_permitted?
false
end
def update_permitted?
return true if acting_user.administrator? || self == acting_user
#any_changed? :accepted_tnc, :username, :email_address, :website, :address_line_1, :address_line_2 ,:towncity ,:countystate,
# :work_address_line_1, :work_address_line_2, :work_towncity, :work_countystate, :work_postcodezip, :work_country,
# :contract_company_name, :contract_website, :contract_address_line_1, :contract_address_line_2, :contract_towncity,
# :contract_countystate, :contract_postcodezip, :contract_country, :email_optout, :password, :password_confirmation
end
def destroy_permitted?
acting_user.administrator?
end
def destroy_permitted?
acting_user.administrator?
end
def company_name_view_permitted?
return true
end
def view_permitted?(field)
return true if self == acting_user || acting_user.administrator? || self.new_record?
return true if field.in? ['password','password_confirmation','company_name']
#return true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment