daily pastebin goal
40%
SHARE
TWEET

Untitled

a guest Jun 30th, 2018 149 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def create_event
  2.     curr_cid = params[:cal_id]
  3.    
  4.     #sets up @calendars
  5.     user=User.find_by_id(session[:uid])
  6.     cals = user.calendars
  7.     h = Hash.new
  8.     cals.each do |cal|
  9.       h[cal.cname] = cal.id
  10.     end
  11.     @calendars = h
  12.     invitees_all_correct=true
  13.    
  14.     if request.post?
  15.       new_cid = params[:cal_id]
  16.       new_event = user.events.build(:ename=>params[:eventName], :starttime=>DateTime.parse(params[:starts_at]), :endtime=>DateTime.parse(params[:ends_at]), :user_id=>session[:uid])
  17.       inviteeArray = params[:invitees].split(',')
  18.       wrong_invitees = ""
  19.       if new_event.save #if all goes well in saving
  20.         eid = new_event.id #get the eid of created event
  21.         inviteeArray.each do |invitee|
  22.           invitee_user = User.find_by_name(invitee)
  23.           if invitee_user.nil? #if we cant find invitee in user table
  24.             invitees_all_correct=false
  25.             wrong_invitees << "#{invitee} "
  26.           else #if we found invitee in user table
  27.             if new_event.invite.nil? #if event has no invite object yet
  28.               new_event.create_invite(:imessage=>params[:inviteMessage], :event_id=>eid)
  29.             elsif new_event.invite.users.empty? || (invitee != user && !new_event.invite.users.include?(invitee_user)) # if no users invited yet, or if user is not owner and not invited yet, invite by creating new invite in Invites
  30.               new_event.invite.users.create(:name=>invitee, :password=>invitee_user.password)
  31.             else #if user is already invited
  32.               invitees_all_correct=false
  33.               wrong_invitees << "#{invitee} "
  34.             end
  35.           end
  36.         end
  37.         if invitees_all_correct #there was no problems with any invitee
  38.           calendar = Calendar.find_by_id(new_cid)
  39.           calendar.events << new_event
  40.           redirect_to :action=>'show_calendar', :cal_id=>new_cid
  41.         else #if someone had a problem
  42.           wrong_invitees.strip
  43.           redirect_to :action=>'show_calendar', :cal_id=>new_cid, :notice=> 'The following invited usernames are invalid/duplicates and invites were not sent: ' << wrong_invitees
  44.         end
  45.       else #if something breaks during saving
  46.         redirect_to :action=>'create_event', :notice=>'An error has occurred.', :cal_id=>curr_cid
  47.       end
  48.     end
  49.   end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top