devise-approvable 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de5148ee11a32aa00bb79e559911a873ccc07953
4
- data.tar.gz: 36095351c6822ddee8f5eecc8544dfa175af4740
3
+ metadata.gz: 1bf6254958c077f9db90bb7ec5dc6d6ef447ca7e
4
+ data.tar.gz: 531778b042291c73449f14ec291da0c11113b943
5
5
  SHA512:
6
- metadata.gz: 889b074e0469a091c7074140052e179d16e19aa4adecc0a56276f87eb88e72a6374f290ba2820984701ee0e33c8f604ff30d6f13bb0699cfe95b72940c6498c8
7
- data.tar.gz: 0cadf9790002c786561f0f77246ec4417e06be7afd018b98411cdb548cd7e6c72ed9bbc0c49bf92025d3bec8af20a2068e8760ce989cdef7a8a1a7d4696bf468
6
+ metadata.gz: 30c89efcedb4d1787decb362dfeb58b154dd618518e885c90fb0900ca4681dc9a766750e95e265c5bceeaebc1de82477217310a1afb86c9102eb60a8b5f9bbda
7
+ data.tar.gz: 064b9c741ad42d97192e334d89afc7206799cee2f20a064d67d2a6c8db139d9e4057c7ef484f794a8657793be900b4c09743ae70966938f054f2ed62e0040c1e
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -0,0 +1,14 @@
1
+ class Devise::ApprovalsController < DeviseController
2
+ # GET /resource/confirmation?confirmation_token=abcdef
3
+ def show
4
+ self.resource = resource_class.approve_by_token(params[:approval_token])
5
+ yield resource if block_given?
6
+
7
+ if resource.errors.empty?
8
+ set_flash_message(:notice, :approved) if is_flashing_format?
9
+ respond_with_navigational(resource){ redirect_to after_approval_path_for(resource_name, resource) }
10
+ else
11
+ respond_with_navigational(resource.errors, status: :unprocessable_entity){ render :new }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ <h2>Dev Account Approval</h2>
2
+
3
+ <%= form_for(resource, as: resource_name, url: approval_path(resource_name), html: { method: :post }) do |f| %>
4
+ <%= devise_error_messages! %>
5
+
6
+ <div class="field">
7
+ <%= f.label :email %><br />
8
+ <%= f.email_field :email, autofocus: true, value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %>
9
+ </div>
10
+
11
+ <div class="actions">
12
+ <%= f.submit "Resend confirmation instructions" %>
13
+ </div>
14
+ <% end %>
15
+
16
+ <%= render "devise/shared/links" %>
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: devise-approvable 0.2.0 ruby lib
5
+ # stub: devise-approvable 0.3.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "devise-approvable"
9
- s.version = "0.2.0"
9
+ s.version = "0.3.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Alex Egg"]
14
- s.date = "2015-04-03"
14
+ s.date = "2015-04-06"
15
15
  s.description = "adds feature to devise"
16
16
  s.email = "eggie5@gmail.com"
17
17
  s.extra_rdoc_files = [
@@ -23,10 +23,16 @@ Gem::Specification.new do |s|
23
23
  "README",
24
24
  "Rakefile",
25
25
  "VERSION",
26
+ "app/controllers/approvals_controller.rb",
27
+ "app/views/devise/approvals/new.html.erb",
26
28
  "config/initializers/devise.rb",
27
29
  "devise-approvable.gemspec",
28
- "lib/approvable.rb",
29
30
  "lib/approvable_mailer.rb",
31
+ "lib/devise_approvable.rb",
32
+ "lib/devise_approvable/mailer.rb",
33
+ "lib/devise_approvable/model.rb",
34
+ "lib/devise_approvable/routes.rb",
35
+ "lib/devise_approvable/version.rb",
30
36
  "test/approvable_test.rb",
31
37
  "views/devise_mailer/approval_instructions.html.erb"
32
38
  ]
@@ -0,0 +1,21 @@
1
+ module DeviseApprovable
2
+ autoload :Mailer, 'devise_approvable/mailer'
3
+ end
4
+
5
+ require 'devise'
6
+ require 'devise_approvable/routes'
7
+ # require 'devise_approvable/rails'
8
+
9
+ module Devise
10
+ # Public: Validity period of the invitation token (default: 0). If
11
+ # invite_for is 0 or nil, the invitation will never expire.
12
+ # Set invite_for in the Devise configuration file (in config/initializers/devise.rb).
13
+ #
14
+ # config.invite_for = 2.weeks # => The invitation token will be valid 2 weeks
15
+ mattr_accessor :admin_email
16
+ @@admin_email = "admin@curbcall.com"
17
+
18
+
19
+ end
20
+
21
+ Devise.add_module :approvable, :controller => :approvals, :model => 'devise_approvable/model', :route => {:approval => [nil, :new, :accept, :show]}
@@ -0,0 +1,11 @@
1
+ require 'devise/version'
2
+
3
+ module DeviseInvitable
4
+ module Mailer
5
+
6
+ def approval_instructions(record, token, opts={})
7
+ @token = token
8
+ devise_mail(record, :approval_instructions, opts)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,95 @@
1
+ module Devise
2
+ module Models
3
+ module Approvable
4
+
5
+ def self.included(base)
6
+ puts "Devise::Models::Approvable module injected into #{self.class} class"
7
+
8
+ base.class_eval do
9
+ extend ClassMethods
10
+
11
+ before_create :generate_approval_token, :if => :approval_required?
12
+ after_create :send_approval_instructions, :unless => :approved?
13
+ end
14
+ end
15
+
16
+ #override
17
+ def active_for_authentication?
18
+ super && approved?
19
+ end
20
+
21
+ #override
22
+ def inactive_message
23
+ if !approved?
24
+ :not_approved
25
+ else
26
+ super # Use whatever other message
27
+ end
28
+ end
29
+
30
+
31
+ def approve!
32
+ self.approved_at = Time.now.utc
33
+ self.approval_token = nil
34
+ save(:validate => false)
35
+ end
36
+
37
+ # Verifies whether a user is confirmed or not
38
+ def approved?
39
+ !!self.approved_at
40
+ end
41
+
42
+ def skip_approval!
43
+ self.approved_at = Time.now.utc
44
+ @skip_approval = true
45
+ end
46
+
47
+ # Send approval instructions by email
48
+ def send_approval_instructions
49
+ unless @raw_approval_token
50
+ generate_approval_token!
51
+ end
52
+
53
+ opts = { to: "dev-approvals@curbcall.com" }
54
+ send_devise_notification(:approval_instructions, @raw_approval_token, opts)
55
+ end
56
+
57
+
58
+ protected
59
+ def approval_required?
60
+ !@skip_approval
61
+ end
62
+
63
+ def generate_approval_token!
64
+ generate_approval_token && save(validate: false)
65
+ end
66
+
67
+ def generate_approval_token
68
+ raw, enc = Devise.token_generator.generate(self.class, :approval_token)
69
+ @raw_approval_token = raw
70
+ self.approval_token = enc
71
+ self.approval_sent_at = Time.now.utc
72
+ end
73
+
74
+ module ClassMethods
75
+ # Find a user by its approvable token and try to approve it.
76
+ # If no user is found, returns a new user with an error.
77
+ # If the user is already approved, create an error for the user
78
+ # Options must have the approval_token
79
+ def approve_by_token(approval_token)
80
+ original_token = approval_token
81
+ approval_token = Devise.token_generator.digest(self, :approval_token, approval_token)
82
+
83
+ approvable = find_or_initialize_with_error_by(:approval_token, approval_token)
84
+ approvable.approve! if approvable.persisted?
85
+ approvable.approval_token = original_token
86
+ approvable
87
+ end
88
+
89
+ Devise::Models.config(self, :approver_class)
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ # Devise.add_module :approvable, :model => 'approvable'
@@ -0,0 +1,9 @@
1
+ module ActionDispatch::Routing
2
+ class Mapper
3
+
4
+ protected
5
+ def devise_approval(mapping, controllers)
6
+ resource :approval, only: [:new, :create, :show], path: mapping.path_names[:approval], controller: controllers[:approvals]
7
+ end
8
+
9
+ end
@@ -0,0 +1,3 @@
1
+ module DeviseApprovable
2
+ VERSION = '1.4.2'
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise-approvable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Egg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-03 00:00:00.000000000 Z
11
+ date: 2015-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shoulda
@@ -92,10 +92,16 @@ files:
92
92
  - README
93
93
  - Rakefile
94
94
  - VERSION
95
+ - app/controllers/approvals_controller.rb
96
+ - app/views/devise/approvals/new.html.erb
95
97
  - config/initializers/devise.rb
96
98
  - devise-approvable.gemspec
97
- - lib/approvable.rb
98
99
  - lib/approvable_mailer.rb
100
+ - lib/devise_approvable.rb
101
+ - lib/devise_approvable/mailer.rb
102
+ - lib/devise_approvable/model.rb
103
+ - lib/devise_approvable/routes.rb
104
+ - lib/devise_approvable/version.rb
99
105
  - test/approvable_test.rb
100
106
  - views/devise_mailer/approval_instructions.html.erb
101
107
  homepage: http://github.com/eggie5/devise-approvable
data/lib/approvable.rb DELETED
@@ -1,176 +0,0 @@
1
- module Devise
2
- module Models
3
- module Approvable
4
- # extend ActiveSupport::Concern #RAILS 3
5
- include Devise::Models::Activatable
6
-
7
- def self.included(base)
8
- base.class_eval do
9
- extend ClassMethods
10
-
11
- before_create :generate_approval_token, :if => :approval_required?
12
- # after_create :send_approval_instructions unless approved?
13
- end
14
- end
15
-
16
- # Confirm a user by setting it's confirmed_at to actual time. If the user
17
- # is already confirmed, add en error to email field
18
- def confirm!
19
- unless_confirmed do
20
- self.confirmation_token = nil
21
- self.confirmed_at = Time.now
22
- save(false)
23
- end
24
- end
25
-
26
- # Verifies whether a user is confirmed or not
27
- def confirmed?
28
- !new_record? && !confirmed_at.nil?
29
- end
30
-
31
- # Send confirmation instructions by email
32
- def send_confirmation_instructions
33
- generate_confirmation_token if self.confirmation_token.nil?
34
- ::DeviseMailer.deliver_confirmation_instructions(self)
35
- end
36
-
37
- # Resend confirmation token. This method does not need to generate a new token.
38
- def resend_confirmation_token
39
- unless_confirmed { send_confirmation_instructions }
40
- end
41
-
42
- # Overwrites active? from Devise::Models::Activatable for confirmation
43
- # by verifying whether an user is active to sign in or not. If the user
44
- # is already confirmed, it should never be blocked. Otherwise we need to
45
- # calculate if the confirm time has not expired for this user.
46
- def active?
47
- super && (!confirmation_required? || confirmed? || confirmation_period_valid?)
48
- end
49
-
50
- # The message to be shown if the account is inactive.
51
- def inactive_message
52
- !confirmed? ? :unconfirmed : super
53
- end
54
-
55
- # If you don't want confirmation to be sent on create, neither a code
56
- # to be generated, call skip_confirmation!
57
- def skip_confirmation!
58
- self.confirmed_at = Time.now
59
- @skip_confirmation = true
60
- end
61
-
62
- def skip_confirm_and_approve!
63
- skip_confirmation!
64
- skip_approval!
65
- end
66
-
67
- def approve!
68
- self.is_approved = true
69
- self.approval_token = nil
70
- send_confirmation_instructions
71
- save(:validate => false)
72
- end
73
-
74
- # Verifies whether a user is confirmed or not
75
- def approved?
76
- self.is_approved
77
- end
78
-
79
- def skip_approval!
80
- self.is_approved = true
81
- @skip_approval = true
82
- end
83
-
84
- # Send confirmation instructions by email
85
- def send_approval_instructions
86
- generate_approval_token if self.approval_token.nil?
87
- ::DeviseMailer.deliver_approval_instructions(self)
88
- end
89
-
90
- protected
91
-
92
- # Callback to overwrite if confirmation is required or not.
93
- def confirmation_required?
94
- !@skip_confirmation
95
- end
96
-
97
- def approval_required?
98
- !@skip_approval
99
- end
100
-
101
- # Checks if the confirmation for the user is within the limit time.
102
- # We do this by calculating if the difference between today and the
103
- # confirmation sent date does not exceed the confirm in time configured.
104
- # Confirm_in is a model configuration, must always be an integer value.
105
- #
106
- # Example:
107
- #
108
- # # confirm_within = 1.day and confirmation_sent_at = today
109
- # confirmation_period_valid? # returns true
110
- #
111
- # # confirm_within = 5.days and confirmation_sent_at = 4.days.ago
112
- # confirmation_period_valid? # returns true
113
- #
114
- # # confirm_within = 5.days and confirmation_sent_at = 5.days.ago
115
- # confirmation_period_valid? # returns false
116
- #
117
- # # confirm_within = 0.days
118
- # confirmation_period_valid? # will always return false
119
- #
120
- def confirmation_period_valid?
121
- confirmation_sent_at && confirmation_sent_at.utc >= self.class.confirm_within.ago
122
- end
123
-
124
- # Checks whether the record is confirmed or not, yielding to the block
125
- # if it's already confirmed, otherwise adds an error to email.
126
- def unless_confirmed
127
- unless confirmed?
128
- yield
129
- else
130
- self.class.add_error_on(self, :email, :already_confirmed)
131
- false
132
- end
133
- end
134
-
135
- # Generates a new random token for confirmation, and stores the time
136
- # this token is being generated
137
- def generate_confirmation_token
138
- self.confirmed_at = nil
139
- self.confirmation_token = Devise.friendly_token
140
- self.confirmation_sent_at = Time.now.utc
141
- end
142
-
143
- def generate_approval_token
144
- self.is_approved = false
145
- self.approval_token = Devise.friendly_token
146
- self.approval_sent_at = Time.now.utc
147
- end
148
-
149
- module ClassMethods
150
- # Attempt to find a user by it's email. If a record is found, send new
151
- # confirmation instructions to it. If not user is found, returns a new user
152
- # with an email not found error.
153
- # Options must contain the user email
154
- def send_confirmation_instructions(attributes={})
155
- confirmable = find_or_initialize_with_error_by(:email, attributes[:email], :not_found)
156
- confirmable.resend_confirmation_token unless confirmable.new_record?
157
- confirmable
158
- end
159
-
160
- # Find a user by it's confirmation token and try to confirm it.
161
- # If no user is found, returns a new user with an error.
162
- # If the user is already confirmed, create an error for the user
163
- # Options must have the confirmation_token
164
- def confirm_by_token(confirmation_token)
165
- confirmable = find_or_initialize_with_error_by(:confirmation_token, confirmation_token)
166
- confirmable.confirm! unless confirmable.new_record?
167
- confirmable
168
- end
169
-
170
- Devise::Models.config(self, :confirm_within)
171
- end
172
- end
173
- end
174
- end
175
-
176
- Devise.add_module :approvable, :model => 'approvable'