public
Description: Makes your models act as textiled.
Homepage: http://errtheblog.com/posts/12-actsastextiled
Clone URL: git://github.com/defunkt/acts_as_textiled.git
defunkt (author)
Sun Jun 03 04:35:21 -0700 2007
commit  eac71a20b391ee28623664658aaf17b26c4ce0cf
tree    2f2d61f415e787bdf1b81deafae1f5539462c2cf
parent  d5fd7eebebede89cb9ffc152d6e08d2364df9bc0
acts_as_textiled / README
100644 122 lines (79 sloc) 2.976 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
= 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.
 
>> Chris Wanstrath
=> chris[at]ozmm[dot]org