Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add in acts_as_textiled api changes

  • Loading branch information...
commit eac71a20b391ee28623664658aaf17b26c4ce0cf 1 parent d5fd7ee
@defunkt authored
View
5 CHANGES
@@ -1,3 +1,8 @@
+= 0.3
+ - Fixed Model.textiled = false bug
+ - Refactored tests
+ - Changed api from @story.description_plain to @story.description(:plain) - kept old way, though
+
= 0.2
* Fix issue with object.attribute_plain overwriting the original attribute [Thanks, James]
View
6 README
@@ -21,10 +21,10 @@ You need RedCloth, of course. And Rails.
>> story.description
=> "<p>This is <strong>cool</strong>.</p>"
- >> story.description_source
+ >> story.description(:source)
=> "This is *cool*."
- >> story.description_plain
+ >> story.description(:plain)
=> "This is cool."
>> story.description = "I _know_!"
@@ -90,7 +90,7 @@ You'll see the Textile plaintext in the text field. It Just Works.
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 %>
+ Description: <br/> <%= text_field_tag :description, @story.description(:source) %>
And there's always object.textiled = false, as demo'd above.
View
2  about.yml
@@ -3,5 +3,5 @@ summary:
homepage: http://errtheblog.com/post/14
plugin: http://require.errtheblog.com/svn/acts_as_textiled
license: MIT
-version: 0.1
+version: 0.3
rails_version: 1.1+
View
1  init.rb
@@ -1,2 +1,3 @@
+require 'RedCloth'
require 'acts_as_textiled'
ActiveRecord::Base.send(:include, Err::Acts::Textiled)
View
34 lib/acts_as_textiled.rb
@@ -6,21 +6,35 @@ def self.included(klass)
end
module ClassMethods
- def acts_as_textiled(*attrs)
+ def acts_as_textiled(*attributes)
@textiled_attributes = []
- @textiled_unicode = "".respond_to? :chars
+ @textiled_unicode = String.new.respond_to? :chars
- ruled = Hash === attrs.last ? attrs.pop : {}
- attrs += ruled.keys
+ ruled = attributes.last.is_a?(Hash) ? attributes.pop : {}
+ attributes += ruled.keys
- attrs.each do |attr|
- define_method(attr) do
- textiled[attr.to_s] ||= RedCloth.new(self[attr], Array(ruled[attr])).to_html if self[attr]
+ type_options = %w( plain source )
+
+ attributes.each do |attribute|
+ define_method(attribute) do |*type|
+ type = type.first
+
+ if type.nil? && self[attribute]
+ textiled[attribute.to_s] ||= RedCloth.new(self[attribute], Array(ruled[attribute])).to_html
+ elsif type.nil? && self[attribute].nil?
+ nil
+ elsif type_options.include?(type.to_s)
+ send("#{attribute}_#{type}")
+ else
+ raise "I don't understand the `#{type}' option. Try #{type_options.join(' or ')}."
+ end
end
- define_method("#{attr}_plain", proc { strip_redcloth_html(__send__(attr)) if __send__(attr) } )
- define_method("#{attr}_source", proc { __send__("#{attr}_before_type_cast") } )
- @textiled_attributes << attr
+
+ define_method("#{attribute}_plain", proc { strip_redcloth_html(__send__(attribute)) if __send__(attribute) } )
+ define_method("#{attribute}_source", proc { __send__("#{attribute}_before_type_cast") } )
+
+ @textiled_attributes << attribute
end
include Err::Acts::Textiled::InstanceMethods
View
14 test/textiled_test.rb
@@ -17,21 +17,27 @@
desc_plain = '_why announces Sandbox'
story.description.should.equal desc_html
- #story.description(:source).should.equal desc_textile
- #story.description(:plain).should.equal desc_plain
+ story.description(:source).should.equal desc_textile
+ story.description(:plain).should.equal desc_plain
story.description_source.should.equal desc_textile
story.description_plain.should.equal desc_plain
# make sure we don't overwrite anything - thanks James
story.description.should.equal desc_html
- #story.description(:source).should.equal desc_textile
- #story.description(:plain).should.equal desc_plain
+ story.description(:source).should.equal desc_textile
+ story.description(:plain).should.equal desc_plain
story.description_source.should.equal desc_textile
story.description_plain.should.equal desc_plain
end
+ specify "should raise when given a non-sensical option" do
+ story = Story.find(1)
+
+ proc { story.description(:cassadaga) }.should.raise
+ end
+
specify "should pick up changes to attributes" do
story = Story.find(2)
Please sign in to comment.
Something went wrong with that request. Please try again.