Class: SocialFramework::Schedule

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

Overview

Class that is the schedule that a user has

Instance Method Summary collapse

Instance Method Details

#confirm_event(event) ⇒ Object

Confirm an event to schedule

Params:
event

Event to confirm

Returns false if no exist a invitation or has no disponibility or true if could confirm event



48
49
50
51
52
53
54
55
# File 'app/models/social_framework/schedule.rb', line 48

def confirm_event(event)
  participant_event = get_participant_event(event)
  return false if participant_event.nil? or not events_in_period(event.start, event.finish).empty?

  relation_user_route(event)
  participant_event.confirmed = true
  participant_event.save
end

#create_event(title, start, duration = nil, description = "", particular = false) ⇒ Object

Create an event to user this schedule

Params:
title

String event title

start

DateTime date and hour to start event

duration

ActiveSupport::Duration of the event, if nil is used until end of start day

description

String event description, default is “”

particular

Boolean set event as private or not, default is false

Returns event created or nil in error case



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/social_framework/schedule.rb', line 16

def create_event(title, start, duration = nil, description = "", particular = false)
  finish = set_finish_date(start, duration)
  return if finish.nil? or not events_in_period(start, finish).empty?

  event_class = ModelFabric.get_class(SocialFramework.event_class)
  event = event_class.create(title: title, start: start, finish: finish,
      description: description, particular: particular)

  unless event.nil?
    participant_event_class.create(event: event, schedule: self, confirmed: true, role: "creator")
  end

  return event
end

#enter_in_event(event) ⇒ Object

Enter in an public event

Params:
event

Event to enter

Returns ParticipantEvent created or nil if that event is particular or already exist events in that period



35
36
37
38
39
40
41
42
# File 'app/models/social_framework/schedule.rb', line 35

def enter_in_event(event)
  return if event.nil? or event.particular or not events_in_period(event.start, event.finish).empty?

  if get_participant_event(event).nil?
    relation_user_route(event)
    participant_event_class.create(event: event, schedule: self, confirmed: true, role: "participant")
  end
end

#events_in_period(start, finish = start.end_of_day) ⇒ Object

Check disponibility in specific time interval

Params:
start

DateTime event start

finish

DateTime event finish, if nil is start.end_of_day

Returns an Array of Event that they are present in the time interval



88
89
90
91
92
93
94
95
96
97
# File 'app/models/social_framework/schedule.rb', line 88

def events_in_period(start, finish = start.end_of_day)
  event_class = ModelFabric.get_class(SocialFramework.event_class)
  events = event_class.joins(:participant_events).where(
    "social_framework_participant_events.schedule_id = ? AND " +
    "social_framework_participant_events.confirmed = ? AND " + 
    "social_framework_events.start < ? AND " +
    "social_framework_events.finish > ?", self.id, true, finish, start).order(start: :asc)

  return events
end

#exit_event(event) ⇒ Object

Exit of an event

Params:
event

Event to exit

Returns ParticipantEvent destroyed or nil if user is creator



61
62
63
64
65
66
67
68
# File 'app/models/social_framework/schedule.rb', line 61

def exit_event(event)
  participant_event = get_participant_event(event)

  if not participant_event.nil? and participant_event.role != "creator"
    self.user.routes.delete(event.route) unless event.route.nil?
    participant_event.destroy
  end
end

#remove_event(event) ⇒ Object

Remove an event created by self.user

Params:
event

Event to remove

Returns Event destroyed or nil if user is not creator



74
75
76
77
78
79
80
81
# File 'app/models/social_framework/schedule.rb', line 74

def remove_event(event)
  participant_event = get_participant_event(event)

  if not participant_event.nil? and participant_event.role == "creator"
    event.route.destroy unless event.route.nil?
    event.destroy
  end
end