Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7596437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:55:04+00:00 2026-05-30T21:55:04+00:00

I find my models getting more and more clumped and messy. below is my

  • 0

I find my models getting more and more clumped and messy. below is my actual user model, any suggestions on cleaning things up and arranging things for better readability?

Im getting frustrated by the unreadability of this all, so any general thoughts on this code are welcome. I really like code that does the same to be positioned together but like I have now is just one big bulk unreadable mess.

# == Schema Information
#
# Table name: users
#
#  id                     :integer(4)      not null, primary key
#  email                  :string(255)     default(""), not null
#  encrypted_password     :string(255)     default(""), not null
#  reset_password_token   :string(255)
#  reset_password_sent_at :datetime
#  remember_created_at    :datetime
#  sign_in_count          :integer(4)      default(0)
#  current_sign_in_at     :datetime
#  last_sign_in_at        :datetime
#  current_sign_in_ip     :string(255)
#  last_sign_in_ip        :string(255)
#  password_salt          :string(255)
#  confirmation_token     :string(255)
#  confirmed_at           :datetime
#  confirmation_sent_at   :datetime
#  unconfirmed_email      :string(255)
#  failed_attempts        :integer(4)      default(0)
#  unlock_token           :string(255)
#  locked_at              :datetime
#  authentication_token   :string(255)
#  username               :string(255)
#  is_blocked             :boolean(1)
#  is_deleted             :boolean(1)
#  role                   :string(255)
#  slug                   :string(255)
#  created_at             :datetime        not null
#  updated_at             :datetime        not null
#  last_seen              :datetime
#  credits                :integer(4)
#

class User < ActiveRecord::Base


  devise :database_authenticatable,
         :registerable,
         :recoverable,
         :rememberable,
         :trackable,
         :validatable,
         :token_authenticatable,
         :encryptable,
         :confirmable,
         :lockable,
         :timeoutable,
         :lastseenable
  #:omniauthable

  attr_accessible :username,
                  :login,
                  :email,
                  :password,
                  :password_confirmation,
                  :remember_me,
                  :profile_attributes,
                  :is_moderated,
                  :is_blocked,
                  :is_deleted,
                  :credits,
                  :role,
                  :confirmed_at,
                  :last_seen,
                  :invite_code


  attr_accessor :login
  #attr_accessor :invite_code

  has_one :profile
  has_one :account

  accepts_nested_attributes_for :profile
  accepts_nested_attributes_for :account

  extend FriendlyId
  friendly_id :username, use: :slugged

  before_create :default_values


  # AFTER CREATE -------------------------------------------------------------------------------------------------------

  after_create :add_account

  def add_account
    self.create_account

  end

  def default_values
    self.credits = -1
    self.invite_code = invite_code
    #self.reset_authentication_token!
    beta = Beta.where(:code => invite_code).first
    beta.used = 1
    beta.save
  end


  # ROLES --------------------------------------------------------------------------------------------------------------

  before_create :setup_default_role_for_new_users
  ROLES = %w[admin default vip]


  # VALIDATIONS --------------------------------------------------------------------------------------------------------


  before_validation { |u| u.username.downcase! }
  before_validation { |u| u.email.downcase! }

  validates_uniqueness_of :username,
                          :email,
                          :case_sensitive => false

  validates_presence_of :email,
                        :username,
                        :invite_code

  validates :username,
            :exclusion => {:in => ["admin", "root", "administrator", "superuser", "myhost", "support", "contact", "chat", "boo"],
                           :message => "is reserved"}


  validate :check_email, :on => :create
  validate :check_invite_code, :on => :create

  def check_invite_code
    errors.add(:invite_code, "Invalid code") unless Beta.where(:code => invite_code, :used => 0).first
  end

# Devise
  def active_for_authentication?
    super && !is_deleted
  end


# Devise
  def confirm!
    #welcome_message
    #super
  end

# Devise
  def soft_delete
    update_attribute(:deleted_at, Time.current)
  end


  def is_moderated?
    return self.is_moderated
  end

  def is_online?
    if self.last_seen < 10.minutes.ago
      return true
    else
      return false
    end
  end

  private

  def setup_default_role_for_new_users
    if self.role.blank?
      self.role = "default"
    end
  end


  def welcome_message
    #::Devise.mailer.welcome_instructions(self).deliver
    ::Devise.mailer.welcome_instructions(self).deliver
  end


  def check_email

    host = email.split("@").last
    username = email.split("@").first

    reserved_word_filters = %w(admin hostmaster root support )

    if /.*(#{reserved_word_filters.join("|")}).*\@/.match(email)
      errors.add(:email, "Invalid username in email")
    end

    if email.include? 'myhost'
      errors.add(:email, "Invalid email")
    end

  end


# DEVISE:  --------------------------------------------------------------------------------------------------

  protected


  def self.find_for_database_authentication(warden_conditions)
    conditions = warden_conditions.dup
    login = conditions.delete(:login)
    where(conditions).where(["lower(username) = :value OR lower(email) = :value", {:value => login.strip.downcase}]).first
  end


end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T21:55:05+00:00Added an answer on May 30, 2026 at 9:55 pm

    You could also get in the habit of treating booleans as first class expressions. For example,

    def is_online?
      if self.last_seen < 10.minutes.ago
        return true
      else
        return false
      end
    end
    

    is more clearly written as:

    def is_online?
      last_seen < 10.minutes.ago
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code App.Model('users').find(function (err, users) { users.forEach(function(user) { console.log(user.username); }); }); //make
I am trying to find all of the types in the Models namespace within
In Java I use getters/setters when I have simple models/pojos. I find that the
How do I find the relevant model where its name equals something? I've tried:
I have two instances of a model and find that they seem to share
I am trying to find out the optimal model for 4-way gradient filling. My
I am trying to find the best way to implement Model using Zend Framework
I'm trying to find the value of a particular model's attribute in rails. Here's
I'm trying to find a way to change/update a model inside a collection without
All the examples I could find of flexible box model show stuff expanding either

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.