http://www.codeguy.io/Ghost 0.6Sat, 25 May 2019 13:22:54 GMT60I've been looking up how :nth-child works at least once per month (sometimes per week) for as long as I can remember. Every time I'm working with a grid of some kind I inevitably come to a point where I need to clear every nth element (and then mutter to]]>http://www.codeguy.io/nth-child-selectors-simplified/dc2a73e1-8c29-4467-a88e-8700983414c1Thu, 21 Apr 2016 20:21:02 GMTI've been looking up how :nth-child works at least once per month (sometimes per week) for as long as I can remember. Every time I'm working with a grid of some kind I inevitably come to a point where I need to clear every nth element (and then mutter to myself "ah, crap. How do I do this, again?!").

This makes a bit more sense - to me anyways. With this I can say "I need every 4th element starting from the first." So:

every_nth = 4
starting_from = 1
:nth-child(4n +1)

Or "I need every third element"

every_nth = 3
starting_from = 0
:nth-child(3n + 0) => :nth-child(3n)

Hopefully that helps someone other than just myself. One thing to make clear is this formula won't work if you need something like "the first five elements..." (:nth-child(-n+5)), but I've never had to do that, so...

On a final note, another option would be to use a grid or framework that just avoids :nth-child all together! I'm sure there's a few out there, but I recently saw one from a friend, Will McMahan over at Thoughtbot called Cask: https://cask.readme.io. Check it out!

]]>http://www.codeguy.io/taking-advantage-of-rails-counter-in-partials/36fbc40a-88e7-4c10-89bc-2e9658048d7bSat, 21 Nov 2015 01:52:00 GMTAs you may know, one of the many magic Rails moves is calling render on a model or collection of models, e.g., = render @products in a view. Rails will just look for a default partial that matches the model name, e.g., if it's a model called User, it'll look for users/_user.html.erb (or haml, or whatever). You can even control this behaviour by giving or overriding a model's method to_partial_path to something else, (yet another) e.g.:

def to_partial_path
"spaghettis/meatball"
end
(yes, I know spaghetti is already plural... just illustrating a point)

Now rails will look for that model's partial in the views folder under spaghettis/_meatball.html.erb.

Now The Counter

Sweet, so now that we have that, there are a few things that Rails makes available in these model partials. One is a _counter variable with a prefix of the collection name that refers to how many times the partial has been rendered. For example, if you have a collection of objects in an instance variable @products, there will be a variable product_counter in each render of the partial. Cool but not a huge deal. For me, the cool part was Rails can handle a collection of mixed models.

What I mean by this is, say if you have a collection of Products and DealProducts that are mixed together in a collection variable @products. Even if Rails is rendering different partials that you've setup for each, it still keeps track of the product_counter correctly. This can be useful if you have to do something like set some tracking properties that are positionally based in a product grid. You can use product_counter to tell you what number the current rendered product is in the grid.

This was really helpful for me because I've had to do some pretty gnarly data-attributes on some product grids in the past. Without this counter I would have had to write some pretty heavy view helpers or some even worse Javascript to take care of it. But, like a lot of things, the good people on the Rails team thought of this ahead of time.

]]>http://www.codeguy.io/cron-jobs-with-ruby-using-rvm/d183b37b-3427-44a2-bbd6-8076aaa2c96bWed, 14 Oct 2015 20:08:00 GMTRecently, I needed to setup a cron job to regularly add a job to a worker queue. I wanted to write in Ruby rather than the usual Bash in order to leverage a gem that could easily connect to the queue. I ran into a couple of issues but once I got it figured out, I gotta say, writing crons in Ruby instead of Bash is waaaaay better.

Installing RVM

I won't go into detail about this. I use RVM to manage my Ruby. Some people I know aren't too fond of it, but to each his own. Just follow these instructions:

The problem...

You want to run a ruby script you wrote as a cron job. Sounds easy enough, I usually run this from the command line:

# ruby /my/awesome/script.rb

So I can just add that to my crontab and it'll be awesome:

*/30 * * * * ruby /path/to/my/script.rb

Nope, cause now in your cron logs you're going to see:

`require': cannot load such file -- some_gem_i_need (LoadError)

Dammit! So turns out my bash profile doesn't get loaded into your cron environment. So you have to make sure that's all loaded in before these scripts will work. Which can be a bit of a nightmare because running and debugging crons is tough. You have to make an edit, install a cron, check the logs and repeat. Ugh. Or you could...

Let RVM setup your cron environment for you

Whaaa? Yeah. This, IMHO, is kinda freakin' awesome. It took my getting almost the entire environment setup before I found out this was a thing. Could have saved me a lot of time, so hopefully it will save someone reading this before bashing their heads in getting cron to run with the correct Ruby environment:

# rvm cron setup

Boom! That will actually setup a new crontab for your user with all the goodies at the top: