Module: ActsAsRelatable::Relatable::InstanceMethods
- Defined in:
- lib/acts_as_relatable/relatable.rb
Instance Method Summary collapse
-
#destroy_relation_with(some_object) ⇒ Object
This method destroy the relationship between self (as a relator object) and some_object (as a related object).
-
#related_to?(some_object) ⇒ Boolean
This method returns true if self is already related with some_object.
-
#relateds(options = {}) ⇒ Object
This method returns relatable objects from a given Array of models Exemple : @category.relateds(:classes => [‘Place’, ‘Event’]) will return => [related_place1, related_place2], “Event” => [related_event1, related_event2].
-
#relates_to!(some_object, bothsided = true) ⇒ Object
This method creates relation between 2 objects.
-
#relation(some_object) ⇒ Object
This method returns the relationship between self and another_object, or nil.
Instance Method Details
#destroy_relation_with(some_object) ⇒ Object
This method destroy the relationship between self (as a relator object) and some_object (as a related object).
92 93 94 95 96 |
# File 'lib/acts_as_relatable/relatable.rb', line 92 def destroy_relation_with some_object relationships_to_destroy = [relation(some_object), some_object.relation(self)] relationships_to_destroy.each { |r| r.destroy if r } relationships.reload end |
#related_to?(some_object) ⇒ Boolean
This method returns true if self is already related with some_object
65 66 67 |
# File 'lib/acts_as_relatable/relatable.rb', line 65 def some_object !!relation(some_object) end |
#relateds(options = {}) ⇒ Object
This method returns relatable objects from a given Array of models Exemple : @category.relateds(:classes => [‘Place’, ‘Event’]) will return => [related_place1, related_place2], “Event” => [related_event1, related_event2]
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/acts_as_relatable/relatable.rb', line 80 def ( = {}) = {} classes = .try(:[], :classes) ? [:classes] : relatable_types classes.each do |c| pluralized_rel = c.to_s.underscore.pluralize relations = self.send("related_#{pluralized_rel}").limit([:limit] || 10) = .merge(pluralized_rel.to_sym => relations) if relations.any? end end |
#relates_to!(some_object, bothsided = true) ⇒ Object
This method creates relation between 2 objects. Returns the relationships in an array or false if the relation already exists. By default a bothsided relationship is created but this can be overiden by seting bothsided to false.
Example :
user = User.find 1
place = Place.find 1
user.relate_to! place
57 58 59 60 61 62 |
# File 'lib/acts_as_relatable/relatable.rb', line 57 def relates_to!(some_object, bothsided=true) return false if (self.(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 end |
#relation(some_object) ⇒ Object
This method returns the relationship between self and another_object, or nil
70 71 72 73 74 75 |
# File 'lib/acts_as_relatable/relatable.rb', line 70 def relation some_object ActsAsRelatable::Relationship.unscoped.where(:relator_id => self.id, :related_id => some_object.id, :relator_type => self.class.base_class.to_s, :related_type => some_object.class.base_class.to_s).first end |