Well, it's holiday season in the US and I've been gradually becoming less productive. I'd like to say it's because I have family over and that I haven't had enough sleep lately, but that's only part of the equation. To combat feeling like I've been doing less, though, I decided to work on some smaller tasks and clean up some code that I wrote previously.
Anyway, I ran into a small error when cleaning up the database seed file -- basically a file that you run to create dummy data for development -- and decided to fix it right then and there. No better time than now, right?
To give some background, our tags have two columns,
text_color_hex. They're supposed to take in a hex code, and we properly restrict it to alphanumeric characters and a limit of 6 of those in a row -- well, that's what I think it is since it's a regex validator and I didn't want to translate it 🙃.
There is, however, an annoying edge case where we can input it a hex code without the pound sign/hashtag/whatever-people-call-it-nowadays in front of the 6 characters, and the validator will throw us an error. There's no reason for that to not be automated, so I went for it.
As I'm writing the method out, I suddenly had a moment of glorious inspiration. In our quiet office, I smiled to myself at my extreme cleverness.
So, in great joy I wrote out a method that puts the pound sign in front of the color attributes if necessary:
def pound_it text_color_hex&.prepend("#") unless text_color_hex&.starts_with?("#") bg_color_hex&.prepend("#") unless bg_color_hex&.starts_with?("#") end
Now, the code could probably be refactored, but the beauty of course lies in the name. This happens every time a tag is validated, so know that you are indeed "pounding it" every time you hit the Preview or Save button for your posts.
Hopefully, this end-of-year post will inspire you to do something similar. What are some funny bits of code that you've run into? Have you written anything in your code and gotten it through all the red tape of your company? Would love to hear your stories!