HighDots Forums  

Custom Validations Altering Variable

Ruby On Rails Talk Ruby On Rails programming language mailing list


Discuss Custom Validations Altering Variable in the Ruby On Rails Talk forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
7H3LaughingMan
 
Posts: n/a

Default Custom Validations Altering Variable - 11-07-2009 , 11:05 AM






I would like to know if it is possible to alter a variable during the
process of a custom validation. I know that it is no longer validation
when your altering data but in my opinion there are times when you
need alter it for simplicity sakes. A good example would be for a
world wide auction house type application and to to convert currency
to one single format, when you validate you check to see if both the
amount is alright along with the currency type. (This is assuming you
can type in what type of currency it is, and for this example is
possible.)

The application I am developing allows the user to type in multiple
currencies at one (This is a D20 RPG, and people type in something
along the line of 12 pp 145 gp 78 sp 21 cp) and so far the validation
process involves converting it all to the lowest currency type. But
for the sake of being DRY I would only need to add another line to set
the variable, but instead it is looking like I would have to repeat
the whole method and add in the single line and change it that way on
creation and updates.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #2  
Old   
Marnen Laibow-Koser
 
Posts: n/a

Default Re: Custom Validations Altering Variable - 11-07-2009 , 12:14 PM






7H3LaughingMan wrote:
Quote:
I would like to know if it is possible to alter a variable during the
process of a custom validation. I know that it is no longer validation
when your altering data but in my opinion there are times when you
need alter it for simplicity sakes.
That's not what validations are for. You want custom setter methods.

Quote:
A good example would be for a
world wide auction house type application and to to convert currency
to one single format, when you validate you check to see if both the
amount is alright along with the currency type. (This is assuming you
can type in what type of currency it is, and for this example is
possible.)
With real-world currencies, it is my understanding that this is a bad
idea.

Quote:
The application I am developing allows the user to type in multiple
currencies at one (This is a D20 RPG, and people type in something
along the line of 12 pp 145 gp 78 sp 21 cp) and so far the validation
process involves converting it all to the lowest currency type.
Again, don't do this in the validator. What you want is something like
this:

CONVERSIONS = {:sp => 10, :gp => 100, p => 1000}

def balance=(string)
# parse the string somehow to get the various amounts, then:

bal = cp
bal += gp * CONVERSIONS[:gp]
# likewise for other currencies

self[:balance] = bal
end

There's room for refactoring here, but you get the idea.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen (AT) marnen (DOT) org
But
Quote:
for the sake of being DRY I would only need to add another line to set
the variable, but instead it is looking like I would have to repeat
the whole method and add in the single line and change it that way on
creation and updates.
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #3  
Old   
Eric
 
Posts: n/a

Default Re: Custom Validations Altering Variable - 11-07-2009 , 01:14 PM



I would put this in a callback in the controller. I suggest starting
at before_save and go from there.

-eric

On Nov 7, 8:05*am, 7H3LaughingMan <austin.brk... (AT) gmail (DOT) com> wrote:
Quote:
I would like to know if it is possible to alter a variable during the
process of a custom validation. I know that it is no longer validation
when your altering data but in my opinion there are times when you
need alter it for simplicity sakes. A good example would be for a
world wide auction house type application and to to convert currency
to one single format, when you validate you check to see if both the
amount is alright along with the currency type. (This is assuming you
can type in what type of currency it is, and for this example is
possible.)

The application I am developing allows the user to type in multiple
currencies at one (This is a D20 RPG, and people type in something
along the line of 12 pp 145 gp 78 sp 21 cp) and so far the validation
process involves converting it all to the lowest currency type. But
for the sake of being DRY I would only need to add another line to set
the variable, but instead it is looking like I would have to repeat
the whole method and add in the single line and change it that way on
creation and updates.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #4  
Old   
Marnen Laibow-Koser
 
Posts: n/a

Default Re: Custom Validations Altering Variable - 11-07-2009 , 01:22 PM



Eric wrote:
Quote:
I would put this in a callback in the controller. I suggest starting
at before_save and go from there.

That would work, but I think it's conceptually wrong for what the OP
wants.

Quote:
-eric
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen (AT) marnen (DOT) org
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #5  
Old   
Eric
 
Posts: n/a

Default Re: Custom Validations Altering Variable - 11-07-2009 , 02:23 PM



I don't agree.

Best,
Eric

On Nov 7, 10:22*am, Marnen Laibow-Koser <rails-mailing-l...@andreas-
s.net> wrote:
Quote:
Eric wrote:
I would put this in a callback in the controller. I suggest starting
at before_save and go from there.

That would work, but I think it's conceptually wrong for what the OP
wants.

-eric

Best,
--
Marnen Laibow-Koserhttp://www.marnen.org
mar... (AT) marnen (DOT) org
--
Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #6  
Old   
7H3LaughingMan
 
Posts: n/a

Default Re: Custom Validations Altering Variable - 11-07-2009 , 03:57 PM



Alright I have set it all up and it will convert it to the lowest form
(and back for debugging purposes), however whenever I try to edit and
save using @armor.update_attributes(params[:armor]) it fails for some
reason. Here is a link to show you what I did with the model...

http://pastebin.com/me807521
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #7  
Old   
Marnen Laibow-Koser
 
Posts: n/a

Default Re: Custom Validations Altering Variable - 11-07-2009 , 04:30 PM



7H3LaughingMan wrote:
Quote:
Alright I have set it all up and it will convert it to the lowest form
(and back for debugging purposes), however whenever I try to edit and
save using @armor.update_attributes(params[:armor]) it fails for some
reason.
Fails in what way?

Quote:
Here is a link to show you what I did with the model...

http://pastebin.com/me807521
I'll check it out in detail later on. On cursory inspection, however,
it looks like it has a lot of problems; I'll make some suggestions when
I'm not trying to type on my iPhone.

Perhaps Eric's before_save suggestion is the right idea after all.
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen (AT) marnen (DOT) org

--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #8  
Old   
7H3LaughingMan
 
Posts: n/a

Default Re: Custom Validations Altering Variable - 11-07-2009 , 08:05 PM



I don't know how it fails but for some reason it refuses to update
when I edit it, I have it setup to where it goes back to the index
when you successfully edit something and when it doesn't work it goes
back to the edit page. It doesn't even throw any error messages.

On Nov 7, 3:30*pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-
s.net> wrote:
Quote:
7H3LaughingMan wrote:
Alright I have set it all up and it will convert it to the lowest form
(and back for debugging purposes), however whenever I try to edit and
save using @armor.update_attributes(params[:armor]) it fails for some
reason.

Fails in what way?

Here is a link to show you what I did with the model...

http://pastebin.com/me807521

I'll check it out in detail later on. *On cursory inspection, however,
it looks like it has a lot of problems; I'll make some suggestions when
I'm not trying to type on my iPhone.

Perhaps Eric's before_save suggestion is the right idea after all.
Best,
--
Marnen Laibow-Koserhttp://www.marnen.org
mar... (AT) marnen (DOT) org

--
Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #9  
Old   
Marnen Laibow-Koser
 
Posts: n/a

Default Re: Custom Validations Altering Variable - 11-07-2009 , 09:59 PM



7H3LaughingMan wrote:
Quote:
I don't know how it fails but for some reason it refuses to update
when I edit it, I have it setup to where it goes back to the index
when you successfully edit something and when it doesn't work it goes
back to the edit page. It doesn't even throw any error messages.
I'll pay special attention to that, then. Are your tests passing?

Quote:
On Nov 7, 3:30�pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen (AT) marnen (DOT) org
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #10  
Old   
7H3LaughingMan
 
Posts: n/a

Default Re: Custom Validations Altering Variable - 11-07-2009 , 10:34 PM



Haven't really created any "proper" tests since I actually only have
one controller at the moment and I can test it via browser. All of my
methods work however whenever I do an update it doesn't touch the set
method to change the variable. (Tested with simple outputing text to
say that it was being used)

On Nov 7, 8:59*pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-
s.net> wrote:
Quote:
7H3LaughingMan wrote:
I don't know how it fails but for some reason it refuses to update
when I edit it, I have it setup to where it goes back to the index
when you successfully edit something and when it doesn't work it goes
back to the edit page. It doesn't even throw any error messages.

I'll pay special attention to that, then. *Are your tests passing?



On Nov 7, 3:30 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-

Best,
--
Marnen Laibow-Koserhttp://www.marnen.org
mar... (AT) marnen (DOT) org
--
Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.