Skip to content

Commit

Permalink
Merge pull request #784 from jmstfv/stylistic-changes
Browse files Browse the repository at this point in the history
Stylistic changes
  • Loading branch information
scambra committed Feb 21, 2019
2 parents db25039 + 246173a commit 71a1991
Show file tree
Hide file tree
Showing 30 changed files with 330 additions and 338 deletions.
54 changes: 25 additions & 29 deletions README.rdoc
Expand Up @@ -16,8 +16,8 @@ Install DeviseInvitable gem, it will also install dependencies (such as devise a

Add DeviseInvitable to your Gemfile (and Devise if you weren't using them):

gem 'devise', '~> 4.2'
gem 'devise_invitable', '~> 1.7.0'
gem 'devise', '~> 4.6.1'
gem 'devise_invitable', '~> 1.7.5'

=== Automatic installation

Expand Down Expand Up @@ -57,7 +57,7 @@ Add t.invitable to your Devise model migration:
t.string :invited_by_type
...
end
add_index :users, :invitation_token, :unique => true
add_index :users, :invitation_token, unique: true

or for a model that already exists, define a migration to add DeviseInvitable to your model:

Expand All @@ -69,7 +69,7 @@ or for a model that already exists, define a migration to add DeviseInvitable to
add_column :users, :invitation_limit, :integer
add_column :users, :invited_by_id, :integer
add_column :users, :invited_by_type, :string
add_index :users, :invitation_token, :unique => true
add_index :users, :invitation_token, unique: true

# Allow null encrypted_password
change_column_null :users, :encrypted_password, :string, true
Expand All @@ -80,11 +80,11 @@ or for a model that already exists, define a migration to add DeviseInvitable to
If you previously used devise_invitable with a :limit on :invitation_token, remove it:

def up
change_column :users, :invitation_token, :string, :limit => nil
change_column :users, :invitation_token, :string, limit: nil
end

def down
change_column :users, :invitation_token, :string, :limit => 60
change_column :users, :invitation_token, :string, limit: 60
end

== Mongoid Field Definitions
Expand All @@ -96,11 +96,11 @@ If you are using Mongoid, define the following fields and indexes within your in
field :invitation_accepted_at, type: Time
field :invitation_limit, type: Integer

index( {invitation_token: 1}, {:background => true} )
index( {invitation_by_id: 1}, {:background => true} )
index( { invitation_token: 1 }, { background: true} )
index( { invitation_by_id: 1 }, { background: true} )

You do not need to define a belongs_to relationship, as DeviseInvitable does this on your behalf:
belongs_to :invited_by, :polymorphic => true
belongs_to :invited_by, polymorphic: true

Remember to create indexes within the MongoDB database after deploying your changes.
rake db:mongoid:create_indexes
Expand All @@ -121,11 +121,11 @@ You can set this configuration option in the Devise initializer as follow:

or directly as parameters to the <tt>devise</tt> method:

devise :database_authenticatable, :confirmable, :invitable, :invite_for => 2.weeks
devise :database_authenticatable, :confirmable, :invitable, invite_for: 2.weeks

* invitation_limit: The number of invitations users can send. The default value of nil means users can send as many invites as they want, there is no limit for any user, invitation_limit column is not used. A setting of 0 means they can't send invitations. A setting n > 0 means they can send n invitations. You can change invitation_limit column for some users so they can send more or less invitations, even with global invitation_limit = 0.

* invite_key: The key to be used to check existing users when sending an invitation. You can use multiple keys. This value must be a hash with the invite key as hash keys, and values that respond to the === operator(includes procs and regexes). The default value is looking for users by email and validating with Devise.email_regexp {:email => Devise.email_regexp}.
* invite_key: The key to be used to check existing users when sending an invitation. You can use multiple keys. This value must be a hash with the invite key as hash keys, and values that respond to the === operator(includes procs and regexes). The default value is looking for users by email and validating with Devise.email_regexp { email: Devise.email_regexp }.

* validate_on_invite: force a record to be valid before being actually invited.

Expand Down Expand Up @@ -175,7 +175,7 @@ To change the controller's behavior, create a controller that inherits from <tt>

Now just tell Devise that you want to use your controller, the controller above is 'users/invitations', so our routes.rb would have this line:

devise_for :users, :controllers => { :invitations => 'users/invitations' }
devise_for :users, controllers: { invitations: 'users/invitations' }

be sure that you generate the views and put them into the controller that you generated, so for this example it would be:

Expand Down Expand Up @@ -231,20 +231,20 @@ Here is an example of what your application controller might need to include in

To send an invitation to a user, use the <tt>invite!</tt> class method. <b>Note: This will create a user, and send an email for the invite.</b> <tt>:email</tt> must be present in the parameters hash. You can also include other attributes in the hash. The record will not be validated.

User.invite!(:email => "new_user@example.com", :name => "John Doe")
User.invite!(email: 'new_user@example.com', name: 'John Doe')
# => an invitation email will be sent to new_user@example.com

If you want to create the invitation but not send it, you can set <tt>skip_invitation</tt> to true.

user = User.invite!(:email => "new_user@example.com", :name => "John Doe") do |u|
user = User.invite!(email: 'new_user@example.com', name: 'John Doe') do |u|
u.skip_invitation = true
end
# => the record will be created, but the invitation email will not be sent

When generating the <tt>accept_user_invitation_url</tt> yourself, you must use the <tt>raw_invitation_token</tt>
the value is temporarily available when you invite a user and will be decrypted when recieved.

accept_user_invitation_url(:invitation_token => user.raw_invitation_token)
accept_user_invitation_url(invitation_token: user.raw_invitation_token)

When <tt>skip_invitation</tt> is used, you must also then set the <tt>invitation_sent_at</tt> field when the user is sent their
token. Failure to do so will yield <tt>Invalid invitation token</tt> error when the user attempts to accept the invite.
Expand All @@ -254,7 +254,7 @@ You can set column, or call <tt>deliver_invitation</tt> to sent invitation and s

You can add <tt>:skip_invitation</tt> to attributes hash if <tt>skip_invitation</tt> is added to <tt>attr_accessible</tt>.

User.invite!(:email => "new_user@example.com", :name => "John Doe", :skip_invitation => true)
User.invite!(email: 'new_user@example.com', name: 'John Doe', skip_invitation: true)
# => the record will be created, but the invitation email will not be sent

<tt>skip_invitation</tt> skips sending the email, but sets <tt>invitation_token</tt>, so <tt>invited_to_sign_up?</tt> on the
Expand All @@ -269,7 +269,7 @@ user.raw_invitation_token is available only to the instance and is not persisted

You can also set <tt>invited_by</tt> when using the <tt>invite!</tt> class method:

User.invite!({:email => "new_user@example.com"}, current_user) # current_user will be set as invited_by
User.invite!({ email: 'new_user@example.com' }, current_user) # current_user will be set as invited_by

=== Sending an invitation after user creation

Expand All @@ -289,7 +289,7 @@ To find by invitation token use the <tt>find_by_invitation_token</tt> class meth

To accept an invitation with a token use the <tt>accept_invitation!</tt> class method. <tt>:invitation_token</tt> must be present in the parameters hash. You can also include other attributes in the hash.

User.accept_invitation!(:invitation_token => params[:invitation_token], :password => "ad97nwj3o2", :name => "John Doe")
User.accept_invitation!(invitation_token: params[:invitation_token], password: 'ad97nwj3o2', name: 'John Doe')

=== Callbacks

Expand Down Expand Up @@ -340,7 +340,7 @@ You would have a User model which is configured as invitable and an Admin model
class ApplicationController < ActionController::Base
protected
def authenticate_inviter!
authenticate_admin!(:force => true)
authenticate_admin!(force: true)
end
end

Expand All @@ -357,11 +357,11 @@ If you want to get all records invited by a resource, you should define has_many

For the default behavior, define it like this:

has_many :invitations, :class_name => self.to_s, :as => :invited_by
has_many :invitations, class_name: self.to_s, as: :invited_by

For the previous example, where admins send invitations to users, define it like this:

has_many :invitations, :class_name => 'User', :as => :invited_by
has_many :invitations, class_name: 'User', as: :invited_by

== I18n

Expand Down Expand Up @@ -422,14 +422,10 @@ https://github.com/scambra/devise_invitable/wiki

== Testing

To test DeviseInvitable for the ActiveRecord ORM with RVM, Ruby 2.2.2:
To run tests:

rvm use 2.2.2
rvm gemset create devise_invitable
rvm gemset use devise_invitable
gem install bundler
bundle
rake test DEVISE_ORM=active_record
bundle install
bundle exec rake test

== Contributors

Expand All @@ -449,4 +445,4 @@ Special thanks to rymai[https://github.com/rymai] for the Rails 3 support, his f

== Copyright

Copyright (c) 2014 Sergio Cambra. See LICENSE for details.
Copyright (c) 2019 Sergio Cambra. See LICENSE for details.
18 changes: 9 additions & 9 deletions app/controllers/devise/invitations_controller.rb
@@ -1,8 +1,8 @@
class Devise::InvitationsController < DeviseController
prepend_before_action :authenticate_inviter!, :only => [:new, :create]
prepend_before_action :has_invitations_left?, :only => [:create]
prepend_before_action :require_no_authentication, :only => [:edit, :update, :destroy]
prepend_before_action :resource_from_invitation_token, :only => [:edit, :destroy]
prepend_before_action :authenticate_inviter!, only: [:new, :create]
prepend_before_action :has_invitations_left?, only: [:create]
prepend_before_action :require_no_authentication, only: [:edit, :update, :destroy]
prepend_before_action :resource_from_invitation_token, only: [:edit, :destroy]

if respond_to? :helper_method
helper_method :after_sign_in_path_for
Expand All @@ -23,12 +23,12 @@ def create

if resource_invited
if is_flashing_format? && self.resource.invitation_sent_at
set_flash_message :notice, :send_instructions, :email => self.resource.email
set_flash_message :notice, :send_instructions, email: self.resource.email
end
if self.method(:after_invite_path_for).arity == 1
respond_with resource, :location => after_invite_path_for(current_inviter)
respond_with resource, location: after_invite_path_for(current_inviter)
else
respond_with resource, :location => after_invite_path_for(current_inviter, resource)
respond_with resource, location: after_invite_path_for(current_inviter, resource)
end
else
respond_with_navigational(resource) { render :new }
Expand All @@ -55,10 +55,10 @@ def update
flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
set_flash_message :notice, flash_message if is_flashing_format?
sign_in(resource_name, resource)
respond_with resource, :location => after_accept_path_for(resource)
respond_with resource, location: after_accept_path_for(resource)
else
set_flash_message :notice, :updated_not_active if is_flashing_format?
respond_with resource, :location => new_session_path(resource_name)
respond_with resource, location: new_session_path(resource_name)
end
else
resource.invitation_token = raw_invitation_token
Expand Down
Expand Up @@ -3,7 +3,7 @@ class DeviseInvitable::RegistrationsController < Devise::RegistrationsController

def build_resource(hash = {})
if hash[:email]
self.resource = resource_class.where(:email => hash[:email]).first
self.resource = resource_class.where(email: hash[:email]).first
if self.resource && self.resource.respond_to?(:invited_to_sign_up?) && self.resource.invited_to_sign_up?
self.resource.attributes = hash
self.resource.send_confirmation_instructions if self.resource.confirmation_required_for_invited?
Expand Down
6 changes: 3 additions & 3 deletions lib/devise_invitable.rb
Expand Up @@ -44,9 +44,9 @@ module Devise
#
# Examples (in config/initializers/devise.rb)
#
# config.invite_key = {:email => /\A[^@]+@[^@]+\z/}
# config.invite_key = { email: /\A[^@]+@[^@]+\z/ }
mattr_accessor :invite_key
@@invite_key = {:email => Devise.email_regexp}
@@invite_key = { email: Devise.email_regexp }

# Public: Resend invitation if user with invited status is invited again
# (default: true)
Expand Down Expand Up @@ -84,4 +84,4 @@ module Devise
@@require_password_on_accepting = true
end

Devise.add_module :invitable, :controller => :invitations, :model => 'devise_invitable/models', :route => {:invitation => [nil, :new, :accept]}
Devise.add_module :invitable, controller: :invitations, model: 'devise_invitable/models', route: { invitation: [nil, :new, :accept] }
2 changes: 1 addition & 1 deletion lib/devise_invitable/controllers/helpers.rb
Expand Up @@ -15,7 +15,7 @@ def after_accept_path_for(resource)
protected

def authenticate_inviter!
send(:"authenticate_#{resource_name}!", :force => true)
send(:"authenticate_#{resource_name}!", force: true)
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/devise_invitable/mailer.rb
Expand Up @@ -4,7 +4,7 @@ module DeviseInvitable
module Mailer

# Deliver an invitation email
def invitation_instructions(record, token, opts={})
def invitation_instructions(record, token, opts = {})
@token = token
devise_mail(record, :invitation_instructions, opts)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/devise_invitable/mapping.rb
Expand Up @@ -3,7 +3,7 @@ module Mapping
private
def default_controllers(options)
options[:controllers] ||= {}
options[:controllers][:registrations] ||= "devise_invitable/registrations"
options[:controllers][:registrations] ||= 'devise_invitable/registrations'
super
end
end
Expand Down
34 changes: 17 additions & 17 deletions lib/devise_invitable/models.rb
Expand Up @@ -17,8 +17,8 @@ module Models
# Examples:
#
# User.find(1).invited_to_sign_up? # => true/false
# User.invite!(:email => 'someone@example.com') # => send invitation
# User.accept_invitation!(:invitation_token => '...') # => accept invitation with a token
# User.invite!(email: 'someone@example.com') # => send invitation
# User.accept_invitation!(invitation_token: '...') # => accept invitation with a token
# User.find(1).accept_invitation! # => accept invitation
# User.find(1).invite! # => reset invitation status and send invitation again
module Invitable
Expand All @@ -31,34 +31,34 @@ module Invitable
included do
include ::DeviseInvitable::Inviter
belongs_to_options = if Devise.invited_by_class_name
{:class_name => Devise.invited_by_class_name}
{ class_name: Devise.invited_by_class_name }
else
{:polymorphic => true}
{ polymorphic: true }
end
if fk = Devise.invited_by_foreign_key
belongs_to_options[:foreign_key] = fk
end
if defined?(ActiveRecord) && defined?(ActiveRecord::Base) && self < ActiveRecord::Base
counter_cache = Devise.invited_by_counter_cache
belongs_to_options.merge! :counter_cache => counter_cache if counter_cache
belongs_to_options.merge! :optional => true if ActiveRecord::VERSION::MAJOR >= 5
belongs_to_options.merge! counter_cache: counter_cache if counter_cache
belongs_to_options.merge! optional: true if ActiveRecord::VERSION::MAJOR >= 5
elsif defined?(Mongoid) && defined?(Mongoid::Document) && self < Mongoid::Document && Mongoid::VERSION >= '6.0.0'
belongs_to_options.merge! :optional => true
belongs_to_options.merge! optional: true
end
belongs_to :invited_by, belongs_to_options

extend ActiveModel::Callbacks
define_model_callbacks :invitation_created
define_model_callbacks :invitation_accepted

scope :no_active_invitation, lambda { where(:invitation_token => nil) }
scope :no_active_invitation, lambda { where(invitation_token: nil) }
if defined?(Mongoid) && defined?(Mongoid::Document) && self < Mongoid::Document
scope :created_by_invite, lambda { where(:invitation_created_at.ne => nil) }
scope :invitation_not_accepted, lambda { where(:invitation_accepted_at => nil, :invitation_token.ne => nil) }
scope :invitation_not_accepted, lambda { where(invitation_accepted_at: nil, :invitation_token.ne => nil) }
scope :invitation_accepted, lambda { where(:invitation_accepted_at.ne => nil) }
else
scope :created_by_invite, lambda { where(arel_table[:invitation_created_at].not_eq(nil)) }
scope :invitation_not_accepted, lambda { where(arel_table[:invitation_token].not_eq(nil)).where(:invitation_accepted_at => nil) }
scope :invitation_not_accepted, lambda { where(arel_table[:invitation_token].not_eq(nil)).where(invitation_accepted_at: nil) }
scope :invitation_accepted, lambda { where(arel_table[:invitation_accepted_at].not_eq(nil)) }

callbacks = [
Expand Down Expand Up @@ -158,7 +158,7 @@ def self.confirmation_required?; false; end
self.downcase_keys if new_record_and_responds_to?(:downcase_keys)
self.strip_whitespace if new_record_and_responds_to?(:strip_whitespace)

if save(:validate => false)
if save(validate: false)
self.invited_by.decrement_invitation_limit! if !was_invited and self.invited_by.present?
deliver_invitation(options) unless skip_invitation
end
Expand Down Expand Up @@ -274,7 +274,7 @@ def generate_invitation_token
end

def generate_invitation_token!
generate_invitation_token && save(:validate => false)
generate_invitation_token && save(validate: false)
end

def new_record_and_responds_to?(method)
Expand All @@ -298,7 +298,7 @@ def invite_key_fields
# email is resent unless resend_invitation is set to false.
# Attributes must contain the user's email, other attributes will be
# set in the record
def _invite(attributes={}, invited_by=nil, options = {}, &block)
def _invite(attributes = {}, invited_by = nil, options = {}, &block)
invite_key_array = invite_key_fields
attributes_hash = {}
invite_key_array.each do |k,v|
Expand Down Expand Up @@ -328,12 +328,12 @@ def _invite(attributes={}, invited_by=nil, options = {}, &block)
[invitable, mail]
end

def invite!(attributes={}, invited_by=nil, options = {}, &block)
def invite!(attributes = {}, invited_by = nil, options = {}, &block)
attr_hash = ActiveSupport::HashWithIndifferentAccess.new(attributes.to_h)
_invite(attr_hash, invited_by, options, &block).first
end

def invite_mail!(attributes={}, invited_by=nil, options = {}, &block)
def invite_mail!(attributes = {}, invited_by = nil, options = {}, &block)
_invite(attributes, invited_by, options, &block).last
end

Expand All @@ -342,7 +342,7 @@ def invite_mail!(attributes={}, invited_by=nil, options = {}, &block)
# the record. If not user is found, returns a new user containing an
# error in invitation_token attribute.
# Attributes must contain invitation_token, password and confirmation
def accept_invitation!(attributes={})
def accept_invitation!(attributes = {})
original_token = attributes.delete(:invitation_token)
invitable = find_by_invitation_token(original_token, false)
if invitable.errors.empty?
Expand Down Expand Up @@ -397,7 +397,7 @@ def after_invitation_accepted(*args, &blk)
# lower + upper case, a digit and a symbol.
# For more unusual rules, this method can be overridden.
def random_password
"aA1!" + Devise.friendly_token[0, 20]
'aA1!' + Devise.friendly_token[0, 20]
end

end
Expand Down

0 comments on commit 71a1991

Please sign in to comment.