2010-12-15

Fresh mac rails development setup - quick and clean

just got a new macbook pro,
logging this here incase I have to do it again soon.
if you have a new mac and you happen to find this blog post soon after I'm posting it, this will save you some time! :)

the sites I used to get help listed here:
https://github.com/mxcl/homebrew/wiki/installation
http://everydayrails.com/2010/06/28/rvm-gemsets-rails3.html
http://blog.theablefew.com/very-simple-homebrew-mysql-and-rails
http://spyrestudios.com/setting-up-a-rails-development-system-on-mac-osx-snow-leopard

exactly what I did, in order, copy and paste these codeblocks:
1. first install xcode(downloaded from the mac dev site)
2. install homebrew:

ruby -e "$(curl -fsSLk https://gist.github.com/raw/323731/install_homebrew.rb)"
3. modify /usr/local permissions:

sudo chown -R $USER /usr/local
curl -Lsf http://github.com/mxcl/homebrew/tarball/master | tar xz --strip 1 -C/usr/local
4.a install RVM
bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
4.b create .bash_profile
touch ~/.bash_profile
4.c add this to ~/.bash_profile
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
5. install ruby 1.8.7(via rvm)
rvm install 1.8.7
6. install ruby 1.9.2(via rvm)
rvm install 1.9.2
7. make rvm's 1.9.2 the system default(and switch to it)
rvm 1.9.2 --default

8.a install mysql
brew install mysql
8.b following instructions printed out after mysql installs: "If this is your first install, automatically load on login with:"
cp /usr/local/Cellar/mysql/5.1.53/com.mysql.mysqld.plist ~/Library/LaunchAgents
launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist
mysql_install_db
/usr/local/Cellar/mysql/5.1.53/bin/mysqladmin -u root password 'YOURPASSWORDHERE'
sudo touch /etc/my.cnf
sudo vim /etc/my.cnf
this is what I put there:
[mysqld]
max_allowed_packet=64M
character-set-server = utf8
default-character-set = utf8

[mysql]
default-character-set = utf8

[client]
default-character-set=utf8

I made a default user for mysql client
touch ~/.my.cnf
vim ~/.my.cnf
pasted in this:
[client]
user = root
password = YOURPASSWORDHERE

install rails gem and dependencies(remember this is for the 1.9.2 version of ruby, each version of ruby in rvm has it's own location for gems(and then there's gemsets, which I won't be setting up at the moment)
gem install rails
install mysql gem, and config for mac:
env ARCHFLAGS="-arch x86_64" gem install mysql --no-rdoc --no-ri -- --with-mysql-config=/usr/local/bin/mysql_config

if this is what you do, you can probably pick it up from here

2010-09-15

UI for Editing serialized ruby Hash stored in database

For one part of a new project of mine I have decided to store lots of data in a hash stored in a database column. Of course I employed this in my model, I needed a way for a user to edit this hash and save it back out to the database. I drew this up on the board today, and so far, in a very short amount of time, I finished how to output the serialized nested Hash.

Here's my logic that implements nested looping partials, only escaping the loop when the item is a string and not another hash. This provided for output of an infinitely nested hash.


And here's the output with a single record I just threw in there:(ignore the style, it's some inline css that took me 5 seconds to write)



Now I will be making this editable, (new key/value pairs, new hashes...) at whatever depth I see necessary for this order data.

2010-09-14

Building a google news sitemap with Rails and XML Builder

I was recently tasked with creating a google news sitemap for our main websites
See google's formatting instructions here:
http://www.google.com/support/news_pub/bin/answer.py?hl=en&answer=74288

This was my first time creating an xml output with rails/builder from scratch, so I'm documenting my experience.

oh yeah, this is an older rails app, version 2.0.2

I found a lot of documentation online about builder and creating the tags, but there is NO documentation anywhere about how to create tags with namespaces. I even looked through my massive collection of ebooks. NOTHING!

So I banged my head on my desk for a few hours(metaphorically), and stumbled upon the syntax by chance.

I really hope this helps someone in the future, because I would have paid actual money to someone for this information.

here's my rails {template}.xml.builder syntax I'm using to generate the proper google news sitemap:(sorry about the indenting, bloggers system screws up this post if I try to use the PRE tag to retain whitespace)



xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
xml.urlset "xmlns"=>"http://www.google.com/schemas/sitemap/0.9",
"xmlns:news"=>"http://www.google.com/schemas/sitemap-news/0.9" do
for a in @articles do
xml.url do
xml.loc(a.smart_permalink_url(true))
xml.news:news do
xml.news:publication do
xml.news(:name, $site[:name])
xml.news(:language, @lang)
end
xml.news(:genres, 'Blog')
xml.news(:publication_date, a.published_at.strftime("%Y-%m-%d"))
xml.news(:title, a.title)
xml.news(:keywords, a.keywords.gsub('_', ' '))
end
end
end
end

2010-09-03

RVM on Ubuntu 10.04

I'm beginning the coding phase on a new project. A few days ago rails 3.0.0 was released. I'm going to hop on that bandwagon. And I've decided i'm going to install ruby 1.9.2.

During the obligatory google'ing I discovered RVM: Ruby Version Manager, and I particularly liked this blog post in which contains the phrase "...let’s move the fuck on people, the first preview of 1.8 was released in 2002, and 1.9 has been here for three years!"

I couldn't agree more with his thoughts on 1.9, especially since it's supposed to be MUCH faster. I remember watching a video of Matz last year speaking in his horribly broken engrish about 1.9. I recall him saying how 1.9 fixed many/all of the fundamental logic flaws in 1.8x. Logic flaws? That sounds like something I want to get away from anyway.

So here I go, Documenting what I did to get RVM up and running on my main development box(Ubuntu 10.04).

you'll need some of this:
sudo apt-get install build-essential

and this:
sudo apt-get install curl

and:
sudo apt-get install zlib1g-dev libreadline5-dev libssl-dev libxml2-dev

NOTE: RVM is ruby code itself, so you'll need ruby and rubygems already installed(if you're reading this I'm sure this is already the case)

then i ran:
sudo gem rvm install

and this is what it spit out:

********************************************************************************

In order to setup rvm for your user's environment you must now run rvm-install.
rvm-install will be found in your current gems bin directory corresponding to where the gem was installed.

rvm-install will install the scripts to your user account and append itself to your profiles in order to
inject the proper rvm functions into your shell so that you can manage multiple rubies.

********************************************************************************
Successfully installed rvm-1.0.2
1 gem installed
Installing ri documentation for rvm-1.0.2...
Installing RDoc documentation for rvm-1.0.2...


OK looks good so far, i ran
rvm-install
like it told me to and got this huge output:


RVM: shell scripts which allow management of multiple ruby interpreters and environments.
RTFM: http://rvm.beginrescueend.com/
HELP: http://webchat.freenode.net/?channels=rvm (#rvm on irc.freenode.net)

* Installing rvm to /home/eric/.rvm/
Correct permissions for base binaries in /home/eric/.rvm/bin...
Copying manpages into place.ls: cannot access ./man: No such file or directory


Notes for Linux ( DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.04
DISTRIB_CODENAME=lucid
DISTRIB_DESCRIPTION="Ubuntu 10.04.1 LTS" )

* NOTE: MRI stands for Matz's Ruby Interpreter (1.8.X, 1.9.X), ree stands for Ruby Enterprise Edition and rbx stands for Rubinius.


* curl is required.

* patch is required (for ree, some ruby head's).

* If you wish to install rbx and/or any MRI head (eg. 1.9.2-head) then you must install and use rvm 1.8.7 first.

* For JRuby (if you wish to use it) you will need:
$ aptitude install curl sun-java6-bin sun-java6-jre sun-java6-jdk
* For MRI & ree (if you wish to use it) you will need (depending on what you are installing):
$ aptitude install build-essential bison openssl libreadline5 libreadline-dev curl git-core zlib1g zlib1g-dev libssl-dev vim libsqlite3-0 libsqlite3-dev sqlite3 libreadline-dev libxml2-dev git-core subversion autoconf
* For IronRuby (if you wish to use it) you will need:
$ aptitude install curl mono-2.0-devel


Eric Young,

Thank you for using rvm. I hope that it makes your work easier and more enjoyable.
If you have any questions, issues and/or ideas for improvement please hop in #rvm on irc.freenode.net and let me know.
My irc nickname is 'wayneeseguin' and I hang out from ~09:00-17:00EST and again from ~21:00EST-~00:00EST.
If I do not respond right away, please hang around after asking your question, I will respond as soon as I am back.
Be sure to get head often as rvm development happens fast, you can do this by running 'rvm update --head'.
w⦿‿⦿t!
~ Wayne

You must now finish the install manually:
1) Place the folowing line at the end of your shell's loading files(.bashrc or .bash_profile for bash and .zshrc for zsh), after all path/variable settings:
[[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm
Please note that this must only occur once - so, you only need to add it the first time you install rvm.
2) Ensure that there is no 'return' from inside the .bashrc file. (otherwise rvm will be prevented from working properly).
This means that if you see '[ -z "$PS1" ] && return' then you must change this line to:
if [[ -n "$PS1" ]] ; then
... original content that was below the && return line ...
fi # <= be sure to close the if. #EOF .bashrc Be absolutely *sure* to REMOVE the '&& return'. If you wish to DRY up your config you can 'source ~/.bashrc' at the bottom of your .bash_profile. placing all non-interactive items in the .bashrc, including the 'source' line above 3) Then CLOSE THIS SHELL and open a new one in order to use rvm. WARNING: you have a 'return' statement in your .bashrc, likely this will cause untold havoc. This means that if you see '[ -z "$PS1" ] && return' then you must change this line to: if [[ -n "$PS1" ]] ; then ... original content that was below the && return line ... fi # <= be sure to close the if. #EOF .bashrc Even if you use zsh you should still adjust the .bashrc as above. If you have any questions about this please visit #rvm on irc.freenode.net. Installation of RVM to /home/eric/.rvm/ is complete.


Ok, so it's warning me about the return statement, I must admit I don't know exactly what this means, so I'll follow the instructions verbatim.

I actually added the:
[[ -s $HOME/.rvm/scripts/rvm ]] && source $HOME/.rvm/scripts/rvm
to the bottom of my ~/.profile file (which is linked from ~/.bashrc)

then I refreshed my bash profile settings with this command:
source ~/.profile

And now when i run
rvm
I get what looks like the usage manual, ok, looking good so far(I think), Now I wanna get me some 1.9.2

rvm install 1.9.2
spit out:
info: Installing Ruby from source to: /home/eric/.rvm/rubies/ruby-1.9.2-p0

info: Downloading ruby-1.9.2-p0, this may take a while depending on your connection...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 8296k 100 8296k 0 0 74580 0 0:01:53 0:01:53 --:--:-- 67103

info: Extracting ruby-1.9.2-p0 ...

info: Configuring ruby-1.9.2-p0, this may take a while depending on your cpu(s)...

info: Compiling ruby-1.9.2-p0, this may take a while depending on your cpu(s)...

info: Installing ruby-1.9.2-p0

info: Installation of ruby-1.9.2-p0 is complete.

info: Updating rubygems for /home/eric/.rvm/gems/ruby-1.9.2-p0@global

info: Updating rubygems for /home/eric/.rvm/gems/ruby-1.9.2-p0

info: adjusting shebangs for ruby-1.9.2-p0 (gem irb erb ri rdoc testrb rake).

info: Importing initial gems...


Awesome, I feel the end is near.

Woo, it's done and it worked:

eric@elydevbox:~$ ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]
eric@elydevbox:~$ rvm 1.9.2
eric@elydevbox:~$ ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
eric@elydevbox:~$ irb
ruby-1.9.2-p0 > puts "hi"
hi
=> nil
ruby-1.9.2-p0 >


RVM WINS THE DAY

an excerpt from http://marcgrabanski.com/articles/gem-management-with-rvm-and-bundler
___________________________
Dont forget about gems:

Gem Management with RVM

RVM has “gemsets” which allow you to organize different sets of gems. If you install gems into the global gemset, then it will be available to you no matter which gemset you are using.

Create the global gemset.
rvm gemset create global
rvm gemset use global


Install gems here that will be used in projects like bundler and passenger.

gem install bundler
gem install passenger


Notice that I never use sudo because rvm puts these gemsets in your user ~/.rvm directory.

Now create and use your project gemset.

rvm gemset create YOUR_GEMSET
rvm gemset use YOUR_GEMSET


Obviously YOUR_GEMSET can be anything you want. Then you are set to go!

2010-05-12

Xapian on Rails

just cloned one of my work projects at home, executed script/server and BAM:
"acts_as_xapian: No Ruby bindings for Xapian installed"

I installed xapian and set this all up on my ubuntu laptop, now again to set it up on my desktop, so I figured I would document it here because I forgot what I did on my laptop.

Here goes the steps I took:
1 - Went to ubuntu package manager, searched for "xapian", installed "libxapian-ruby1.8"
2 - run "rake xapian:rebuild_index models="Article"

done :),
maybe this was too easy for a blog post. whatever

2010-05-10

my solution for rails ajax'd child-model form fields

I am mid-early development of a new interface for a web application. I can say that the interface will be making full use of as much ajax as is necessary and possible, and of course I'm using accepts_nested_attributes_for on the parent model. One problem I encountered early on was how to populate additional form fields using ajax.
The first solution I stumbled upon was the plugin ryan bates made to go along with Railscasts episode #196. This worked, but I needed something more flexible.

One of the obvious issues here is how to turn "model[childmodel_attributes][0][attribute]"(created by the form builder) into "model[childmodel_attributes][(random_number)][attribute]".
The solution is not in the documentation!

Try a random number!:
<% form_for @model do |f| %>
<% f.fields_for :childmodel, :child_index => (Time.now.usec + rand(1-999)) do |builder| %>

I use that code in my view to generate fields on a freshly built child model instance built from the rjs-calling controller.
using this method you have a more granular control over the content for the new record, a step above the model, in case you need such flexibility.

<% if builder.object.id.nil? %>
form fields here...

sorry I'm super tired, if this isn't clearly written, I don't doubt it. Actually I may make a video about my solution, because it's brilliant.

2010-05-02

ubuntu 10.04 install nvidia driver

Today I upgraded my laptop and desktop machines to Ubuntu 10.04 LTS. Best upgrade so far. No problems at all.

I'm posting this here for my future reference on how I installed the nvidia driver:

downloaded the driver 195.xxx from nvidia.com

drop to a terminal screen "ctrl + alt + f1"

Kill all instances of X and gdm, "service gdm stop"
and then I had to do "ps -e" to find the remaining X process and manually kill it by it's process ID.

"sudo kill xxxx"

Then edit this file as root:
"sudo nano /etc/modprobe.d/blacklist.conf"

Add these lines and save: (this part is very important, especially the nouveau part, that new driver really screws with the nvidia one)

blacklist vga16fb
blacklist nouveau
blacklist rivafb
blacklist nvidiafb
blacklist rivatv

then:
"sudo apt-get --purge remove nvidia-*"

"chmod 744 NVIDIA-Linux-x86_64-195.36.24-pkg2.run"

"sudo sh NVIDIA-Linux-x86_64-195.36.24-pkg2.run"

after a successful install either reboot or: "sudo service gdm start" or just "startx" if you dont need gdm running.

2010-03-11

macports mac port

I started setting up my new iMac from scratch today for developing ruby and ruby on rails. This is quite different for me, i'm used to Ubuntu for rails stuff. But hey, mac is super sexy so I'll be using that from now on.

Here's some notes for myself and others to reference later.

This is what I've done so far on Snow Leopard 10.6.2

1: install xcode

2: update xcode

3: installed MacPorts-1.8.2-10.6-SnowLeopard.dmg from http://distfiles.macports.org/MacPorts/

4. sudo port install mysql5-devel mysql5-devel-server rb-rubygems rb-rmagick

5. then I ran sudo gem update

6. sudo gem install rails mongrel sqlite3-ruby

7. sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config

8. I just did "sudo gem install nokogiri" and I got an error:
ERROR: Error installing nokogiri:
invalid gem format for /opt/local/lib/ruby/gems/1.8/cache/nokogiri-1.4.1.gem

I resolved this by removing that file
sudo rm /opt/local/lib/ruby/gems/1.8/cache/nokogiri-1.4.1.gem
then doing
sudo gem install -V nokogiri
again and for some reason it worked.

9. sudo port install memcached

10. then a gem i was installing via "sudo gem install" told me it required 1.3.6, I guess port provided me with 1.3.5. so I let gem update itself.. "sudo gem --system update" it was either that or "sudo gem update --system". this brought me to 1.3.6

11. oh yeah and I needed to add the mysql bin dir to my ~/.profile file, making my .profile path line look like this
export PATH=/opt/local/bin:/opt/local/lib/mysql5/bin:/opt/local/sbin:$PATH


12.

this is all I can remember doing, it there were any other hiccups i'll return to add the solutions