devise_capturable 0.0.5 → 0.0.6

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/README.md CHANGED
@@ -20,7 +20,7 @@ You will need to perform these steps to setup the gem.
20
20
  #### Add gem to Gemfile
21
21
 
22
22
  ```ruby
23
- gem "devise_capturable", :git => "git://github.com/runemadsen/devise-capturable.git"
23
+ gem "devise_capturable"
24
24
  ```
25
25
 
26
26
  #### Add `:capturable` to your `User` model
@@ -113,21 +113,34 @@ You can delete these settings from your embed code, as the gem will set them for
113
113
  ## Changing defaults
114
114
 
115
115
 
116
- #### Overriding `set_capturable_params`
116
+ #### Overriding `before_capturable_create`
117
117
 
118
- There are times where you might want to save more than the `email` of your user in the Rails `User` model. You can override the `set_capturable_params` instance method to do this. Here's an example where I'm also saving the `uuid`. The `capture_data` parameter passed to the function is the Janrain Capture `entity` JSON result that has a bunch of information about the user.
118
+ There are times where you might want to save more than the `email` of your user in the Rails `User` model. You can override the `before_capturable_create` instance method to do this. Here's an example where I'm also saving the `uuid`. The `capture_data` parameter passed to the function is the Janrain Capture `entity` JSON result that has a bunch of information about the user, and the `params` parameter is the controller parameters.
119
119
 
120
120
  ```ruby
121
121
  class User < ActiveRecord::Base
122
122
  devise ..., :capturable
123
- attr_accessible ..., :email, uuid
124
- def set_capturable_params(capture_data)
123
+ def before_capturable_create(capture_data, params)
125
124
  self.email = capture_data["email"]
126
125
  self.uuid = capture_data["uuid"]
127
126
  end
128
127
  end
129
128
  ```
130
129
 
130
+ #### Overriding `before_capturable_sign_in`
131
+
132
+ You might also want to update your Rails model if the data changes on the Janrain side. You can use the `before_capturable_sign_in` method for this. This method is empty by default, but can be defined in your model to update certain attributes. Here's an example where we update the fake "is_awesome" attribute.
133
+
134
+ ```ruby
135
+ class User < ActiveRecord::Base
136
+ devise ..., :capturable
137
+ def before_capturable_sign_in(capture_data, params)
138
+ self.is_awesome = capture_data["is_awesome"]
139
+ self.save!
140
+ end
141
+ end
142
+ ```
143
+
131
144
  #### Overriding `find_with_capturable_params`
132
145
 
133
146
  When a user logs in, Devise will call the Capture API and try to find a user with the email returned by the API. You can change this by overriding the `find_with_capturable_params` instance method in your `User` model. Here's an example where I'm telling Devise to find the user by the `uuid` instead.
@@ -10,24 +10,18 @@ module Devise
10
10
  end
11
11
  end
12
12
 
13
- # This is called from strategy and is used to fill a user model before saving
14
- # It defaults to just setting the uuid, but you can override this in your user model
15
- def set_capturable_params(capture_data)
13
+ # This is called from strategy and is used to fill a user model before creating it
14
+ # It defaults to just setting the email, but you can override this in your user model
15
+ def before_capturable_create(capture_data, params)
16
16
  self.email = capture_data["email"]
17
17
  end
18
18
 
19
- module ClassMethods
20
-
21
- # Configuration params accessible within +Devise.setup+ procedure (in initalizer).
22
- #
23
- # Devise.setup do |config|
24
- # config.capturable_auto_create_account = true
25
- # end
26
- ::Devise::Models.config(self, :capturable_auto_create_account)
19
+ # This is called from strategy and *can* be used to update an existing user model if
20
+ # the data changes on the janrain side. It defaults to doing nothing.
21
+ def before_capturable_sign_in(capture_data, params)
22
+ end
27
23
 
28
- def capturable_auto_create_account?
29
- self.capturable_auto_create_account
30
- end
24
+ module ClassMethods
31
25
 
32
26
  # This is called from strategy and is used to find a user when returning from janrain
33
27
  # It defaults to find_by_uuid, but you can override this in your user model
@@ -9,7 +9,7 @@ module Devise
9
9
  class Capturable < ::Devise::Strategies::Base
10
10
 
11
11
  def valid?
12
- valid_controller? && valid_params? && mapping.to.respond_to?(:find_with_capturable_params) && mapping.to.method_defined?(:set_capturable_params)
12
+ valid_controller? && valid_params? && mapping.to.respond_to?(:find_with_capturable_params) && mapping.to.method_defined?(:before_capturable_create) && mapping.to.method_defined?(:before_capturable_sign_in)
13
13
  end
14
14
 
15
15
  def authenticate!
@@ -21,17 +21,18 @@ module Devise
21
21
  fail!(:capturable_invalid) unless token['stat'] == 'ok'
22
22
 
23
23
  entity = Devise::Capturable::API.entity(token['access_token'])
24
- user = klass.find_with_capturable_params(entity["result"])
25
-
26
- unless klass.capturable_auto_create_account?
27
- fail!(:capturable_invalid)
28
- return
24
+ user = klass.find_with_capturable_params(entity["result"])
25
+
26
+ if user
27
+ user.before_capturable_sign_in(entity["result"], params)
28
+ else
29
+ user = klass.new
30
+ user.before_capturable_create(entity["result"], params)
31
+ user.save
29
32
  end
30
-
31
- user ||= klass.new
32
- user.set_capturable_params(entity["result"])
33
- user.save(:validate => false)
33
+
34
34
  success!(user)
35
+
35
36
  rescue Exception => e
36
37
  fail!(:capturable_invalid)
37
38
  end
@@ -1,5 +1,5 @@
1
1
  module Devise
2
2
  module Capturable
3
- VERSION = "0.0.5"
3
+ VERSION = "0.0.6"
4
4
  end
5
5
  end
@@ -11,8 +11,6 @@ module Devise
11
11
  mattr_accessor :capturable_server
12
12
  mattr_accessor :capturable_client_id
13
13
  mattr_accessor :capturable_client_secret
14
- mattr_accessor :capturable_auto_create_account
15
- @@capturable_auto_create_account = true
16
14
  end
17
15
 
18
16
  I18n.load_path.unshift File.join(File.dirname(__FILE__), *%w[devise_capturable locales en.yml])
data/spec/spec_helper.rb CHANGED
@@ -9,4 +9,5 @@ end
9
9
 
10
10
  RSpec.configure do |config|
11
11
  config.mock_with :rspec
12
+ config.color_enabled = true
12
13
  end
@@ -18,37 +18,38 @@ describe 'Devise::Capturable' do
18
18
  @strategy.should_receive(:mapping).and_return(@mapping)
19
19
  @strategy.should_receive(:params).at_least(1).and_return(PARAMS)
20
20
  @user = User.new
21
- @user.stub(:set_capturable_params)
22
21
  Devise::Capturable::API.stub(:token).and_return(TOKEN)
23
22
  Devise::Capturable::API.stub(:entity).and_return(ENTITY)
24
23
  end
24
+
25
+ describe "for an existing user" do
25
26
 
26
- it "should authenticate and set capturable params if a user exists in database" do
27
- User.stub(:capturable_auto_create_account?).and_return(true)
28
- @user.stub(:save).and_return(true)
29
- User.should_receive(:find_with_capturable_params).with(ENTITY["result"]).and_return(@user)
30
- @user.should_receive(:set_capturable_params).with(ENTITY["result"]).and_return(true)
31
- @strategy.should_receive(:"success!").with(@user).and_return(true)
32
- lambda { @strategy.authenticate! }.should_not raise_error
27
+ it "should call #before_capturable_sign_in and authenticate" do
28
+ @user.stub(:save).and_return(true)
29
+ User.should_receive(:find_with_capturable_params).with(ENTITY["result"]).and_return(@user)
30
+ @user.should_receive(:before_capturable_sign_in).with(ENTITY["result"], PARAMS).and_return(true)
31
+ @strategy.should_receive(:"success!").with(@user).and_return(true)
32
+ lambda { @strategy.authenticate! }.should_not raise_error
33
+ end
34
+
33
35
  end
34
36
 
35
- describe 'when no user exists in database' do
37
+ describe 'for a new user' do
36
38
 
37
39
  before(:each) do
38
40
  User.should_receive(:find_with_capturable_params).and_return(nil)
41
+ User.should_receive(:new).and_return(@user)
39
42
  end
40
43
 
41
- it "should fail unless capturable_auto_create_account" do
42
- User.should_receive(:"capturable_auto_create_account?").and_return(false)
44
+ it "should call #before_capturable_create and fail is unsuccessful" do
45
+ @user.should_receive(:"before_capturable_create").with(ENTITY["result"], PARAMS).and_return(false)
43
46
  @strategy.should_receive(:"fail!").with(:capturable_invalid).and_return(true)
44
47
  lambda { @strategy.authenticate! }.should_not raise_error
45
48
  end
46
49
 
47
- it "should create a new user and success if capturable_auto_create_account" do
48
- User.should_receive(:"capturable_auto_create_account?").and_return(true)
49
- User.should_receive(:new).and_return(@user)
50
- @user.should_receive(:"set_capturable_params").with(ENTITY["result"]).and_return(true)
51
- @user.should_receive(:save).with({ :validate => false }).and_return(true)
50
+ it "should call #before_capturable_create and succeed if successful" do
51
+ @user.should_receive(:"before_capturable_create").with(ENTITY["result"], PARAMS).and_return(true)
52
+ @user.should_receive(:save).and_return(true)
52
53
  @strategy.should_receive(:"success!").with(@user).and_return(true)
53
54
  lambda { @strategy.authenticate! }.should_not raise_error
54
55
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise_capturable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-26 00:00:00.000000000 Z
12
+ date: 2013-11-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty