github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

defunkt / acts_as_textiled

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 119
    • 17
  • Source
  • Commits
  • Network (17)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Click here to lend your support to: acts_as_textiled and make a donation at www.pledgie.com ! Edit Pledgie Setup

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Makes your models act as textiled. — Read more

  cancel

http://errtheblog.com/posts/12-actsastextiled

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Only require RedCloth if not already loaded 
Kevin Ansfield (author)
Thu Jul 03 07:23:10 -0700 2008
defunkt (committer)
Tue Oct 13 23:59:13 -0700 2009
commit  fce0e1f58f01403568bee122a256cfab1adc6b5b
tree    8ae6c995d3af936ffa9a6b15550e3fdaaa8e4faa
parent  6a3a952e6c6287fde06b449e8a0257db4f8ef15e
acts_as_textiled /
name age
history
message
file CHANGES Sun Jun 03 04:35:21 -0700 2007 add in acts_as_textiled api changes [defunkt]
file LICENSE Wed Aug 09 22:22:32 -0700 2006 initial import [defunkt]
file README.rdoc Tue Mar 11 23:20:48 -0700 2008 readme is rdoc format [defunkt]
file Rakefile Tue Aug 21 15:38:04 -0700 2007 yet more rdoc changes [defunkt]
file about.yml Sun Jun 03 04:35:21 -0700 2007 add in acts_as_textiled api changes [defunkt]
file init.rb Tue Oct 13 23:59:13 -0700 2009 Only require RedCloth if not already loaded Si... [Kevin Ansfield]
directory lib/ Sun Jun 03 04:35:21 -0700 2007 add in acts_as_textiled api changes [defunkt]
directory test/ Sun Jun 03 04:35:21 -0700 2007 add in acts_as_textiled api changes [defunkt]
README.rdoc

Acts as Textiled

This simple plugin allows you to forget about constantly rendering Textile in your application. Instead, you can rest easy knowing the Textile fields you want to display as HTML will always be displayed as HTML (unless you tell your code otherwise).

No database modifications are needed.

You need RedCloth, of course. And Rails.

Usage

  class Story < ActiveRecord::Base
    acts_as_textiled :body_text, :description
  end

  >> story = Story.find(3)
  => #<Story:0x245fed8 ... >

  >> story.description
  => "<p>This is <strong>cool</strong>.</p>"

  >> story.description(:source)
  => "This is *cool*."

  >> story.description(:plain)
  => "This is cool."

  >> story.description = "I _know_!"
  => "I _know_!"

  >> story.save
  => true

  >> story.description
  => "<p>I <em>know</em>!</p>"

  >> story.textiled = false
  => false

  >> story.description
  => "I _know_!"

  >> story.textiled = true
  => true

  >> story.description
  => "<p>I <em>know</em>!</p>"

Different Modes

RedCloth supports different modes, such as :lite_mode. To use a mode on a specific attribute simply pass it in as an options hash after any attributes you don’t want to mode-ify. Like so:

  class Story < ActiveRecord::Base
    acts_as_textiled :body_text, :description => :lite_mode
  end

Or:

  class Story < ActiveRecord::Base
    acts_as_textiled :body_text => :lite_mode, :description => :lite_mode
  end

You can also pass in multiple modes per attribute:

  class Story < ActiveRecord::Base
    acts_as_textiled :body_text, :description => [ :lite_mode, :no_span_caps ]
  end

Get it? Now let’s say you have an admin tool and you want the text to be displayed in the text boxes / fields as plaintext. Do you have to change all your views?

Hell no.

form_for

Are you using form_for? If you are, you don’t have to change any code at all.

  <% form_for :story, @story do |f| %>
    Description: <br/> <%= f.text_field :description %>
  <% end %>

You’ll see the Textile plaintext in the text field. It Just Works.

form tags

If you’re being a bit unconvential, no worries. You can still get at your raw Textile like so:

  Description: <br/> <%= text_field_tag :description, @story.description(:source) %>

And there’s always object.textiled = false, as demo’d above.

Pre-fetching

acts_as_textiled locally caches rendered HTML once the attribute in question has been requested. Obviously this doesn’t bode well for marshalling or caching.

If you need to force your object to build and cache HTML for all textiled attributes, call the textilize method on your object.

If you’re real crazy you can even do something like this:

  class Story < ActiveRecord::Base
    acts_as_textiled :body_text, :description

    def after_find
      textilize
    end
  end

All your Textile will now be ready to go in spiffy HTML format. But you probably won’t need to do this.

Enjoy.

  • By Chris Wanstrath [ chris[at]ozmm[dot]org ]
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server