Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
Parameter transformations use string keys
Browse files Browse the repository at this point in the history
Because of how ActionController::Params#to_h works the keys will be
strings instead of symbols. For this reason we must setup our
transformations to use strings in order for them to be applied.
  • Loading branch information
halogenandtoast committed Jun 8, 2016
1 parent fc08ec6 commit 7fd2528
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 13 deletions.
3 changes: 2 additions & 1 deletion NEWS.rdoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
== master
== 1.0.1
* Wrap helper_method calls in respond_to?(:helper_method)
* param_transformations now correctly use string keys

== 1.0.0
* Do not perform lookup if no params are passed to lookup
Expand Down
11 changes: 11 additions & 0 deletions lib/monban.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ def self.hash_token(token)
config.hashing_method.call(token)
end


# performs transformations on params for signing up and
# signing in
#
# @param params [Hash] hash of parameters to transform
# @see Monban::Configuration#param_transofmrations
# @return [Hash] hash with transformed parameters
def self.transform_params(params)
ParamTransformer.new(params, config.param_transformations).to_h
end

# the user class
#
# @see Monban::Configuration#setup_class_defaults
Expand Down
10 changes: 3 additions & 7 deletions lib/monban/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def initialize
# @see #creation_method=
def default_creation_method
->(params) do
updated_params = transform_params(params)
updated_params = Monban.transform_params(params)
Monban.config.user_class.create(updated_params)
end
end
Expand All @@ -54,7 +54,7 @@ def default_hashing_method
# @see Monban.config.user_class
def default_find_method
->(params) do
updated_params = transform_params(params)
updated_params = Monban.transform_params(params)
Monban.config.user_class.find_by(updated_params)
end
end
Expand Down Expand Up @@ -134,12 +134,8 @@ def setup_warden_serialization

def setup_param_transformations
@param_transformations = {
email: ->(value) { value.downcase }
"email" => ->(value) { value.downcase }
}
end

def transform_params(params)
ParamTransformer.new(params, param_transformations).to_h
end
end
end
6 changes: 3 additions & 3 deletions lib/monban/controller_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ def sign_up user_params

def authenticate_session session_params, field_map = nil
token_field = Monban.config.user_token_field
session_params_hash = session_params.to_h.symbolize_keys
password = session_params_hash.fetch(token_field)
user = Monban.lookup(session_params_hash.except(token_field), field_map)
params_hash = Monban.transform_params(session_params).symbolize_keys
password = params_hash.fetch(token_field)
user = Monban.lookup(params_hash.except(token_field), field_map)
authenticate(user, password)
end

Expand Down
4 changes: 2 additions & 2 deletions lib/monban/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Monban
# 1.0.0
VERSION = "1.0.0"
# 1.0.1
VERSION = "1.0.1"
end
14 changes: 14 additions & 0 deletions spec/features/user/user_signs_in_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'spec_helper'

feature 'User signs in' do
scenario 'with mismatched email case' do
user = User.create!(email: "[email protected]", password_digest: "password")

visit sign_in_path
fill_in "session[email]", with: "[email protected]"
fill_in "session[password]", with: "password"
click_button "go"

expect(current_path).to eq posts_path
end
end
9 changes: 9 additions & 0 deletions spec/features/visitor/visitor_signs_up_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
expect(page.current_path).to eq(posts_path)
end

scenario 'with uppercase email' do
visit sign_up_path
fill_in 'user_email', with: '[email protected]'
fill_in 'user_password', with: 'password'
click_on 'go'

expect(User.last.email).to eq('[email protected]')
end

scenario 'multiple users' do
visit sign_up_path
fill_in 'user_email', with: '[email protected]'
Expand Down
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
require 'monban'
require 'capybara'

Monban.test_mode!
Warden.test_mode!

RSpec.configure do |config|
config.include Warden::Test::Helpers
config.include Monban::Test::Helpers, type: :feature
config.order = "random"
config.after :each do
Monban.test_reset!
end
end

def with_monban_config(hash, &block)
Expand Down

0 comments on commit 7fd2528

Please sign in to comment.