RubyFlow : The Ruby Community Blog

Home   Submit   Sign Up   Log In   leaders   Twitter   RSS Feed  
 

NathanLong — 28 posts

http://nathanmlong.com

Users of MetaSearch/Ransack, have you seen Searchlight? It's a low-magic search gem that's powerful and easy to understand. It helps you map form options to methods you write, and inside those methods, you can do whatever you need, from `where(name: name)` to complex SQL queries.

As of the 3.0 release, it's also totally ORM-neutral, so whether you're using ActiveRecord, Sequel, Mongoid, or something else, you can build searches with Searchlight. Check it out!
Suppose you're using Nokogiri to parse some HTML or XML, and an element you need is missing. What should happen? If you said "An exception", try my tiny new gem, nokogiri_bang_finders. It simply adds lookup methods to Nokogiri that raise if nothing is found: at!, at_xpath!, and at_css!
Environment variables control which program is run when you type "ls" and which editor Git uses by default. They're the way Heroku recommends you configure your SaaS apps, and they let you pass data between programs in different languages.

Understanding Unix Environment Variables will help you a lot as a programmer.
I've posted some brief notes from Sam Saffron’s “Measuring Ruby” talk at Golden Gate Ruby Conference 2013.

(Personally, I thought Saffron's rack-mini-profiler was the coolest thing he demonstrated.)
Two new features have just landed in Authority: one to help ensure you don't forget any authorizations, and one to make working with nested resources easier. Here's a short post explaining them.
I just posted a followup post to my previous post, "Better Single-Table Inheritance", addressing some of the points readers have emailed me about.
The state_machine gem is a great tool for putting business rules into your Ruby applications. But sometimes rules have exceptions. How do you handle those?

In this post, I’ll show you how my team has used state_machine together with Authority to allow certain people to make exceptions to the rules.
Quick video intro to Searchlight, the lightweight search gem  bookfinder-searchlight-demo.herokuapp.com
Searchlight is a lightweight search gem that's both easy to use and extremely flexible. (At least half its awesomeness is owing to my coauthor, Adam Hunter.)

To show you how it works, I've just released an introductory video, along with the demo app behind it and the code for that app.

Enjoy!
How should the Authority gem support Rails 4's new view caching approach? I've just opened a Github issue on the project and would love to get some input from the community, especially those already using the two together.
Searchlight is a lightweight gem for building searches in your app. Searchlight follows the same philosophy as Authority: low-magic, very little DSL. Mostly you'll write plain Ruby methods for your logic; Searchlight just helps you tie it together.

Searchlight integrates nicely with ActiveRecord and Rails forms, but can be used in any Ruby program. Check it out on Github or Rubygems.
I’ve seen several articles recently on the evils of single table inheritance in Rails apps. While it can be problematic, I recently tried an approach to STI that I think works well.

Here’s what it boils down to: Put common attributes in a single table, non-shared attributes in separate tables with foreign key references, and use object delegation so that each model transparently pulls what it needs from both.

For details, see this post.
Normally, classes and modules get names only when they're assigned to a constant. If you make your own, like with Class.new, they're anonymous.

Adam Hunter's new "Named" gem lets you give them names without assigning them to constants. Just use Named::Class.new('CustomClass') or Named::Module.new('MyModule').

Check it out on Rubygems and Github.
I just blogged a general outline of how our team built a single-sign-on app and how that influenced the design of our authorization gem, Authority.
Can you write good code if you don't understand what you're doing? I was recently surprised to discover that sometimes, with a good test suite, you can.
Adam Hunter and I recently released Dossier, a Rails engine for turning SQL queries into various report formats with minimal effort. The SQL can be hand-written and use all of your database's features, or generated by something like ActiveRecord's `to_sql`. See the README on Github for details.
If you use Vim and Ruby, here's an awesome way to transform text in Vim using Ruby scripts.
Authority provides a sensible, ORM-neutral way to authorize actions on your Rails models.

But what if you need to authorize something for which there is no model, like access to a stats page? The new "generic `can?`" feature takes care of that nicely -- just don't overuse it.
Using quick shell aliases  nathanmlong.com
Temporary shell aliases are a pretty obvious idea, but do you remember to make them when you need them?
If you need to reference local gems in your Gemfile, but don't want to annoy your coworkers by hardcoding a path in your home directory, here's a quick solution.
Rspec 2.11 introduces a new syntax: `expect(foo).to eq('bar')` instead of `foo.should eq('bar')`.

If you're using Vim, you can record and play back macros to make converting your tests less of a chore. Here's how.
Did you know that you can control the behavior of "random" methods in Ruby - things like `sample` and `shuffle` and `rand`? I just wrote a blog post explaining how.
Christmas, as 2 solid months of in-store nostalgia have ensured we know, is coming soonish.

But how can we do our shopping from the safety of home until we know who to buy for? Isn't there some sort of hat we have to draw names from?

Hats!!? That's so pre-internet. Here's a Ruby script to choose and notify secret Santas by email.
Nearly all my web apps use Javascript form validation in addition to the usual server-side variety. My go-to tool has been the jQuery Validation plugin. It's a great plugin, but the project needs some love. If you'd like to support version 2.0, check out its new campaign on Pledgie, which explains what needs to be done, how to contribute, and what donations would be used for.
If you want easy, flexible and powerful access control for your Rails app, consider the combo of Devise, rolify and Authority.

Here's the basic idea:

* Devise provides authentication: lets users sign up and sign in, so that you know who they are.
* rolify helps you assign roles to users and check which roles they have.
* Authority helps you use those roles, or any other logic you like (user points, info from a single-sign-on app, etc) to control who can do what.

I've written a short tutorial on the rolify wiki to help you get started.
Authority is the Rails authorization gem that's as simple and flexible as Ruby inheritence. We recently released the 2.0 version, and it's rock-solid: tons of passing multi-platform tests and lots of clear documentation. Come check it out on Github or Rubygems.
Rails authorization just got simpler: Authority 1.0.0 is released! It's ORM-neutral, super-flexible, and has no confusing syntax to learn; all your logic goes in one or more Ruby classes called "authorizers". Plentiful testing and documentation awaits your perusal.
At work, we just upgraded a Rails engine to use the Rails 3.1 asset pipeline. Getting our Jasmine tests to run in the engine's dummy app wasn't easy. Here's the solution we came up with.
If you're running your Ruby scripts on a cron job, it's important to log the output in case something goes wrong. Recording the output is as simple as appending "> somefile.txt" to your cron job. But can you avoid cluttering your code with lots of "puts" statements? Using a bit of metaprogramming, you can write a module that adds logging to whatever methods you specify. In this blog post, I'll show you how.