devise_invitable 1.4.2 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise_invitable might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 751224bca4a80bf6eb364d3c11d58a2771d36acf
4
- data.tar.gz: e5a102a756fd4061ee2bffdc345dfc1dd94f15f5
3
+ metadata.gz: 9ebdddd2e1426fdde9ad700f7f60e4cedf3e48ab
4
+ data.tar.gz: 3a783c0d76c73a4a6af2c07cda15b80904fecdcf
5
5
  SHA512:
6
- metadata.gz: d7ed72e6393da5bd986570e981d13c3bc3b1d39af6b12c0ebc7499e4b95d838ed8458833f631f766934c46808b2a9c2026a78d307c2f78196b9addecc4541d57
7
- data.tar.gz: 5ace95e6bdb544c6e67669d7c6576ba09a74985c92974db6a43a2898b28080cf2bb9d7615acf138969efc24c4949e2149dc1ed51dea4c7512c6933d52c9ccbf5
6
+ metadata.gz: b85b1eabee1011a5175f98ca92f05f39a1f49e3da2666f17bafa8a87d22115700d6f7beee89261a5096d70dcfeec473834dc43bfcaa5a967b63fc78b7789bb7f
7
+ data.tar.gz: 483b574690f8a3064b7d4f941a7fad4e321fe89d677192b82e68d0d0f3feba49d92125054f17e89521c2810e9feb80de12ee135895728d7d1f633bdd845845ea
@@ -1,5 +1,5 @@
1
1
  = DeviseInvitable
2
- {<img src="https://travis-ci.org/scambra/devise_invitable.png"/>}[http://travis-ci.org/scambra/devise_invitable]
2
+ {<img src="https://travis-ci.org/scambra/devise_invitable.png"/>}[http://travis-ci.org/scambra/devise_invitable] {<img src="https://codeclimate.com/github/scambra/devise_invitable/badges/gpa.svg"/}[https://codeclimate.com/github/scambra/devise_invitable]
3
3
 
4
4
  It adds support to devise[http://github.com/plataformatec/devise] for sending invitations by email (it requires to be authenticated) and accept the invitation setting the password.
5
5
 
@@ -256,9 +256,11 @@ You can add :skip_invitation to attributes hash if skip_invitation is added to a
256
256
  User.invite!(:email => "new_user@example.com", :name => "John Doe", :skip_invitation => true)
257
257
  # => the record will be created, but the invitation email will not be sent
258
258
 
259
- Skip_invitation skips sending the email, but sets invitation_token, so invited_to_sign_up? on the
259
+ Skip_invitation skips sending the email, but sets invitation_token, so <tt>invited_to_sign_up?</tt> on the
260
260
  resulting user returns true.
261
261
 
262
+ To check if a perticular user is created by invitation, irrespective to state of invitation one can use <tt>created_by_invite?</tt>
263
+
262
264
  **Warning**
263
265
 
264
266
  When using skip_invitation you must send the email with the user object instance that generated the tokens, as
@@ -297,8 +299,9 @@ The callbacks support all options and arguments available to the standard callba
297
299
 
298
300
  A pair of scopes to find those users that have accepted, and those that have not accepted, invitations are defined:
299
301
 
300
- User.invitation_accepted # => returns all Users for whom the invitation_accepted_at attribute is not nil
302
+ User.invitation_accepted # => returns all Users for whom the invitation_accepted_at attribute is not nil
301
303
  User.invitation_not_accepted # => returns all Users for whom the invitation_accepted_at attribute is nil
304
+ User.created_by_invite # => returns all Users who are created by invitations, irrespective to invitation status
302
305
 
303
306
  == Integration in a Rails application
304
307
 
@@ -47,9 +47,11 @@ module Devise
47
47
 
48
48
  scope :no_active_invitation, lambda { where(:invitation_token => nil) }
49
49
  if defined?(Mongoid) && defined?(Mongoid::Document) && self < Mongoid::Document
50
+ scope :created_by_invite, lambda { where(:invitation_sent_at.ne => nil) }
50
51
  scope :invitation_not_accepted, lambda { where(:invitation_accepted_at => nil, :invitation_token.ne => nil) }
51
52
  scope :invitation_accepted, lambda { where(:invitation_accepted_at.ne => nil) }
52
53
  else
54
+ scope :created_by_invite, lambda { where(arel_table[:invitation_sent_at].not_eq(nil)) }
53
55
  scope :invitation_not_accepted, lambda { where(arel_table[:invitation_token].not_eq(nil)).where(:invitation_accepted_at => nil) }
54
56
  scope :invitation_accepted, lambda { where(arel_table[:invitation_accepted_at].not_eq(nil)) }
55
57
 
@@ -85,6 +87,11 @@ module Devise
85
87
  end
86
88
  end
87
89
 
90
+ # Verify wheather a user is created by invitation, irrespective to invitation status
91
+ def created_by_invite?
92
+ invitation_sent_at.present?
93
+ end
94
+
88
95
  # Verifies whether a user has been invited or not
89
96
  def invited_to_sign_up?
90
97
  persisted? && invitation_token.present?
@@ -102,23 +109,24 @@ module Devise
102
109
 
103
110
  # Reset invitation token and send invitation again
104
111
  def invite!(invited_by = nil)
112
+ # This is an order-dependant assignment, this can't be moved
105
113
  was_invited = invited_to_sign_up?
106
114
 
107
115
  # Required to workaround confirmable model's confirmation_required? method
108
116
  # being implemented to check for non-nil value of confirmed_at
109
- if self.new_record? && self.respond_to?(:confirmation_required?, true)
117
+ if new_record_and_responds_to?(:confirmation_required?)
110
118
  def self.confirmation_required?; false; end
111
119
  end
112
120
 
113
121
  yield self if block_given?
114
- generate_invitation_token if self.invitation_token.nil? || (!skip_invitation || @raw_invitation_token.nil?)
122
+ generate_invitation_token if no_token_present_or_skip_invitation?
115
123
  self.invitation_created_at = Time.now.utc
116
124
  self.invitation_sent_at = self.invitation_created_at unless skip_invitation
117
125
  self.invited_by = invited_by if invited_by
118
126
 
119
127
  # Call these before_validate methods since we aren't validating on save
120
- self.downcase_keys if self.new_record? && self.respond_to?(:downcase_keys, true)
121
- self.strip_whitespace if self.new_record? && self.respond_to?(:strip_whitespace, true)
128
+ self.downcase_keys if new_record_and_responds_to?(:downcase_keys)
129
+ self.strip_whitespace if new_record_and_responds_to?(:strip_whitespace)
122
130
 
123
131
  if save(:validate => false)
124
132
  self.invited_by.decrement_invitation_limit! if !was_invited and self.invited_by.present?
@@ -143,7 +151,7 @@ module Devise
143
151
  end
144
152
 
145
153
  def after_password_reset
146
- super
154
+ super if defined?(super)
147
155
  accept_invitation! if invited_to_sign_up?
148
156
  end
149
157
 
@@ -219,6 +227,14 @@ module Devise
219
227
  generate_invitation_token && save(:validate => false)
220
228
  end
221
229
 
230
+ def new_record_and_responds_to?(method)
231
+ self.new_record? && self.respond_to?(method, true)
232
+ end
233
+
234
+ def no_token_present_or_skip_invitation?
235
+ self.invitation_token.nil? || (!skip_invitation || @raw_invitation_token.nil?)
236
+ end
237
+
222
238
  module ClassMethods
223
239
  # Return fields to invite
224
240
  def invite_key_fields
@@ -308,6 +324,7 @@ module Devise
308
324
  Devise::Models.config(self, :invite_key)
309
325
  Devise::Models.config(self, :resend_invitation)
310
326
  Devise::Models.config(self, :allow_insecure_sign_in_after_accept)
327
+
311
328
  end
312
329
  end
313
330
  end
@@ -1,3 +1,3 @@
1
1
  module DeviseInvitable
2
- VERSION = '1.4.2'
2
+ VERSION = '1.5.0'
3
3
  end
@@ -6,10 +6,10 @@ class DeviseInvitableAddTo<%= table_name.camelize %> < ActiveRecord::Migration
6
6
  t.datetime :invitation_sent_at
7
7
  t.datetime :invitation_accepted_at
8
8
  t.integer :invitation_limit
9
- t.references :invited_by, :polymorphic => true
9
+ t.references :invited_by, polymorphic: true
10
10
  t.integer :invitations_count, default: 0
11
11
  t.index :invitations_count
12
- t.index :invitation_token, :unique => true # for invitable
12
+ t.index :invitation_token, unique: true # for invitable
13
13
  t.index :invited_by_id
14
14
  end
15
15
 
@@ -22,7 +22,7 @@ class DeviseInvitableAddTo<%= table_name.camelize %> < ActiveRecord::Migration
22
22
 
23
23
  def down
24
24
  change_table :<%= table_name %> do |t|
25
- t.remove_references :invited_by, :polymorphic => true
25
+ t.remove_references :invited_by, polymorphic: true
26
26
  t.remove :invitations_count, :invitation_limit, :invitation_sent_at, :invitation_accepted_at, :invitation_token, :invitation_created_at
27
27
  end
28
28
  change_column_null :<%= table_name %>, :encrypted_password, false
@@ -1,6 +1,6 @@
1
1
  <h2><%= t 'devise.invitations.edit.header' %></h2>
2
2
 
3
- <%= simple_form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => { :method => :put } do |f| %>
3
+ <%= simple_form_for resource, as: resource_name, url: invitation_path(resource_name), html: { method: :put } do |f| %>
4
4
  <%= devise_error_messages! %>
5
5
  <%= f.hidden_field :invitation_token %>
6
6
 
@@ -1,6 +1,6 @@
1
1
  <h2><%= t "devise.invitations.new.header" %></h2>
2
2
 
3
- <%= simple_form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :post} do |f| %>
3
+ <%= simple_form_for resource, as: resource_name, url: invitation_path(resource_name), html: { method: :post} do |f| %>
4
4
  <%= devise_error_messages! %>
5
5
 
6
6
  <% resource.class.invite_key_fields.each do |field| -%>
@@ -431,6 +431,21 @@ class InvitableTest < ActiveSupport::TestCase
431
431
  user.reload
432
432
  assert !user.valid_password?('new_password')
433
433
  end
434
+
435
+ test 'should check if created by invitation' do
436
+ user = User.invite!(:email => "valid@email.com")
437
+ assert user.created_by_invite?
438
+
439
+ invited_user = User.accept_invitation!(
440
+ :invitation_token => Thread.current[:token],
441
+ :password => 'new_password',
442
+ :password_confirmation => 'new_password',
443
+ :username => 'a'
444
+ )
445
+ user.reload
446
+ assert user.created_by_invite?
447
+ end
448
+
434
449
 
435
450
  test 'should set other attributes on accepting invitation' do
436
451
  user = new_user(:password => nil, :password_confirmation => nil)
@@ -611,19 +626,23 @@ class InvitableTest < ActiveSupport::TestCase
611
626
  assert !user.errors.empty?
612
627
  end
613
628
 
614
- test "should count accepted and not accepted invitations" do
629
+ test "should count invited, created_by_invite, accepted and not accepted invitations" do
615
630
  assert_equal 0, User.invitation_not_accepted.count
616
631
  assert_equal 0, User.invitation_accepted.count
632
+ assert_equal 0, User.created_by_invite.count
617
633
 
618
634
  User.invite!(:email => "invalid@email.com")
635
+ User.invite!(:email => "another_invalid@email.com")
619
636
  user = User.invite!(:email => "valid@email.com")
620
637
 
621
- assert_equal 2, User.invitation_not_accepted.count
638
+ assert_equal 3, User.invitation_not_accepted.count
622
639
  assert_equal 0, User.invitation_accepted.count
640
+ assert_equal 3, User.created_by_invite.count
623
641
 
624
642
  user.accept_invitation!
625
- assert_equal 1, User.invitation_not_accepted.count
643
+ assert_equal 2, User.invitation_not_accepted.count
626
644
  assert_equal 1, User.invitation_accepted.count
645
+ assert_equal 3, User.created_by_invite.count
627
646
  end
628
647
 
629
648
  test "should preserve return values of Devise::Recoverable#reset_password!" do
@@ -20,6 +20,5 @@ module RailsApp
20
20
  class Application < Rails::Application
21
21
  config.filter_parameters << :password
22
22
  config.action_mailer.default_url_options = { :host => "localhost:3000" }
23
- config.i18n.enforce_available_locales = true
24
23
  end
25
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise_invitable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergio Cambra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-27 00:00:00.000000000 Z
11
+ date: 2015-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler