aboutsummaryrefslogtreecommitdiff
blob: 1bd76e292622d6e8c6aa0769db33bd036333bcf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require 'permissions/set.rb'
class ProjectAcceptance < ActiveRecord::Base

  hobo_model # Don't put anything above this

  fields do
    accepting_nick :string, :null => false, :null => false
    accepted       :boolean, :default => false
    timestamps
  end

  belongs_to :user, :null => false
  attr_readonly :user

  validates_presence_of   :user, :accepting_nick
  validates_uniqueness_of :accepting_nick, :scope => :user_id

  named_scope :find_by_user_name_and_accepting_nick, lambda { |user_name, accepting_nick| {
    :joins => :user, :conditions => ['users.name = ? AND accepting_nick = ?', user_name, accepting_nick] } }

  def create_permitted?
    # Recruiters can create project_acceptances
    # Project leads can create project_acceptances they should approve
    return true if acting_user.role.is_recruiter?
    return true if acting_user.project_lead && accepting_nick == acting_user.nick
    false
  end

  multi_permission :update, :destroy, :edit do
    # Allow admins everything
    return true if acting_user.administrator?

    # Allow users mentor and recruiters if not accepted and
    # accepted was not changed
    recruiter_user_or_mentor =  acting_user.role.is_recruiter? ||
                                user._?.mentor_is?(acting_user)

    return true if recruiter_user_or_mentor && !accepted && !accepted_changed?

    # Allow user with nick accepting_nick to change :accepted
    return true if (acting_user.nick ==  accepting_nick) && only_changed?(:accepted)

    # Allow CRU new records to recruiters and project leads
    return true if new_record? && acting_user.role.is_recruiter?
    return true if new_record? && acting_user.project_lead

    false
  end

  def view_permitted(field)
    # Allow user(relation), mentor of user and recruiters to view
     return true if user_is?(acting_user)
     return true if acting_user.role.is_recruiter?
     return true if user.mentor_is?(acting_user)

     false
  end

  # Returns new project acceptance with user = recruit, accepting_nick = lead.nick
  # if lead is marked as project_lead AND there isn't such a project acceptance yet
  def self.new_for_users(recruit, lead)
    return nil unless lead.project_lead
    return nil unless recruit.signed_up?
    return nil unless ProjectAcceptance.first(:conditions => { :accepting_nick => lead.nick, :user_id => recruit.id}).nil?
    ProjectAcceptance.new :accepting_nick => lead.nick, :user => recruit
  end
end