Class: SocialFramework::User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/social_framework/user.rb

Overview

User class based in devise, represents the user entity to authenticate in system

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loginObject

Get login if not blank, username or email Used to autenticate in system Returns login, username or email



10
11
12
# File 'app/models/social_framework/user.rb', line 10

def 
  @login
end

Class Method Details

.find_for_database_authentication(warden_conditions) ⇒ Object

In authentication get user with key passed.

Params:
warden_conditions

Hash with login, email or username to authenticate user, if login search users by username or email

Returns User in case user found or nil if not



43
44
45
46
47
48
49
50
51
# File 'app/models/social_framework/user.rb', line 43

def self.find_for_database_authentication(warden_conditions)
  conditions = warden_conditions.dup
 
  if  = conditions.delete(:login)
    where(conditions.to_h).where(["username = :value OR email = :value", { :value =>  }]).first
  elsif conditions.has_key?(:username) || conditions.has_key?(:email)
    where(conditions.to_h).first
  end
end

Instance Method Details

#confirm_relationship(user, label) ⇒ Object

Confirm relationship

Params:
user

User to confirm

label

String to confirm

Returns true if relationship confirmed or false if no



99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/models/social_framework/user.rb', line 99

def confirm_relationship(user, label)
  return false if user.nil? or user == self

  edge = self.edges.select { |edge| edge.origin == user and edge.label == label }.first

  unless edge.nil? 
    edge.active = true
    return edge.save
  end

  return false
end

#create_relationship(destiny, label, bidirectional = true, active = false) ⇒ Object

Create relationship beteween users

Params:
destiny

User relationship destiny

label

String relationship type

bidirectional

Boolean define relationship is bidirectional or not

active

Boolean define relationship like active or inactive

Returns true if relationship created or false if no



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/social_framework/user.rb', line 60

def create_relationship(destiny, label, bidirectional=true, active=false)
  return false if destiny.nil? or destiny == self
  edge_class = ModelFabric.get_class(SocialFramework.edge_class)

  edge = edge_class.where(["(origin_id = :origin_id AND destiny_id = :destiny_id OR 
    destiny_id = :origin_id AND origin_id = :destiny_id) AND label = :label",
    { origin_id: self.id, destiny_id: destiny.id, label: label }]).first

  if edge.nil?
    edge = edge_class.create origin: self, destiny: destiny, active: active,
      bidirectional: bidirectional, label: label

    return (not edge.nil?)
  end

  return false
end

#create_route(title, distance, locations, mode_of_travel = "driving", accepted_deviation = 0) ⇒ Object

Add a new route to user

Params:
title

String title of route

+distance

Integer route size

+locations

Array of hash with the locations

+mode_of_travel

String mode of travel, can be: driving, bicycling, walking, transit

+accepted_deviation

Integer maximum accepted deviation to route

Returns Route created or nil if break



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'app/models/social_framework/user.rb', line 168

def create_route(title, distance, locations, mode_of_travel = "driving", accepted_deviation = 0)
  begin
    route_class = ModelFabric.get_class(SocialFramework.route_class)
    location_class = ModelFabric.get_class(SocialFramework.location_class)

    return unless mode_of_travel == "driving" or mode_of_travel == "bicycling" or
                  mode_of_travel == "walking" or mode_of_travel == "transit"
    route = route_class.new title: title, distance: distance,
                            mode_of_travel: mode_of_travel, accepted_deviation: accepted_deviation
    route.users << self
    route.save

    locations.each do |location|
      new_location = location_class.create route: route, latitude: location[:latitude],
          longitude: location[:longitude]
    end
    return route
  rescue
    route.destroy unless route.nil?
    Rails.logger.warn "Couldn't create route"
    return
  end
end

#edgesObject

Get all related edges with origin or destiny equal self Returns Related edges with self



19
20
21
22
23
24
# File 'app/models/social_framework/user.rb', line 19

def edges
  edge_class = ModelFabric.get_class(SocialFramework.edge_class)
  edge_class.where(["(origin_id = :id AND bidirectional = :not_bidirectional) OR
    (bidirectional = :bidirectional AND (origin_id = :id OR destiny_id = :id))",
    { id: id, bidirectional: true, not_bidirectional: false }])
end

#graph(graph_strategy = NetworkHelper::GraphStrategyDefault, elements_factory = ElementsFactoryDefault) ⇒ Object

Get my intance graph Returns Graph instance



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

def graph(graph_strategy = NetworkHelper::GraphStrategyDefault, elements_factory = ElementsFactoryDefault)
  graph_context = NetworkHelper::GraphContext.new(self.id, graph_strategy, elements_factory)

  if graph_context.graph.network.empty?
    graph_context.graph.build(self)
  end

  return graph_context.graph
end

#relationships(label = "all", status = true, created_by = "any") ⇒ Object

Get all users with specific relationship

Params:
label

String to search

+status

Boolean to get active or inactive edges

+created_by

String represent type relationships created by self or not. Pass self, any or other

Returns Array with users found



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/models/social_framework/user.rb', line 118

def relationships(label = "all", status = true, created_by = "any")
  edges = self.edges.select do |edge|
    creation_condiction = true

    if created_by == "self"
      creation_condiction = edge.origin == self
    elsif created_by == "other"
      creation_condiction = edge.destiny == self
    end
    
    (edge.label == label or label == "all") and edge.active == status and creation_condiction
  end

  users = Set.new

  edges.each do |edge|
    users << (edge.origin != self ? edge.origin : edge.destiny)
  end

  return users
end

#remove_relationship(destiny, label) ⇒ Object

Remove relationship beteween users

Params:
destiny

User relationship destiny

label

String relationship type

Returns Edge destroyed between the users



83
84
85
86
87
88
89
90
91
92
# File 'app/models/social_framework/user.rb', line 83

def remove_relationship(destiny, label)
  return if destiny.nil? or destiny == self
  edge_class = ModelFabric.get_class(SocialFramework.edge_class)

  edge = edge_class.where(["(origin_id = :origin_id AND destiny_id = :destiny_id OR 
    destiny_id = :origin_id AND origin_id = :destiny_id) AND label = :label",
    { origin_id: self.id, destiny_id: destiny.id, label: label }]).first

  self.edges.destroy(edge.id) unless edge.nil?
end

#scheduleObject

Create user schedule if not exists Return user schedule



154
155
156
157
158
# File 'app/models/social_framework/user.rb', line 154

def schedule
  return if self.id.nil?
  schedule_class = ModelFabric.get_class(SocialFramework.schedule_class)
  schedule_class.find_or_create_by(user: self)
end