2882-use-uniq-for-habtm.patch

Use uniq on the HABTM association instead of a db unique index - Eric Davis, 2009-05-18 18:41

Download (3.06 KB)

View differences:

app/models/project.rb
23 23
  has_many :members, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}"
24 24
  has_many :users, :through => :members
25 25
  has_many :enabled_modules, :dependent => :delete_all
26
  has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
26
  has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position", :uniq => true
27 27
  has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
28 28
  has_many :issue_changes, :through => :issues, :source => :journals
29 29
  has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
app/models/tracker.rb
30 30
    end
31 31
  end
32 32
  
33
  has_and_belongs_to_many :projects
33
  has_and_belongs_to_many :projects, :uniq => true
34 34
  has_and_belongs_to_many :custom_fields, :class_name => 'IssueCustomField', :join_table => "#{table_name_prefix}custom_fields_trackers#{table_name_suffix}", :association_foreign_key => 'custom_field_id'
35 35
  acts_as_list
36 36

  
db/migrate/20090518163007_remove_projects_trackers_unique_index.rb
1
class RemoveProjectsTrackersUniqueIndex < ActiveRecord::Migration
2
  def self.up
3
    remove_index :projects_trackers, :name => :projects_trackers_unique
4
  end
5

  
6
  def self.down
7
    add_index :projects_trackers, [:project_id, :tracker_id], :name => :projects_trackers_unique, :unique => true
8
  end
9
end
test/functional/projects_controller_test.rb
118 118
    assert_equal 'weblog', project.description 
119 119
    assert_equal true, project.is_public?
120 120
  end
121

  
122
  def test_post_add_with_trackers
123
    @request.session[:user_id] = 1
124
    trackers = [Tracker.find_by_name('Bug')]
125
    post(:add,
126
         :project => {
127
           :name => "blog", 
128
           :description => "weblog",
129
           :identifier => "blog",
130
           :is_public => 1,
131
           :custom_field_values => { '3' => 'Beta' },
132
           :tracker_ids => trackers.collect(&:id)
133
         },
134
         :enabled_modules => ["issue_tracking"])
135
    assert_redirected_to '/projects/blog/settings'
136
    
137
    project = Project.find_by_name('blog')
138
    assert_equal trackers, project.trackers
139
  end
121 140
  
122 141
  def test_post_add_by_non_admin
123 142
    @request.session[:user_id] = 2