Created
September 10, 2012 07:58
-
-
Save ka8725/3689548 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Event::InvitationsController < ApplicationController | |
| load_resource :event | |
| before_filter :authorize_resource | |
| def new; end | |
| def create | |
| unless params[:users_to_invite].blank? | |
| ids = { groups: [], users: [] } | |
| params[:users_to_invite].each do |user_identifier| | |
| table_name, id = user_identifier.split('.') | |
| if id.blank? | |
| ids[table_name.to_sym] = case table_name | |
| when 'users' then current_user.friend_ids | |
| when 'groups' then current_user.joined_group_ids | |
| else nil | |
| end | |
| else | |
| ids[table_name.to_sym].push(id.to_i) | |
| end | |
| end | |
| inviter_id = current_user.id | |
| users_not_invited = [inviter_id] + @event.invitee_ids + @event.visitor_ids | |
| user_ids = (Group.participant_ids_for_groups(ids[:groups]) + ids[:users] - users_not_invited).uniq | |
| user_ids.each do |invitee_id| | |
| if User.find(invitee_id).profile.event_invitation? | |
| EventInvitation.create do |item| | |
| item.inviter_id = inviter_id | |
| item.invitee_id = invitee_id | |
| item.event = @event | |
| end | |
| end | |
| end | |
| end | |
| unless params[:emails_to_invite].blank? | |
| emails = ActiveSupport::JSON.decode(params[:emails_to_invite]).to_a.select do |email| | |
| user = User.find_by_email(email) | |
| (user.present? && user.profile.event_email_notification?) || user.blank? | |
| end | |
| emails.each do |email| | |
| EventMailer.invite(@event.id, @event.title, email.strip).deliver! | |
| end | |
| end | |
| redirect_to @event, notice: I18n.t('invitation.has_been_sent') | |
| end | |
| private | |
| def authorize_resource | |
| authorize! :update, @event | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment