ciawalsh/iamME

View on GitHub
app/models/user.rb

Summary

Maintainability
A
0 mins
Test Coverage
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable

  attr_accessor :login

  has_many :events
  has_many :calendars, dependent: :destroy

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :authentication_keys => [:login]

  validates :username,
            :presence => true,
            :uniqueness => {
              :case_sensitive => false
            }
  after_create :build_me_cal



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

  def build_me_cal
    calendar = Calendar.new
    calendar.name = "Me"
    calendar.user_id = self.id
    calendar.save
    self.calendars << calendar
  end

  def get_me_calendar
    self.calendars.find_by(name: "Me")
  end


end