Skip to content

Commit

Permalink
Merging from dev branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Winton Welsh committed Oct 31, 2008
1 parent fbf1614 commit 8683492
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/acts_as_relationable.rb
Expand Up @@ -23,8 +23,6 @@ def acts_as_relationable(*types)
table = options[:table]
fields = options[:fields] || []
fields = [ fields ] unless fields.respond_to?(:flatten)

before_save :save_relationship_fields

has_many :parent_relationships, :class_name => 'Relationship', :as => :child
has_many :child_relationships, :class_name => 'Relationship', :as => :parent
Expand Down Expand Up @@ -57,6 +55,7 @@ def acts_as_relationable(*types)
end

self.class_eval do
# Records reader
define_method type do |*args|
if (read_attribute(:type) || self.class.to_s) < (args.empty? ? type.classify : args[0].to_s)
eval "self.child_#{type}"
Expand All @@ -68,29 +67,36 @@ def acts_as_relationable(*types)
end

fields.each do |field|
# Relationship field reader
define_method field.to_s do
read_attribute(field) || nil
end

# Relationship field writer
self.class_eval do
define_method field.to_s + '=' do |value|
modified = read_attribute(:modified_relationship_fields) || []
modified << field
write_attribute :modified_relationship_fields, modified.uniq
write_attribute field, value
end
define_method field.to_s do
read_attribute(field) || nil
end
end
end
end

include ActsAsRelationable::InstanceMethods
extend ActsAsRelationable::SingletonMethods
unless self.respond_to?(:save_relationship_fields)
include ActsAsRelationable::InstanceMethods
before_save :save_relationship_fields
end
extend ActsAsRelationable::SingletonMethods
end
end

module SingletonMethods
end

module InstanceMethods
# Before save
def save_relationship_fields
return unless read_attribute(:relationship_id) && read_attribute(:modified_relationship_fields)
r = Relationship.find self.relationship_id
Expand Down

0 comments on commit 8683492

Please sign in to comment.