Class: Relationship

Inherits:
ActiveRecord::Base show all
Defined in:
app/models/relationship.rb

Overview

This class models a relationship between two users.

For example, John is the brother of Sue.

who: John        relationship.user1    relationship.who
is:  Brother     relationship.is       relationship.name
of:  Sue         relationship.user2    relationship.of

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ActiveRecord::Base

#readonly?

Class Method Details

.add(params) ⇒ Object

Adding new relationships:

Relationship.add( who: john_user, is: :brother, of: :sue_user )

which is the same as:

Relationship.create( who: john_user, is: :brother, of: :sue_user )


56
57
58
# File 'app/models/relationship.rb', line 56

def self.add( params )
  self.create( params )
end

Instance Method Details

#isObject

John is the brother of Sue.

-------           is: brother


31
32
33
# File 'app/models/relationship.rb', line 31

def is
  self.name
end

#is=(name) ⇒ Object



34
35
36
# File 'app/models/relationship.rb', line 34

def is=( name )
  self.name = name
end

#ofObject

John is the brother of Sue.

---    of: Sue


41
42
43
# File 'app/models/relationship.rb', line 41

def of 
  self.user2
end

#of=(user) ⇒ Object



44
45
46
# File 'app/models/relationship.rb', line 44

def of=( user )
  self.user2 = user
end

#of_by_titleObject

Access method for the second user being given by his title.



71
72
73
# File 'app/models/relationship.rb', line 71

def of_by_title
  self.of.title if self.of
end

#of_by_title=(title) ⇒ Object



74
75
76
# File 'app/models/relationship.rb', line 74

def of_by_title=( title )
  self.of = User.find_by_title( title )
end

#whoObject

John is the brother of Sue. —- who: John



21
22
23
# File 'app/models/relationship.rb', line 21

def who
  self.user1
end

#who=(user) ⇒ Object



24
25
26
# File 'app/models/relationship.rb', line 24

def who=( user )
  self.user1 = user
end

#who_by_titleObject

Access method for the first user being given by his title.



62
63
64
# File 'app/models/relationship.rb', line 62

def who_by_title
  self.who.title if self.who
end

#who_by_title=(title) ⇒ Object



65
66
67
# File 'app/models/relationship.rb', line 65

def who_by_title=( title )
  self.who = User.find_by_title( title )
end