I'm trying to figure out the "best" way to support discovery of custom validator errors on a Rails 3 model.
My model has both standard and custom validators. I need to check for validation errors from the custom validators, in both controller and template.
My slap-dash solution was to add an additional error message keyed to the field name suffixed with a validator-specific extension. For example, the standard message would be added to errors[:foo], and a custom message to errors[:foo__bar].
This works, but I really don't like it much--hence my plea for ideas. Here's the options I've considered so far, in no particular order.
(1) Add a model function that hides the logic. Easy, but vaguely discomforting.
(2) Check for the validator in the model's "initialize" method and if it's there, add an instance method. Similar to (1), but still far-removed from the validator.
(3) Expose something in the validator. Add it to the model class via an include, or dynamically in the model initializer.
(4) Have the validator add a method (foo_xxx? or something similar) to the instance at validation time. Only models that have been validated would have those methods, which gives me the heebie-jeebies.
(5) Pass in the class (instance?) in the EachValidator's options hash, and have the validator tweak the class (instance?) at init time. Having to pass in the class I'm already in seems janky, but...?
Updated:
(6) The "validates_with" also looks for a "setup" method in the validator (not an ActiveModel::EachValidator, but an ActiveModel::Validator) and passes "self" to it. "Sexy" validations use "validates_with" underneath, so this may be another approach.
The nature of my question, and my list of possible solutions, makes me wonder if I'm thinking about this the wrong way, and/or missing something *completely* obvious--am I?
Tuesday, August 09, 2011
Monday, August 08, 2011
Bash function of the day
I push the command line on people, because it's generally the most efficient way to do several different things. My devs resist me, although I don't understand why... part of it is that most places I've worked at are Windows houses. No problem, say I: Cygwin to the rescue. Seriously, gang, I'm not just making this up.
(I'll grant, however, that there are some Finder/Explorer replacements that wrap up some of this functionality pretty nicely in a GUI, including having a command line in whatever directory is open--but it's still a command line, and you still should know how to use it.
Two things I do a lot from the command line are find files, and find files containing a pattern. Here's two drop-dead simple Bash functions that do this. Seriously, doesn't get much easier.
function fn {
find . -type f -name "$1"
}
(I'll grant, however, that there are some Finder/Explorer replacements that wrap up some of this functionality pretty nicely in a GUI, including having a command line in whatever directory is open--but it's still a command line, and you still should know how to use it.
Two things I do a lot from the command line are find files, and find files containing a pattern. Here's two drop-dead simple Bash functions that do this. Seriously, doesn't get much easier.
function fn {
find . -type f -name "$1"
}
function fng {
find . -type f -name "$1" | xargs grep "$2"
}
find . -type f -name "$1" | xargs grep "$2"
}
Friday, August 05, 2011
Mysterious Facebook JS error, resolved!
Two FB apps, both using FB.ui to publish to a stream. One worked, one didn't, nobody knew why. I needed to know.
Nutshell: FB JS code was inadvertently included twice (not by me, which always makes things more difficult). This caused the app_id and app_key values in the FB.ui call to go away--everything else was there, but two pieces critical (obviously) weren't. Note that the app code didn't explicitly do any initializations twice, the act of including the code twice was enough.
We ended up doing the FB share a different way due to time pressures, but before we did, we had multiple people looking at the app trying to figure out what was wrong. In retrospect, I think I should have diagnosed the problem a day earlier than I did, and there's a couple of reasons why.
The most obvious dumb thing was that I didn't look at the wire traffic and pick apart the FB.ui call's URLs. That was ignorance; this was the first FB issue I've ever tried to diagnose--I had no clue where to look. That step, however, ended up pointing me in the right direction (eventually).
I also should have created a blank sample app earlier, as a sanity check. This was partially inertia, partially time constraints, partially wasn't sure how easy/difficult it was to do, partially a belief that the FB pros would be able to figure it out, so I did other stuff and waited--didn't need to.
Once I had the working app, compared the generated URLs, things progressed rapidly. First I made the blank app structurally similar to the broken app: put the JS code into a partial (it's a Rails app), cut-and-pasted the init code, etc. I ignored styling--I made an assumption it was a JS issue fairly early on, and pursued that path first.
The blank app continued to work, so I switched my attention to the broken app, and stripped out everything except the FB code. Workie workie... made some additional guesses, started poking through the partials, and found a partial with two issues--a repeated div id used by the init code, and an include of the FB JS file.
I don't actually know if it was the include or the div id: the FB JS code is inserted dynamically as a div's child element--a div with the id that was repeated :/ Either way, it was broken HTML, and broken JS.
Here's what bothers me about this exercise: multiple people looked at this and couldn't figure it out. Identical code worked in other apps (we knew (or at least "thought strongly") it was something app-specific), the docs made it seem like what I was trying should work. It should have been easier to diagnose.
Code that fails sans clueage is a pain. Disallowing multiple loads of the same JS is easy, and it would have saved a few people a frustrating amount of time diagnosing what ended up being ridiculously easy, and (somewhat) obvious.
Wrong code should either be impossible, or act very wrong, not just somewhat, and quietly, wrong. That notion reminded me of an old Spolsky article which is tangentially related, but speaks to making things that are broken break in such a way it's obvious: fail fast.
Friday, July 01, 2011
Textmate and rvm, behave!
Several sites describe the process necessary to get TextMate to use either a wrapped (via the rvm wrapper command) or an rvm-specified default Ruby interpreter (including gemsets, basically anything you can use with the rvm use command).
Following just those steps never worked for me, even when I explicitly gave a wrapped TM_RUBY: it would still do an exec ruby in there and blow up. I've solved this (at least temporarily) by adding my ~/.rvm/bin directory to TextMate's PATH shell variable.
Sunday, January 30, 2011
Java/Struts Interview Back Online!
Aww yeah; it's back, YouTubier than ever. (The audio conversion is a bit spikey--I'll have to try it again I think.)
Watch it on YouTube!
Myeeeeah update! Update! myeeeeeeeeeaaaaaaaaa Update! Implementing googling!
For those keeping score at home... still haven't found anyone I actually *want* to hire, although there have been a few people that would have been solid workers, I really need someone thinking a bit more abstractly.
Watch it on YouTube!
Myeeeeah update! Update! myeeeeeeeeeaaaaaaaaa Update! Implementing googling!
For those keeping score at home... still haven't found anyone I actually *want* to hire, although there have been a few people that would have been solid workers, I really need someone thinking a bit more abstractly.
Sunday, July 18, 2010
Back from the dead.
As I prepare to interview developers again, I'm reminded of my last post--while part of me is curious to see how this round will go, part of me dreads the process. I also noticed I haven't blogged in a Really Long Time and really should start again.
*grumble*
One issue I know I'll have this time around is that we're really running in a legacy environment, but I need developers with current development skills, both to (a) bring our existing codebase into this decade while fitting into a Java 1.4/Struts 1 environment (don't ask :( and (b) all new code has to be developed like it was 2010, and be easily movable when we finally drop everything onto a new server/new Java version (later this year).
I'm also fundamentally wary of developers with *only* Java on their resume accompanied by the usual litany of buzzwords: it basically means I *have* to do phone screens to get past the BS that recruiters and employees themselves put on their resume--made worse by an HR department that isn't clever enough to see past even the most rudimentary BS, then get confused why I'm rejecting so many candidates...
"Usually positions are filled within three interviews."
Really? And you wonder why our last SEC filing included the choice phrase "unable to attract top technical and managerial talent"? Oh, and thanks for that, by the way.
So I need to attract the kind of developer *I* want--but they won't want to work in the only environment I can offer them for the immediate future.
Bummer.
Saturday, June 20, 2009
Best JEE Interview Ever?
UPDATED WITH NEW LINK TO AUDIO.
Some time ago a friend forwarded me an audio recording of an interview he conducted. No names are used (except for John's), no companies are identified--to protect the (very) guilty. A few words were unintelligible. While the transcript it entertaining on its own, when read along with the audio it may actually kill you. Here's the audio.
Update update... meeaaah update!
N: We're going to move on to Struts questions, I see that you've done some Struts here?
S: Yeah.
N: Okay. Ah have you used Struts 2 at all?
S: Yeah yeah Struts 2.
N: Ah, can you tell me a key difference between Struts 1 and Struts 2?
S: Umm mmmah Struts umm mahhh in Struts 2 mmmahh ahhh is ah Struts 1 is implemented using MVC-1 architecture whereas in Struts 2 ahhhh ah is basically defined and ah ah improved version of the Struts 1 ah which defines um which is defined by MVC-2 or model 2 architecture.
N: Isn't ah model 2 architecture just using servlets and JSP, as opposed to...
S: Yeah.
N: ... C... GI?
S: Mmm ah CGI also inserted into Struts 2.
N: Oh. Okay. Um... if I have... Have you ever seen... Are you familiar with ah wizards in the Windows OS?
S: Yeah.
N: Okay. If I had to do the same thing on a website where I have a tax form but I don't want to collect all thirty pieces of information at once, I want to collect five pieces of information at a time and then submit at the end after showing a user a a uh confirmation page that redisplays all the information they added, how can I collect five pieces of information at a time then on the last page display all that information?
S: Nnnyeah ah that we can do by nyehhh specific uh amounts of um nehh we can call ah mm nn eh we can call the uh servlet engine in such a way that uh one way we neuh we need five uh five of something ah um we need nyeahh any number then we can also ah ah nah such way nyeahtoken by token process we can do it.
N: Okay. Um... Have you used JSPs at all?
S: Yeah JS-
N: I'm sorry, I'm sorry; JavaScript.
S: JavaScript, yeah.
N: Ahhh... have you used AJAX?
S: Yeah AJAX uh I know theoretical concept and uh I just mmnnuh not the implementation in the AJAX. I know it's just ah uh uh it's nuh there is an uh jax engine in between the web server and the last ??? and web server am and the ehhh process so neh the jax engine mmm aiinh eh work uh like um ah ??? for loading the web page directly into the server um ah jax engine ah do the transaction between the data ah day data transaction um by calling in the Java ???.
N: Okay. Tell me about your work with the ah I'm I'm assuming this is Google Web Toolkit?
S: Ahhhh Google Web Toolkit I have but um Yes! Google Web euh Web Toolkit is an ahhh um just like an API which um
N: But what did... what did you do with it?
S: No ah I have not used that.
N: Umm okay it says GWT on your um
S: Yeah Google Web Toolkit.
N: But you haven't used it.
S: Uhhhhh used instance of ah just not ??? theoretical ??? and ah while developing while developing a portlet ah portlet development we have just uh eeah do uh uh time submission by mmmby implementing googling and that web Google Web Toolkit is a ready toolkit that um nyeah that ??? information which helps to develop the portal developortal.
N: Okay ummm. The last couple questions will be on SQL.
S: Sure.
N: Can you tell me the syntax for an update statement.
S: Sorry?
N: Can you tell me the syntax for an update statement.
S: Ahhhh select um eeeah insert ob ahhh ahh insert update ah hm insert update um neah table ??? ??? insert update oy ah table parameter mmm na for mmm for uhhhhm uhhhh nuh ??? ??? um by mm by inserting uh we can ah by insert mmnnyea eh insert mm Update! Update! uhm Update! table name ah nn by inserting ah some parameter name into the table. Like that neuhhh nuh up ah trigger I mean ueh the eh select star from select star from the table name neaaah nuh for to return the different parameters from the table name.
N: Okay. Um well that wraps up ahhh our portion of this; ah we'll get back in touch with... whom. Ah I don't know who got these but we'll get in touch with that person.
S: Ya sure.
J: Thank you.
S: Heaya thank you, myeah John, and thank you...
J&N: Bye.
N: He forgot my name.
Some time ago a friend forwarded me an audio recording of an interview he conducted. No names are used (except for John's), no companies are identified--to protect the (very) guilty. A few words were unintelligible. While the transcript it entertaining on its own, when read along with the audio it may actually kill you. Here's the audio.
Update update... meeaaah update!
N: We're going to move on to Struts questions, I see that you've done some Struts here?
S: Yeah.
N: Okay. Ah have you used Struts 2 at all?
S: Yeah yeah Struts 2.
N: Ah, can you tell me a key difference between Struts 1 and Struts 2?
S: Umm mmmah Struts umm mahhh in Struts 2 mmmahh ahhh is ah Struts 1 is implemented using MVC-1 architecture whereas in Struts 2 ahhhh ah is basically defined and ah ah improved version of the Struts 1 ah which defines um which is defined by MVC-2 or model 2 architecture.
N: Isn't ah model 2 architecture just using servlets and JSP, as opposed to...
S: Yeah.
N: ... C... GI?
S: Mmm ah CGI also inserted into Struts 2.
N: Oh. Okay. Um... if I have... Have you ever seen... Are you familiar with ah wizards in the Windows OS?
S: Yeah.
N: Okay. If I had to do the same thing on a website where I have a tax form but I don't want to collect all thirty pieces of information at once, I want to collect five pieces of information at a time and then submit at the end after showing a user a a uh confirmation page that redisplays all the information they added, how can I collect five pieces of information at a time then on the last page display all that information?
S: Nnnyeah ah that we can do by nyehhh specific uh amounts of um nehh we can call ah mm nn eh we can call the uh servlet engine in such a way that uh one way we neuh we need five uh five of something ah um we need nyeahh any number then we can also ah ah nah such way nyeahtoken by token process we can do it.
N: Okay. Um... Have you used JSPs at all?
S: Yeah JS-
N: I'm sorry, I'm sorry; JavaScript.
S: JavaScript, yeah.
N: Ahhh... have you used AJAX?
S: Yeah AJAX uh I know theoretical concept and uh I just mmnnuh not the implementation in the AJAX. I know it's just ah uh uh it's nuh there is an uh jax engine in between the web server and the last ??? and web server am and the ehhh process so neh the jax engine mmm aiinh eh work uh like um ah ??? for loading the web page directly into the server um ah jax engine ah do the transaction between the data ah day data transaction um by calling in the Java ???.
N: Okay. Tell me about your work with the ah I'm I'm assuming this is Google Web Toolkit?
S: Ahhhh Google Web Toolkit I have but um Yes! Google Web euh Web Toolkit is an ahhh um just like an API which um
N: But what did... what did you do with it?
S: No ah I have not used that.
N: Umm okay it says GWT on your um
S: Yeah Google Web Toolkit.
N: But you haven't used it.
S: Uhhhhh used instance of ah just not ??? theoretical ??? and ah while developing while developing a portlet ah portlet development we have just uh eeah do uh uh time submission by mmmby implementing googling and that web Google Web Toolkit is a ready toolkit that um nyeah that ??? information which helps to develop the portal developortal.
N: Okay ummm. The last couple questions will be on SQL.
S: Sure.
N: Can you tell me the syntax for an update statement.
S: Sorry?
N: Can you tell me the syntax for an update statement.
S: Ahhhh select um eeeah insert ob ahhh ahh insert update ah hm insert update um neah table ??? ??? insert update oy ah table parameter mmm na for mmm for uhhhhm uhhhh nuh ??? ??? um by mm by inserting uh we can ah by insert mmnnyea eh insert mm Update! Update! uhm Update! table name ah nn by inserting ah some parameter name into the table. Like that neuhhh nuh up ah trigger I mean ueh the eh select star from select star from the table name neaaah nuh for to return the different parameters from the table name.
N: Okay. Um well that wraps up ahhh our portion of this; ah we'll get back in touch with... whom. Ah I don't know who got these but we'll get in touch with that person.
S: Ya sure.
J: Thank you.
S: Heaya thank you, myeah John, and thank you...
J&N: Bye.
N: He forgot my name.
Subscribe to:
Posts (Atom)