Project

General

Profile

Feature #2055 ยป 2055_git.diff

Jean-Baptiste Barth, 2010-06-24 22:44

View differences:

app/models/query.rb
185 185
    user_values = []
186 186
    user_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
187 187
    if project
188
      user_values += project.users.sort.collect{|s| [s.name, s.id.to_s] }
188
      user_values += User.in_project_hierarchy_of(project).sort.collect{|s| [s.name, s.id.to_s] }
189 189
    else
190
      project_ids = User.current.projects.collect(&:id)
191
      if project_ids.any?
192
        # members of the user's projects
193
        user_values += User.active.find(:all, :conditions => ["#{User.table_name}.id IN (SELECT DISTINCT user_id FROM members WHERE project_id IN (?))", project_ids]).sort.collect{|s| [s.name, s.id.to_s] }
190
      projects = User.current.projects
191
      if projects.any?
192
        # members of the user's projects and subprojects
193
        user_values += User.in_project_hierarchy_of(projects).sort.collect{|s| [s.name, s.id.to_s] }
194 194
      end
195 195
    end
196 196
    @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => user_values } unless user_values.empty?
app/models/user.rb
237 237
    find(:first, :conditions => ["LOWER(mail) = ?", mail.to_s.downcase])
238 238
  end
239 239
  
240
  # Active members of project or subprojects
241
  def self.in_project_hierarchy_of(projects)
242
    projects = [projects] unless projects.is_a?(Array)
243
    #add the (visible) subprojects
244
    projects = projects.map do |project|
245
      project.self_and_descendants.all(:conditions => Project.visible_by(User.current))
246
    end.flatten
247
    #search active users in all these projects
248
    active.all(
249
      :conditions => [ "#{User.table_name}.id IN (SELECT DISTINCT user_id FROM members WHERE project_id IN (?))",
250
                       projects.map(&:id) ]
251
    )
252
  end
253
  
240 254
  def to_s
241 255
    name
242 256
  end
test/fixtures/members.yml
60 60
  project_id: 2
61 61
  user_id: 8
62 62
  mail_notification: false
63
members_011:
64
  id: 11
65
  created_on: 2006-07-19 19:35:33 +02:00
66
  project_id: 3
67
  user_id: 8
68
  mail_notification: false
test/unit/query_test.rb
351 351
    assert !q.editable_by?(manager)
352 352
    assert !q.editable_by?(developer)
353 353
  end
354

  
355
  context "#available_filters" do
356
    should "include users of subprojects" do
357
      query = Query.new(:project => Project.find(1), :name => '_')
358
      users = query.available_filters["assigned_to_id"][:values]
359
      assert users.map{|u|u[1]}.include?("8")
360
    end
361
  end
354 362
end
test/unit/user_test.rb
219 219
    end
220 220
  end
221 221

  
222
  context "User#in_project_hierarchy_of" do
223
    should "return users of projets AND subprojects" do
224
      #Project(3) is child of Project(1)
225
      #User(8) is only member of Project(3)
226
      users = User.in_project_hierarchy_of(Project.find(1))
227
      assert users.map(&:id).include?(8)
228
    end
229
    
230
    should "not return users of subprojects not visible by current user" do
231
      #Project(5) is private child of Project(1)
232
      #User(1) is in Project(5), but not current user (Anonymous)
233
      users = User.in_project_hierarchy_of(Project.find(1))
234
      assert !users.map(&:id).include?(1)
235
    end
236
  end
237

  
222 238
  def test_roles_for_project
223 239
    # user with a role
224 240
    roles = @jsmith.roles_for_project(Project.find(1))
    (1-1/1)