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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d20b7998d1b4e813f82b634d8ed324f3b3c4a250
4
- data.tar.gz: 5e983a833985e6f7713a5ab2e51f901e768ae304
3
+ metadata.gz: 354b452dc2fffa362fb324bd5f8ffce81aaed53f
4
+ data.tar.gz: fe0c07886e04e48e3911e4e9be9eb0c3d361cda5
5
5
  SHA512:
6
- metadata.gz: 8675a33a4639c43d14beaa2e50caf1113b032bea2b38b66e5fc7375dc9631fbd8c70762ca8bcf4017836ac565e3aa7b53b90b0ff3f48cccf9077efc1e81d26ad
7
- data.tar.gz: 971bfcc4ac6f9d6cccd8544879513c93ae887c5a9a68f2180433ae53fd10001b6227f94acc0dbf588706cadbfe0bc5a4c88555725c2805948a03b6e91772ae67
6
+ metadata.gz: 69b001bd110f2ffd4b081d35def31a39a02adb452a96d3f3cae97660470ef4d6c9da5ecd80ec5168595d7c85e2308531c9d578158f0f3927392419eef7bcd7e0
7
+ data.tar.gz: 3720cf91a52ec9ca0b7a9cb331d8590b3294336ab4bfeb0dd43f6ab2be50058b2461f6bdc0701c4007ca399f2d9ad3da608b24ae48c5318588e43e0443543230
@@ -1,61 +1,103 @@
1
1
  class Invitation::InvitesController < ApplicationController
2
2
  def new
3
- @invite = invite_from_params
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
- @invite = invite_from_params
9
- @invite.sender_id = current_user.id
10
- logger.debug '@invite: ' + @invite.inspect
11
- if @invite.save
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 { redirect_to url_after_invite }
26
- format.json { render json: @invite.as_json(except: :token) }
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
- private
39
+ protected
32
40
 
33
41
 
34
42
  # Override this if you want to do something more complicated for existing users.
35
- def after_invite_existing_user
36
- # Add the user to the organization
37
- @invite.invitable.add_invited_user(@invite.recipient)
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
- # Override if you want to do something more complicated for new users. By default we do nothing.
41
- def after_invite_new_user
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
- def url_after_invite
46
- @invite.invitable
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
- # Build new Invite from params.
50
- def invite_from_params
51
- Invite.new(invite_params)
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
- def invite_params
55
- return params.require(:invite).permit(:invitable_id, :invitable_type, :email) if params[:invite]
56
- Hash.new
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)
@@ -14,6 +14,9 @@ class Invite < ActiveRecord::Base
14
14
  validates :invitable, presence: true
15
15
  validates :sender, presence: true
16
16
 
17
+ def existing_user?
18
+ recipient != nil
19
+ end
17
20
 
18
21
  def generate_token
19
22
  self.token = SecureRandom.hex(20).encode('UTF-8')
@@ -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: "Invitation issued to %{email}"
9
- invite_error: "Unable to issue invitation"
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 <b>%{invitable}</b> at %{url}. You now have permissions to access it."
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 <b>%{invitable}</b> at %{url}. You can accept the invitation with the url below."
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.<br />Your account won't be created until you access the link above and set your password."
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."
@@ -3,7 +3,7 @@ require 'rails/generators/base'
3
3
  #
4
4
  # deploy view and locale assets
5
5
  #
6
- module Authenticate
6
+ module Invitation
7
7
  module Generators
8
8
  class ViewsGenerator < Rails::Generators::Base
9
9
  source_root File.expand_path("../../../../..", __FILE__)
@@ -1,3 +1,3 @@
1
1
  module Invitation
2
- VERSION = '0.0.2'
2
+ VERSION = '0.1.0'
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.0.2
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-13 00:00:00.000000000 Z
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