Module: Attendable::ActsAsAttendable::ClassMethods

Defined in:
lib/attendable/acts_as_attendable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_attendable(name, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/attendable/acts_as_attendable.rb', line 12

def acts_as_attendable(name, options = {})
  
  class_name = options[:class_name] || name.to_s.classify 
  attendee_class_name = options[:by].present? ? options[:by].to_s.classify : "User"
  table_name = class_name.tableize
  has_many name, as: :attendable, dependent: :destroy, class_name: class_name
  has_many :attendees, conditions: "#{table_name}.rsvp_status = 'attending'", through: name, source: :invitable, source_type: attendee_class_name
  clazz = class_name.constantize
  
  # instance methods 
  define_method "is_member?" do |user| 
    puts 'is member? ' + user.to_s
    puts 'clazz: ' + clazz.to_s
    clazz.where(invitable: user, attendable: self).count > 0 if !user.nil?
  end

  define_method "is_invited?" do |user| 
    clazz.where(invitable: user, attendable: self).count > 0 if !user.nil?
  end
  
  define_method "is_attending?" do |user| 
    clazz.where(invitable: user, attendable: self, rsvp_status: 'attending').count > 0 if !user.nil?
  end
  
  define_method "has_declined?" do |user| 
    clazz.where(invitable: user, attendable: self, rsvp_status: 'declined').count > 0 if !user.nil?
  end
  
  define_method "invite" do |user| 
    if user.is_a?(String)
      invitation_key = user
      is_invited = clazz.where(invitation_key: invitation_key, attendable: self).count > 0
      if !is_invited
        return clazz.create(attendable: self, invitation_key: invitation_key, rsvp_status: 'pending')
      end
    elsif !is_member?(user)
      clazz.create(attendable: self, invitation_key: invitation_key, rsvp_status: 'pending')
    end
  end
  
  define_method "accept_invitation" do |invitation_token, invitable| 
    if (invitation_token && invitable)
      if !is_member?(invitable)
        # only process invitation token if invitable is not already set
        token_member = clazz.where(invitation_token: invitation_token, attendable: self)[0]
        if token_member
          if token_member.invitable.nil?
            # create member invitable
            token_member.invitable = invitable
            if !token_member.save
              # error while saving
            end
          elsif token_member.invitable != invitable
            # member's invitable is not the current invitable
            return nil
          end
          return token_member
        end
      else
        # if a member for invitable already exists, return the current invitable's member
        token_member = clazz.where(invitable: invitable).first
        return token_member
      end
    end
  end
end