Vixiom Axioms

June 29, 2008

Display Rails associations in a Flex DataGridColumn

Filed under: ActionScript, Flex, Ruby, Ruby on Rails Alastair @ 10:17 am

Frameworks like Ruby on Rails and CakePHP make it easy to set up model associations with belongs_to, has_many, and the ever popular has_and_belongs_to_many. However, getting those associations to show up in a Flex DataGridColumn’s dataField isn’t immediately obvious, you’d assume you could just do parent.child or child.parent but that just gives a blank column. After some digging I found the answer is to use a labelFunction.

In the example below there are two models in Rails, ‘Group’ and ‘Category’, Group has_many Categories and Category belongs_to Group. Here’s the Flex code for the Categories DataGrid, groupName is the labelFunction that spits out a Category’s Group name (equivalent to @category.group.name in ruby):

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%">

    <mx:Script>
        <![CDATA[
            import mx.controls.dataGridClasses.DataGridColumn;

            private function groupName( item:Object, column:DataGridColumn ):String
            {
                return item.group.name;
            }

        ]]>
    </mx:Script>

    <mx:DataGrid id="dataGrid" width="100%" height="100%">
        <mx:columns>
            <mx:DataGridColumn headerText="Name" dataField="name" />
            <mx:DataGridColumn headerText="Group" labelFunction="groupName" />
        </mx:columns>
    </mx:DataGrid>

</mx:VBox>
Digg! submit Display Rails associations in a Flex DataGridColumn to stumbleupon.com submit Display Rails associations in a Flex DataGridColumn to del.icio.us submit Display Rails associations in a Flex DataGridColumn to reddit.com Like this post? subscribe to the feed.

March 5, 2008

Interesting Links: YUIRails, Ebb faster than Mongrel and thin

Filed under: Ajax, JavaScript, Merb, Ruby on Rails Alastair @ 7:23 am

The JavaScript library I use most after prototype is YUI so a big thanks to Chetan Patil for making it much easier to use in Rails.

Ruby Inside has a post on Ebb a small and fast web server for hosting Rails and Merb applications (and soon Django).

Ebb is a small, extremely high performance Web / HTTP server designed specifically for hosting applications built upon Web frameworks such as Rails and Merb (and, in future, apps on other non-Ruby frameworks.) The design is event based (similar to that used by Ruby daemons that use EventMachine) but Ebb itself is written in C and dispatches requests to Rack adapters. This is a real leapfrog over the popular Mongrel and Thin daemons which are primarily written in Ruby, and results in scary levels of performance.

Digg! submit Interesting Links: YUIRails, Ebb faster than Mongrel and thin to stumbleupon.com submit Interesting Links: YUIRails, Ebb faster than Mongrel and thin to del.icio.us submit Interesting Links: YUIRails, Ebb faster than Mongrel and thin to reddit.com Like this post? subscribe to the feed.

February 19, 2008

Mosso offers ‘new’ cloud hosting

Filed under: Media Temple, Mosso, Ruby on Rails, SliceHost Alastair @ 9:01 am

I got all excited this morning reading TechCrunch with their post about Rackspace Offerering Cloud Computing with Mosso. Once I clicked through to investigate further it seems the ‘new’ cloud hosting is just their old hosting plan with 30GB of storage slashed off (down to 50GB) so they can charge you 0.50/GB a month for any overage.

It’s the deal of the century!? a bonus $15/month to get back to their old limit of 80GB. Still no shell access, and still $25-85/month per Rails site.

I had high hopes for Mosso and Rails but after trying them I felt I was being nickeled and dimed, the lack of SSH didn’t bother me as they’re good about installing stuff for you, the kicker was $20/month if you wanted to add SSL. I also didn’t see marked speed improvements over shared (or grid) hosts like Media Temple (who host this blog, but not on the grid on a VPS).

All my Rails sites continue to be hosted on SliceHost, actually my secret recipe is a combo of Media Temple for email, FTP, and static stuff then either DNS trickery or .htaccess rewrites to apps on SliceHost. The best of both worlds.

Anyway I call shenanigans on Mosso.

Digg! submit Mosso offers ‘new’ cloud hosting to stumbleupon.com submit Mosso offers ‘new’ cloud hosting to del.icio.us submit Mosso offers ‘new’ cloud hosting to reddit.com Like this post? subscribe to the feed.

February 11, 2008

Interesting Links: Rails and BlazeDS, Yahoo! map components for AS3

Derek shows how to push AMF encoded messages from the server with Rails through BlazeDS.

Ted introduces the Yahoo! map components for AS3.

Combine the two and you could build a Flex/Rails app that tracks someone’s position in real time.

Digg! submit Interesting Links: Rails and BlazeDS, Yahoo! map components for AS3 to stumbleupon.com submit Interesting Links: Rails and BlazeDS, Yahoo! map components for AS3 to del.icio.us submit Interesting Links: Rails and BlazeDS, Yahoo! map components for AS3 to reddit.com Like this post? subscribe to the feed.

February 4, 2008

LiteSpeed adds a Rails staging environment

Filed under: LiteSpeed, Mongrel, Ruby on Rails Alastair @ 11:54 pm

Via Dark as Light, the easy way to deploy Rails adds a staging environment.

Here are the LiteSpeed update instructions:

1. Click ‘update’ in the LiteSpeed administration console
2. There is no step 2
3. see #2

Why more people don’t use LiteSpeed is beyond me. Hosting a Rails app takes only a couple more steps with no mongrel_clusters or load balancing shenanigans.

Digg! submit LiteSpeed adds a Rails staging environment to stumbleupon.com submit LiteSpeed adds a Rails staging environment to del.icio.us submit LiteSpeed adds a Rails staging environment to reddit.com Like this post? subscribe to the feed.

February 2, 2008

Flex, Flash, and Ruby hourly billing rates

HotGigs has a feature where they collect and aggregate the hourly bill rates of the consultants on their site. Here are the average hourly bill rates for Flex, Flash, and Ruby, surprisingly they have sub-categories for Flash all the way down to Flash Remoting but there’s just one category for Ruby with no Rails sub-category.

For the Rails rate I’d guess that Rails is to Ruby as Flex is to ActionScript. I threw PHP in there as well to mix it up.

ActionScript hourly bill rates
ActionScript bill rate (low): $50.00
ActionScript bill rate (high): $75.00
ActionScript pay rate (low): $32.50
ActionScript pay rate (high): $48.75
Average hourly bill rate: $62.50

Adobe Flex hourly bill rates
Adobe Flex bill rate (low): $75.00
Adobe Flex bill rate (high): $125.00
Adobe Flex pay rate (low): $48.75
Adobe Flex pay rate (high): $81.25
Average hourly bill rate: $100.00

Flash hourly bill rates
Flash bill rate (low): $50.00
Flash bill rate (high): $75.00
Flash pay rate (low): $32.50
Flash pay rate (high): $48.75
Average hourly bill rate: $62.50

Flash Design (no full data but this was the average)
Average hourly bill rate: $50

Flash Remoting hourly bill rates
Flash Remoting bill rate (low): $60.00
Flash Remoting bill rate (high): $80.00
Flash Remoting pay rate (low): $39.00
Flash Remoting pay rate (high): $52.00
Average hourly bill rate: $70.00

Ruby hourly bill rates
Ruby bill rate (low): $75.00
Ruby bill rate (high): $95.00
Ruby pay rate (low): $48.75
Ruby pay rate (high): $61.75
Average hourly bill rate: $85.00

PHP hourly bill rates
PHP bill rate (low): $70.00
PHP bill rate (high): $90.00
PHP pay rate (low): $45.50
PHP pay rate (high): $58.50
Average hourly bill rate: $80.00

Flex had an average hourly bill rate of $70 a couple of months ago so it’s on the move (what recession?), if you’re an ActionScript Developer still doing Flash work get on the Flex train and raise those rates!

The rates seem about right to me (actually remoting seems low), what do you think?

Digg! submit Flex, Flash, and Ruby hourly billing rates to stumbleupon.com submit Flex, Flash, and Ruby hourly billing rates to del.icio.us submit Flex, Flash, and Ruby hourly billing rates to reddit.com Like this post? subscribe to the feed.

January 22, 2008

Regular expressions make my head hurt

Filed under: RegEx, Ruby, Ruby on Rails Alastair @ 1:58 pm

A site to dull the pain Rubular. Via Ruby Inside.

Digg! submit Regular expressions make my head hurt to stumbleupon.com submit Regular expressions make my head hurt to del.icio.us submit Regular expressions make my head hurt to reddit.com Like this post? subscribe to the feed.

January 18, 2008

Rails 2.0 and link_to_remote :with

Filed under: Ajax, JavaScript, Ruby, Ruby on Rails Alastair @ 11:22 am

I’m moving an old app to Rails 2.0 and other than fixing some routes Ajax calls with link_to_remote and the :with parameter was the only thing that gave me some trouble. In the old version I was grabbing the current value of a drop down list with Prototype and passing it along using :with like this

<%= link_to_remote image_tag(cms/add_16.gif, :id => color_add),
      :url => { :controller => colors, :action => new_ajax,
      :id => @product.id },
      :with => { color_id: $F(’color_id’) }
%>

But in Rails 2.0 the parameter wouldn’t go along for the ride, it seemed that the new authenticity_token that gets sent with Ajax calls was messing things up. Here’s the fix

<%= link_to_remote image_tag(cms/add_16.gif, :id => color_add),
      :url => { :controller => colors, :action => new_ajax,
      :id => @product.id },
      :with => ‘color_id=’+$F(’color_id’)
%>

I freely admit JavaScript/Ajax is my weakest language so if I was doing it wrong the entire time let me know :)

Digg! submit Rails 2.0 and link_to_remote :with to stumbleupon.com submit Rails 2.0 and link_to_remote :with to del.icio.us submit Rails 2.0 and link_to_remote :with to reddit.com Like this post? subscribe to the feed.

Rails: cleaner partials in forms

Filed under: Ruby, Ruby on Rails Alastair @ 11:04 am

Damian pointed me to the patch ELC Technologies committed to Rails.

As he mentions previously you did something like

<% form_for(@client) do |f| %>
    <%= render :partial => form, :locals => {:f => f} %>
    <%= submit_tag Create %>
<% end %>

but now you can just do…

<% form_for(@client) do |f| %>
    <%= render :partial => f %>
    <%= submit_tag Create %>
<% end %>

Cleaner and more intuitive, nice!

Digg! submit Rails: cleaner partials in forms to stumbleupon.com submit Rails: cleaner partials in forms to del.icio.us submit Rails: cleaner partials in forms to reddit.com Like this post? subscribe to the feed.

December 28, 2007

Hacking attachment_fu to work with Flash/Flex uploads and crop square images

Filed under: Flash, Flex, Ruby, Ruby on Rails Alastair @ 1:19 pm

Rick Olson’s attachment_fu is my favorite file upload plug-in because let’s you use three different image manipulation tools [rmagick, mini-magick, image science] and storage options [file system, database, amazon s3]. However it doesn’t yet support two features I use on every CMS I build, Flash/Flex file upload (images will upload but won’t be resized) and square image cropping. Here’s how to tweak it to get both features working.

First up, support for Flash/Flex upload (I should really drop the ‘Flash/’ part as I only use Flex now) , first up Flex upload… Ilya Devers posted the solution on Google groups, but I get to claim 1% credit as my blog is mentioned in his post :P

The problem is really on the Flex side of things as all uploads come through as ‘application/octet-stream’ for the mime-type. attachement_fu can upload any kind of file so it checks the mime-type before running it’s resize code, since it’s looking for an image it skips over the Flex uploaded files. Ilya’s rather ingenious solution is to override attachment_fu and use the file system to check the file type. To overide attachment_fu add the ‘uploaded_data=’ and ‘get_content_type’ methods to your upload model.

class Upload < ActiveRecord::Base
  belongs_to :image

  has_attachment :content_type => :image,
                 :storage => :file_system,
                 :processor => MiniMagick,
                 :max_size => 2000.kilobytes,
                 :resize_to => 620×465>,
                 :thumbnails => { :thumb => [90, 90] }

  #override from has_attachment plugin
  def uploaded_data=(file_data)
    return nil if file_data.nil? || file_data.size == 0
    self.filename = file_data.original_filename if respond_to?(:filename)
    if file_data.is_a?(StringIO)
      file_data.rewind
      self.temp_data = file_data.read
    else
      self.temp_path = file_data.path
    end
    # in the original the next line occured earlier, and just used file_data.content_type
    self.content_type = get_content_type((file_data.content_type.chomp if file_data.content_type))
  end

  #uses the os’s “file” utility to determine the file type, yanked and modified slightly from file_column.
  def get_content_type(fallback=nil)
    begin
      content_type = `file -bi “#{File.join(temp_path)}`.chomp
      content_type = fallback unless $?.success?
      content_type.gsub!(/;.+$/,) if content_type
      content_type
    rescue
      fallback
    end
  end
end

Next is cropping square images with mini-magick. Currently if you request a square image attachment_fu will stretch rather than crop the image, this time I’ll ‘borrow’ the solution from Craig Ambrose. This time you have to dig deeper down into the depths of the rails plugins directory to edit ‘vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb’ and replace the resize_image method with the following.

# Performs the actual resizing operation for a thumbnail
def resize_image(img, size)
  size = size.first if size.is_a?(Array) && size.length == 1
  if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
    if size.is_a?(Fixnum)
      resize_and_crop(img, size)
    else
      size[0] == size[1] ? resize_and_crop(img, size[0]) : img.resize(size.join(x))
    end
  else
    img.resize(size.to_s)
  end
  self.temp_path = img
end

def resize_and_crop(image, square_size)
  if image[:width] < image[:height]
    shave_off = ((image[:height] - image[:width])/2).round
    image.shave(0x#{shave_off})
  elsif image[:width] > image[:height]
    shave_off = ((image[:width] - image[:height])/2).round
    image.shave(#{shave_off}x0)
  end
  image.resize(#{square_size}x#{square_size})
  return image
end

To crop an image you use ‘:thumb => [90, 90]‘ as in the Model code above. That’s it!

Digg! submit Hacking attachment_fu to work with Flash/Flex uploads and crop square images to stumbleupon.com submit Hacking attachment_fu to work with Flash/Flex uploads and crop square images to del.icio.us submit Hacking attachment_fu to work with Flash/Flex uploads and crop square images to reddit.com Like this post? subscribe to the feed.
Older Posts »