天天看點

ClassMethods -- joins

class SelectCourseApply < ActiveRecord::Base

  STATUS_REQUEST  = "REQUEST" # 提出選課請求

  STATUS_ACCEPT   = "ACCEPT"  # 準許

  STATUS_REJECT   = "REJECT"  # 拒絕

  attr_accessible :course, :user, :status

  belongs_to :course

  belongs_to :user

  validates :course, :user, :presence => true

  validates :user_id,  :uniqueness => {:scope => :course_id}

  validates :status, :inclusion => [STATUS_REQUEST, STATUS_ACCEPT, STATUS_REJECT]

  scope :by_status, lambda{|status| {:conditions => ['status = ?', status]} }

  scope :by_course, lambda{|course| {:conditions => ['course_id = ?', course.id]} }

  module CourseMethods

    def self.included(base)

      base.has_many :select_course_applies

      base.extend ClassMethods

    end

    module ClassMethods

      def by_status_and_user(status,user)

        Course.joins(:select_course_applies).where(

        'select_course_applies.status = ? AND select_course_applies.user_id = ?',status, user.id)

      end

    end

    # 8 查詢一個課程的所有”沒有處理“的選課請求(status == REQUEST)

    def request_select_course_applies

      self.select_course_applies.by_status(STATUS_REQUEST)

    end

    # 6 查詢課程被哪些使用者“選中”(status == ACCEPT 的情況)

    def selected_users

      User.by_status_and_course(STATUS_ACCEPT,self)

    end

    # 5 查詢課程被哪些使用者“選了”(status == REQUEST 的情況)

    def apply_select_users

      User.by_status_and_course(STATUS_REQUEST,self)

    end

    # 4 判斷一個使用者是否"選中"某門課(status == ACCEPT 的情況)

    def is_selected?(user)

      user.selected_courses.include?(self)

    end

    #3 判斷一個使用者是否“選了”某門課(status == REQUEST 的情況)

    def is_apply_select?(user)

      # user.apply_select_courses.include?(SelectCourseApply.find_by_course_id_and_user_id(self.id, user.id))

      user.apply_select_courses.include?(self)

    end

  end

  module UserMethods

    def self.included(base)

      base.has_many :select_course_applies

      base.has_many :courses, :through => :select_course_applies

      base.extend ClassMethods

    end

    module ClassMethods

      def by_status_and_course(status,course)

        User.joins(:select_course_applies).where(

        'select_course_applies.status = ? AND select_course_applies.course_id = ?',status, course.id)

      end

    end

    # 7 使用者發起一個選課請求

    def select_course(course)

      return if course.is_apply_select?(self) || course.is_selected?(self)

      if self.courses.include?(courses)

        self.select_course_applies.by_course(course).first.update_attributes :status => STATUS_REQUEST

      else

        self.select_course_applies.create(:course => course, :status => STATUS_REQUEST)

      end

    end

    # 2 查詢使用者“選中”的課程清單(status == ACCEPT 的情況)

    def selected_courses

      Course.by_status_and_user(STATUS_ACCEPT,self)

    end

    # 1 查詢使用者"選了"的課程清單(status == REQUEST 的情況)

    def apply_select_courses

      Course.by_status_and_user(STATUS_REQUEST,self)

    end

  end

end