Skip to content
This repository has been archived by the owner on Apr 3, 2018. It is now read-only.

Commit

Permalink
Fixed ordering issue with adding delegated attributes to the Configur…
Browse files Browse the repository at this point in the history
…ation.
  • Loading branch information
ihoka committed Feb 19, 2011
1 parent ce581f9 commit 17f0a70
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 33 deletions.
19 changes: 10 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,32 @@ Changelog
=========

### 0.7.0
* (ihoka) Support for associating multiple FriendlyAttributes models with a single ActiveRecord model
* (ihoka) Documentation
* (ihoka,cristi) Support for associating multiple FriendlyAttributes models with a single ActiveRecord model
* (ihoka,cristi) FriendlyAttributes::Details renamed to FriendlyAttributes::Base
* (ihoka,cristi) Documentation

### 0.6.1
* (ihoka) Extended #changed? on the ActiveRecord model to indicate the record has been changed if the associated Friendly has changed.
* (ihoka,cristi) Extended #changed? on the ActiveRecord model to indicate the record has been changed if the associated Friendly has changed.

### 0.6.0
* (ihoka) Added #friendly_details_build_options method to the ActiveRecord model, allowing to specify default attributes when initially building the Friendly model.
* (ihoka,cristi) Added #friendly_details_build_options method to the ActiveRecord model, allowing to specify default attributes when initially building the Friendly model.

### 0.5.0

* (ihoka) Added configurable active_record_key to the FriendlyDetails model. active_record_key affects the name of the generated Friendly index table and the attribute in which the ActiveRecord model ID is stored. It defaults to :active_record_id.
* (ihoka,cristi) Added configurable active_record_key to the FriendlyDetails model. active_record_key affects the name of the generated Friendly index table and the attribute in which the ActiveRecord model ID is stored. It defaults to :active_record_id.

### 0.4.0

* (ihoka) Added #attributes method to FriendlyAttributes::Details base class.
* (ihoka,cristi) Added #attributes method to FriendlyAttributes::Details base class.

### 0.3.2

* (ihoka) Added description to spec matcher.
* (ihoka,cristi) Added description to spec matcher.

### 0.3.1

* (ihoka) added spec matchers
* (ihoka,cristi) added spec matchers

### 0.3.0

* (ihoka) Extended the DSL to allow passing a hash of options when defining friendly_attributes.
* (ihoka,cristi) Extended the DSL to allow passing a hash of options when defining friendly_attributes.
7 changes: 4 additions & 3 deletions lib/friendly_attributes/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ def initialize(active_record_model)

def add(delegator)
details_delegators << delegator
delegator.delegated_attributes.each do |name, _|
attributes[name] = delegator.friendly_model
end
end

def add_attribute(name, friendly_model)
attributes[name] = friendly_model
end

def model_for_attribute(attr)
Expand Down
2 changes: 2 additions & 0 deletions lib/friendly_attributes/details_delegator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def delegated_attribute(name, klass)
attribute name, klass
delegated_method(:"#{name}")
delegated_method(:"#{name}=")

active_record_model.friendly_attributes_configuration.add_attribute(name, friendly_model)
end

def delegated_method(name)
Expand Down
28 changes: 7 additions & 21 deletions spec/friendly_attributes/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,12 @@
configuration.add(delegator)
configuration.details_delegators.should == [delegator]
end

it "merges the delegator's attributes into the attributes hash" do
configuration.add(delegator)

configuration.attributes.should == {
:foo => UserDetails,
:bar => UserDetails
}

another_delegator = mock(FriendlyAttributes::DetailsDelegator,
:delegated_attributes => { :baz => Date },
:friendly_model => UserSecondDetails)

configuration.add(another_delegator)
configuration.attributes.should == {
:foo => UserDetails,
:bar => UserDetails,
:baz => UserSecondDetails
}
end

describe "#add_attribute" do
it "adds the attribute to the attributes collection" do
configuration.add_attribute(:foo, UserDetails)
configuration.attributes.should == { :foo => UserDetails }
end
end

Expand Down Expand Up @@ -66,9 +53,8 @@

describe "#model_for_attribute" do
it "returns the FriendlyAttributes model associated with the attribute" do
configuration.add_attribute(:foo, UserDetails)
configuration.model_for_attribute(:foo).should == UserDetails
configuration.model_for_attribute(:bar).should == UserDetails
configuration.model_for_attribute(:baz).should == UserSecondDetails
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/friendly_attributes/details_delegator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,9 @@
friendly_instance.should_receive(:some_attribute=).with(:value)
ar_instance.some_attribute = :value
end

it "adds the attribute to the configuration" do
ar_instance.friendly_attributes_configuration.attributes.should == { :some_attribute => friendly_model }
end
end
end
13 changes: 13 additions & 0 deletions spec/friendly_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@
end
end

describe "reading" do
let(:user) { User.create!(:name => "Stan", :email => "stan@example.com", :birth_year => 1984, :subscribed => true) }

it "through the attr_reader" do
user.name.should == "Stan"
end

it "through read_friendly_attribute" do
user.read_friendly_attribute(:name).should == "Stan"
user.read_friendly_attribute(:birth_year).should == 1984
end
end

describe "updating" do
context "with Friendly attributes" do
let(:user) { User.create(:name => "Stan", :email => "stan@example.com", :second_int => 200) }
Expand Down

0 comments on commit 17f0a70

Please sign in to comment.