Skip to content

Commit

Permalink
Added ::delegate_has_one support
Browse files Browse the repository at this point in the history
  • Loading branch information
pahanix committed Jan 9, 2010
1 parent 431e8d6 commit 50322f5
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/delegate_belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
# u.changed? # => false
# u.firstname = 'Bobby'
# u.changed? # => true
#
# Todo - add has_one support. fairly straightforward addition
##
module DelegateBelongsTo

module ClassMethods
Expand All @@ -28,6 +25,13 @@ def delegate_belongs_to(association, *attributes)

delegates_attributes_to association, *attributes
end

def delegate_has_one(association, *attributes)
options = attributes.extract_options!
has_one association, options unless reflect_on_association(association)

delegates_attributes_to association, *attributes
end

# belongs_to :contact
# delegates_attributes_to :contact
Expand Down
2 changes: 2 additions & 0 deletions spec/app_root/app/models/profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Profile < ActiveRecord::Base
end
2 changes: 2 additions & 0 deletions spec/app_root/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class User < ActiveRecord::Base
end
4 changes: 4 additions & 0 deletions spec/app_root/db/migrate/01_create_users.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :username, :password
t.timestamps
end
create_table :user_defaults do |t|
t.string :username, :password
t.timestamps
Expand Down
12 changes: 12 additions & 0 deletions spec/app_root/db/migrate/03_create_profiles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateProfiles < ActiveRecord::Migration
def self.up
create_table :profiles do |t|
t.string :about, :hobby
t.integer :user_id
end
end

def self.down
drop_table :profiles
end
end
85 changes: 85 additions & 0 deletions spec/model/has_one_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
require File.join(File.dirname(__FILE__), '..', 'spec_helper')

describe DelegateBelongsTo, 'with has one delegation' do

before :all do
@fields = [:about, :hobby]
User.delegate_has_one :profile
end

before :each do
@user = User.new
end

it 'should declare the association' do
User.reflect_on_association(:profile).should_not be_nil
end

it 'creates reader methods for the columns' do
@fields.each do |col|
@user.should respond_to(col)
end
end

it 'creates writer methods for the columns' do
@fields.each do |col|
@user.should respond_to("#{col}=")
end
end

describe "reading from no contact" do
it "should return nil as firstname" do
@user.about.should be_nil
end

it "should return nil as lastname" do
@user.hobby.should be_nil
end
end


describe "reading from existing contact" do
before :each do
@user.build_profile
@user.profile.about = "I'm John"
@user.profile.hobby = "Basketball"
end

it "should read about" do
@user.about.should == "I'm John"
end

it "should read hobby" do
@user.hobby.should == "Basketball"
end
end

describe "assigning value to delegators" do
it "should initialize association" do
@user.profile.should be_nil
@user.about = "I'm John"
@user.profile.should_not be_nil
@user.about.should == "I'm John"
end

it "should NOT initialize association second time" do
@user.about = "I'm John"
profile_object_id = @user.profile.object_id
@user.hobby = "Basketball"
@user.profile.object_id.should == profile_object_id
@user.about.should == "I'm John"
end

describe "#save" do
it "should clear changed_attribute in dirty assosiations" do
@user.about = "I'm John"
@user.send(:changed_attributes).size.should == 1
@user.profile.send(:changed_attributes).size.should == 1
@user.save
@user.send(:changed_attributes).size.should == 0
@user.profile.send(:changed_attributes).size.should == 0
end
end

end
end

0 comments on commit 50322f5

Please sign in to comment.