Archive

Posts Tagged ‘rails’

Converting attachment_fu to Paperclip

October 1st, 2009 mindtonic No comments

I am making the transition from attachment_fu to paperclip on a very large project of mine. I wrote the following method, inside of my old attachment_fu model ItemImage to do the work. I wrote it to run from inside the rails console. It gives handy messages out letting you know what happened. In my case you run:

$ script/console
>> ItemImage::convert_fu
def self.convert_fu
  for item in Item.all
    # has an image
    if item.item_image
      # the image exists
      filename = "public" + item.item_image.public_filename
      if File::exists?(filename)
        image = File.open(filename)
        item.image = image
        item.save
        puts "#{item.name} Image Converted"
      else
        puts "====> #{item.name} Image could not be found"
      end
    else
      puts "#{item.name} has no image"
    end
  end
end

Reference: http://thewebfellas.com/blog/2008/11/2/goodbye-attachment_fu-hello-paperclip

Categories: Gems, Ruby On Rails Tags: ,

Rails Javascript and CSS Compression

July 30th, 2009 mindtonic No comments

To speed up site loading, there are two great plugins for “compressing” your stylesheets and javascript.

The first is bundle-fu. This plugin does not actually compress the files, but rather combines all of the individual documents into one before shipping it out to the client browser.

$ script/plugin install git://github.com/timcharper/bundle-fu.git

And then you simply wrap your sheet and script calls in the bundle method:

  <% bundle do %>
    ...
    <%= javascript_include_tag "prototype" %>
    <%= stylesheet_link_tag "basic.css" %>
    <%= calendar_date_select_includes "red" %>
    
    ...
  <% end %>

The Second plugin, which actually does compress all of your files, is sbecker’s asset_packager. This one is a lot more involved, so check out the github site for more information.

Available Fields from FeedNormalizer Gem

July 8th, 2009 mindtonic No comments

The FeedNormalizer Gem does an excellent job of returning a single unified object from many of several different syndicated feed types. This saves you from having to worry about whether you are dealing with an rss, atom or some other kind of feed.

The following is a list of the available fields for both the Feed object itself and the Entries objects associated as articles in the channel.

FeedNormalizer::Feed

  • title
  • description
  • id
  • last_updated
  • copyright
  • authors / author
  • urls / url
  • image
  • generator
  • items / channel

FeedNormalizer::Entry

  • content
  • description
  • title
  • date_published
  • urls / url
  • id
  • authors / author
  • copyright
  • categories

Etags

Etags and last_modified are crucial elements to sending proper request headers if you hope to receive conditional get responses and save yourself some overhead in feed processing. FeedNormalizer does not provide access to these elements as they are actually part of the http response headers, and not the feed itself.

While heavily studying the art of feed processing, I encountered Feezirra and Feedtosis, both excellent libraries for feed processing. My own application required a modified version of their concept, but there was a lot to learn in their code.

Feedtosis uses the HttpHeaders process to collect this information from a Curl::Easy object. I adapted the Feedtosis code to meet my needs like this:

require 'http_headers'

class Feed < ActiveRecord::Base
  def store_header_information
    headers = HttpHeaders.new(curl.header_str)
    self.response_code = curl.response_code
    self.etag = headers.etag unless headers.etag.nil?
    self.last_modified = headers.last_modified unless headers.last_modified.nil?
  end
end
Categories: Gems Tags: , , ,

Shoulda Testing With Ultrasphinx

June 24th, 2009 mindtonic 2 comments

I use the Shoulda testing framework for my Rails applications. After installing and beginning to use the Ultrasphinx plugin, I needed to figure out how to configure and run in a testing environment. This blog entry held the answer: Stephen Celis: Testing with Ultrasphinx

Ultrasphinx MySql Location Error

June 24th, 2009 mindtonic 1 comment

After installing all of the necessary elements for Spinx and Ultrasphinx, I began following the instructions from Snax Fauna to get the system up and running.

rake ultrasphinx:configure

worked fine, but I encountered the following error upon attempting to index my models:

rake ultrasphinx:index
...
dyld: Library not loaded: /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib
Referenced from: /usr/local/bin/indexer
Reason: image not found

In my configuration for development, I use the MAMP MySql server. The rake configure command correctly pulled all of the connection information from database.yml, so that was not the problem.

I searched around and finally encountered this solution from Michael Hartl:

$ sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

It worked brilliantly. Thanks Michael.