Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Renamed 'association' to 'associated' as Rails 3.1 introduces conflicting 'association' method.
  • Loading branch information
Eugene Diachkin committed Jun 5, 2011
1 parent aab76bc commit ba013fb
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/acts_as_audited/audit.rb
Expand Up @@ -12,7 +12,7 @@
class Audit < ActiveRecord::Base
belongs_to :auditable, :polymorphic => true
belongs_to :user, :polymorphic => true
belongs_to :association, :polymorphic => true
belongs_to :associated, :polymorphic => true

before_create :set_version_number, :set_audit_user

Expand Down
4 changes: 2 additions & 2 deletions lib/acts_as_audited/auditor.rb
Expand Up @@ -93,7 +93,7 @@ def acts_as_audited(options = {})
end

def has_associated_audits
has_many :associated_audits, :as => :association, :class_name => "Audit"
has_many :associated_audits, :as => :associated, :class_name => "Audit"
end

end
Expand Down Expand Up @@ -210,7 +210,7 @@ def audit_destroy
end

def write_audit(attrs)
attrs[:association] = self.send(audit_associated_with) unless audit_associated_with.nil?
attrs[:associated] = self.send(audit_associated_with) unless audit_associated_with.nil?
self.audit_comment = nil
self.audits.create attrs if auditing_enabled
end
Expand Down
6 changes: 3 additions & 3 deletions lib/generators/acts_as_audited/templates/install.rb
Expand Up @@ -3,8 +3,8 @@ def self.up
create_table :audits, :force => true do |t|
t.column :auditable_id, :integer
t.column :auditable_type, :string
t.column :association_id, :integer
t.column :association_type, :string
t.column :associated_id, :integer
t.column :associated_type, :string
t.column :user_id, :integer
t.column :user_type, :string
t.column :username, :string
Expand All @@ -17,7 +17,7 @@ def self.up
end

add_index :audits, [:auditable_id, :auditable_type], :name => 'auditable_index'
add_index :audits, [:association_id, :association_type], :name => 'association_index'
add_index :audits, [:associated_id, :associated_type], :name => 'associated_index'
add_index :audits, [:user_id, :user_type], :name => 'user_index'
add_index :audits, :created_at
end
Expand Down
@@ -0,0 +1,23 @@
class <%= migration_class_name %> < ActiveRecord::Migration
def self.up
if index_exists? :audits, [:association_id, :association_type], :name => 'association_index'
remove_index :audits, :name => 'association_index'
end

rename_column :audits, :association_id, :associated_id
rename_column :audits, :association_type, :associated_type

add_index :audits, [:associated_id, :associated_type], :name => 'associated_index'
end

def self.down
if index_exists? :audits, [:associated_id, :associated_type], :name => 'associated_index'
remove_index :audits, :name => 'associated_index'
end

rename_column :audits, :associated_type, :association_type
rename_column :audits, :associated_id, :association_id

add_index :audits, [:association_id, :association_type], :name => 'association_index'
end
end
8 changes: 7 additions & 1 deletion lib/generators/acts_as_audited/upgrade_generator.rb
Expand Up @@ -48,9 +48,15 @@ def migrations_to_be_applied
if columns.include?('auditable_parent_id')
yield :rename_parent_to_association
else
yield :add_association_to_audits
unless columns.include?( 'associated_id' )
yield :add_association_to_audits
end
end
end

if columns.include?( 'association_id' )
yield :rename_association_to_associated
end
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/acts_as_audited_spec.rb
Expand Up @@ -175,17 +175,17 @@
let(:owned_company) { OwnedCompany.create!(:name => 'The auditors', :owner => owner) }

it "should record the associated object on create" do
owned_company.audits.first.association.should == owner
owned_company.audits.first.associated.should == owner
end

it "should store the associated object on update" do
owned_company.update_attribute(:name, 'The Auditors')
owned_company.audits.last.association.should == owner
owned_company.audits.last.associated.should == owner
end

it "should store the associated object on destroy" do
owned_company.destroy
owned_company.audits.last.association.should == owner
owned_company.audits.last.associated.should == owner
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/db/schema.rb
Expand Up @@ -27,8 +27,8 @@
create_table :audits, :force => true do |t|
t.column :auditable_id, :integer
t.column :auditable_type, :string
t.column :association_id, :integer
t.column :association_type, :string
t.column :associated_id, :integer
t.column :associated_type, :string
t.column :user_id, :integer
t.column :user_type, :string
t.column :username, :string
Expand All @@ -41,7 +41,7 @@
end

add_index :audits, [:auditable_id, :auditable_type], :name => 'auditable_index'
add_index :audits, [:association_id, :association_type], :name => 'association_index'
add_index :audits, [:associated_id, :associated_type], :name => 'associated_index'
add_index :audits, [:user_id, :user_type], :name => 'user_index'
add_index :audits, :created_at
end
18 changes: 18 additions & 0 deletions test/db/version_5.rb
@@ -0,0 +1,18 @@
ActiveRecord::Schema.define do
create_table :audits, :force => true do |t|
t.column :auditable_id, :integer
t.column :auditable_type, :string
t.column :user_id, :integer
t.column :user_type, :string
t.column :username, :string
t.column :action, :string
t.column :audited_changes, :text
t.column :version, :integer, :default => 0
t.column :comment, :string
t.column :created_at, :datetime
t.column :remote_address, :string
t.column :association_id, :integer
t.column :association_type, :string
end
end

13 changes: 12 additions & 1 deletion test/upgrade_generator_test.rb
Expand Up @@ -41,7 +41,7 @@ class UpgradeGeneratorTest < Rails::Generators::TestCase
end
end

test "should add 'associated_id' and 'associated_type' to audits table" do
test "should add 'association_id' and 'association_type' to audits table" do
load_schema 4

run_generator %w(upgrade)
Expand All @@ -51,4 +51,15 @@ class UpgradeGeneratorTest < Rails::Generators::TestCase
assert_match /add_column :audits, :association_type, :string/, content
end
end

test "should rename 'association_id' to 'associated_id' and 'association_type' to 'associated_type'" do
load_schema 5

run_generator %w(upgrade)

assert_migration "db/migrate/rename_association_to_associated.rb" do |content|
assert_match /rename_column :audits, :association_id, :associated_id/, content
assert_match /rename_column :audits, :association_type, :associated_type/, content
end
end
end

0 comments on commit ba013fb

Please sign in to comment.