last day (15 days later) » 

3:42 PM
1
A: Can anyone break down the code into a simpler form?

awesome# arg is Hash, so assign to variable and downcase x = warden_conditions[:signin].downcase # create duplicate to preserve orig c = warden_conditions.dup # delete `:signin` c.delete(:signin) # if email, only search for email if x =~ /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/ y = self.wh...

 
Thanks for this whole code, but again what your code has to do with this.. where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => signin.downcase }]).first
Can u write the equivalent query for it.
 
@theJava Basically it's not equivalent, but close enough for illustrative purposes. Hope it helps. lower(username) queries column as if it was all lowercase with signin.downcase so devise doesn't need to know if any records were stored with uppercase or lowercase chars.
 
where(["lower(username) = :value OR lower(email) = :value", { :value => signin.downcase }]).first Does this part of code perform check on where(condition)
 
@theJava the regexp is to explain the "long way" with two queries that the devise query turns into one using OR
@theJava No, where(["lower(username) = :value OR lower(email) = :value", { :value => signin.downcase }]) is only performed on the records that remain after this separate query where(conditions)
hi
 
Hello
where(["lower(username) = :value OR lower(email) =
:value", { :value => signin.downcase }]).first
Can i output this some where
["lower(username) = :value OR lower(email) =
:value", { :value => signin.downcase }]
in console with dummy values
 
3:43 PM
do you use rib?
i mean IRB
 
like
 
rails console
 
puts ["lower(username) =:value OR lower(email) =:value", {:value => "something"}]
 
$ rails console
 
from what i understood
puts ["lower(username) =:value OR lower(email) =:value", {:value => "something"}] the value something gets replaced on both the sides
right?
 
3:44 PM
you can do this at anytime to see the contents raise conditions.inspect
that means that either username or email equals "something"
 
perfect
 
lower() just allows lowercase to match
awesome!
 
what do we call that way doing things around
puts ["lower(username) =:value OR lower(email) =:value", {:value => "something"}]
sorry if i am too troubling u
[] = array
 
no worries
 
we have a string ""
then a comma
then a object
i am coming from JS background
 
3:47 PM
this is a form or interpolation
 
when i do a signin
this method is being called
 
in backend that you don't see the string is being parsed
 
no i am pretty new
conditions = warden_conditions.dup
 
to sql like this "lower(username) = "something" OR lower(email) = "something"
.dup is duplicating/cloning the conditions hash
so as to not disrupt the original
so that the original could be reused
 
Is ruby pass by reference?
not pass by value where we get a copy?
 
←[1m←[35mUser Load (1.0ms)←[0m SELECT "users".* FROM "users" WHERE (lower(use
rname) = 'some@gmail.com' OR lower(email) =
'some@gmail.com') ORDER BY "users"."id" ASC LIMIT 1
 
in that method it's just the writer's preference
 
cool... i will read that
above is my query i got from server console
i understand the second part
where(condition) where is this performed
 
where(condition) is not accounted for
where condition is so that the devise lib can provide more options
condition is another hash
that you do not see in the methods
it is being passed into the method with devise_condtions
delete(signin) is what separates the signing from the original conditions
 
if i am not wrong
where(conditions).where(["lower(username) = :value OR lower(email) =
:value", { :value => signin.downcase }]).first
this whole code
produces
[1m←[35mUser Load (1.0ms)←[0m SELECT "users".* FROM "users" WHERE (lower(use
rname) = 'some@gmail.com' OR lower(email) =
'some@gmail.com') ORDER BY "users"."id" ASC LIMIT 1
this query
 
3:56 PM
irb(main):007:0> devise_conditions = {:signin => "cool@gmail.com", :other_1 => 3, :other_2 => 4}
=> {:signin=>"cool@gmail.com", :other_1=>3, :other_2=>4}
irb(main):008:0> conditions = devise_conditions.dup
=> {:signin=>"cool@gmail.com", :other_1=>3, :other_2=>4}
irb(main):009:0> signin = conditions.delete(:signin)
=> "cool@gmail.com"
irb(main):010:0> signin
=> "cool@gmail.com"
irb(main):011:0> conditions
=> {:other_1=>3, :other_2=>4}
irb(main):012:0> devise_conditions
=> {:signin=>"cool@gmail.com", :other_1=>3, :other_2=>4}
first see what the conditions are with raise conditions.inspect
 
sec
 
in the code
 
{:signin=>"cool@gmail.com"}
 
delete returns the value of the key that was deleted ruby-doc.org/core-1.9.3/Hash.html#method-i-delete
 
if signin = conditions.delete(:signin) would be true
sigin = cool@gmail.com
right?
 
3:59 PM
yes
 
where(conditions).where(["lower(username) = :value OR lower(email) =
:value", { :value => signin.downcase }]).first
now what is where(conditions) doing here?
 
conditions.delete is removing key :signin and at the same time using the return to populate var signin
 
k
 
the conditions there are additional query params... I don't know what's inside of them
they are specified by devise
probably for additional functionality of devise
 
u mean this part is devise specific
where(conditions)
 
4:01 PM
yes, but they have to be valid AR syntax
 
AR?
 
it cold be as simple as {:limit => 1}
AR is active record
 
perfect
cool... i got to the whole scene
the first one is devise specific
and second is our query
 
yes. awesome! glad I could help. my twitter is @soawesomeman if you need ruby help
gotta go to work bye
 
Sure
thanks a lot
take care
 
4:04 PM
np, anytime
 

last day (15 days later) »