New movie rental service

Archive for the 'Rails' Category

So, while I was doing some work with my plugin, I realized that there were a couple things that could be improved.

  1. You had to specify parameters as an array, even if there was only one parameter.
  2. You had to specify the body as a string - making it cumbersome to create multi-statement functions.

Well, both of those issues are in the past. You can still use an array for parameters and string for body, but you can also use an array for body and a string for a (single) parameter - or any combination of such.

So, grab version 0.2 and have at it.

I’m working on a Rails app using RJS and Prototype Window Class. I needed to be able to return an anonymous JavaScript function in my RJS template but when passing it in a string to page.call it kept getting quoted and didn’t work right (as you would expect).

I solved it by creating a JavaScriptFunction class and helpers so that it’s easily usable in an RJS template. Browse the plugin or install it:

# With subversion
./script/plugin install -x http://svn.vanderbrew.com/svn/repos/plugins/javascript_function/

# Without subversion
./script/plugin install http://svn.vanderbrew.com/svn/repos/plugins/javascript_function/

To use it, just do:

page.call 'Dialog.confirm', "New Thing - do you want to add it?",
            { :windowParameters => { :width => 300 },
              :sokLabel => 'Yes',
              :cancelLabel => 'No',
              :buttonClass => 'myButtonClass',
              :id => 'myDialogId',
              :cancel => anonymous_javascript_function(:parameters => ["win"],
                         :body => "$('#{@element_id}').innerHTML = '#{escape_javascript @thing.name}'"),
              :sok => anonymous_javascript_function(:parameters => ["win"],
                         :body => "return true;")
            }

It’s my first plugin so I’m open to any feedback you have about it. Please use the Contact form to reach me.

Note - the keys :sokLabel and :sok shouldn’t have an ’s’ in the front for real implementation with the JS library - I did that to prevent an emoticon from appearing thanks to WordPress.

I purchased a copy of Chad Fowler’s excellent Rails Recipes recently. He’s got a few recipes dealing with edit in place and one about building local autocomplete. I pulled these recipes together into a single solution that gives me an edit in place with local autocomplete functionality.

Rails Helper
I created a couple Rails helper functions to encapsulate the functionality so I can call it with one function in my view. View the source, with syntax highlighting.

JavaScript
I had to extend the existing Scriptaculous code to support my specific functionality. View the source, with syntax highlighting.

Implementation
Now, you just include the JS in your page and use the new helper to build the UI component.

<%= in_place_editor_field_with_local_autocomplete :recipe,
                                                  :cookbook_title,
                                                  {},
                                                  {},
                                                  {
                                                    :catalog => ‘cookbooks’,
                                                    :fullSearch => true,
                                                    :frequency => 0,
                                                    :minChars => 2
                                                  }
%>

Update
Ola Bini has developed an InPlaceEditor with Autocomplete plugin for Rails. Definitely worth a look.