Project

General

Profile

Defect #28243 » principal.diff

Anonymous, 2018-02-25 20:04

View differences:

app/models/principal.rb (working copy)
84 84

  
85 85
  # Principals that are members of a collection of projects
86 86
  scope :member_of, lambda {|projects|
87
    projects = [projects] if projects.is_a?(Project)
88
    if projects.blank?
89
      where("1=0")
90
    else
91
      ids = projects.map(&:id)
92
      active.where("#{Principal.table_name}.id IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
93
    end
87
    return where('1=0') if projects.blank?
88

  
89
    projects = [projects] unless projects.respond_to?(:map)
90
    ids = projects.map(&:id)
91
    active.where("#{Principal.table_name}.id IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
94 92
  }
93

  
95 94
  # Principals that are not members of projects
96 95
  scope :not_member_of, lambda {|projects|
97
    projects = [projects] unless projects.is_a?(Array)
98
    if projects.empty?
99
      where("1=0")
100
    else
101
      ids = projects.map(&:id)
102
      where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
103
    end
96
    return where('1=0') if projects.blank?
97

  
98
    projects = [projects] unless projects.respond_to?(:map)
99
    ids = projects.map(&:id)
100
    where("#{Principal.table_name}.id NOT IN (SELECT DISTINCT user_id FROM #{Member.table_name} WHERE project_id IN (?))", ids)
104 101
  }
105
  scope :sorted, lambda { order(*Principal.fields_for_order_statement)}
106 102

  
103
  scope :sorted, lambda { order(*Principal.fields_for_order_statement) }
104

  
107 105
  before_create :set_default_empty_values
108 106
  before_destroy :nullify_projects_default_assigned_to
109 107

  
test/unit/principal_test.rb (working copy)
60 60
  end
61 61

  
62 62
  def test_member_of_scope_should_be_empty_for_no_projects
63
    assert_equal [], Principal.member_of([]).sort
63
    assert_equal [], Principal.member_of([])
64 64
  end
65 65

  
66 66
  def test_not_member_of_scope_should_return_users_that_have_no_memberships
......
73 73
  end
74 74

  
75 75
  def test_not_member_of_scope_should_be_empty_for_no_projects
76
    assert_equal [], Principal.not_member_of([]).sort
76
    assert_equal [], Principal.not_member_of([])
77 77
  end
78 78

  
79
  def test_that_not_member_of_scope_accepts_active_record_relation
80
    projects_relation = Project.where(:name => 'eCookbook')
81
    projects_array = projects_relation.to_a
82

  
83
    assert_nothing_raised do
84
      Principal.not_member_of(projects_relation)
85
    end
86
    assert_equal Principal.not_member_of(projects_array),
87
                 Principal.not_member_of(projects_relation)
88
  end
89

  
79 90
  def test_sorted_scope_should_sort_users_before_groups
80 91
    scope = Principal.where(:type => ['User', 'Group'])
81 92
    users = scope.select {|p| p.is_a?(User)}.sort
(1-1/2)