invitation 0.4.5 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7fb6ecb6c28da3b30a263c6463d7414c0f51833d
4
- data.tar.gz: 20809ed7eeb1a17b9670ca1bf367239c4a756b08
3
+ metadata.gz: 4a8f89d369c24f891983b625b72501697feb718b
4
+ data.tar.gz: 63d62ec182515cfc19cb56c0f8b277627807a3dc
5
5
  SHA512:
6
- metadata.gz: 19fd8f1e9fd2ee55f6098c86e5d4ffcae60913c29696b3180ed1a4aeaf953d0e0f4a373ec4977f8a09e4201b6acbdff6cf2fd98baf6bcb61c8813619ff872a3b
7
- data.tar.gz: 6b3cff19c001aa3cd885e1753adbc14cd53e6d57985f289b93b03ee0bf0a01774e6648dccfdc6467694fbb992dbd57aabc34e842f081c59f1377aa2d7bf13fdf
6
+ metadata.gz: d09d203673dcaf4b5a1016b8f4043767a91f1c09ed881e7c962ae8a5f549c50bc6c8eacfacfcac3988dbd0db315b13eef5361e9757f8fda4accd18256399ea63
7
+ data.tar.gz: 9c9beaad4470e5dad9fa5c0796090d7c9ca2a7552b63023fdbbfa63019ffe7228ffaa42db55ad060a8f044173dee4af8f35b13bed7e1ff5528354d8c9ed302c7
@@ -1,133 +1,106 @@
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
- #
13
- class Invitation::InvitesController < ApplicationController
14
- def new
15
- @invite = InviteForm.new(invite_params)
16
- render template: 'invites/new'
17
- end
1
+ module Invitation
18
2
 
19
- # Create one or more Invite instances.
20
- # invite: { invitable_id, invitable_type, email or emails:[] }
21
3
  #
22
- def create
23
- failures = []
24
- invites = InviteForm.new(invite_params).build_invites(current_user)
25
- ActiveRecord::Base.transaction do
26
- invites.each { |invite| invite.save ? do_invite(invite) : failures << invite.email }
4
+ # Invitation::InvitesController - issue invitations to users via email address.
5
+ #
6
+ # Controller uses an (inner class) Form object that is not a persisted model. The
7
+ # form can accept many email addresses, and creates on Invite per email address.
8
+ #
9
+ # Subclass and modify/extend, or copy the controller into your app with `rails generate invitation:controller`.
10
+ #
11
+ # Common extensions could include:
12
+ # * add authorization checks: subclass and add before_actions to :new and :create.
13
+ # * override after_invite_existing_user or after_invite_new_user
14
+ #
15
+ class InvitesController < ApplicationController
16
+
17
+ def new
18
+ @invite = InviteForm.new(invite_params)
19
+ render template: 'invites/new'
27
20
  end
28
- logger.info "!!!!!!!!!!!!!!!!!!!!! INSIDE CREATE: current_user: #{current_user.inspect}"
29
- respond_to do |format|
30
- format.html do
31
- if failures.empty?
32
- flash[:notice] = t('invitation.flash.invite_issued', count: invites.count)
33
- else
34
- flash[:error] = t('invitation.flash.invite_error', count: failures.count, email: failures.to_sentence)
35
- end
36
- redirect_to url_after_invite(invites.first) # FIXME: redirect to back
21
+
22
+ #
23
+ # Create one or more Invite instances.
24
+ # invite: { invitable_id, invitable_type, email or emails:[] }
25
+ #
26
+ def create
27
+ failures = []
28
+ invites = InviteForm.new(invite_params).build_invites(current_user)
29
+ ActiveRecord::Base.transaction do
30
+ invites.each { |invite| invite.save ? do_invite(invite) : failures << invite.email }
37
31
  end
38
- format.json do
39
- if failures.empty?
40
- # If we received a single email, json response should be a scalar, not an array.
41
- invites = params[:invite].key?('email') ? invites.first : invites
42
- render json: invites.as_json(except: [:token, :created_at, :updated_at]), status: 201
43
- else
44
- render json: {
45
- message: t('invitation.flash.invite_error', count: failures.count, email: failures.to_sentence),
46
- status: :unprocessable_entity
47
- }
32
+
33
+ logger.info "!!!!!!!!!!!!!!!!!!!!! INSIDE CREATE: current_user: #{current_user.inspect}"
34
+ respond_to do |format|
35
+ format.html do
36
+ if failures.empty?
37
+ flash[:notice] = t('invitation.flash.invite_issued', count: invites.count)
38
+ else
39
+ flash[:error] = t('invitation.flash.invite_error', count: failures.count, email: failures.to_sentence)
40
+ end
41
+ redirect_to url_after_invite(invites.first) # FIXME: redirect to back
42
+ end
43
+ format.json do
44
+ if failures.empty?
45
+ # If we received a single email, json response should be a scalar, not an array.
46
+ invites = params[:invite].key?('email') ? invites.first : invites
47
+ render json: invites.as_json(except: [:token, :created_at, :updated_at]), status: 201
48
+ else
49
+ render json: {
50
+ message: t('invitation.flash.invite_error', count: failures.count, email: failures.to_sentence),
51
+ status: :unprocessable_entity
52
+ }
53
+ end
48
54
  end
49
55
  end
50
56
  end
51
- end
52
57
 
53
- private
54
-
55
- # A form object pretends to be 'invite', but accepts both 'email' and 'emails'.
56
- # It knows how to build all of the invite instances.
57
- class InviteForm
58
- include ActiveModel::Model
59
- attr_accessor :invitable_id, :invitable_type, :email, :emails
60
- attr_reader :invitable
58
+ private
61
59
 
62
- def self.model_name # form masquerades as 'invite'
63
- ActiveModel::Name.new(self, nil, 'Invite')
60
+ # Override this if you want to do something more complicated for existing users.
61
+ # For example, if you have a more complex permissions scheme than just a simple
62
+ # has_many relationship, enable it here.
63
+ def after_invite_existing_user(invite)
64
+ # Add the user to the invitable resource/organization
65
+ invite.invitable.add_invited_user(invite.recipient)
64
66
  end
65
67
 
66
- def initialize(attributes = {})
67
- @emails ||= []
68
- super
68
+ # Override if you want to do something more complicated for new users.
69
+ # By default we don't do anything extra.
70
+ def after_invite_new_user(invite)
69
71
  end
70
72
 
71
- def invitable
72
- @invitable ||= @invitable_type.classify.constantize.find(@invitable_id)
73
+ # After an invite is created, redirect the user here.
74
+ # Default implementation doesn't return a url, just the invitable.
75
+ def url_after_invite(invite)
76
+ invite.invitable
73
77
  end
74
78
 
75
- def build_invites(current_user)
76
- all_emails.reject(&:blank?).collect do |e|
77
- Invite.new(invitable_id: @invitable_id, invitable_type: @invitable_type, sender_id: current_user.id, email: e)
78
- end
79
+ def invite_params
80
+ params[:invite] ? params.require(:invite).permit(:invitable_id, :invitable_type, :email, emails: []) : {}
79
81
  end
80
82
 
81
- private
82
-
83
- def all_emails
84
- @emails + [@email]
85
- end
86
- end
87
-
88
- # Override this if you want to do something more complicated for existing users.
89
- # For example, if you have a more complex permissions scheme than just a simple
90
- # has_many relationship, enable it here.
91
- def after_invite_existing_user(invite)
92
- # Add the user to the invitable resource/organization
93
- invite.invitable.add_invited_user(invite.recipient)
94
- end
95
-
96
- # Override if you want to do something more complicated for new users.
97
- # By default we don't do anything extra.
98
- def after_invite_new_user(invite)
99
- end
100
-
101
- # After an invite is created, redirect the user here.
102
- # Default implementation doesn't return a url, just the invitable.
103
- def url_after_invite(invite)
104
- invite.invitable
105
- end
106
-
107
- def invite_params
108
- params[:invite] ? params.require(:invite).permit(:invitable_id, :invitable_type, :email, emails: []) : {}
109
- end
110
-
111
- # Invite user by sending email.
112
- # Existing users are granted permissions via #after_invite_existing_user.
113
- # New users are granted permissions via #after_invite_new_user, currently a null op.
114
- def do_invite(invite)
115
- if invite.existing_user?
116
- deliver_email(InviteMailer.existing_user(invite))
117
- after_invite_existing_user(invite)
118
- invite.save
119
- else
120
- deliver_email(InviteMailer.new_user(invite))
121
- after_invite_new_user(invite)
83
+ # Invite user by sending email.
84
+ # Existing users are granted permissions via #after_invite_existing_user.
85
+ # New users are granted permissions via #after_invite_new_user, currently a null op.
86
+ def do_invite(invite)
87
+ if invite.existing_user?
88
+ deliver_email(InviteMailer.existing_user(invite))
89
+ after_invite_existing_user(invite)
90
+ invite.save
91
+ else
92
+ deliver_email(InviteMailer.new_user(invite))
93
+ after_invite_new_user(invite)
94
+ end
122
95
  end
123
- end
124
96
 
125
- # Use deliver_later from rails 4.2+ if available.
126
- def deliver_email(mail)
127
- if mail.respond_to?(:deliver_later)
128
- mail.deliver_later
129
- else
130
- mail.deliver
97
+ # Use deliver_later from rails 4.2+ if available.
98
+ def deliver_email(mail)
99
+ if mail.respond_to?(:deliver_later)
100
+ mail.deliver_later
101
+ else
102
+ mail.deliver
103
+ end
131
104
  end
132
105
  end
133
106
  end
@@ -0,0 +1,36 @@
1
+ module Invitation
2
+
3
+ # A form object pretends to be 'invite', but accepts both 'email' and 'emails'.
4
+ # It knows how to build all of the invite instances.
5
+ class InviteForm
6
+ include ActiveModel::Model
7
+
8
+ attr_accessor :invitable_id, :invitable_type, :email, :emails
9
+ attr_reader :invitable
10
+
11
+ def self.model_name # form masquerades as 'invite'
12
+ ActiveModel::Name.new(self, nil, 'Invite')
13
+ end
14
+
15
+ def initialize(attributes = {})
16
+ @emails ||= []
17
+ super
18
+ end
19
+
20
+ def invitable
21
+ @invitable ||= @invitable_type.classify.constantize.find(@invitable_id)
22
+ end
23
+
24
+ def build_invites(current_user)
25
+ all_emails.reject(&:blank?).collect do |e|
26
+ Invite.new(invitable_id: @invitable_id, invitable_type: @invitable_type, sender_id: current_user.id, email: e)
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def all_emails
33
+ @emails + [@email]
34
+ end
35
+ end
36
+ end
@@ -15,6 +15,10 @@ module Invitation
15
15
  def create_mailers
16
16
  directory 'app/mailers'
17
17
  end
18
+
19
+ def create_forms
20
+ directory 'app/forms'
21
+ end
18
22
  end
19
23
  end
20
24
  end
@@ -1,3 +1,3 @@
1
1
  module Invitation
2
- VERSION = '0.4.5'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  end
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.4.5
4
+ version: 0.5.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: 2018-03-10 00:00:00.000000000 Z
11
+ date: 2018-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -207,6 +207,7 @@ extra_rdoc_files: []
207
207
  files:
208
208
  - Rakefile
209
209
  - app/controllers/invitation/invites_controller.rb
210
+ - app/forms/invitation/invite_form.rb
210
211
  - app/mailers/invite_mailer.rb
211
212
  - app/models/invite.rb
212
213
  - app/views/invite_mailer/existing_user.html.erb
@@ -235,7 +236,6 @@ files:
235
236
  - lib/invitation/user.rb
236
237
  - lib/invitation/user_registration.rb
237
238
  - lib/invitation/version.rb
238
- - lib/tasks/invitation_tasks.rake
239
239
  homepage: http://github.com/tomichj/invitation
240
240
  licenses:
241
241
  - MIT
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :invitation do
3
- # # Task goes here
4
- # end