-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extracted InviteForm to app/forms/invitation/invite_form.rb
- Loading branch information
Showing
6 changed files
with
134 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,133 +1,106 @@ | ||
# | ||
# Invitation::InvitesController - issue invitations to users via email address. | ||
# | ||
# Controller uses an (inner class) Form object that is not a persisted model. The | ||
# form can accept many email addresses, and creates on Invite per email address. | ||
# | ||
# Subclass and modify or extend, or copy the controller into your app with `rails generate invitation:controller`. | ||
# | ||
# Common extensions include: | ||
# * add authorization checks: subclass and add before_actions to :new and :create. | ||
# * override after_invite_existing_user or after_invite_new_user | ||
# | ||
class Invitation::InvitesController < ApplicationController | ||
def new | ||
@invite = InviteForm.new(invite_params) | ||
render template: 'invites/new' | ||
end | ||
module Invitation | ||
|
||
# Create one or more Invite instances. | ||
# invite: { invitable_id, invitable_type, email or emails:[] } | ||
# | ||
def create | ||
failures = [] | ||
invites = InviteForm.new(invite_params).build_invites(current_user) | ||
ActiveRecord::Base.transaction do | ||
invites.each { |invite| invite.save ? do_invite(invite) : failures << invite.email } | ||
# Invitation::InvitesController - issue invitations to users via email address. | ||
# | ||
# Controller uses an (inner class) Form object that is not a persisted model. The | ||
# form can accept many email addresses, and creates on Invite per email address. | ||
# | ||
# Subclass and modify/extend, or copy the controller into your app with `rails generate invitation:controller`. | ||
# | ||
# Common extensions could include: | ||
# * add authorization checks: subclass and add before_actions to :new and :create. | ||
# * override after_invite_existing_user or after_invite_new_user | ||
# | ||
class InvitesController < ApplicationController | ||
|
||
def new | ||
@invite = InviteForm.new(invite_params) | ||
render template: 'invites/new' | ||
end | ||
logger.info "!!!!!!!!!!!!!!!!!!!!! INSIDE CREATE: current_user: #{current_user.inspect}" | ||
respond_to do |format| | ||
format.html do | ||
if failures.empty? | ||
flash[:notice] = t('invitation.flash.invite_issued', count: invites.count) | ||
else | ||
flash[:error] = t('invitation.flash.invite_error', count: failures.count, email: failures.to_sentence) | ||
end | ||
redirect_to url_after_invite(invites.first) # FIXME: redirect to back | ||
|
||
# | ||
# Create one or more Invite instances. | ||
# invite: { invitable_id, invitable_type, email or emails:[] } | ||
# | ||
def create | ||
failures = [] | ||
invites = InviteForm.new(invite_params).build_invites(current_user) | ||
ActiveRecord::Base.transaction do | ||
invites.each { |invite| invite.save ? do_invite(invite) : failures << invite.email } | ||
end | ||
format.json do | ||
if failures.empty? | ||
# If we received a single email, json response should be a scalar, not an array. | ||
invites = params[:invite].key?('email') ? invites.first : invites | ||
render json: invites.as_json(except: [:token, :created_at, :updated_at]), status: 201 | ||
else | ||
render json: { | ||
message: t('invitation.flash.invite_error', count: failures.count, email: failures.to_sentence), | ||
status: :unprocessable_entity | ||
} | ||
|
||
logger.info "!!!!!!!!!!!!!!!!!!!!! INSIDE CREATE: current_user: #{current_user.inspect}" | ||
respond_to do |format| | ||
format.html do | ||
if failures.empty? | ||
flash[:notice] = t('invitation.flash.invite_issued', count: invites.count) | ||
else | ||
flash[:error] = t('invitation.flash.invite_error', count: failures.count, email: failures.to_sentence) | ||
end | ||
redirect_to url_after_invite(invites.first) # FIXME: redirect to back | ||
end | ||
format.json do | ||
if failures.empty? | ||
# If we received a single email, json response should be a scalar, not an array. | ||
invites = params[:invite].key?('email') ? invites.first : invites | ||
render json: invites.as_json(except: [:token, :created_at, :updated_at]), status: 201 | ||
else | ||
render json: { | ||
message: t('invitation.flash.invite_error', count: failures.count, email: failures.to_sentence), | ||
status: :unprocessable_entity | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
# A form object pretends to be 'invite', but accepts both 'email' and 'emails'. | ||
# It knows how to build all of the invite instances. | ||
class InviteForm | ||
include ActiveModel::Model | ||
attr_accessor :invitable_id, :invitable_type, :email, :emails | ||
attr_reader :invitable | ||
private | ||
|
||
def self.model_name # form masquerades as 'invite' | ||
ActiveModel::Name.new(self, nil, 'Invite') | ||
# Override this if you want to do something more complicated for existing users. | ||
# For example, if you have a more complex permissions scheme than just a simple | ||
# has_many relationship, enable it here. | ||
def after_invite_existing_user(invite) | ||
# Add the user to the invitable resource/organization | ||
invite.invitable.add_invited_user(invite.recipient) | ||
end | ||
|
||
def initialize(attributes = {}) | ||
@emails ||= [] | ||
super | ||
# Override if you want to do something more complicated for new users. | ||
# By default we don't do anything extra. | ||
def after_invite_new_user(invite) | ||
end | ||
|
||
def invitable | ||
@invitable ||= @invitable_type.classify.constantize.find(@invitable_id) | ||
# After an invite is created, redirect the user here. | ||
# Default implementation doesn't return a url, just the invitable. | ||
def url_after_invite(invite) | ||
invite.invitable | ||
end | ||
|
||
def build_invites(current_user) | ||
all_emails.reject(&:blank?).collect do |e| | ||
Invite.new(invitable_id: @invitable_id, invitable_type: @invitable_type, sender_id: current_user.id, email: e) | ||
end | ||
def invite_params | ||
params[:invite] ? params.require(:invite).permit(:invitable_id, :invitable_type, :email, emails: []) : {} | ||
end | ||
|
||
private | ||
|
||
def all_emails | ||
@emails + [@email] | ||
end | ||
end | ||
|
||
# Override this if you want to do something more complicated for existing users. | ||
# For example, if you have a more complex permissions scheme than just a simple | ||
# has_many relationship, enable it here. | ||
def after_invite_existing_user(invite) | ||
# Add the user to the invitable resource/organization | ||
invite.invitable.add_invited_user(invite.recipient) | ||
end | ||
|
||
# Override if you want to do something more complicated for new users. | ||
# By default we don't do anything extra. | ||
def after_invite_new_user(invite) | ||
end | ||
|
||
# After an invite is created, redirect the user here. | ||
# Default implementation doesn't return a url, just the invitable. | ||
def url_after_invite(invite) | ||
invite.invitable | ||
end | ||
|
||
def invite_params | ||
params[:invite] ? params.require(:invite).permit(:invitable_id, :invitable_type, :email, emails: []) : {} | ||
end | ||
|
||
# Invite user by sending email. | ||
# Existing users are granted permissions via #after_invite_existing_user. | ||
# New users are granted permissions via #after_invite_new_user, currently a null op. | ||
def do_invite(invite) | ||
if invite.existing_user? | ||
deliver_email(InviteMailer.existing_user(invite)) | ||
after_invite_existing_user(invite) | ||
invite.save | ||
else | ||
deliver_email(InviteMailer.new_user(invite)) | ||
after_invite_new_user(invite) | ||
# Invite user by sending email. | ||
# Existing users are granted permissions via #after_invite_existing_user. | ||
# New users are granted permissions via #after_invite_new_user, currently a null op. | ||
def do_invite(invite) | ||
if invite.existing_user? | ||
deliver_email(InviteMailer.existing_user(invite)) | ||
after_invite_existing_user(invite) | ||
invite.save | ||
else | ||
deliver_email(InviteMailer.new_user(invite)) | ||
after_invite_new_user(invite) | ||
end | ||
end | ||
end | ||
|
||
# Use deliver_later from rails 4.2+ if available. | ||
def deliver_email(mail) | ||
if mail.respond_to?(:deliver_later) | ||
mail.deliver_later | ||
else | ||
mail.deliver | ||
# Use deliver_later from rails 4.2+ if available. | ||
def deliver_email(mail) | ||
if mail.respond_to?(:deliver_later) | ||
mail.deliver_later | ||
else | ||
mail.deliver | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Invitation | ||
|
||
# A form object pretends to be 'invite', but accepts both 'email' and 'emails'. | ||
# It knows how to build all of the invite instances. | ||
class InviteForm | ||
include ActiveModel::Model | ||
|
||
attr_accessor :invitable_id, :invitable_type, :email, :emails | ||
attr_reader :invitable | ||
|
||
def self.model_name # form masquerades as 'invite' | ||
ActiveModel::Name.new(self, nil, 'Invite') | ||
end | ||
|
||
def initialize(attributes = {}) | ||
@emails ||= [] | ||
super | ||
end | ||
|
||
def invitable | ||
@invitable ||= @invitable_type.classify.constantize.find(@invitable_id) | ||
end | ||
|
||
def build_invites(current_user) | ||
all_emails.reject(&:blank?).collect do |e| | ||
Invite.new(invitable_id: @invitable_id, invitable_type: @invitable_type, sender_id: current_user.id, email: e) | ||
end | ||
end | ||
|
||
private | ||
|
||
def all_emails | ||
@emails + [@email] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.