invitation 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/invitation/invites_controller.rb +75 -33
- data/app/models/invite.rb +3 -0
- data/app/views/invite_mailer/existing_user.text.erb +7 -0
- data/app/views/invite_mailer/new_user.text.erb +9 -0
- data/config/locales/invitation.en.yml +9 -5
- data/lib/generators/invitation/views/views_generator.rb +1 -1
- data/lib/invitation/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 354b452dc2fffa362fb324bd5f8ffce81aaed53f
|
4
|
+
data.tar.gz: fe0c07886e04e48e3911e4e9be9eb0c3d361cda5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 69b001bd110f2ffd4b081d35def31a39a02adb452a96d3f3cae97660470ef4d6c9da5ecd80ec5168595d7c85e2308531c9d578158f0f3927392419eef7bcd7e0
|
7
|
+
data.tar.gz: 3720cf91a52ec9ca0b7a9cb331d8590b3294336ab4bfeb0dd43f6ab2be50058b2461f6bdc0701c4007ca399f2d9ad3da608b24ae48c5318588e43e0443543230
|
@@ -1,61 +1,103 @@
|
|
1
1
|
class Invitation::InvitesController < ApplicationController
|
2
2
|
def new
|
3
|
-
|
3
|
+
attrs = params[:invite] ? params.require(:invite).permit(:invitable_id, :invitable_type, :email, emails: []) : {}
|
4
|
+
@invite = Invite.new(attrs)
|
4
5
|
render template: 'invites/new'
|
5
6
|
end
|
6
7
|
|
8
|
+
# invite: { invitable_id, invitable_type, email or emails:[] }
|
7
9
|
def create
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
#if the user already exists
|
13
|
-
if @invite.recipient != nil
|
14
|
-
deliver_email(InviteMailer.existing_user(@invite))
|
15
|
-
after_invite_existing_user
|
16
|
-
else
|
17
|
-
deliver_email(InviteMailer.new_user(@invite))
|
18
|
-
after_invite_new_user
|
19
|
-
end
|
20
|
-
flash[:notice] = t('invitation.flash.invite_issued', email: @invite.email)
|
21
|
-
else
|
22
|
-
flash[:error] = t('invitation.flash.invite_error')
|
10
|
+
failures = []
|
11
|
+
invites = build_invites
|
12
|
+
ActiveRecord::Base.transaction do
|
13
|
+
invites.each{ |invite| invite.save ? do_invite(invite) : failures << invite.email }
|
23
14
|
end
|
15
|
+
|
24
16
|
respond_to do |format|
|
25
|
-
format.html {
|
26
|
-
|
17
|
+
format.html {
|
18
|
+
if failures.empty?
|
19
|
+
flash[:notice] = t('invitation.flash.invite_issued', count: invites.count)
|
20
|
+
else
|
21
|
+
flash[:error] = t('invitation.flash.invite_error', count: failures.count, email: failures.to_sentence)
|
22
|
+
end
|
23
|
+
redirect_to url_after_invite(invites.first) # FIXME - redirect to back
|
24
|
+
}
|
25
|
+
format.json {
|
26
|
+
if failures.empty?
|
27
|
+
# If we received a single email, json response should be a scalar, not an array.
|
28
|
+
invites = params[:invite].has_key?('email') ? invites.first : invites
|
29
|
+
render json: invites.as_json(except: [:token, :created_at, :updated_at]), status: 201
|
30
|
+
else
|
31
|
+
render json:{ message: t('invitation.flash.invite_error', count: failures.count, email: failures.to_sentence),
|
32
|
+
status: :unprocessable_entity }
|
33
|
+
end
|
34
|
+
}
|
27
35
|
end
|
28
36
|
end
|
29
37
|
|
30
38
|
|
31
|
-
|
39
|
+
protected
|
32
40
|
|
33
41
|
|
34
42
|
# Override this if you want to do something more complicated for existing users.
|
35
|
-
|
36
|
-
|
37
|
-
|
43
|
+
# For example, if you have a more complex permissions scheme than just a simple
|
44
|
+
# has_many relationship, enable it here.
|
45
|
+
def after_invite_existing_user(invite)
|
46
|
+
# Add the user to the invitable resource/organization
|
47
|
+
invite.invitable.add_invited_user(invite.recipient)
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
# Override if you want to do something more complicated for new users.
|
52
|
+
# By default we don't do anything extra.
|
53
|
+
def after_invite_new_user(invite)
|
38
54
|
end
|
39
55
|
|
40
|
-
|
41
|
-
|
56
|
+
|
57
|
+
# After an invite is created, redirect the user here.
|
58
|
+
# Default implementation doesn't return a url, just the invitable.
|
59
|
+
def url_after_invite(invite)
|
60
|
+
invite.invitable
|
42
61
|
end
|
43
62
|
|
44
|
-
|
45
|
-
|
46
|
-
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def build_invites
|
67
|
+
attributes = invite_params_for_create
|
68
|
+
attributes[:emails].collect{ |e| Invite.new(invitable_id: attributes[:invitable_id],
|
69
|
+
invitable_type: attributes[:invitable_type],
|
70
|
+
sender_id: current_user.id,
|
71
|
+
email: e) }
|
47
72
|
end
|
48
73
|
|
49
|
-
|
50
|
-
|
51
|
-
|
74
|
+
|
75
|
+
# Paramsters used in #create. Allow :email or :emails in payload.
|
76
|
+
# Copy :email scalar to :emails array, so we only have to process :emails attribute.
|
77
|
+
def invite_params_for_create
|
78
|
+
params[:invite][:emails] ||= []
|
79
|
+
if params[:invite][:email]
|
80
|
+
params[:invite][:emails] << params[:invite][:email]
|
81
|
+
end
|
82
|
+
params.require(:invite).permit(:invitable_id, :invitable_type, emails: [])
|
52
83
|
end
|
53
84
|
|
54
|
-
|
55
|
-
|
56
|
-
|
85
|
+
|
86
|
+
# Invite user by sending email.
|
87
|
+
# Existing users are granted permissions via #after_invite_existing_user.
|
88
|
+
# New users are granted permissions via #after_invite_new_user, currently a null op.
|
89
|
+
def do_invite(invite)
|
90
|
+
if invite.existing_user?
|
91
|
+
deliver_email(InviteMailer.existing_user(invite))
|
92
|
+
after_invite_existing_user(invite)
|
93
|
+
invite.save
|
94
|
+
else
|
95
|
+
deliver_email(InviteMailer.new_user(invite))
|
96
|
+
after_invite_new_user(invite)
|
97
|
+
end
|
57
98
|
end
|
58
99
|
|
100
|
+
|
59
101
|
# Use deliver_later from rails 4.2+ if available.
|
60
102
|
def deliver_email(mail)
|
61
103
|
if mail.respond_to?(:deliver_later)
|
data/app/models/invite.rb
CHANGED
@@ -0,0 +1,7 @@
|
|
1
|
+
<%= t('invitation.invite_mailer.existing_user.hello', email: @invite.email) %>
|
2
|
+
|
3
|
+
<%= raw t('invitation.invite_mailer.existing_user.someone_invited_you',
|
4
|
+
sender: @invite.sender.email, invitable: @invite.invitable.invitable_name, url: root_url) %>
|
5
|
+
|
6
|
+
<%= t('invitation.invite_mailer.existing_user.ignore', sender: @invite.sender.email) %>
|
7
|
+
|
@@ -0,0 +1,9 @@
|
|
1
|
+
<%= t('invitation.invite_mailer.new_user.hello', email: @invite.email) %>
|
2
|
+
|
3
|
+
<%= t('invitation.invite_mailer.new_user.someone_invited_you',
|
4
|
+
sender: @invite.sender.email, invitable: @invite.invitable.invitable_name, url: root_url) %>
|
5
|
+
|
6
|
+
<%= t('invitation.invite_mailer.new_user.accept') %>:
|
7
|
+
<%= @user_registration_url %>
|
8
|
+
|
9
|
+
<%= t('invitation.invite_mailer.new_user.ignore') %>
|
@@ -5,17 +5,21 @@ en:
|
|
5
5
|
email: "Email"
|
6
6
|
invitation:
|
7
7
|
flash:
|
8
|
-
invite_issued:
|
9
|
-
|
8
|
+
invite_issued:
|
9
|
+
one: "Invitation issued"
|
10
|
+
other: "Invitations issued"
|
11
|
+
invite_error:
|
12
|
+
one: "Unable to issue invitation to %{email}"
|
13
|
+
other: "Unable to issue invitations to %{email}"
|
10
14
|
invite_mailer:
|
11
15
|
existing_user:
|
12
16
|
subject: "Invitation instructions"
|
13
17
|
hello: "Hello %{email}"
|
14
|
-
someone_invited_you: "%{sender} has invited you to
|
18
|
+
someone_invited_you: "%{sender} has invited you to %{invitable} at %{url}. You now have permissions to access it."
|
15
19
|
ignore: "If you don't want this permission added or feel it is in error, please contact %{sender}."
|
16
20
|
new_user:
|
17
21
|
subject: "Invitation instructions"
|
18
22
|
hello: "Hello %{email}"
|
19
|
-
someone_invited_you: "%{sender} has invited you to
|
23
|
+
someone_invited_you: "%{sender} has invited you to %{invitable} at %{url}. You can accept the invitation with the url below."
|
20
24
|
accept: "Accept invitation"
|
21
|
-
ignore: "If you don't want to accept the invitation, please ignore this email
|
25
|
+
ignore: "If you don't want to accept the invitation, please ignore this email. Your account won't be created until you access the link above and set your password."
|
data/lib/invitation/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: invitation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Tomich
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -168,7 +168,9 @@ files:
|
|
168
168
|
- app/mailers/invite_mailer.rb
|
169
169
|
- app/models/invite.rb
|
170
170
|
- app/views/invite_mailer/existing_user.html.erb
|
171
|
+
- app/views/invite_mailer/existing_user.text.erb
|
171
172
|
- app/views/invite_mailer/new_user.html.erb
|
173
|
+
- app/views/invite_mailer/new_user.text.erb
|
172
174
|
- app/views/invites/new.html.erb
|
173
175
|
- config/locales/invitation.en.yml
|
174
176
|
- config/routes.rb
|