Wow it's been a year since I've last posted!
I'm currently checking out dragonfly, the new kid on the block file processor. It's mean to replace stuff like paperclip and attachment_fu. To make things simple, especially if you don't have a lot of files, when migrating from either of those to dragonfly it may be simpler to just download the file and upload it again via dragonfly. This way you don't have to study the internals of each file management plugin.
So, here's how to download a file.
require 'net/http'
url = URI.parse "http://url.to/file.name"
Net::HTTP.start url.host do |http|
response = http.get url.path
MyModel.create! :file => response.body
# Here you tell your model to create the file.
# This is what the create action in the controller calls
# but instead of params[:file], you have response.body
end
Thursday, April 15, 2010
Friday, April 17, 2009
Wednesday, April 1, 2009
Stars and acts_as_rateable
I recently wanted to implement a star system in Rails. I looked at this rateable plugin and followed this tutorial to create the stars.
It wasn't as cut and dried as I had hoped, so here is the code I used. It's pretty complete. Disclaimer: I used haml and sass and if you don't, you should still be able to understand the code. In case you don't use sass, here's the generated CSS that you get from the sass file.
It wasn't as cut and dried as I had hoped, so here is the code I used. It's pretty complete. Disclaimer: I used haml and sass and if you don't, you should still be able to understand the code. In case you don't use sass, here's the generated CSS that you get from the sass file.
Friday, March 27, 2009
before_create should return the right boolean!
This is a small thing, but I wasted an hour or so figuring this thing out. I had an a before_create where I set a boolean variable to false and then my tests started to fail? Why? Because before_create will stop the creation of the record if it returns false!
class A << AR::Base
before_create :set_enabled
def set_enabled
self.enabled = false
true # so that it doesn't fail
end
end
Friday, March 20, 2009
Passenger, Monit, Postfix
Setting up Passenger was quite easy, coming from the world of Mongrel. For the most part I followed this. It's pretty comprehensive, but I'll just add some stuff that I had to do on my own.
This is far from a comprehensive list, and if you just follow this things won't work. This also assumes you have _some_ knowledge of how Postfix, Monit, Linux works.
Postfix
I accept and read mail somewhere else (not on the VPS that sends mail). For example, I may use Google to accept mail on the Internet, however, I don't connect my server to Google to send mail. Because of this, when postfix on my server tried to send mail to mydomain.com, it always something like "there is no user on this server". So I opened up /etc/postfix/main.cf and make sure that mydestination doesn't contain your domain name. mydestination tells postfix what resides on your local machine.
Also, to use it, my Rails app's setting were like this:
SilverRack and Sources
Installing the clean 8.04 on SilverRack, /etc/apt/sources.list is virtually empty. You need to add:
Then "sudo apt-get update" after
Monit
The tutorial above didn't have any tutorial for Monit or God.rb. Install the dependencies first:
Then monit (Make sure this is the latest and greatest version):
Then make the monit file for Apache. This file basically tells monit what to watch out for, and how.
Sample /etc/monit.d/apache.monitrc
check process apache with pidfile /var/run/apache2.pid
start program = "/etc/init.d/apache2 start"
stop program = "/etc/init.d/apache2 stop"
if cpu > 60% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart
if totalmem > 200.0 MB for 5 cycles then restart
if children > 250 then restart
if loadavg(5min) greater than 20 for 8 cycles then alert
group yourappnamehere
Since I use workling, I also made a seperate file for it:
server$ sudo vi /etc/monit.d/workling.monitrc
and paste (do a "which ruby" to find out where ruby is and replace the /usr/bin/ruby if needed)
start program = "/bin/bash -c 'HOME=/var/www/yourappname/current RAILS_ENV=production /usr/bin/ruby /var/www/yourappname/current/script/workling_client start'"
stop program = "/bin/bash -c '/usr/bin/ruby /var/www/yourappname/current/script/workling_client stop'"
if totalmem is greater than 180.0 mb for 5 cycles then restart
if cpu is greater than 90% for 5 cycles then restart
if 20 restarts within 20 cycles then timeout
group yourappname
Then reload monit via "sudo monit reload".
Now monit needs to be told to start automatically when we startup and when/if it crashes, we'll use upstart (this is for Ubuntu only?)
and paste (do a "which monit" to find out where monit is and replace the /usr/local/bin/monit if needed)
This is far from a comprehensive list, and if you just follow this things won't work. This also assumes you have _some_ knowledge of how Postfix, Monit, Linux works.
Postfix
I accept and read mail somewhere else (not on the VPS that sends mail). For example, I may use Google to accept mail on the Internet, however, I don't connect my server to Google to send mail. Because of this, when postfix on my server tried to send mail to mydomain.com, it always something like "there is no user
mydestination = localhost.localdomain, localhost
Also, to use it, my Rails app's setting were like this:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "localhost",
:port => 25,
:domain => "www.theinksquad.com"
}
SilverRack and Sources
Installing the clean 8.04 on SilverRack, /etc/apt/sources.list is virtually empty. You need to add:
deb http://ubuntu.media.mit.edu/ubuntu/ hardy main restricted universe multiverse
deb-src http://ubuntu.media.mit.edu/ubuntu/ hardy restricted main multiverse universe
deb http://ubuntu.media.mit.edu/ubuntu/ hardy-updates main restricted universe multiverse
deb-src http://ubuntu.media.mit.edu/ubuntu/ hardy-updates restricted main multiverse universe
deb http://security.ubuntu.com/ubuntu/ hardy-security main restricted universe multiverse
deb-src http://security.ubuntu.com/ubuntu/ hardy-security restricted main multiverse universe
Then "sudo apt-get update" after
Monit
The tutorial above didn't have any tutorial for Monit or God.rb. Install the dependencies first:
sudo apt-get install flex byacc bison -y
Then monit (Make sure this is the latest and greatest version):
wget http://mmonit.com/monit/dist/beta/monit-5.0_beta7.tar.gz
tar -xzvf monit-5.0_beta7.tar.gz && cd monit-5.0_beta7/ && sudo ./configure && sudo make && sudo make install && cd ../
sudo mkdir /etc/monit.d
Then make the monit file for Apache. This file basically tells monit what to watch out for, and how.
Sample /etc/monit.d/apache.monitrc
check process apache with pidfile /var/run/apache2.pid
start program = "/etc/init.d/apache2 start"
stop program = "/etc/init.d/apache2 stop"
if cpu > 60% for 2 cycles then alert
if cpu > 80% for 5 cycles then restart
if totalmem > 200.0 MB for 5 cycles then restart
if children > 250 then restart
if loadavg(5min) greater than 20 for 8 cycles then alert
group yourappnamehere
Since I use workling, I also made a seperate file for it:
server$ sudo vi /etc/monit.d/workling.monitrc
and paste (do a "which ruby" to find out where ruby is and replace the /usr/bin/ruby if needed)
check process workling with pidfile /var/www/yourappname/shared/log/workling.pid
start program = "/bin/bash -c 'HOME=/var/www/yourappname/current RAILS_ENV=production /usr/bin/ruby /var/www/yourappname/current/script/workling_client start'"
stop program = "/bin/bash -c '/usr/bin/ruby /var/www/yourappname/current/script/workling_client stop'"
if totalmem is greater than 180.0 mb for 5 cycles then restart
if cpu is greater than 90% for 5 cycles then restart
if 20 restarts within 20 cycles then timeout
group yourappname
Then reload monit via "sudo monit reload".
Now monit needs to be told to start automatically when we startup and when/if it crashes, we'll use upstart (this is for Ubuntu only?)
server$ sudo vi /etc/event.d/service_monit and paste (do a "which monit" to find out where monit is and replace the /usr/local/bin/monit if needed)
# This is an event.d (upstart) script to keep monit running# To install disable the old way of doing things:
#
# /etc/init.d/monit stop && update-rc.d -f monit remove
#
# then put this script here: /etc/event.d/monit
#
# You can manually start and stop monit like this:
#
# start monit
# stop monit
#
# Michael Hale (http://halethegeek.com)
start on startup
start on runlevel 2
start on runlevel 3
start on runlevel 4
start on runlevel 5
stop on runlevel 0
stop on runlevel 6
exec /usr/local/bin/monit -Ic /etc/monit/monitrc
respawn
Wednesday, March 18, 2009
Moving To Passenger, Workling
I've used backgroundrb for a while now. Coupled with mongrels, monit, it wasn't a pleasant experience. Oh, I forgot to mention that I was learning Linux at the time (still am actually), so it was very difficult for me.
Lately, I've been comtemplating moving my production environment to Passenger, Apache, Workling and God. The next few posts will talk about my experience and what I did. :)
For now, let me just say that this help page played a big part in speeding up my staging.
Lately, I've been comtemplating moving my production environment to Passenger, Apache, Workling and God. The next few posts will talk about my experience and what I did. :)
For now, let me just say that this help page played a big part in speeding up my staging.
Tuesday, March 3, 2009
Cleaning Up Your ActiveRecord Sessions
I was looking for a way to do this and found a good resource to find out how (without coding it yourself!)
http://www.quarkruby.com/2007/10/21/sessions-and-cookies-in-ruby-on-rails#sexpire refers to a plugin called limited session but the link is no longer valid. Check out http://iprog.com/project/limited_sessions
Update: this breaks in Rails 2.3, I think it's because of the lazy loading. I've never learned much about sessions, so for now I'll move back to file stored.
http://www.quarkruby.com/2007/10/21/sessions-and-cookies-in-ruby-on-rails#sexpire refers to a plugin called limited session but the link is no longer valid. Check out http://iprog.com/project/limited_sessions
Update: this breaks in Rails 2.3, I think it's because of the lazy loading. I've never learned much about sessions, so for now I'll move back to file stored.
Tuesday, February 17, 2009
View helpers in flash
The other day I wanted a nice checkout link in my flash when users added a product to their cart (apart from the checkout in cart area). I soon realized (or remembered) that you don't have access to view helpers in the controller, where you usually assign flash messages.
I spent about 2 hours researching on how to do this but got no answer. People resorted to fancy stuff just to put links in their flash messages. I thought the answer was in the render method. I first tried to use it but it didn't work because you can't call render/redirect_to twice in the same action:
So I looked at the source and tried to figure out what it was in render that read the file and spat out the text. 10 minutes later, I got this to work:
So now I can use link_to and other nice view helpers in that partial and it will all render nicely in the flash message. :)
I spent about 2 hours researching on how to do this but got no answer. People resorted to fancy stuff just to put links in their flash messages. I thought the answer was in the render method. I first tried to use it but it didn't work because you can't call render/redirect_to twice in the same action:
flash[:notice] = render :partial => "some/partial"
So I looked at the source and tried to figure out what it was in render that read the file and spat out the text. 10 minutes later, I got this to work:
flash[:notice] = @template.render(:partial => "some/partial")
So now I can use link_to and other nice view helpers in that partial and it will all render nicely in the flash message. :)
Thursday, February 12, 2009
Smart Activation With Single Sign On
This post is related to another post of mine about getting the rpx single sign on to work.
Scenario:
Requisites
When the user finally has all their stuff filled up and submits the form, if they're on single sign on, then the user will be activated based on the comparison between the email_was and the current email they're trying to save. If they're different, then they're not activated and the user observer sends out the activation mail. If they are the same email, then the account is activated and they have to do one less step. :)
If you have any comments or suggestions please let me know.
Scenario:
- You are using single sign on but require the user to have emails (Facebook does not return email) or require other fields (eg. accept terms of use).
- You allow users to specify another email besides the one that is returned by the single sign on service (SSOS)
- If the user changes that email, you want them to validate it (you send an email to them with a link to activate their account), but if they don't change it (meaning they use the same email that is returned by the SSOS), then the account will be automatically activated
Requisites
- Make sure you got single sign on working (you're able to read responses and stuff)
When the user finally has all their stuff filled up and submits the form, if they're on single sign on, then the user will be activated based on the comparison between the email_was and the current email they're trying to save. If they're different, then they're not activated and the user observer sends out the activation mail. If they are the same email, then the account is activated and they have to do one less step. :)
If you have any comments or suggestions please let me know.
Sunday, February 8, 2009
Blocks in View
I had a bunch of "if logged_in? && current_user.admin?" in my views, which made it very ugly. I saw an old video (it is also covered again) from Railscasts and knew this would be my answer.
However, I wanted more customization though. What if I didn't want to wrap my admin_area in a div? What if I wanted to add conditions on the fly and not make a zillion [enter_some_name]_area methods?
This would be particularly useful if, for example, I only want the "delete" option of an object to appear to admins and if the object if destroyable.
In haml:
Take a look at my helper method to see what I did. I'd appreciate any comments!
However, I wanted more customization though. What if I didn't want to wrap my admin_area in a div? What if I wanted to add conditions on the fly and not make a zillion [enter_some_name]_area methods?
This would be particularly useful if, for example, I only want the "delete" option of an object to appear to admins and if the object if destroyable.
In haml:
- admin_area(:conditions => @object.destroyable?) do
= link_to "Destroy", ...
Take a look at my helper method to see what I did. I'd appreciate any comments!
Saturday, February 7, 2009
Form Builder
I don't know why I never know about Form Builder! I was making my own form helpers but in the completely stupid way - in the helper files, which made it clunky. Check out these two posts that helped me out a lot. Although it can seem complicated because of all the (what I think is) meta programming, I suggest you just dive in and explore.
Friday, February 6, 2009
RPX Now: Single Sign-on
As I write this, I'm trying to install the single sign-on capability that RPX Now gives you on my Rails app. I've tried OpenID before (using open_id_authentication plugin and ruby-openid gem) and it was mightly complicated, plus you couldn't only use other 3rd party accounts.
RPX Now claims to be super easy and so far, with grosser's rpx_now gem, it almost seems too easy -- except there are no tutorials that I can find on Google. There are examples in gem's github site but they don't explain much.
This is my attempt at writing one.
First, install the gem (instructions in the github site). If you want to put it in your environment.rb file, it should look like this (remove or replace the version number with an updated one):
Check out actual responses (I just removed any personal data).
RPX Now claims to be super easy and so far, with grosser's rpx_now gem, it almost seems too easy -- except there are no tutorials that I can find on Google. There are examples in gem's github site but they don't explain much.
This is my attempt at writing one.
First, install the gem (instructions in the github site). If you want to put it in your environment.rb file, it should look like this (remove or replace the version number with an updated one):
config.gem "grosser-rpx_now", :lib => "rpx_now", :version => "0.3", :source => "http://gems.github.com"Then create an RpxController and add code similar to this to your user model. Of course, you don't have to process the data yourself, but I wanted to (thus my rpx_controller is a bit longer, and I had to read the response in the User model) so that if the rpx profile is missing anything, it will render a something like my normal login page that basically has a form_for(@user ...) where they can fill up the missing data.
Check out actual responses (I just removed any personal data).
Hosting Services
I don't run websites with thousands of users at a time, however, I thought I'd still announce what I use for my hosting services.
VPS: SilverRack
Good price, quick service. They don't spend time fussing over you though, but that's expected in a VPS.
Shared: HostingRails (disclaimer: affiliate link)
I've tried BlueHost, Dreamhost, but I've found HostingRails to be the easiest to deal with. Their support replies lightning fast, and don't diss you even if your problems are your fault!
VPS: SilverRack
Good price, quick service. They don't spend time fussing over you though, but that's expected in a VPS.
Shared: HostingRails (disclaimer: affiliate link)
I've tried BlueHost, Dreamhost, but I've found HostingRails to be the easiest to deal with. Their support replies lightning fast, and don't diss you even if your problems are your fault!
Tuesday, February 3, 2009
Using --trace; cucumber & machinist order of loading files
I followed this tutorial to easily use Cucumber with a fixture replacement plugin called Machinist. It worked great on my desktop, but when I moved to my laptop, running rake features didn't work - an "uninitialized constant Sham" error popped up and I spent a day figuring it out.
Although the context of this problem is Cucumber and Machinist (which includes Sham), this is a post that talks about using --trace when you run into problems similar to this.
IF YOU'RE FAMILIAR WITH CUCUMBER & MACHINIST...
The problem occured partially because I put my blueprints.rb file where env.rb is. I believe cucumber loads everything there in alphabetical order (doesn't explain why my desktop loaded things differently). The problem is that blueprints.rb tries to call "Sham" which is something that is loaded by Machinist, which in turn is loaded during or after env.rb, not before.
IF YOU'RE NOT FAMILIAR NEITHER CUCUMBER NOR MACHINIST...
The problem occured because of the order files were loaded. I do not know why files were loaded differently in my desktop vs the laptop, but what the heck -- I found the error by using --trace and actually read the big bad ugly code that rake conveniently hides from us.
http://pastie.org/379188
So the lesson is: actually read what --trace spits out! :)
Although the context of this problem is Cucumber and Machinist (which includes Sham), this is a post that talks about using --trace when you run into problems similar to this.
IF YOU'RE FAMILIAR WITH CUCUMBER & MACHINIST...
The problem occured partially because I put my blueprints.rb file where env.rb is. I believe cucumber loads everything there in alphabetical order (doesn't explain why my desktop loaded things differently). The problem is that blueprints.rb tries to call "Sham" which is something that is loaded by Machinist, which in turn is loaded during or after env.rb, not before.
IF YOU'RE NOT FAMILIAR NEITHER CUCUMBER NOR MACHINIST...
The problem occured because of the order files were loaded. I do not know why files were loaded differently in my desktop vs the laptop, but what the heck -- I found the error by using --trace and actually read the big bad ugly code that rake conveniently hides from us.
http://pastie.org/379188
So the lesson is: actually read what --trace spits out! :)
Friday, January 23, 2009
Speeding up emailing in tests
I lived with the fact that my test ground to a halt when my tests ran code that had to send emails. It finally got so bad I started asking around about it.
I knew about a method to test ActionMailer and not send out emails... and I thought: why not apply these to ActionMailer when it's in the test environment? I ran my tests with rspec. Check out the pastie to see the code.
I knew about a method to test ActionMailer and not send out emails... and I thought: why not apply these to ActionMailer when it's in the test environment? I ran my tests with rspec. Check out the pastie to see the code.
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.
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:
That will tell you the package you need to install.
4. Mine was libmysqlclient15-dev:
5. Try installing Sphinx again, it should work this time.
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.
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:
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!
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 (
I got the compiled *.deb files in the parent directory!
To install them (note that your files will look different):
Having trouble viewing that? See the pastie.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
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.
So I edited it (gedit
dpkg-buildpackage -rfakeroot -uc -bin 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
Subscribe to:
Posts (Atom)