photography portfolio
projects
A more detailed section is in the works. For now, here are a few to look at.
Ruby Array to Javascript Array Mar 10

Need to pass a Ruby Array to a Javascript Array?
Here’s a little gem I found a while ago that I’m reusing in my current project.


>> a_ruby_array = ["one", "two", "three"]
=> ["one", "two", "three"]
>> "['#{a_ruby_array.join('\',\'')}']"
=> "['one','two','three']"

Validations For Multiple Nested Model Forms Mar 06

Here’s a little trick I found when handling multiple nested models in a form that require validations for each model.

Spec time. For example, we have a model, Author, which has_many Books and has_many Magazines. Book and Magazine has validations. On a single page, we create a three forms for Book, Magazine, and Author. There is also an option select that a user can choose if the Author has a Book or a Magazine.

Pretty straight forward. Let’s do some code.


#models/author.rb
class Author < ActiveRecord::Base
  has_many :books
  has_many :magazines

  accepts_nested_attributes_for :books
  accepts_nested_attributes_for :magazines

  validates_presence_of :name
end


#models/book.rb
class Book < ActiveRecord::Base
  belongs_to :author
  validates_presence_of :title, 
                        :genre
end


#models/magazine.rb
class Magazine < ActiveRecord::Base
  belongs_to :author
  validates_presence_of :title, 
                        :genre
end

Basic model setup with validations and accepts_nested_attributes_for. More info on that can be found in the Ruby on Rails API – Nested Attributes.

Moving on to the form setup..


#views/authors/new.html.erb
<% form_for @author do |f| %>
<%= f.error_messages %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.radio_button("media_type", "book" %><%= f.label :media_type_book, 'Book' %> [?]
    <%= f.radio_button("media_type", "magazine" %><%= f.label :media_type_magazine, 'Magazine' %> [?]
  </p>
  <% f.fields_for :books do |book| %>
    <p>
      <%= book.label :title %><br />
      <%= book.text_field :title %>
    </p>
    <p>
      <%= book.label :genre %><br />
      <%= book.text_field :genre %>
    </p>
  <% end %>

  <% f.fields_for :magazines do |magazine| %>
    <p>
      <%= magazine.label :title %><br />
      <%= magazine.text_field :title %>
    </p>
    <p>
      <%= magazine.label :genre %><br />
      <%= magazine.text_field :genre %>
    </p>
  <% end %>
  <%= f.submit 'Submit' %>
<% end %>
  

That sets up the form with the nested model, now for the controller code.


#controllers/authors_controller.rb
class AuthorsController < ApplicationController
  def new
    @author = Author.new
    books = @author.books.build #this builds the nested form in the view
    magazines = @author.magazines.build #this builds the nested form in the view
  end

  def create
    param_hash = params[:author]
    if params[:author][:media_type] == "book"
      param_hash.delete("books_attributes")
    elsif params[:author][:media_type] == "magazine"
      param_hash.delete("magazines_attributes")
    end
    @author = Author.new(param_hash)
    @author.save
  end
end

Instead of passing the params straight to Author.new, it’s put into a hash variable. Then the params get checked for a title and genre, if empty delete the key from the hash and pass to Author.new. Activerecord wont see the book param and will skip the validations for it.

Android Apps Mar 05

Is it me or do 90% of the Android apps look similar to Windows 3.1 software?

I’ve been developing an Android app over the past few weeks and I noticed that the GUI quality for Android apps is lacking. I’m pretty sure this is do to the lack of filtering that Google does, which Apple requires.

While looking through dozens and dozens of apps, I came across comments such as, “if this looked better, i’d give it a 5 star.”

That got me thinking about this app I’m currently working on. If I make this app beautiful, I could probably sell it for a $1 more!

I believe that a majority of app users, iPhone or Android, really want pretty interfaces over anything else.

Google Buzz Feb 12

I’m actually pretty surprised that I would be this excited about Google Buzz. I guess it could be the fact that it’s integrated into a system I already use that makes it appealing. Or perhaps I’m tired of limiting what I have to say and read to 140 characters. If the idea behind 140 characters was based on SMS, and SMS is now outdated because we’re all sending emails from our phone, shouldn’t the next step in technology evolution be limitless character status updates? With photos and video attachments too!

I remember a service around the same time Twitter gained it’s popularity called Pownce. They tried to accomplish a social network based around status updates with no limits, and failed, while everyone jumped on the 140 character limit text only status update bandwagon. Maybe people are tired of this limit and Buzz will pull people away from Twitter? Or will Buzz be too complicated to use and the 140 character limit and it’s simplicity will keep their users separated for now?

For myself, I love Twitter and I love Google. So I’ll keep both. For now. I’m making an app that will sync my tweets with my buzzes and my buzzes with my tweets.(not really)

Radiant CMS and some Feb 03

I’ve been pretty busy working on my current project and needed a break to let my mind rest and decided to try out Radiant CMS.

A few hours and I fully converted my website over, with a little hacking to get my URLs to match. I was very impressed on how easy it was to setup. I actually thought there would be more to do.

Anyways, I’m definitely excited to play with some of the extensions for Radiant CMS. For now, comments are gone, but they’ll be back soon I hope.

Also would like to mention that my site feels faster by at least 50%. Loving the built in caching.

Update: Comments are back, took all of 5 minutes to setup Disqus!

Adding A Simple Search In 5 Minutes to a Ruby on Rails Site Oct 24

Prototyping a site in Ruby on Rails and need to add a simple search feature in less than 5 minutes?

Note: This should be for smaller projects because I’ll be using regular SQL queries for searching. If you’re looking for something more advanced, try thinking_sphinx(sphinx engine) or sunspot_solr(java lucene solr engine).

I’ll be using Searchlogic and will_paginate in this demo.


#install searchlogic
sudo gem install searchlogic

#install will_paginate
gem sources -a http://gemcutter.org
sudo gem install will_paginate

Then add the gem to the environment file.


#config/environment.rb

config.gem "binarylogic-searchlogic", :lib => "searchlogic"
config.gem 'will_paginate', :version => '~> 2.3.11'

If you prefer to use github or the plugin version, check out their docs. I also suggest reading their docs to learn more about all the neat functions Searchlogic can do. It’s a very powerful and useful plugin.

We’ll be adding the search to Posts, so let’s start by adding the search route to our posts.


#config/routes.rb

map.resources :posts, :collection => { :search => [ :post, :get ] }

This will give you the path /posts/search GET and POST.

Next add the search method to the PostsController.


#app/controllers/posts_controller.rb

class PostsController < ApplicationController

  def index
    @posts = Post.paginate(:all, :order => 'created_at desc', :per_page => 25, :page => params[:page])
  end

  def search
    if request.post?

      #pass the post into a get with the 'q' param
      redirect_to search_posts_path(:q => params[:search][:query])

    elsif request.get?

      unless params[:q].blank?

        #Searchlogics title_like_all to search within titles in the Post model for any of the words in the query.
        #words in the string are split into an array by spaces
        search = Post.title_like_all(params[:q].to_s.split).descend_by_created_at

        #paginate the results
        @posts = search.paginate(:per_page => 25, :page => params[:page])

        #pass the query object to the view to let the user know what they searched for
        @query = params[:q]

      end

      #render the Post index.html.erb
      render 'index'

    end
  end

end

The last part is the search form in your view.


<p>
	<% form_for :search, :url => { :controller => 'posts', :action => 'search' } do |f| %>
		<%= f.text_field :query, :value => @query%>
		<%= f.submit "Search", :disable_with => "Searching..."  %>
	<% end %>
</p>

Zeiss Planar 50/f2 vs Voigtlander Nokton 50/f1.1 Oct 19

I have the Voigtlander Nokton 50/f1.1 for the next week and I’m planning on running several rolls side-by-side against my Zeiss Planar 50/f2.

I’m considering replacing the Planar with this so we’ll see. I’m a little skeptical though, since Zeiss bokeh is pretty hard to beat imo.

Stay tuned for results and photos!

Zeiss 50/f2 Planar vs Voigtlander 50/f1.1 Nokton - time to play!

More Turkey Oct 02

L1001219

L1001198

Sheep Sep 07

L1001994

A Fresh Snow Leopard Sep 05

I’ve become so normalized to formatting and wiping my OS clean every year/update during my Windows days that I decided to do the same with Snow Leopard. I also decided it would be a great time to upgrade the harddrive in my laptop to the WD 320gb 7200rpm.

After finishing that I followed the guides over at Hivelogic to get everything setup and running again. One thing to note was installing a ruby 1.8.6 since Snow Leopard by default installs 1.8.7 and my production server is running on Ruby Enterprise(1.8.6).

Compiling Ruby, RubyGems, and Rails on Snow Leopard
Compiling MySQL on Snow Leopard
Compiling Git on Snow Leopard