Permalink
Browse files

Merge pull request #2 from culturecode/master

Added the ability to specify a relationship strength when creating relationships, also simplified indices
  • Loading branch information...
2 parents 2050f4f + 85e9ec4 commit 748ad54dda07b8259f16386a268086ccd6a5e802 @fmalamitsas committed Oct 5, 2011
View
10 lib/acts_as_relatable/relatable.rb
@@ -54,11 +54,11 @@ module InstanceMethods
# user = User.find 1
# place = Place.find 1
# user.relate_to! place
- def relates_to!(some_object, bothsided=true)
+ def relates_to!(some_object, bothsided=true, strength=nil)
return false if (self.related_to?(some_object) || self.eql?(some_object))
- ActsAsRelatable::Relationship.unscoped.create(:related => some_object, :relator => self)
- ActsAsRelatable::Relationship.unscoped.create(:related => self, :relator => some_object) if bothsided == true
+ ActsAsRelatable::Relationship.unscoped.create(:related => some_object, :relator => self, :strength => strength)
+ ActsAsRelatable::Relationship.unscoped.create(:related => self, :relator => some_object, :strength => strength) if bothsided == true
end
# This method returns true if self is already related with some_object
@@ -77,12 +77,14 @@ def relation some_object
# This method returns relatable objects from a given Array of models
# Exemple : @category.relateds(:classes => ['Place', 'Event']) will return
# {"Place" => [related_place1, related_place2], "Event" => [related_event1, related_event2]}
+ # :strength => 'some_strength' will return all relateds with strength = 'some_strength'
def relateds(options = {})
relateds = {}
- classes = options.try(:[], :classes) ? options[:classes] : relatable_types
+ classes = options[:classes] || relatable_types
classes.each do |c|
pluralized_rel = c.to_s.underscore.pluralize
relations = self.send("related_#{pluralized_rel}").limit(options[:limit] || 10)
+ relations = relations.where(:strength => options[:strength]) if options[:strength]
relateds = relateds.merge(pluralized_rel.to_sym => relations) if relations.any?
end
relateds
View
4 lib/acts_as_relatable/relationship.rb
@@ -10,8 +10,8 @@ class Relationship < ::ActiveRecord::Base
validate :self_relation
# This method relates two object together
- def self.make_between(relator, related, bothsided=true)
- relator.relates_to!(related, bothsided)
+ def self.make_between(relator, related, bothsided=true, strength=nil)
+ relator.relates_to!(related, bothsided, strength)
end
protected
View
11 lib/generators/acts_as_relatable/templates/migration.rb
@@ -6,23 +6,16 @@ def self.up
t.string :relator_type
t.integer :related_id
t.string :related_type
+ t.string :strength
t.timestamps
end
# Relationship indexes
- add_index :relationships, ["related_id"], :name => "index_relationships_on_related_id"
- add_index :relationships, ["related_type", "related_id"], :name => "poly_relationships_related"
- add_index :relationships, ["relator_id"], :name => "index_relationships_on_relator_id"
- add_index :relationships, ["relator_type", "relator_id"], :name => "poly_relationships_relator"
+ add_index :relationships, [:relator_id, :relator_type, :related_type]
end
def self.down
drop_table :relationships
-
- remove_index :relationships, :name => "index_relationships_on_related_id"
- remove_index :relationships, :name => "poly_relationships_related"
- remove_index :relationships, :name => "index_relationships_on_relator_id"
- remove_index :relationships, :name => "poly_relationships_relator"
end
end

0 comments on commit 748ad54

Please sign in to comment.