Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
SocialStream::Models::Subject
Defined in:
app/models/user.rb

Overview

Every social network must have users, and a social network builder couldn’t be the exception.

Social Stream uses the awesome gem github.com/plataformatec/devise for managing authentication

Almost all the logic of the interaction between User and the rest of classes in Social Stream is done through Actor. The glue between User and Actor is in SocialStream::Models::Subject

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SocialStream::Models::Subject

#to_param

Class Method Details

.find_for_authentication(conditions) ⇒ Object

presence ID and login



85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/user.rb', line 85

def find_for_authentication(conditions)
  if (  = conditions[:email] ).present?
    if  =~ /@/
      find_by_email()
    else
      find_by_slug()
    end
  else
    super
  end
end

.find_or_create_for_facebook_oauth(hash, signed_in_resource = nil) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/user.rb', line 126

def find_or_create_for_facebook_oauth(hash, signed_in_resource = nil)
  puts hash.inspect
  auth = Authentication.find_by_provider_and_uid(hash["provider"], hash["uid"])
  user = User.find_by_email(hash["info"]["email"])

  if user.nil?
    user = User.create!(:name => hash["info"]["name"], :email => hash["info"]["email"], :password => Devise.friendly_token[0,20])
  end

  if auth.nil?
    auth = Authentication.create!(:user_id => user.id, :uid =>hash["uid"], :provider => hash["provider"])
  end

  user
end

.find_or_create_for_linkedin_oauth(hash, signed_in_resource = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'app/models/user.rb', line 142

def find_or_create_for_linkedin_oauth(hash,signed_in_resource=nil)
  auth = Authentication.find_by_uid_and_provider(hash["uid"],hash["provider"])
  if auth==nil
    user = User.create!(:name => hash["info"]["name"], :email => '[email protected]', :password => Devise.friendly_token[0,20])
    auth = Authentication.create!(:user_id => user.id, :uid =>hash["uid"], :provider => hash["provider"])
    user
  else
    user = User.find_by_id(auth.user_id)
    user
  end
end

.find_or_initialize_with_error_by_email(value, error) ⇒ Object

Overwrite devise default method to support finding with actor.email



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/models/user.rb', line 106

def find_or_initialize_with_error_by_email(value, error)
  if value.present?
    record = find_by_email(value)
  end
  
  unless record
    record = new
    
    if value.present?
      record.email = value
    else
      error = :blank
    end
    
    record.errors.add(:email, error)
  end
  
  record
end

.find_or_initialize_with_errors(required_attributes, attributes, error = :invalid) ⇒ Object



97
98
99
100
101
102
103
# File 'app/models/user.rb', line 97

def find_or_initialize_with_errors(required_attributes, attributes, error=:invalid)
  if required_attributes == [:email]
    find_or_initialize_with_error_by_email(attributes[:email], error)
  else
    super
  end
end

Instance Method Details

#recent_groupsObject



37
38
39
40
41
42
# File 'app/models/user.rb', line 37

def recent_groups
  contact_subjects(:type => :group, :direction => :sent) do |q|
    q.select("contacts.created_at").
      merge(Contact.recent)
  end
end

#representedObject

Subjects this user can acts as



45
46
47
48
49
50
51
52
53
# File 'app/models/user.rb', line 45

def represented
  candidates = contact_actors(:direction => :sent).map(&:id)

  contact_subjects(:direction => :received) do |q|
    q.joins(:sent_ties => { :relation => :permissions }).
      merge(Permission.represent).
      where(:id => candidates)
  end
end