last day (15 days later) » 

4:42 PM
0
A: Conditional validation for ActiveModel

iMacTiaTry using grouping conditional validation with_options if: :is_full_validation? do |user| user.validates ... # pay attention to the 'user.' in front of each validation!! user.validates ... end def is_full_validation? full_validation == true end

 
@Marc-AndréLafortune LOL, you're right! I just copy-and-pasted the method from the question, but YES! Rails has his conventions and is better to follow them. Let me change the answer
 
This was my attempt as well but it does not work. I inserted puts full_validation just above the return full_validation == true. And full_validation is set to false however the validation for those fields within with_options statement is still done resulting in validation errors.
 
In your question you tried the same, but you forgot the ? at the end of the method. In rails, ? is part of the method name
 
@iMacTia gist.github.com/anonymous/cf9639461e549697c8e2 This is how I have it now. I do have the ? sign but still same problem. I made sure that after I create a new instance of the object I also call user.full_validation = false.
 
At this point, I'm wondering if ActiveInteraction supports conditional validations...
 
4:42 PM
It includes ActiveModel I would assume wouldn't it two an error or exception otherwise ?
In the def is_full_validation? .. end method I added the following statement puts "Validation: " + full_validation.to_s and than return full_validation == true.
But nothing get's printed out to console
from that puts statement
 
so it actually doesn't even call the validation method
I double-checked the ActiveInteraction and yes, it seems it included the ActiveRecord validation
 
That seems to be the problem but I don't understand as to why is_full_validation method is not called.
 
Never used ActiveInteraction, do you also have a User model extending ActiveRecord::Base? Or is it replaced by the one that extends ActiveInteraction?
 
It's replaced by ActiveInteraction
 
ok...
what if you change the condition in the is_full_validation to full_validation == false?
still ignoring or it stops?
(sometimes puts are not printed, debugging is better if you can)
 
4:59 PM
It still ignores it
 
can you debug?
 
I replaced puts with logger but nothing comes out of it still not being called
Don't have debugger available here
 
Rails 3 or 4?
 
Rails 4
 
ok
try this
with_options if: Proc.new { |u| u.full_validation }
or you can still use the method
with_options if: Proc.new { |u| u.is_full_validation? }
this worked for me
 
5:11 PM
Let's see
 
if it works also for you, I'll change the answer
let me know :)
 
Still includes the dields
with_options if: Proc.new { |u| u.is_full_validation? } do
validates :name, presence: true,
format: {
with: /^[a-zA-Z\d\s]*$/,
:multiline => true
}

validates :surname, presence: true,
format: {
with: /^[a-zA-Z\d\s]*$/,
:multiline => true
}

validates :phone, presence: true
end

def is_full_validation?
full_validation == true
end
 
you miss the parameter |user|
with_options if: Proc.new { |u| u.is_full_validation? } do |user|
and all user.validates
also, try to force (for now) the condition in the is_full_validation? method to a simple 'false'
 
damn it still nothing
 
I copy-and-paste my code so you can double-check
 
5:23 PM
ill set full_validation to false
 
  with_options if: proc { |w| w.is_full_validation? } do |w|
    w.validates :currency, presence: true
  end
  def is_full_validation?
    false
  end
just try this
it worked for me
(of course use your validation, not mine
)
 
with_options if: Proc.new { |u| u.is_full_validation? } do |user|
user.validates :name, presence: true,
format: {
with: /^[a-zA-Z\d\s]*$/,
:multiline => true
}

user.validates :surname, presence: true,
format: {
with: /^[a-zA-Z\d\s]*$/,
:multiline => true
}

user.validates :phone, presence: true
end

def is_full_validation?
false
end
I also set full_validation to false by default in the constructor
 
and still not working even like this?!
 
Nope
I have no idea
what the hell is happening
 
can you switch to ActiveRecord::Base or something breaks?
 
5:27 PM
gist.github.com/anonymous/6818467fc4750d302b66 This is how I have it now Controller + Model
Has to be ActiveModel then because it's not getting stored in the databse
database*
 
if you don't pass validation it doesn't save to DB
I was reading ActiveInteraction readme
# The execute method is called only if the inputs validate. It
# does your business action. The return value will be stored in
# `result`.
def execute
user = User.create!(email: email, name: name)
if newsletter_subscribe
NewsletterSubscriptions.create(email: email, user_id: user.id)
end
UserMailer.async(:deliver_welcome, user.id)
user
end
end
they call User.create!
so they must have a class User < ActiveRecord::Base
you're mixing the 2 things, I think
 
xD
I'll switch to ActiveRecord::Base see what happens
 
:D
good idea
 
undefined method `string' for User(Table doesn't exist)
xD
 
it seems a useful gem, but read carefully how it works
you don't even have the table XD ok man, you have some work to do ;)
ignore ActiveInteraction for now
and try to use the old ActiveRecord
 
5:34 PM
No I don't the data is not getting stored into DB
I just use the model for validation and to store user related api call methods.
 
do you know how to proceed?
 
Nope
Dead in the water
 
database migrations, never used?
ah wait, you don't want a user table?
 
I don't need to store this data into the database.
Nope I don't store the data to Database
 
how do you want to login users without saving them to the db?!
your AuthController will never works
 
5:40 PM
Basically I validate the input pass the data to api service and get back an SSID string I store it and retrive other users data with that SSID
 
wow, ok...
 
Was the clients decision xD It gets worse and worse. They want everyone to use their API service which means you are making 2-3 requests to the api service and best of all login api service is basically domain.com/api/login/username/password
Unencrypted plain text password
xD
I guess esiest fix is to simply provide dummy data for the 3 fields
 
I can't believe it
is the most stupid thing I've ever heard
but clients are clients
however
at this point, simply convert all validation "ActiveRecord-style" to a unique rails function
with normal checks
so for example
"presence" is != nil
"length" is string.length > 6
and so on
don't know for regex, but I'm sure there a ruby function also for this
before going on this direction, however
you can try one last thing
I've seen that ActiveInteraction should be called like this
UserSignup.run(params[:user])
so in your case, you should call
User.run({email: 'something@company.com', name: 'Name', ...})
you haven't used the "run" method yet
give it a try, like shown in the readme of ActiveInteraction
 
if it still don't work, go for the plain-ruby conversion
sorry can't be more helpful :(
I have to go, let me know in the Question thread, I'm curious now :)
bye
 

last day (15 days later) »