Skip to content

Commit eac71a2

Browse files
committed
add in acts_as_textiled api changes
1 parent d5fd7ee commit eac71a2

File tree

6 files changed

+44
-18
lines changed

6 files changed

+44
-18
lines changed

CHANGES

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
= 0.3
2+
- Fixed Model.textiled = false bug
3+
- Refactored tests
4+
- Changed api from @story.description_plain to @story.description(:plain) - kept old way, though
5+
16
= 0.2
27

38
* Fix issue with object.attribute_plain overwriting the original attribute [Thanks, James]

README

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ You need RedCloth, of course. And Rails.
2121
>> story.description
2222
=> "<p>This is <strong>cool</strong>.</p>"
2323

24-
>> story.description_source
24+
>> story.description(:source)
2525
=> "This is *cool*."
2626

27-
>> story.description_plain
27+
>> story.description(:plain)
2828
=> "This is cool."
2929

3030
>> story.description = "I _know_!"
@@ -90,7 +90,7 @@ You'll see the Textile plaintext in the text field. It Just Works.
9090
If you're being a bit unconvential, no worries. You can still get at your
9191
raw Textile like so:
9292

93-
Description: <br/> <%= text_field_tag :description, @story.description_source %>
93+
Description: <br/> <%= text_field_tag :description, @story.description(:source) %>
9494

9595
And there's always object.textiled = false, as demo'd above.
9696

about.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ summary:
33
homepage: http://errtheblog.com/post/14
44
plugin: http://require.errtheblog.com/svn/acts_as_textiled
55
license: MIT
6-
version: 0.1
6+
version: 0.3
77
rails_version: 1.1+

init.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
require 'RedCloth'
12
require 'acts_as_textiled'
23
ActiveRecord::Base.send(:include, Err::Acts::Textiled)

lib/acts_as_textiled.rb

+24-10
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,35 @@ def self.included(klass)
66
end
77

88
module ClassMethods
9-
def acts_as_textiled(*attrs)
9+
def acts_as_textiled(*attributes)
1010
@textiled_attributes = []
1111

12-
@textiled_unicode = "".respond_to? :chars
12+
@textiled_unicode = String.new.respond_to? :chars
1313

14-
ruled = Hash === attrs.last ? attrs.pop : {}
15-
attrs += ruled.keys
14+
ruled = attributes.last.is_a?(Hash) ? attributes.pop : {}
15+
attributes += ruled.keys
1616

17-
attrs.each do |attr|
18-
define_method(attr) do
19-
textiled[attr.to_s] ||= RedCloth.new(self[attr], Array(ruled[attr])).to_html if self[attr]
17+
type_options = %w( plain source )
18+
19+
attributes.each do |attribute|
20+
define_method(attribute) do |*type|
21+
type = type.first
22+
23+
if type.nil? && self[attribute]
24+
textiled[attribute.to_s] ||= RedCloth.new(self[attribute], Array(ruled[attribute])).to_html
25+
elsif type.nil? && self[attribute].nil?
26+
nil
27+
elsif type_options.include?(type.to_s)
28+
send("#{attribute}_#{type}")
29+
else
30+
raise "I don't understand the `#{type}' option. Try #{type_options.join(' or ')}."
31+
end
2032
end
21-
define_method("#{attr}_plain", proc { strip_redcloth_html(__send__(attr)) if __send__(attr) } )
22-
define_method("#{attr}_source", proc { __send__("#{attr}_before_type_cast") } )
23-
@textiled_attributes << attr
33+
34+
define_method("#{attribute}_plain", proc { strip_redcloth_html(__send__(attribute)) if __send__(attribute) } )
35+
define_method("#{attribute}_source", proc { __send__("#{attribute}_before_type_cast") } )
36+
37+
@textiled_attributes << attribute
2438
end
2539

2640
include Err::Acts::Textiled::InstanceMethods

test/textiled_test.rb

+10-4
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,27 @@
1717
desc_plain = '_why announces Sandbox'
1818

1919
story.description.should.equal desc_html
20-
#story.description(:source).should.equal desc_textile
21-
#story.description(:plain).should.equal desc_plain
20+
story.description(:source).should.equal desc_textile
21+
story.description(:plain).should.equal desc_plain
2222

2323
story.description_source.should.equal desc_textile
2424
story.description_plain.should.equal desc_plain
2525

2626
# make sure we don't overwrite anything - thanks James
2727
story.description.should.equal desc_html
28-
#story.description(:source).should.equal desc_textile
29-
#story.description(:plain).should.equal desc_plain
28+
story.description(:source).should.equal desc_textile
29+
story.description(:plain).should.equal desc_plain
3030

3131
story.description_source.should.equal desc_textile
3232
story.description_plain.should.equal desc_plain
3333
end
3434

35+
specify "should raise when given a non-sensical option" do
36+
story = Story.find(1)
37+
38+
proc { story.description(:cassadaga) }.should.raise
39+
end
40+
3541
specify "should pick up changes to attributes" do
3642
story = Story.find(2)
3743

0 commit comments

Comments
 (0)