Tuesday, October 21, 2008

RSpec, attachment_fu

I've been looking for a solution to force RSpec tests to override the attachment_fu storage setting. Since I use s3, it was very inefficient when I'd run my tests because the assets would be saved in s3. Not only does it cost money, but it was very slow.

After spamming #rubyonrails I finally got an answer from _martinS_. He told me to do something like this: http://pastie.org/296895 - and it worked! I hope this helps you guys.

Sunday, September 28, 2008

Compiling Sphinx in Ubuntu

I had just watched Ryan Bates' screencast on Sphinx and I wanted to install it on Ubuntu. Took me a while to find out how, so here are my instructions. I'm on Ubuntu 8.04

1. Download the latest realease. It's 0.9.8 as of writing.
2. Install it with their instructions. I ran into a problem though - I have MySQL already but not the files Sphinx needs to install. If you ran into this problem, continue:
3. In console, type:

mysql_config --cflags

That will tell you the package you need to install.
4. Mine was libmysqlclient15-dev:

sudo apt-get install libmysqlclient15-dev

5. Try installing Sphinx again, it should work this time.

Tuesday, September 9, 2008

attachment_fu, Rails 2.1 and before_thumbnail_saved

I was working on before_thumbnail_saved callback in Rails 2.1 and it seems it has changed! It didn't work with me anymore. At least my old code didn't.

Apparently, you only pass the thumbnail into the loop.

Read this blog post for details.

Friday, September 5, 2008

Rake: Update or create images generated by attachment_fu

Did you upload a bunch of images and find out that the new layout needs a new size of the photos, and you didn't specify this in the has_attachment line of your model?

This happened to me, and I wanted an easy to way update all images in the database. I didn't test this with millions of records or anything, but if you want to fix this up please leave a comment.

Notes:
  • To use, simply edit has_attachment line with the new thumbnail sizes you want.
  • IdeaImage is the model that I want to update
  • parentless is a named_scope that gets only the IdeaImages whose parent_id's are null.

namespace :images do
desc "Updates all the thumbnails of idea_images based on the thumbnails hash in the model"
task(:update_idea_images => :environment) do
IdeaImage.parentless.each do |i|
#destroy the thumbnails first
i.thumbnails.each { |thumbnail| thumbnail.destroy } if i.thumbnailable?
#then recreate the thumbnails
i.attachment_options[:thumbnails].each { |suffix, size| i.create_or_update_thumbnail(i.create_temp_file, suffix, *size) }
end
end
end

Friday, August 1, 2008

Installing the latest gVim and Vim in Ubuntu Hardy Heron

I recently got a new computer and reinstalled Ubuntu and found out that my old post on how to install the latest gVim in Hardy Heron didn't work anymore. This is what I did to install the latest one. Thanks godlygeek!


sudo apt-get build-dep vim
sudo apt-get install build-essential devscripts fakeroot dpkg-dev
apt-get source vim
cd vim-7.1
(cd upstream/patches; python get_patches.py)
dch -v 1:7.1-138+1ubuntu4~$(whoami)1 "Updated with patches through $(ls upstream/patches | grep -v '\.py$' | tail -n 1)"
sed -i -e '/gvimtutor/d' -e "1idebian/tmp/usr/bin/gvimtutor\t\t\t\tusr/bin/" debian/vim-runtime.install.in
dpkg-buildpackage -rfakeroot -uc -b
Having trouble viewing that? See the pastie.

Now, you're supposed to have .deb files in the parent directory. I ran into a problem though in the last line. Here are the last few lines of output after running dpkg-buildpackage -rfakeroot -uc -b: http://pastie.org/246789

To fix this, godlygeek asked me to edit vim-7.1/debian/vim-runtime.install.in and remove the line with gvimtutor in it. See my vim-runtime.install.in file.

So I edited it (gedit
vim-7.1/debian/vim-runtime.install.in), then ran the last line (
dpkg-buildpackage -rfakeroot -uc -b
in vim-7.1 directory again.

I got the compiled *.deb files in the parent directory!

To install them (note that your files will look different):

sudo dpkg -i vim-full_7.1-138+1ubuntu4~ramon1_all.deb
sudo dpkg -i vim-gui-common_7.1-138+1ubuntu4~ramon1_all.deb
sudo dpkg -i vim-runtime_7.1-138+1ubuntu4~ramon1_all.deb
sudo dpkg -i vim-gnome_7.1-138+1ubuntu4~ramon1_i386.deb

Sunday, July 27, 2008

gVim and placing scripts

It seems that everytime I install gVim in Ubuntu the place to put scripts is different. If anyone can shed some light on this please let me know. The last time I installed it, I put the scripts in /usr/share/vim/vimcurrent

Wednesday, June 11, 2008

config/initializers/new_rails_defaults.rb and problems with Rails 2.0.2

I was trying to debug a Rails 2.0.2 app that didn't work on HostingRails. I froze the gems in the vendor folder and stuff, so that couldn't be the problem. When I looked at the cPanel error log, I saw this:


[Wed Jun 11 08:34:18 2008] [notice] mod_fcgid: process /home/royalpa/public_html/dispatch.fcgi(20133) exit(communication error), terminated by calling exit(), return code: 1
from /home/royalpa/site_files/royalpalm/config/initializers/new_rails_defaults.rb:5


I never saw new_rails_defaults.rb before so I looked at it and looked at my older Rails apps. It wasn't there. I suppose it's there because when you do "rails appname" in hostingrails, it makes a Rails 2.1 skeleton. It thinks you're going to make a 2.1 app! Delete it and it should work.

Friday, May 23, 2008

Ruby, Rails, Mongrel, Monit, Nginx on Ubuntu 7.10 Gutsy

Ubuntu 7.10 on SilverRack

I used to have a tutorial here, and I had gotten the files from an existing server. I made it for myself. Since ezra posted making sure I wasn't violating any copyrights, I've decided to take it down, since I can't prove where those files were taken from.

Sunday, May 11, 2008

Rails.vim breaks in Hardy Heron

Well, Vim breaks when using rails.vim in Ubuntu 8.04 Hardy Heron. Took me a few days to decide to actually try and fix it and not downgrade back down to Gutsy.

This post explains what you can do to fix this error. We'll upgrade to the latest Vim. Because the error is caused by an old version of Vim and rails.vim, you need to update it. You can read about it in this thread. That was my starting point to find the fix.

1) Download and install the latest version of Vim

svn co https://vim.svn.sourceforge.net/svnroot/vim/vim7
cd vim7
./configure --with-features=huge
sudo make
sudo make install

2) Install all the plugins and scripts again
3) If you use haml, when I downloaded the latest version today of the haml syntax script, I had some illegal carriage return errors. I had to open haml.vim then save it via

:w ++ff=unix

Saturday, March 22, 2008

GawingAttachable

Do you want to be able to attach files to something? I needed to do that today and I made a plugin called Gawing Attachable (translated: make_attachable). It allows you to add attachments to something you make attachable.

Yes, there are many things you can still do to this, but like they say: make something quick and fix it up later on.

Check out the README before installing.
script/plugin install http://lucidph.dreamhosters.com/gawing_attachable/trunk

Generating a resource in the routes.rb file

When making a generator, you may want to add something to the routes.rb file. To understand this you need to know how to make generators. Take a look at Ryan Bates' Railscast on how to do this.

To add a normal resource, do it this way:


class GawingFileableGenerator < Rails::Generator::Base
def manifest
record do |m|
...
m.route_resources :files
end
end
end


If you want to add other stuff like :member or :collection, do it this way


class GawingFileableGenerator < Rails::Generator::Base
def manifest
record do |m|
...
route_string = ":files, :member => { :download => :get }"
def route_string.to_sym; to_s; end
def route_string.inspect; to_s; end
m.route_resources route_string
end
end
end

Wednesday, March 12, 2008

acts_as_state_machine: make initial state exists!

Ugh.. I tried out acts_as_state_machine and I kept getting this nil error and I had no idea why. I mean, I followed the code in agilewebdevelopment.com properly. Here's the code in that website:


acts_as_state_machine :initial => :created, :column => 'status'

# These are all of the states for the existing system.
state :submitted
state :processing
state :nonprofit_reviewing
state :accepted


Oh... apparently, you need to add


state :created


to it as well :)

Monday, March 3, 2008

Expected User, got User; forcing to reload plugins

I made my own plugin and started getting this error. This plugin is a variation of the acts_as_voteable plugin by Juixe. I did some searching around, I found this blog post that explains the fix.

Apparently, in development mode, there are issues when it comes to reloading plugins. Because the model "Vote" in my case, or "Comment" in the blog post's case, is defined inside the plugin, Rails doesn't see that it should load it again. Anyway, better to read that post for an explanation.

What I did to fix it was add this code at the end of the environment.rb file:
 # Array of plugins with Application model dependencies.
reloadable_plugins = ["acts_as_commentable"]

# Force these plugins to reload, avoiding stale object references.
reloadable_plugins.each do |plugin_name|
reloadable_path = RAILS_ROOT + "/vendor/plugins/#{plugin_name}/lib"
Dependencies.load_once_paths.delete(reloadable_path)
end
See the code.

Wednesday, February 27, 2008

Creating your own plugin

I finally forced myself to make my own plugin instead of having controllers and models all over the place. Of course, I waited for the right time to make a plugin that wasn't too different, and needed to be different at the same time.

I chose to make another version of acts_as_voteable by Juixe. Here are the steps I took
  1. I watched Ryan Bates' screencast on making plugins
  2. I coped acts_as_voteable and starting editing things one by one to see what broke.
  3. When I needed a clearer and more advanced explanation (like adding helpers and such), I referred to Geoffrey G's howto on creating plugins.
It took me about 4 hours to figure the whole thing out (I'm a slow learner.. and I already knew about polymorphic associations!).

The change I had to make to acts_as_voteable was that I had to have 3 kinds of votes: positive, neutral and negative. Very small change, right? :)

You can get my plugin like this:

script/plugin install http://lucidph.dreamhosters.com/gawing_voteable/trunk

Monday, February 25, 2008

Syntax highlighting

I did a fresh install of Ubuntu on my laptop, and I installed gVim (rails.vim) as well. To my surprise, there was no syntax highlighting! Good thing a quick ":syntax on" fixed that.

Friday, February 8, 2008

Expected "model.rb" to define Model

I was getting this error:

ActionView::TemplateError (Expected /home/lucidph/public_html/rails/pbed/app/models/article_photo.rb to define ArticlePhoto) on line #5 of articles/gen_xml.xml.builder

A lot of people get this with ferret and some bug with it, but I didn't have ferret. The problem: I forgot to upload attachment_fu to the server!

Friday, February 1, 2008

Deploying in Bluehost

I started writing this entry not knowing what is wrong with my app in Bluehost - except that it isn't running. I know this because I get a :

Application error

Rails application failed to start properly

1) First I made sure it had all the gems it needed. Since we wanted to freeze our gems, we had put them in the gem folder.

I added this to our environment.rb

ENV['GEM_PATH'] = '/home/YOUR_USERNAME/.gems:/usr/lib/ruby/gems/1.8'

2) Looking at the production.log, now I see an error. What's the error? Among other stuff, the one that's most readable is "Missing helper file helpers/formatting_helper.rb".

I upload it.

3) It works.

Tuesday, January 22, 2008

Sorting an array

I was following this tutorial that shows you how to sort an array. My action looked like this:

def index
@articles = NodeType.find(1).nodes + NodeType.find(2).nodes
@articles.sort_by { |article| article.updated_at }
end

But that didn't work! It wasn't sorting at all.. at least as far as I could tell. Strange, because this worked:

def index
@articles = NodeType.find(1).nodes + NodeType.find(2).nodes
@articles = @articles.sort_by { |article| article.updated_at }
end

It seems you have to assign it to an object first, before returning it.

Wednesday, January 16, 2008

redirect_back_or_default in Rails 2.0

Since they removed redirect_back_or_default in Rails 2.0, and I still had a need to do something like that, I made method you can put under the private portion of your ApplicationController:

def redirect_back_or(path)
redirect_to :back
rescue ActionController::RedirectBackError
redirect_to path
end

and you use it just like the redirect_back_or_default method.

Thursday, January 3, 2008

TemplateError, Markaby, HostingRails

Markaby in HostingRails causes some TemplateError. To fix it, in vendor\plugins\markaby\lib\markaby\rails.rb, replace

compile_and_render_template(template_extension, template, file_path, local_assigns)

with

compile_and_render_template(@@template_handlers[template_extension.to_sym], template, file_path, local_assigns)

source: comments section of a posting in the RoR blog.