I work for a lead generation company. Part of the application I’m working required us to know how many people viewed a particular item in the system. So, my co-worker and I created an Impression model that tracked each appearance of the associated object. We created a polymorphic association to allow impressions to be gathered on several different models.
The trick was that we found ourselves writing the same functionality on each of the models that needed impressions gathered. So, we took that functionality as well as the has_many polymorphic association and moved them into a module. Here’s what the module looks like:
When I first started playing with HTML, the way you centered your HTML elements was to place a <center> tag around them. If you try using a <center> tag in your HTML in any type of IDE, you’ll get a deprecation warning. (If you’re new to programming, deprecation means that the functionality is slated to be phased out.)
So, what do you do instead? Well, there are two instances in which I was wishing I knew how to get the same effects I got from <center>: centering text and centering elements. [click to continue…]
A lot of new Ruby developers I’ve worked with have seen the symbol notation—starting with a :— and have been confused by what a symbol actually is. There is a lot of information out there that is confusing as well. Here’s a brief rundown of what symbols are and how they are used.
A lot of people try Test Driven Development by writing the test, writing the code, and then wondering what the big deal is. Here’s the process I follow along with an explanation of why each step helps.
I’ve had a few discussions with people under various circumstances regarding Test First or Test Driven Development (TDD). Some people swear by TDD, while other don’t see how it could possibly work because “I don’t know how my code works until I write it.” My answer to this is “That’s why you need to write the test first.”
My initial exposure to Test Driven Development was while working for a client at SolutionStream. The client insisted that all development be done Test First. At first, it was a painful process. The tests took a while to write, and I really just wanted to get into solving the problem. As time went on, however, I found that writing the tests first, did a few things that made coding much more pleasant. [click to continue…]
Having used both Test::Unit and RSpec, I have to agree with Jim Weirich: the difference between the two is primarily semantics. It seems to me that functionally, they are both equally capable of verifying and specifying code. However, the way in which you write the tests—the semantics—is the primary difference between the two. That being the case, I prefer the semantics of RSpec. [click to continue…]
I’ve recently seen several requests come through from people trying to figure out how to call controller methods from their views. The answer is pretty simple. Your controller instance, which is calling your view is stored in the instance variable @controller. So, calling public methods is as simple as this:
To call a private method you can do this:
You should note that if you’re calling controller methods frequently in your views, you should consider putting them into helpers to make them available that way. If you need to manipulate or manage some data, the controller is the place to access the models for this, not the views.