Archive for July, 2009

19
Jul
09

Mock Cookies in Rails Tests the Easy Way

I got really tired of mocking out cookies every single time I made a reference to cookies in my test. It looked like a bunch of spaghetti code that made me mock out every single cookie call that I could imagine. Don’t get me wrong, I think mocks are wonderful, but when you have to mock out every single call, I don’t think the pain is worth the gain. I found it much easier to mock cookies like this as like this, which gives a pretty good approximation of rails cookies. It won’t work for 100% of cookie testing cases, but for 99.9%, it works like a charm.

I use rspec to test, but you could easily port the Cookie class to any testing framework that you want.

def stub_cookies
  stub!(:cookies).and_return(MockCookie.new)
end

class MockCookie < Hash

  def [](name)
    super(name.to_s)
  end
 
  def []=(key, options)
    if options.is_a?(Hash)
      options.symbolize_keys!
    else
      options = { :value => options }
    end
    super(key.to_s, options[:value])
   end

  def delete(key,opts)
    super(key)
  end

end

08
Jul
09

DRY Views in Rails with Blocks

I’m a fan of the DRY (Don’t Repeat Yourself) principal. I think that computers were invented for a reason and that reason was not so that I could do the same thing over and over. If it’s one thing that computers do well, it’s doing the exact same thing ad nauseum. So, I do as much as I can in order not to repeat myself a lot when coding.

I have an case now where within the main portion where content is rendered sometimes there separate content on the right side, and sometimes there is not. For example, with no content on the right side, it looks like so:

  <div id="left">
    I am left content
  </div>

Simple enough, but when there is content on the right side, the left side has to get a little smaller, so there is a class added called “with_left” to the left div that makes the width less to accommodate the right content.

  <div id="right">
    I am right content
  </div>
  <div id="left" class="with_right">
    I am left content
  </div>

The problem comes in when the right content is different from page to page and I’m not exactly sure if there is going to be any right content to begin with, as it relies on some dynamic data. Sure, I could do something like this for every page, but I would get annoying fast, and I’m sure there would be one instance where I would forget to do it the right way:

  <% if right_content %>
    <div id="right">
      <%= right_content %>
    </div>
  <% end %>
  <div id="left" <%= 'class="with_right"' if right_content -%>>
    I am left content
  </div>

So, in comes Ruby to the rescue. I heart blocks and I’m glad that ruby has them, because this is a great case where they come in handy. Instead of doing the above logic over and over again, I can create a method called main_content that allows me to do this in a more generic way.

  def main_content(params={},&amp;block)
    right_content = params[:right_content]
    unless right_content.blank?
      right_div = <<-HTML
        <div id="right">
          #{right_content}
        </div>
      HTML
      concat(right_div)
      concat('<div id="left" class="with_right">')
    else
      concat('<div id="left">')
    end
    yield
    concat('</div>')
  end

Now I can use this method like so:


  <% main_content(:right_content=>render(:partial=>'account_actions')) do %>
    <h3>Edit Account</h3>
  <% end >%

If the partial account_actions renders anything, I will get this:

  <div id="right">
    Output of account_actions partial
  </div>
  <div id="left" class="with_right">
    <h3>Edit Account</h3>
  </div>

If the account_actions partial doesn’t end up rendering anything, then I get:

  <div>
    <h3>Edit Account</h3>
  </div>

Nice! Everything looks good, so I can start adding some tests to it. Here’s how I tested it with rspec:

  describe 'main content renderer' do
    
    before do
      @buffer = ""
      stub!(:output_buffer).and_return(@buffer)
    end
    
    it 'will render only the left div when there is no right content' do
      main_content{}
      output_buffer.should have_tag("div#left")
    end
    
    it 'will put the yielded content in the left div' do
      main_content{concat("Yielded content")}
      output_buffer.should have_tag("div#left","Yielded content")
    end
    
    it 'will display the right content in the right div if there is any' do
      main_content(:right_content=&gt;'Right'){}
      output_buffer.should have_tag("div#right","Right")
    end
    
    it 'will add a class of "with_right" to the left div if there is right content' do
      main_content(:right_content=&gt;'Right'){}
      output_buffer.should have_tag("div#left.with_right")
    end
    
  end

07
Jul
09

Twitter as a non-profit?

Let me first say that I don’t use Twitter. I see why people like it and I see why it is popular. I’m not one of those curmudgeons who bemoans its use, I just have enough things to keep up with. Someday, I’ll hope on the proverbial bandwagon.

Even though I don’t use it, it’s hard to do much anymore without hearing about it. Whenever there is a news event now, it seems like people are tweeting about it or the source came from a tweet (ala Jeff Goldblum’s non-death).

So, yes, Twitter is popular. And everyone has been speculating on how Twitter is going to make money, because as of now, they are surviving on VC dollars and the hope of making money down the road. A quick search on google will give you an idea as to how people have tried to make their own revenue model for Twitter.

Most of these models rely on some sort of ad delivery system, which is a pretty standard way to make dough. However, given the large number of people who use Twitter indirectly via the API, most ads within the page will not be seen by the users. That leaves you with putting ads in the tweets themselves, which will probably do nothing but make users feel as if their privacy has been invaded by a door-to-door salesman who just happened to show up in their Twitter feed.

So, I say let’s make Twitter a non-profit, much in the way that Wikipedia works. Just like Wikipedia, Twitter has totally user generated content. Also, they are both excellent data sources in their own way. Wikipedia for more historical data, Twitter for more transient and current opinions.

I think with a large passionate user base like Twitter, raising funds to keep it going would not be too hard. I also think it would allow them to keep developing a platform that is good for the community at large instead of business stakeholders who want a return on their money. Twitter is a good platform and I’d hate to see them go down because of a lack of monetary plan or, even worse, because it alienates its users by pumping in ads where they are not wanted.

Let’s get those dollar signs out of your eyes and do something reasonable. So, Twitter Foundation, anyone?




Stay Updated

or

Get new posts via email

Software Blogs - BlogCatalog Blog Directory

 

July 2009
M T W T F S S
« Jun    
 12345
6789101112
13141516171819
20212223242526
2728293031  

Follow

Get every new post delivered to your Inbox.