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
Search Repo:
readme is rdoc format
defunkt (author)
Tue Mar 11 23:20:48 -0700 2008
commit  6a3a952e6c6287fde06b449e8a0257db4f8ef15e
tree    9785a0c77e398f11ee0daeea256c3d7a02fa698f
parent  3869d8ea49c7edbcc37685f362838d66966727d9
0
...
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
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,121 +1 @@
0
-= Acts as Textiled
0
-
0
-This simple plugin allows you to forget about constantly rendering Textile in
0
-your application. Instead, you can rest easy knowing the Textile fields you
0
-want to display as HTML will always be displayed as HTML (unless you tell your
0
-code otherwise).
0
-
0
-No database modifications are needed.
0
-
0
-You need RedCloth, of course. And Rails.
0
-
0
-== Usage
0
-
0
- class Story < ActiveRecord::Base
0
- acts_as_textiled :body_text, :description
0
- end
0
-
0
- >> story = Story.find(3)
0
- => #<Story:0x245fed8 ... >
0
-
0
- >> story.description
0
- => "<p>This is <strong>cool</strong>.</p>"
0
-
0
- >> story.description(:source)
0
- => "This is *cool*."
0
-
0
- >> story.description(:plain)
0
- => "This is cool."
0
-
0
- >> story.description = "I _know_!"
0
- => "I _know_!"
0
-
0
- >> story.save
0
- => true
0
-
0
- >> story.description
0
- => "<p>I <em>know</em>!</p>"
0
-
0
- >> story.textiled = false
0
- => false
0
-
0
- >> story.description
0
- => "I _know_!"
0
-
0
- >> story.textiled = true
0
- => true
0
-
0
- >> story.description
0
- => "<p>I <em>know</em>!</p>"
0
-
0
-== Different Modes
0
-
0
-RedCloth supports different modes, such as :lite_mode. To use a mode on
0
-a specific attribute simply pass it in as an options hash after any
0
-attributes you don't want to mode-ify. Like so:
0
-
0
- class Story < ActiveRecord::Base
0
- acts_as_textiled :body_text, :description => :lite_mode
0
- end
0
-
0
-Or:
0
-
0
- class Story < ActiveRecord::Base
0
- acts_as_textiled :body_text => :lite_mode, :description => :lite_mode
0
- end
0
-
0
-You can also pass in multiple modes per attribute:
0
-
0
- class Story < ActiveRecord::Base
0
- acts_as_textiled :body_text, :description => [ :lite_mode, :no_span_caps ]
0
- end
0
-
0
-Get it? Now let's say you have an admin tool and you want the text to be displayed
0
-in the text boxes / fields as plaintext. Do you have to change all your views?
0
-
0
-Hell no.
0
-
0
-== form_for
0
-
0
-Are you using form_for? If you are, you don't have to change any code at all.
0
-
0
- <% form_for :story, @story do |f| %>
0
- Description: <br/> <%= f.text_field :description %>
0
- <% end %>
0
-
0
-You'll see the Textile plaintext in the text field. It Just Works.
0
-
0
-== form tags
0
-
0
-If you're being a bit unconvential, no worries. You can still get at your
0
-raw Textile like so:
0
-
0
- Description: <br/> <%= text_field_tag :description, @story.description(:source) %>
0
-
0
-And there's always object.textiled = false, as demo'd above.
0
-
0
-== Pre-fetching
0
-
0
-acts_as_textiled locally caches rendered HTML once the attribute in question has
0
-been requested. Obviously this doesn't bode well for marshalling or caching.
0
-
0
-If you need to force your object to build and cache HTML for all textiled attributes,
0
-call the +textilize+ method on your object.
0
-
0
-If you're real crazy you can even do something like this:
0
-
0
- class Story < ActiveRecord::Base
0
- acts_as_textiled :body_text, :description
0
-
0
- def after_find
0
- textilize
0
- end
0
- end
0
-
0
-All your Textile will now be ready to go in spiffy HTML format. But you probably
0
-won't need to do this.
0
-
0
-Enjoy.
0
-
0
-* By Chris Wanstrath [ chris[at]ozmm[dot]org ]
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
0
@@ -1 +1,121 @@
0
+= Acts as Textiled
0
+
0
+This simple plugin allows you to forget about constantly rendering Textile in
0
+your application. Instead, you can rest easy knowing the Textile fields you
0
+want to display as HTML will always be displayed as HTML (unless you tell your
0
+code otherwise).
0
+
0
+No database modifications are needed.
0
+
0
+You need RedCloth, of course. And Rails.
0
+
0
+== Usage
0
+
0
+ class Story < ActiveRecord::Base
0
+ acts_as_textiled :body_text, :description
0
+ end
0
+
0
+ >> story = Story.find(3)
0
+ => #<Story:0x245fed8 ... >
0
+
0
+ >> story.description
0
+ => "<p>This is <strong>cool</strong>.</p>"
0
+
0
+ >> story.description(:source)
0
+ => "This is *cool*."
0
+
0
+ >> story.description(:plain)
0
+ => "This is cool."
0
+
0
+ >> story.description = "I _know_!"
0
+ => "I _know_!"
0
+
0
+ >> story.save
0
+ => true
0
+
0
+ >> story.description
0
+ => "<p>I <em>know</em>!</p>"
0
+
0
+ >> story.textiled = false
0
+ => false
0
+
0
+ >> story.description
0
+ => "I _know_!"
0
+
0
+ >> story.textiled = true
0
+ => true
0
+
0
+ >> story.description
0
+ => "<p>I <em>know</em>!</p>"
0
+
0
+== Different Modes
0
+
0
+RedCloth supports different modes, such as :lite_mode. To use a mode on
0
+a specific attribute simply pass it in as an options hash after any
0
+attributes you don't want to mode-ify. Like so:
0
+
0
+ class Story < ActiveRecord::Base
0
+ acts_as_textiled :body_text, :description => :lite_mode
0
+ end
0
+
0
+Or:
0
+
0
+ class Story < ActiveRecord::Base
0
+ acts_as_textiled :body_text => :lite_mode, :description => :lite_mode
0
+ end
0
+
0
+You can also pass in multiple modes per attribute:
0
+
0
+ class Story < ActiveRecord::Base
0
+ acts_as_textiled :body_text, :description => [ :lite_mode, :no_span_caps ]
0
+ end
0
+
0
+Get it? Now let's say you have an admin tool and you want the text to be displayed
0
+in the text boxes / fields as plaintext. Do you have to change all your views?
0
+
0
+Hell no.
0
+
0
+== form_for
0
+
0
+Are you using form_for? If you are, you don't have to change any code at all.
0
+
0
+ <% form_for :story, @story do |f| %>
0
+ Description: <br/> <%= f.text_field :description %>
0
+ <% end %>
0
+
0
+You'll see the Textile plaintext in the text field. It Just Works.
0
+
0
+== form tags
0
+
0
+If you're being a bit unconvential, no worries. You can still get at your
0
+raw Textile like so:
0
+
0
+ Description: <br/> <%= text_field_tag :description, @story.description(:source) %>
0
+
0
+And there's always object.textiled = false, as demo'd above.
0
+
0
+== Pre-fetching
0
+
0
+acts_as_textiled locally caches rendered HTML once the attribute in question has
0
+been requested. Obviously this doesn't bode well for marshalling or caching.
0
+
0
+If you need to force your object to build and cache HTML for all textiled attributes,
0
+call the +textilize+ method on your object.
0
+
0
+If you're real crazy you can even do something like this:
0
+
0
+ class Story < ActiveRecord::Base
0
+ acts_as_textiled :body_text, :description
0
+
0
+ def after_find
0
+ textilize
0
+ end
0
+ end
0
+
0
+All your Textile will now be ready to go in spiffy HTML format. But you probably
0
+won't need to do this.
0
+
0
+Enjoy.
0
+
0
+* By Chris Wanstrath [ chris[at]ozmm[dot]org ]

Comments

    No one has commented yet.