Rails has_file Plugin

Posted by John Wulff Wed, 09 May 2007 00:48:00 GMT

has_file is an easy-to-use Rails plugin that provides file manipulation methods to an ActiveRecord model.

SVN , Google Code Project

Basic Example
$ cd MyRailsApp
$ script/plugin install http://rails-has-file.googlecode.com/svn/trunk
class Node < ActiveRecord::Base
  has_file :donkey
end
$ script/console
Loading development environment.
>> n = Node.new
  => #nil}, @new_record=true>
>> n.donkey = "secret donkey message" 
  => "secret donkey message" 
>> n.donkey_file_path
  => "/tmp/node-20123394-donkey" 
>> n.donkey
  => "secret donkey message" 
>> n.save!
  => true
>> n.donkey_file_path
  => "/Users/jwulff/Development/rails/resources/node-4-donkey" 
>> n.donkey
  => "secret donkey message" 
>> n.destroy
  => #"2007-05-08 15:07:47"}>
>> File.exists? "/Users/jwulff/Development/rails/resources/node-4-donkey" 
  => false

Some Notes:

  • Temporary files are employed for unsaved records. Meaning you can use the set/get methods before saving the record.
  • By default files are saved to RAILS_ROOT/resources. You can change this by passing a path in along with the declaration like so:
    has_file :donkey, '/var/stuff'
  • Upon destruction of the record
    record.destroy
    associated files will be deleted.

Rails CRUD Actions Plugin 3

Posted by John Wulff Sun, 08 Apr 2007 09:06:00 GMT

For a while now I’ve been using a plugin I wrote that provides simple and versatile CRUD actions to Rails controllers.

About six months ago I was getting really fed up with writing the same actions over and over again: new, create, show, edit, update, list, destroy. So, I decided to abstract them out into a plugin.

All I have to do to get almost all the CRUD goodness I need is:
class NodeController < ApplicationController
  include CrudActions
end

Now the NodeController has new, create, show, edit, update, list, and destroy actions for the Node ActiveRecord Model. Likewise if CrudActions were included in ArticleController , ArticleController would have actions for the Article Model.

All of the actions work as you’d expect and assign variables for your views appropriately (@node, @nodes, @paginator, etc.).

For customization you can easily wrap the actions provided like so:
class NodeController < ApplicationController
  include CrudActions

  alias_method :original_list, :list
  def list
    Node.with_scope(:find => { :conditions => 'id != 1' } ) do
      original_list
    end
  end
end

The source is hosted at Google Code: http://code.google.com/p/rails-crud-actions.

The meat of the plugin is in crud_actions.rb.

There is still plenty of room for this concept to grow, implementing respond_to for example. However, I’m not sure that I’m going down a wise path here. Everything about this approach feels shaky… but it is so damned convenient.

Please have a look. I’d really like some feedback.

XML Validation Plugin

Posted by John Wulff Wed, 04 Oct 2006 03:19:00 GMT

I’ve written a new Rails plugin to validate xml.

class Article < ActiveRecord::Base
  validates_xml :body
end
Then
Article.new(:body => "<span>This will fail").save!
will throw
ActiveRecord::RecordInvalid: Validation failed: Body is not valid xml

Get it here: https://secure.near-time.com/svn/plugins/trunk/validates_xml

Rails Google Maps Plugin 2

Posted by John Wulff Sun, 06 Aug 2006 03:03:00 GMT

I’ve written a Google Maps plugin for Rails. Right now it is fairly simple but it will grow as I need more functionality.

I was going to use the Cartographer plugin but I wanted something a little more tailored to my needs so I decided to create my own. However, looking at the Cartographer code was helpful in overcoming some of the hurdles I encountered (stupid IE).

To install:
$ cd my_rails_app
$ script/plugin install https://secure.near-time.com/svn/plugins/trunk/unbacked_dom_id/
$ script/plugin install https://secure.near-time.com/svn/plugins/trunk/google_maps/
You’ll need to define your API key in your environment.rb
# Signup for a key at http://www.google.com/apis/maps/signup.html
GOOGLE_APPLICATION_ID = "insert key here"
Then, all you have to do is put something like this in a view:
<%
map = GoogleMap.new
map.markers << GoogleMapMarker.new(:map => map, 
                                   :lat => 47.6597, 
                                   :lng => -122.318,
                                   :html => 'My House')
-%>
<%= map.to_html %>
<%= map.div %>

For more examples and info check out the ever-evolving README.

Doesn’t get much easier…

UPDATE: My UnbackedDomId plugin is used in the GoogleMaps plugin. It is now required. The instructions above have been updated. If you’re already up and running, you’ll see breakage until you do this:

$ cd my_rails_app
$ script/plugin install https://secure.near-time.com/svn/plugins/trunk/unbacked_dom_id/

TinyMCE (WYSIWYG) for Ruby on Rails 15

Posted by John Wulff Thu, 01 Jun 2006 00:45:00 GMT

Blake Watters has just created a TinyMCE plugin for Ruby on Rails.

How-To integrate TinyMCE into your Rails app (assumes you’re using svn).

  1. Add the plugin to your svn:externals and get the latest code.
    $ cd MyRailsApp
    $ script/plugin install -x https://secure.near-time.com/svn/plugins/trunk/tiny_mce
  2. Add the following to your application.rhtml
    <% # Include TinyMCE before other JS to avoid problems -%>
    <%= javascript_include_tiny_mce_if_used %>
    <%= tiny_mce if using_tiny_mce? %>
  3. Install the JavaScript
    $ rake tiny_mce:scripts:install
  4. Activate TinyMCE for a controller. (Uses TinyMCE for any TextArea).
    class MyController < ApplicationController
      uses_tiny_mce
      ...
That is it! Not too much work for full featured WYSIWYG eh? Now, to really make it sing try this.
uses_tiny_mce(:options => {:theme => 'advanced',
  :browsers => %w{msie gecko},
  :theme_advanced_toolbar_location => "top",
  :theme_advanced_toolbar_align => "left",
  :theme_advanced_resizing => true,
  :theme_advanced_resize_horizontal => false,
  :paste_auto_cleanup_on_paste => true,
  :theme_advanced_buttons1 => %w{formatselect fontselect fontsizeselect bold italic underline strikethrough separator justifyleft justifycenter justifyright indent outdent separator bullist numlist forecolor backcolor separator link unlink image undo redo},
  :theme_advanced_buttons2 => [],
  :theme_advanced_buttons3 => [],
  :plugins => %w{contextmenu paste}},
  :only => [:new, :edit, :show, :index])

Awesome work Blake.

UPDATE: This info is now in the Ruby on Rails Wiki.