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.

April 7, 2008

Run your web applications on Google’s infrastructure

Filed under: Django, Ruby Alastair @ 11:01 pm

Google releases App Engine. It’s Python only for now, but there are rumors more languages (Ruby!) will soon be available.

Here’s a video intro. And Django runs out of the box (in the box?) - well some parts are missing like no relational db - still you can’t beat the one line deployment!

Docs! Running Django on Google App Engine

Digg! submit Run your web applications on Google’s infrastructure to stumbleupon.com submit Run your web applications on Google’s infrastructure to del.icio.us submit Run your web applications on Google’s infrastructure to reddit.com Like this post? subscribe to the feed.

March 28, 2008

PureMVC for Ruby

Filed under: ActionScript, Flash, Flex, Ruby Alastair @ 6:23 am

The PureMVC framework is considered one of the best for Flash/Flex development (definitely the best documented), I hadn’t checked out the site in a while (which has undergone an overhaul and is much improved!) and since my last visit PureMVC is now available for not only AS2 and AS3 but C#, ColdFusion, Java, Perl, PHP, Python, and Ruby as well.

I should say plans for Ruby as they are looking for a project owner to work on the port. I’d volunteer myself (famous last words) but while I love Ruby I don’t know the ins and outs of the language as well as I do ActionScript.

In any case having one framework “to rule them all” would be a great boost to productivity as you wouldn’t have to mentally switch gears between the front (Flash/Flex) and back (PHP/Ruby etc.) ends.

Digg! submit PureMVC for Ruby to stumbleupon.com submit PureMVC for Ruby to del.icio.us submit PureMVC for Ruby to reddit.com Like this post? subscribe to the feed.

March 24, 2008

Build Flash with Ruby

Filed under: Flash, Ruby Alastair @ 7:05 am

Via Flex on Rails…

Using HotRuby it is possible to use straight Ruby to build a Flash app.

Digg! submit Build Flash with Ruby to stumbleupon.com submit Build Flash with Ruby to del.icio.us submit Build Flash with Ruby to reddit.com Like this post? subscribe to the feed.

February 26, 2008

Run Ruby code in the Flash Player?

Filed under: AIR, ActionScript, Flash, Flex, RIA, Ruby, Silverlight Alastair @ 6:30 pm

Or even better write Flash/Flex Rich Internet Applications with Ruby? Ted Patrick says it may soon be possible.

When Microsoft released Silverlight the one feature that got a lot of people excited was that you could use the language you were most familiar with to build a RIA. Apparently Adobe has an internal project which allows any C or C++ code to run in the Flash Player or on AIR. This means that any language built on C/C++ will also run which means that Java, Python, and my beloved Ruby could also run. Schwing! :)

Ted is a Python guy so he talks about IronPython and JPython but of course Ruby has JRuby and IronRuby so I’m sure the behavior would be similar.

Like many organizations Adobe has lots of legacy C/C++ code ranging from PhotoShop filters, to PDF renderers, to readers and writers of every file format in existence, font libraries, to very complex vector renderers, and text layout code. Beyond Adobe there are many open source libraries that could be leveraged as components as well. The big thing for me is that these are not ports of these libraries, they run identical to the original source code down. For example the behavior of Python in Flash Player is identical to C-Python vs the ported behavior under the IronPython and Jython projects. The goal here is to bring lots of these legacy assets, code libraries, and languages into Flash Player and Adobe AIR perfectly so that any developer can leverage them cross-platform to build software. It would not shock me to see some of these components added into the Flash Player component cache so that they essentially are built into the player on first use.

InfoWorld has more.

Digg! submit Run Ruby code in the Flash Player? to stumbleupon.com submit Run Ruby code in the Flash Player? to del.icio.us submit Run Ruby code in the Flash Player? 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 »