invitation 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/controllers/invitation/invites_controller.rb +53 -23
- data/app/views/invites/new.html.erb +0 -1
- data/lib/invitation/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 106aa33fdda1af6e4a185946e1f1eb05c2621d90
|
4
|
+
data.tar.gz: 23f8f4b5ab5927052b1258ec728cdb04af2c1028
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1fd3cc4ca6941ebe309af5f616338d0f966bd627a3d0c5fd0f836ef2d07308ea5e437c2b3ec5632075a56d54edb9f9f8b2cc1dd4e20a26e236265c06245b5489
|
7
|
+
data.tar.gz: cbb961fc6d801a5983de88b10986ca0436d4fab2a2fde6515bc8ad292d87df6ad8b6eee705f2262770c2524cb86670045b20662f9bd4cd0d368e7586c09c8a62
|
@@ -1,14 +1,26 @@
|
|
1
|
+
#
|
2
|
+
# Invitation::InvitesController - issue invitations to users via email address.
|
3
|
+
#
|
4
|
+
# Controller uses an (inner class) Form object that is not a persisted model. The
|
5
|
+
# form can accept many email addresses, and creates on Invite per email address.
|
6
|
+
#
|
7
|
+
# Subclass and modify or extend, or copy the controller into your app with `rails generate invitation:controller`.
|
8
|
+
#
|
9
|
+
# Common extensions include:
|
10
|
+
# * add authorization checks: subclass and add before_actions to :new and :create.
|
11
|
+
# * override after_invite_existing_user or after_invite_new_user.
|
12
|
+
#
|
1
13
|
class Invitation::InvitesController < ApplicationController
|
14
|
+
|
2
15
|
def new
|
3
|
-
|
4
|
-
@invite = Invite.new(attrs)
|
16
|
+
@invite = InviteForm.new(invite_params)
|
5
17
|
render template: 'invites/new'
|
6
18
|
end
|
7
19
|
|
8
20
|
# invite: { invitable_id, invitable_type, email or emails:[] }
|
9
21
|
def create
|
10
22
|
failures = []
|
11
|
-
invites = build_invites
|
23
|
+
invites = InviteForm.new(invite_params).build_invites(current_user)
|
12
24
|
ActiveRecord::Base.transaction do
|
13
25
|
invites.each{ |invite| invite.save ? do_invite(invite) : failures << invite.email }
|
14
26
|
end
|
@@ -39,6 +51,42 @@ class Invitation::InvitesController < ApplicationController
|
|
39
51
|
protected
|
40
52
|
|
41
53
|
|
54
|
+
# A form object pretends to be 'invite', but accepts both 'email' and 'emails'.
|
55
|
+
# It knows how to build all of the invite instances.
|
56
|
+
class InviteForm
|
57
|
+
include ActiveModel::Model
|
58
|
+
attr_accessor :invitable_id, :invitable_type, :email, :emails
|
59
|
+
attr_reader :invitable
|
60
|
+
|
61
|
+
def self.model_name # form masquerades as 'invite'
|
62
|
+
ActiveModel::Name.new(self, nil, 'Invite')
|
63
|
+
end
|
64
|
+
|
65
|
+
def initialize(attributes = {})
|
66
|
+
@emails ||= []
|
67
|
+
super
|
68
|
+
end
|
69
|
+
|
70
|
+
def invitable
|
71
|
+
@invitable ||= @invitable_type.classify.constantize.find(@invitable_id)
|
72
|
+
end
|
73
|
+
|
74
|
+
def build_invites(current_user)
|
75
|
+
all_emails.reject{ |e| e.blank? }.collect{ |e|
|
76
|
+
Invite.new(invitable_id: @invitable_id, invitable_type: @invitable_type, sender_id: current_user.id, email: e) }
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def all_emails
|
82
|
+
@emails + [@email]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
|
42
90
|
# Override this if you want to do something more complicated for existing users.
|
43
91
|
# For example, if you have a more complex permissions scheme than just a simple
|
44
92
|
# has_many relationship, enable it here.
|
@@ -61,28 +109,10 @@ class Invitation::InvitesController < ApplicationController
|
|
61
109
|
end
|
62
110
|
|
63
111
|
|
64
|
-
|
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) }
|
72
|
-
end
|
73
|
-
|
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: [])
|
112
|
+
def invite_params
|
113
|
+
params[:invite] ? params.require(:invite).permit(:invitable_id, :invitable_type, :email, emails: []) : {}
|
83
114
|
end
|
84
115
|
|
85
|
-
|
86
116
|
# Invite user by sending email.
|
87
117
|
# Existing users are granted permissions via #after_invite_existing_user.
|
88
118
|
# New users are granted permissions via #after_invite_new_user, currently a null op.
|
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.1.
|
4
|
+
version: 0.1.1
|
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-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|