Prepend to javascript_include_tag :defaults

JavaScript, Ruby on Rails No Comments »

Yes, there is register_javascript_include_default but it will only append to your sources.

To prepend (I’ll use this to have the excellent JS.Class libraries loaded before anything else), proceed like so:

While in Rails 3, you can set

config.action_view.javascript_expansions[:defaults].unshift(’my_prepend_1′, my_prepend_2′)

in your application.rb file,

in Rails 2.3.x you create an initializer (e.g. config/initializers/my_js_defaults.rb) and reset the JAVASCRIPT_DEFAULT_SOURCES constant as so:

module ActionView::Helpers::AssetTagHelper
  JAVASCRIPT_DEFAULT_SOURCES = (remove_const :JAVASCRIPT_DEFAULT_SOURCES).unshift(’my_prepend_1′, ‘my_prepend_2′)
   reset_javascript_include_default
end

This post gave me the idea. It concerns Rails 3 (before the above config.action_view.javascript_expansions which was introduced in Rails 3 RC)  but the code also works with Rails 2.3.x.

 

 

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Learning Javascript with Crockford and Spidermonkey

JavaScript No Comments »

As JavaScript is the language I’m planning to learn more in-depth this year, primarily to approach the latest map-reduce API’s of the latests NoSQL databases (CouchDB, riak etc.), I’ve recently made good progress thanks to two invaluable resources:

Crockford Videos

First, Douglas Crockford’s lecture videos at the YUI theater. He has created three separate series, The JavaScript Programming language, Advanced JavaScript. and more recently, Crockford on JavaScript, all available on the same page in the YUI theater. The third episode (Act III) of Crockford on Javascript gives you a tour de force of functional programming and is probably the most valuable of all the videos for advanced programmers.

He also states in his first lecture that there are no good books on JavaScript, except perhaps a single one, JavaScript: The Definitive Guide By David Flannagan. You may also want to check out Crockford’s own books which I haven’t had the chance to read yet.

Finally, always use Crockford’s jslint to check your javascript code quality. It’s a great teaching tool in and of itself and will help you find all those missing semi-colons and curly braces.

Spidermonkey Command Line

Interactively checking code on the command line is standard practice in the ruby and python universes. Now you can easily have the same code checker at your fingertips with JavaScript. In Ubuntu (8.04 and higher) simply

sudo apt-get install spidermonkey-bin

, and then type

> js

to get the beautiful prompt:

js>

Here are some tips for working with the spidermonkey command line. And, to use jslint from the command line, check out this post.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Netbeans Regular Expressions Find and Replace

Netbeans, Programming, Quanta Plus No Comments »

Powerful find and replace with regular expressions should be the hallmark of every IDE worth its mettle. MS Visual Studio does it so-so, but lo and behold, Netbeans (at least in version 6.1) is even worse.

So I wanted to put foo at the beginning of every line in a text, which calls for a regex like:

^.*

and replace like:

foo $0

However, Netbeans only ever replaces the first match in a text, over and over again. It also hangs up easily in the process.

As with many a Netbeans quirk, it’s Quanta Plus to the rescue again:

Quanta_plus_utf-10

Works like a charm.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Populate an ActiveRecord Model with Images Using attachment_fu

Programming, Ruby on Rails No Comments »

Is there anything more painful than manually populating your model with sample data? The thought alone makes most developers’ skin scrawl because we write code to do the work for us and abhor manually entering fugacious data.

As always, first things first, watch the relevant Railscast, which will introduce you to populator and faker, both great tools you will soon find indispensable. And here’s a little rake task to populate your model with images, useful to see how it looks with product thumbnails or the like. This is just sample code which you should edit to suit your needs. And attachment_fu has to be configured and working. The file, which could be named attachment_populate.rb belongs in the lib/tasks/ directory:

namespace :attachment do
  desc "Add an image to all MyModel items for visual checking"
  task :add_image_to_all_mymodels  => :environment do
    require 'action_controller'
    require 'action_controller/test_process.rb'
    path = "#{RAILS_ROOT}/public/images/samples/sample.jpg"
    mimetype = "image/jpeg"
    MyModel.find(:all).each do |mymodel|
      @attachment = Attachment.new(:uploaded_data => ActionController::TestUploadedFile.new(path, mimetype))
      @attachment.mymodel_id = mymodel.id
      @attachment.save
    end
  end
end

Call with

rake attachment:add_image_to_all_mymodels

The pertinent documentation is in the #{RAILS_ROOT}/vendor/plugins/attachment_fu/README under “attachment_fu scripting”:

# required to use ActionController::TestUploadedFile
require ‘action_controller’
require ‘action_controller/test_process.rb’

path = “./public/images/x.jpg”

# mimetype is a string like “image/jpeg”. One way to get the mimetype for a given file on a UNIX system
# mimetype = `file -ib #{path}`.gsub(/\n/,”")

mimetype = “image/jpeg”

# This will “upload” the file at path and create the new model.
@attachable = AttachmentMetadataModel.new(:uploaded_data => ActionController::TestUploadedFile.new(path, mimetype))
@attachable.save

 And where do you get more info about creating rake tasks? Railscasts of course, who needs books?

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Change the Netbeans Windows Layout

Netbeans, Programming No Comments »

In Netbeans there currently (Version 6.1) is no built-in option to save a windows layout, you can only do a “Reset Windows”  (in the Window menu) to get back to the factory layout.
I’ve been looking for a solution to get a *cough* Visual Studio *cough* like layout (all navigation on the right of the code window) and found a great plugin called Perspective (click this link twice as for some reason on the first click it will give you the plugin home page) that lets you save a layout and switch easily between layouts (er, perpectives). You can’t download it using the Netbeans plugin installer as it’s not signed, so you’ll have to download and install from disk: 

Netbeans_window_layout_perspective_plugin

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in