Skip to content

Commit

Permalink
Merge pull request #3 from oggy/master
Browse files Browse the repository at this point in the history
Nest history class inside model class
  • Loading branch information
seejohnrun committed Sep 16, 2011
2 parents 5a85618 + bf25464 commit a8f563a
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 21 deletions.
7 changes: 5 additions & 2 deletions lib/track_history.rb
Expand Up @@ -43,8 +43,11 @@ def historical_class
def define_historical_model(base, model_name, table_name, track_reference, &block)

# figure out the model name
model_name ||= "#{base.name}History"
@klass_reference = Object.const_set(model_name, Class.new(ActiveRecord::Base))
model_name ||= "#{base.name}::History"
class_path = model_name.split(/::/)
inner_name = class_path.pop
outer = class_path.inject(Object) { |outer, inner_name| outer.const_get(inner_name) }
@klass_reference = outer.const_set(inner_name, Class.new(ActiveRecord::Base))
@klass_reference.send(:table_name=, table_name) unless table_name.nil?

unless @klass_reference.table_exists?
Expand Down
4 changes: 2 additions & 2 deletions spec/examples/alter_track_spec.rb
Expand Up @@ -20,13 +20,13 @@ class Thing < ActiveRecord::Base
# clean up each time
before(:each) do
Thing.destroy_all
ThingHistory.destroy_all
Thing::History.destroy_all
end

it 'should work with altered column names' do
thing = Thing.create(:name => 'john')
thing.update_attributes(:name => 'john2')
history = ThingHistory.first
history = Thing::History.first
history.modifications.should == ['name']
history.name_from.should == 'john'
history.name_to.should == 'john2'
Expand Down
4 changes: 2 additions & 2 deletions spec/examples/annotation_only_spec.rb
Expand Up @@ -21,15 +21,15 @@ def john; 'hi'; end
# clean up each time
before(:each) do
Beer.destroy_all
BeerHistory.destroy_all
Beer::History.destroy_all
end

it 'should not need a reference column in order to record histories' do
user = Beer.create(:name => 'john')
user.update_attributes(:name => 'john2')
user.respond_to?(:histories).should == false

history = BeerHistory.first
history = Beer::History.first
history.modifications.should == ['name']
history.name_before.should == 'john'
history.name_after.should == 'john2'
Expand Down
6 changes: 3 additions & 3 deletions spec/examples/annotation_option_spec.rb
Expand Up @@ -23,14 +23,14 @@ def something
# clean up each time
before(:each) do
Drink.destroy_all
DrinkHistory.destroy_all
Drink::History.destroy_all
end

it 'should be able to alias a field' do
drink = Drink.create(:name => 'john')
drink.update_attributes(:name => 'john2')
DrinkHistory.first.respond_to?(:something).should == false
DrinkHistory.first.special.should == 'note'
Drink::History.first.respond_to?(:something).should == false
Drink::History.first.special.should == 'note'
end

end
2 changes: 1 addition & 1 deletion spec/examples/basic_enum_spec.rb
Expand Up @@ -18,7 +18,7 @@ class BasicUser < ActiveRecord::Base
# clean up each time
before(:each) do
BasicUser.destroy_all
BasicUserHistory.destroy_all
BasicUser::History.destroy_all
end

it 'should record an action on create when there is an action field' do
Expand Down
2 changes: 1 addition & 1 deletion spec/examples/complex_user_spec.rb
Expand Up @@ -26,7 +26,7 @@ def to_s; "user: #{name}"; end
# clean up each time
before(:each) do
ComplexUser.destroy_all
ComplexUserHistory.destroy_all
ComplexUser::History.destroy_all
end

it 'should automatically annotate with a note on changes' do
Expand Down
2 changes: 1 addition & 1 deletion spec/examples/different_model_user_spec.rb
Expand Up @@ -39,7 +39,7 @@ class TableUser < ActiveRecord::Base
user = TableUser.create(:name => 'john')
user.update_attributes(:name => 'john2')
user.histories.size.should == 1
user.histories.first.should be_a(TableUserHistory) # shouldn't change
user.histories.first.should be_a(TableUser::History) # shouldn't change
end

end
Expand Down
8 changes: 4 additions & 4 deletions spec/examples/expand_history_spec.rb
Expand Up @@ -25,23 +25,23 @@ def instance_note
# clean up each time
before(:each) do
Door.destroy_all
DoorHistory.destroy_all
Door::History.destroy_all
end

it 'should be able to define class methods' do
DoorHistory.class_note.should == 'class_note'
Door::History.class_note.should == 'class_note'
end

it 'should be able to define instance methods' do
door = Door.create(:name => 'john')
door.update_attributes(:name => 'john2')
DoorHistory.first.instance_note.should == 'instance_note'
Door::History.first.instance_note.should == 'instance_note'
end

it 'should not have historical_fields as an instance method' do
door = Door.create(:name => 'john')
door.update_attributes(:name => 'john2')
DoorHistory.first.should_not respond_to(:historical_fields)
Door::History.first.should_not respond_to(:historical_fields)
end

end
Expand Down
4 changes: 2 additions & 2 deletions spec/examples/half_save_spec.rb
Expand Up @@ -34,8 +34,8 @@ def note
before(:each) do
SlugUser.destroy_all
SluggedUser.destroy_all
SlugUserHistory.destroy_all
SluggedUserHistory.destroy_all
SlugUser::History.destroy_all
SluggedUser::History.destroy_all
end

# use case for slugs
Expand Down
6 changes: 3 additions & 3 deletions spec/examples/user_spec.rb
Expand Up @@ -19,7 +19,7 @@ class User < ActiveRecord::Base
# clean up each time
before(:each) do
User.destroy_all
UserHistory.destroy_all
User::History.destroy_all
end

it 'should be able to get the user from :user' do
Expand Down Expand Up @@ -103,8 +103,8 @@ class User < ActiveRecord::Base
user.histories.size.should == 1

User.destroy_all
UserHistory.count.should == 1
UserHistory.first.user_id.should == user_id
User::History.count.should == 1
User::History.first.user_id.should == user_id
end

end

0 comments on commit a8f563a

Please sign in to comment.