Project

General

Profile

Patch #7115 » 7115-improve-project-methods-to-enable-disable-modules.diff

Jean-Baptiste Barth, 2010-12-16 18:39

View differences:

app/models/project.rb
510 510
    if module_names && module_names.is_a?(Array)
511 511
      module_names = module_names.collect(&:to_s).reject(&:blank?)
512 512
      # remove disabled modules
513
      enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)}
513
      enabled_modules.each {|mod| disable_module!(mod) unless module_names.include?(mod.name)}
514 514
      # add new modules
515
      module_names.reject {|name| module_enabled?(name)}.each {|name| enabled_modules << EnabledModule.new(:name => name)}
515
      module_names.each {|name| enable_module!(name)}
516 516
    else
517 517
      enabled_modules.clear
518 518
    end
......
522 522
  def enabled_module_names
523 523
    enabled_modules.collect(&:name)
524 524
  end
525

  
526
  # Enable a specific module
527
  #
528
  # Examples:
529
  #   project.enable_module!(:issue_tracking)
530
  #   project.enable_module!("issue_tracking")
531
  def enable_module!(name)
532
    enabled_modules << EnabledModule.new(:name => name.to_s) unless module_enabled?(name)
533
  end
534

  
535
  # Disable a module if it exists
536
  #
537
  # Examples:
538
  #   project.disable_module!(:issue_tracking)
539
  #   project.disable_module!("issue_tracking")
540
  #   project.disable_module!(project.enabled_modules.first)
541
  def disable_module!(target)
542
    target = enabled_modules.detect{|mod| target.to_s == mod.name} if target.respond_to?(:to_sym)
543
    target.destroy unless target.blank?
544
  end
525 545
  
526 546
  safe_attributes 'name',
527 547
    'description',
test/unit/project_test.rb
553 553
    assert_nil Project.next_identifier
554 554
  end
555 555
  
556
  context "enabled_modules" do
557
    setup do
558
      @project = Project.find(1)
559
    end
556 560

  
557
  def test_enabled_module_names_should_not_recreate_enabled_modules
558
    project = Project.find(1)
559
    # Remove one module
560
    modules = project.enabled_modules.slice(0..-2)
561
    assert modules.any?
562
    assert_difference 'EnabledModule.count', -1 do
563
      project.enabled_module_names = modules.collect(&:name)
561
    should "define module by names and preserve ids" do
562
      # Remove one module
563
      modules = @project.enabled_modules.slice(0..-2)
564
      assert modules.any?
565
      assert_difference 'EnabledModule.count', -1 do
566
        @project.enabled_module_names = modules.collect(&:name)
567
      end
568
      @project.reload
569
      # Ids should be preserved
570
      assert_equal @project.enabled_module_ids.sort, modules.collect(&:id).sort
571
    end
572

  
573
    should "enable a module" do
574
      @project.enabled_module_names = []
575
      @project.reload
576
      assert_equal [], @project.enabled_module_names
577
      #with string
578
      @project.enable_module!("issue_tracking")
579
      assert_equal ["issue_tracking"], @project.enabled_module_names
580
      #with symbol
581
      @project.enable_module!(:gantt)
582
      assert_equal ["issue_tracking", "gantt"], @project.enabled_module_names
583
      #don't add a module twice
584
      @project.enable_module!("issue_tracking")
585
      assert_equal ["issue_tracking", "gantt"], @project.enabled_module_names
586
    end
587

  
588
    should "disable a module" do
589
      #with string
590
      assert @project.enabled_module_names.include?("issue_tracking")
591
      @project.disable_module!("issue_tracking")
592
      assert ! @project.reload.enabled_module_names.include?("issue_tracking")
593
      #with symbol
594
      assert @project.enabled_module_names.include?("gantt")
595
      @project.disable_module!(:gantt)
596
      assert ! @project.reload.enabled_module_names.include?("gantt")
597
      #with EnabledModule object
598
      first_module = @project.enabled_modules.first
599
      @project.disable_module!(first_module)
600
      assert ! @project.reload.enabled_module_names.include?(first_module.name)
564 601
    end
565
    project.reload
566
    # Ids should be preserved
567
    assert_equal project.enabled_module_ids.sort, modules.collect(&:id).sort
568 602
  end
569 603

  
570 604
  def test_copy_from_existing_project
(1-1/2)