josevalim-auth_helpers 0.2.1 → 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.
- data/CHANGELOG +7 -0
- data/README +12 -1
- data/lib/auth_helpers/controller/confirmable.rb +71 -0
- data/lib/auth_helpers/controller/helpers.rb +63 -0
- data/lib/auth_helpers/controller/recoverable.rb +79 -0
- metadata +5 -2
data/CHANGELOG
CHANGED
data/README
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
AuthHelpers
|
2
2
|
License: MIT
|
3
|
-
Version: 0.
|
3
|
+
Version: 0.3.0
|
4
4
|
|
5
5
|
You can also read this README in pretty html at the GitHub project Wiki page:
|
6
6
|
|
@@ -13,6 +13,17 @@ AuthHelpers is a collection of modules to improve your Authlogic models. It
|
|
13
13
|
mainly exposes some convenience methods to help on confirmation and passwords
|
14
14
|
management, it also deals automatically with associations and notifications.
|
15
15
|
|
16
|
+
Authlogic also include some controllers to deal with the modules added. For
|
17
|
+
example, if you add Recoverable to your model, you can have all password
|
18
|
+
management for free, if you inherit from the Recoverable controller:
|
19
|
+
|
20
|
+
class AccountPasswordsController < AuthHelpers::Controller:Recoverable
|
21
|
+
end
|
22
|
+
|
23
|
+
This functionality is powered by InheritedResources, available at:
|
24
|
+
|
25
|
+
http://github.com/josevalim/inherited_resources
|
26
|
+
|
16
27
|
Installation
|
17
28
|
------------
|
18
29
|
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module AuthHelpers
|
2
|
+
module Controller
|
3
|
+
class Confirmable < ::ApplicationController
|
4
|
+
unloadable
|
5
|
+
|
6
|
+
include InheritedResources::BaseHelpers
|
7
|
+
include AuthHelpers::Controller::Helpers
|
8
|
+
|
9
|
+
class << self
|
10
|
+
alias :has_confirmable :set_class_accessors_with_class
|
11
|
+
end
|
12
|
+
|
13
|
+
# GET /account/confirmation/new
|
14
|
+
def new(&block)
|
15
|
+
object = get_or_set_with_send(:new)
|
16
|
+
respond_to(:with => object, &block)
|
17
|
+
end
|
18
|
+
alias :new! :new
|
19
|
+
|
20
|
+
# POST /account/confirmation
|
21
|
+
# POST /account/confirmation.xml
|
22
|
+
def create(&block)
|
23
|
+
object = get_or_set_with_send(:find_and_resend_confirmation_instructions, params[self.instance_name])
|
24
|
+
respond_block, redirect_block = select_block_by_arity(block)
|
25
|
+
|
26
|
+
if object.errors.empty?
|
27
|
+
set_flash_message!(:notice, 'We sent confirmation instructions to your email, please check your inbox.')
|
28
|
+
|
29
|
+
respond_to_with_dual_blocks(true, block) do |format|
|
30
|
+
format.html { redirect_to_block_or_scope_to(redirect_block, :session) }
|
31
|
+
format.all { head :ok }
|
32
|
+
end
|
33
|
+
else
|
34
|
+
set_flash_message!(:error)
|
35
|
+
options = { :with => object.errors, :status => :unprocessable_entity }
|
36
|
+
|
37
|
+
respond_to_with_dual_blocks(false, block, options) do |format|
|
38
|
+
format.html { render :action => 'new' }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
alias :create! :create
|
43
|
+
|
44
|
+
# GET /account/confirmation?account[perishable_token]=xxxx
|
45
|
+
# GET /account/confirmation.xml?account[perishable_token]=xxxx
|
46
|
+
def show(&block)
|
47
|
+
object = get_or_set_with_send(:find_and_confirm, params[self.instance_name])
|
48
|
+
respond_block, redirect_block = select_block_by_arity(block)
|
49
|
+
|
50
|
+
if object.errors.empty?
|
51
|
+
set_flash_message!(:notice, '{{resource_name}} was successfully confirmed.')
|
52
|
+
|
53
|
+
respond_to_with_dual_blocks(true, block) do |format|
|
54
|
+
format.html { redirect_to_block_or_scope_to(redirect_block, :session) }
|
55
|
+
format.all { head :ok }
|
56
|
+
end
|
57
|
+
else
|
58
|
+
set_flash_message!(:error, object.errors.on(:perishable_token))
|
59
|
+
options = { :with => object.errors, :status => :unprocessable_entity }
|
60
|
+
|
61
|
+
respond_to_with_dual_blocks(false, block, options) do |format|
|
62
|
+
format.html { redirect_to :action => 'new' }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
alias :show! :show
|
67
|
+
|
68
|
+
protected :show!, :new!, :create!
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module AuthHelpers
|
2
|
+
module Controller
|
3
|
+
module Helpers
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
protected
|
11
|
+
|
12
|
+
# Writes the inherited hook for the included class based on its name.
|
13
|
+
#
|
14
|
+
def inherited(base) #:nodoc:
|
15
|
+
super
|
16
|
+
|
17
|
+
base.send :cattr_accessor, :resource_class, :instance_name, :route_name, :instance_writter => false
|
18
|
+
|
19
|
+
# Converts:
|
20
|
+
#
|
21
|
+
# Mockable::ConfirmationsController #=> mockable
|
22
|
+
# MockablePasswordsController #=> mockable
|
23
|
+
#
|
24
|
+
resource = base.controller_path.gsub('/', '_').split('_')[0..-2].join('_')
|
25
|
+
|
26
|
+
begin
|
27
|
+
base.resource_class = resource.classify.constantize
|
28
|
+
base.route_name = resource.singularize
|
29
|
+
base.instance_name = resource.singularize
|
30
|
+
rescue NameError
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def set_class_accessors_with_class(klass)
|
36
|
+
self.resource_class = klass
|
37
|
+
self.instance_name = klass.name.downcase
|
38
|
+
self.route_name = klass.name.downcase
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
protected
|
43
|
+
|
44
|
+
# If a block is given, redirect to the url in the block, otherwise
|
45
|
+
# try to call the url given by scope, for example:
|
46
|
+
#
|
47
|
+
# new_account_session_url
|
48
|
+
# new_account_password_url
|
49
|
+
#
|
50
|
+
def redirect_to_block_or_scope_to(redirect_block, scope) #:nodoc:
|
51
|
+
redirect_to redirect_block ? redirect_block.call : send("new_#{self.route_name}_#{scope}_url")
|
52
|
+
end
|
53
|
+
|
54
|
+
# Try to get the instance variable, otherwise send the args given to
|
55
|
+
# the resource class and store the result in the same instance variable.
|
56
|
+
#
|
57
|
+
def get_or_set_with_send(*args) #:nodoc:
|
58
|
+
instance_variable_get("@#{self.instance_name}") || instance_variable_set("@#{self.instance_name}", resource_class.send(*args))
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module AuthHelpers
|
2
|
+
module Controller
|
3
|
+
class Recoverable < ::ApplicationController
|
4
|
+
unloadable
|
5
|
+
|
6
|
+
include InheritedResources::BaseHelpers
|
7
|
+
include AuthHelpers::Controller::Helpers
|
8
|
+
|
9
|
+
class << self
|
10
|
+
alias :has_recoverable :set_class_accessors_with_class
|
11
|
+
end
|
12
|
+
|
13
|
+
# GET /account/password/new
|
14
|
+
def new(&block)
|
15
|
+
object = get_or_set_with_send(:new)
|
16
|
+
respond_to(:with => object, &block)
|
17
|
+
end
|
18
|
+
alias :new! :new
|
19
|
+
|
20
|
+
# POST /account/password
|
21
|
+
# POST /account/password.xml
|
22
|
+
def create(&block)
|
23
|
+
object = get_or_set_with_send(:find_and_send_reset_password_instructions, params[self.instance_name])
|
24
|
+
respond_block, redirect_block = select_block_by_arity(block)
|
25
|
+
|
26
|
+
if object.errors.empty?
|
27
|
+
set_flash_message!(:notice, 'We sent instruction to reset your password, please check your inbox.')
|
28
|
+
|
29
|
+
respond_to_with_dual_blocks(true, block) do |format|
|
30
|
+
format.html { redirect_to_block_or_scope_to(redirect_block, :session) }
|
31
|
+
format.all { head :ok }
|
32
|
+
end
|
33
|
+
else
|
34
|
+
set_flash_message!(:error)
|
35
|
+
options = { :with => object.errors, :status => :unprocessable_entity }
|
36
|
+
|
37
|
+
respond_to_with_dual_blocks(false, block, options) do |format|
|
38
|
+
format.html { render :action => 'new' }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
alias :create! :create
|
43
|
+
|
44
|
+
# GET /account/password/edit?account[perishable_token]=xxxx
|
45
|
+
def edit(&block)
|
46
|
+
object = get_or_set_with_send(:new)
|
47
|
+
object.perishable_token = params[self.instance_name].try(:fetch, :perishable_token)
|
48
|
+
respond_to(:with => object, &block)
|
49
|
+
end
|
50
|
+
alias :edit! :edit
|
51
|
+
|
52
|
+
# PUT /account/password
|
53
|
+
# PUT /account/password.xml
|
54
|
+
def update(&block)
|
55
|
+
object = get_or_set_with_send(:find_and_reset_password, params[self.instance_name])
|
56
|
+
respond_block, redirect_block = select_block_by_arity(block)
|
57
|
+
|
58
|
+
if object.errors.empty?
|
59
|
+
set_flash_message!(:notice, 'Your password was successfully reset.')
|
60
|
+
|
61
|
+
respond_to_with_dual_blocks(true, block) do |format|
|
62
|
+
format.html { redirect_to_block_or_scope_to(redirect_block, :session) }
|
63
|
+
format.all { head :ok }
|
64
|
+
end
|
65
|
+
else
|
66
|
+
set_flash_message!(:error)
|
67
|
+
options = { :with => object.errors, :status => :unprocessable_entity }
|
68
|
+
|
69
|
+
respond_to_with_dual_blocks(false, block, options) do |format|
|
70
|
+
format.html { render :action => 'edit' }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
alias :update! :update
|
75
|
+
|
76
|
+
protected :new!, :create!, :edit!, :update!
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: josevalim-auth_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- "Jos\xC3\xA9 Valim"
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-05-03 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -37,6 +37,9 @@ files:
|
|
37
37
|
- init.rb
|
38
38
|
- lib/auth_helpers.rb
|
39
39
|
- lib/auth_helpers/notifier.rb
|
40
|
+
- lib/auth_helpers/controller/confirmable.rb
|
41
|
+
- lib/auth_helpers/controller/helpers.rb
|
42
|
+
- lib/auth_helpers/controller/recoverable.rb
|
40
43
|
- lib/auth_helpers/model/associatable.rb
|
41
44
|
- lib/auth_helpers/model/confirmable.rb
|
42
45
|
- lib/auth_helpers/model/recoverable.rb
|