Permalink
Browse files

Merge pull request #283 from grrrisu/master

create_from provider with block
  • Loading branch information...
2 parents 90f751d + 45ee1ef commit d09facd054c91dff1e748df3802e784240a5c059 @NoamB committed May 11, 2012
@@ -72,6 +72,10 @@ def access_token(provider)
#
# And this will cause 'moishe' to be set as the value of :username field.
# Note: Be careful. This method skips validations model.
+ # Instead you can pass a block, if the block returns false the user will not be created
+ #
+ # create_from(provider) {|user| user.some_check }
+ #
def create_from(provider)
provider = provider.to_sym
@provider = Config.send(provider)
@@ -91,11 +95,17 @@ def create_from(provider)
attrs.each do |k,v|
@user.send(:"#{k}=", v)
end
+
+ if block_given?
+ return false unless yield @user
+ end
+
@user.save(:validate => false)
user_class.sorcery_config.authentications_class.create!({config.authentications_user_id_attribute_name => @user.id, config.provider_attribute_name => provider, config.provider_uid_attribute_name => @user_hash[:uid]})
end
@user
end
+
end
end
end
@@ -186,6 +186,20 @@ def test_create_from_provider
redirect_to "blu", :alert => "Failed!"
end
end
+
+ def test_create_from_provider_with_block
+ provider = params[:provider]
+ login_from(provider)
+ @user = create_from(provider) do |user|
+ # check uniqueness of username
+ User.where(:username => user.username).empty?
+ end
+ if @user
+ redirect_to "bla", :notice => "Success!"
+ else
+ redirect_to "blu", :alert => "Failed!"
+ end
+ end
protected
@@ -101,6 +101,20 @@ def test_create_from_provider
end
end
+ def test_create_from_provider_with_block
+ provider = params[:provider]
+ login_from(provider)
+ @user = create_from(provider) do |user|
+ # check uniqueness of username
+ User.where(:username => user.username).empty?
+ end
+ if @user
+ redirect_to "bla", :notice => "Success!"
+ else
+ redirect_to "blu", :alert => "Failed!"
+ end
+ end
+
protected
@@ -106,6 +106,20 @@ def test_create_from_provider
end
end
+ def test_create_from_provider_with_block
+ provider = params[:provider]
+ login_from(provider)
+ @user = create_from(provider) do |user|
+ # check uniqueness of username
+ User.where(:username => user.username).empty?
+ end
+ if @user
+ redirect_to "bla", :notice => "Success!"
+ else
+ redirect_to "blu", :alert => "Failed!"
+ end
+ end
+
protected
@@ -32,6 +32,25 @@
end.should change(User, :count).by(1)
User.first.username.should == "Noam Ben Ari"
User.first.created_at.should_not be_nil
- end
+ end
+
+ describe "with a block" do
+
+ before(:each) do
+ user = User.new(:username => 'Noam Ben Ari')
+ user.save!(:validate => false)
+ user.authentications.create(:provider => 'twitter', :uid => '456')
+ end
+
+ it "should not create user" do
+ sorcery_model_property_set(:authentications_class, Authentication)
+ sorcery_controller_external_property_set(:facebook, :user_info_mapping, {:username => "name"})
+ lambda do
+ # test_create_from_provider_with_block in controller will check for uniqueness of username
+ get :test_create_from_provider_with_block, :provider => "facebook"
+ end.should_not change(User, :count)
+ end
+
+ end
end
end
@@ -33,5 +33,23 @@
User.first.username.should == "coming soon to sorcery gem: twitter and facebook authentication support."
User.first.created_at.should_not be_nil
end
+
+ describe "with a block" do
+
+ before(:each) do
+ user = User.new(:username => 'nbenari')
+ user.save!(:validate => false)
+ user.authentications.create(:provider => 'github', :uid => '456')
+ end
+
+ it "should not create user" do
+ sorcery_model_property_set(:authentications_class, Authentication)
+ sorcery_controller_external_property_set(:twitter, :user_info_mapping, {:username => "screen_name"})
+ lambda do
+ get :test_create_from_provider_with_block, :provider => "twitter"
+ end.should_not change(User, :count)
+ end
+
+ end
end
end

0 comments on commit d09facd

Please sign in to comment.