Plugins - Auto Convert Fields

StarAdd to favorites

Overview

This plugin lets you define model fields which should be automatically converted with helpers from the TextHelper module, or a custom block. It works by redefining the field= method, so it applies immediately. If you already have a custom setter, it will be aliased and called after the conversion. You can also use the old setter explicitly by calling field_without_auto_convert=.

Usage

Use the class method auto_convert to specify fields, and pass symbols for the helpers in the :with option:

  class MyModel < ActiveRecord::Base
    auto_convert :name, :text, :with => :strip_tags
  end

You can specify multiple helpers by passing an array. The helpers will be run in the order they’re specified in:

  class MyModel < ActiveRecord::Base
    auto_convert :name, :text, :with => [ :strip_tags, :simple_format ]
  end

For more complex cases, pass a block which will be given the value. The helpers are also available in the context of the block:

  class MyModel < ActiveRecord::Base
    auto_convert :name, :text do |value|
      strip_tags(value).strip.gsub(/[^\w ]/, '')
    end
  end

Markus Koller

http://snafu.diarrhea.ch/software/rails#auto_convert_fields

http://dev.diarrhea.ch/svn/rails_plugins/trunk/auto_convert_fields

Rails' (MIT)

  • Currently 4.7/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Model

Tags

Comments

Add a comment

Search Plugins

Query syntax

Plugins by Category

Sponsors

Rails Kits: Get Code. Get Moving.

Have a comment?