Skip to content

Commit

Permalink
Merge pull request #73 from zertico/association_aliases
Browse files Browse the repository at this point in the history
Added aliases options for associations
  • Loading branch information
geemus committed Aug 26, 2014
2 parents 740b276 + c2b1039 commit 3e2a4f1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
12 changes: 10 additions & 2 deletions lib/fog/core/associations/default.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
module Fog
module Associations
class Default
attr_reader :model, :name
attr_reader :model, :name, :aliases

def initialize(model, name, collection_name)
def initialize(model, name, collection_name, options)
@model = model
@name = name
model.associations[name] = collection_name
@aliases = options.fetch(:aliases, [])
create_setter
create_getter
create_aliases
end

def create_aliases
Array(aliases).each do |alias_name|
model.aliases[alias_name] = name
end
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions lib/fog/core/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ def attribute(name, options = {})
Fog::Attributes::const_get(type).new(self, name, options)
end

def has_one(name, collection_name)
Fog::Associations::OneModel.new(self, name, collection_name)
def has_one(name, collection_name, options = {})
Fog::Associations::OneModel.new(self, name, collection_name, options)
end

def has_many(name, collection_name)
Fog::Associations::ManyModels.new(self, name, collection_name)
def has_many(name, collection_name, options = {})
Fog::Associations::ManyModels.new(self, name, collection_name, options)
end

def has_one_identity(name, collection_name)
Fog::Associations::OneIdentity.new(self, name, collection_name)
def has_one_identity(name, collection_name, options = {})
Fog::Associations::OneIdentity.new(self, name, collection_name, options)
end

def has_many_identities(name, collection_name)
Fog::Associations::ManyIdentities.new(self, name, collection_name)
def has_many_identities(name, collection_name, options = {})
Fog::Associations::ManyIdentities.new(self, name, collection_name, options)
end

def identity(name, options = {})
Expand Down
14 changes: 12 additions & 2 deletions spec/fog_attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class FogAttributeTestModel < Fog::Model
attribute :default, :default => 'default_value', :aliases => :some_name
attribute :another_default, :default => false

has_one :one_object, :single_associations
has_one :one_object, :single_associations, :aliases => :single
has_many :many_objects, :multiple_associations
has_one_identity :one_identity, :single_associations
has_many_identities :many_identities, :multiple_associations
has_many_identities :many_identities, :multiple_associations, :aliases => :multiple

def service
Service.new
Expand Down Expand Up @@ -268,6 +268,11 @@ class FogMultipleAssociationsModel < Fog::Model
model.one_object = FogSingleAssociationModel.new(:id => '123')
assert_equal model.one_object.attributes, { :id => '123' }
end

it "should create an alias to single" do
model.merge_attributes(:single => FogSingleAssociationModel.new(:id => '123'))
assert_equal model.one_object.attributes, { :id => '123' }
end
end

describe ".has_one_identity" do
Expand Down Expand Up @@ -337,6 +342,11 @@ class FogMultipleAssociationsModel < Fog::Model
assert_equal model.many_identities.first.attributes, { :id => '456' }
end
end

it "should create an alias to multiple" do
model.merge_attributes(:multiple => [ '456' ])
assert_equal model.many_identities.first.attributes, { :id => '456' }
end
end

describe "#all_attributes" do
Expand Down

0 comments on commit 3e2a4f1

Please sign in to comment.