Sha256: c6e8014f1b5fe8ca1ec57433fa2cd94e5881dbe2399e350a52ecb6748f67b347

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

class <%= class_name %> < ActiveRecord::Base

  hobo_user_model # Don't put anything above this

  fields do
    username :string, :login => true, :name => true
    email_address :email_address
    administrator :boolean, :default => false
    timestamps
  end

  # 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 count == 0 }
  
  
  # --- Signup lifecycle --- #

  lifecycle do

    initial_state :active

    create :anybody, :signup, 
           :params => [:username, :email_address, :password, :password_confirmation],
           :become => :active, :if => proc {|_, u| u.guest?}

    transition :nobody, :request_password_reset, { :active => :active } do
      UserMailer.deliver_forgot_password(self, lifecycle.generate_key)
    end

    transition :with_key, :reset_password, { :active => :active }, 
               :update => [ :password, :password_confirmation ]

  end
  

  # --- Hobo Permissions --- #

  def creatable_by?(creator)
    creator.administrator? || !administrator
  end

  def updatable_by?(updater, new)
    updater.administrator? || (updater == self && only_changed_fields?(new, :password, :password_confirmation))
  end

  def deletable_by?(deleter)
    deleter.administrator?
  end

  def viewable_by?(viewer, field)
    true
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hobo-0.8 rails_generators/hobo_user_model/templates/model.rb