From ff594b2bc94ff2a942fe6ca05672387722dee686 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 16 Nov 2008 16:01:18 +0100 Subject: =?UTF-8?q?Added=20default=5Fscope=20to=20Base=20[#1381=20state:co?= =?UTF-8?q?mmitted]=20(Pawe=C5=82=20Kondzior)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- activerecord/CHANGELOG | 16 +++++++++++ activerecord/lib/active_record/base.rb | 10 +++++++ activerecord/test/cases/method_scoping_test.rb | 38 ++++++++++++++++++++++++++ activerecord/test/models/developer.rb | 12 ++++++++ 4 files changed, 76 insertions(+) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index c2299b56ad..84605b60ef 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,3 +1,19 @@ +*2.3.0/3.0* + +* Added default_scope to Base #1381 [Paweł Kondzior]. Example: + + class Person < ActiveRecord::Base + default_scope :order => 'last_name, first_name' + end + + class Company < ActiveRecord::Base + has_many :people + end + + Person.all # => Person.find(:all, :order => 'last_name, first_name') + Company.find(1).people # => Person.find(:all, :order => 'last_name, first_name', :conditions => { :company_id => 1 }) + + *2.2.1 [RC2] (November 14th, 2008)* * Ensure indices don't flip order in schema.rb #1266 [Jordi Bunster] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index dcc8277849..9481c12b26 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2016,6 +2016,16 @@ module ActiveRecord #:nodoc: @@subclasses[self] + extra = @@subclasses[self].inject([]) {|list, subclass| list + subclass.subclasses } end + # Sets the default options for the model. The format of the + # method_scoping argument is the same as in with_scope. + # + # class Person << ActiveRecord::Base + # default_scope :find => { :order => 'last_name, first_name' } + # end + def default_scope(options = {}) + self.scoped_methods << { :find => options, :create => options.is_a?(Hash) ? options[:conditions] : {} } + end + # Test whether the given method and optional key are scoped. def scoped?(method, key = nil) #:nodoc: if current_scoped_methods && (scope = current_scoped_methods[method]) diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index ff10bfaf3e..79b24cd4fd 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -522,6 +522,44 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase end +class DefaultScopingTest < ActiveRecord::TestCase + fixtures :developers + + def test_default_scope + expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_method_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_nested_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_scope(:find => { :order => 'name DESC'}) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end + + def test_nested_exclusive_scope + expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end + + def test_overwriting_default_scope + expected = Developer.find(:all, :order => 'salary').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary } + assert_equal expected, received + end +end + =begin # We disabled the scoping for has_one and belongs_to as we can't think of a proper use case diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index c08476f728..1844014011 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -77,3 +77,15 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base raise if projects.empty? end end + +class DeveloperOrderedBySalary < ActiveRecord::Base + self.table_name = 'developers' + default_scope :order => "salary DESC" + + def self.all_ordered_by_name + with_scope(:find => { :order => "name DESC" }) do + find(:all) + end + end + +end -- cgit v1.2.3 From ca23287b448c2e007a5c93e43e762a10e0007b7a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 16 Nov 2008 16:35:52 +0100 Subject: =?UTF-8?q?Revert=20"Added=20default=5Fscope=20to=20Base=20[#1381?= =?UTF-8?q?=20state:committed]=20(Pawe=C5=82=20Kondzior)"=20--=20won't=20g?= =?UTF-8?q?el=20with=20threads.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit ff594b2bc94ff2a942fe6ca05672387722dee686. --- activerecord/CHANGELOG | 16 ----------- activerecord/lib/active_record/base.rb | 10 ------- activerecord/test/cases/method_scoping_test.rb | 38 -------------------------- activerecord/test/models/developer.rb | 12 -------- 4 files changed, 76 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 84605b60ef..c2299b56ad 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,19 +1,3 @@ -*2.3.0/3.0* - -* Added default_scope to Base #1381 [Paweł Kondzior]. Example: - - class Person < ActiveRecord::Base - default_scope :order => 'last_name, first_name' - end - - class Company < ActiveRecord::Base - has_many :people - end - - Person.all # => Person.find(:all, :order => 'last_name, first_name') - Company.find(1).people # => Person.find(:all, :order => 'last_name, first_name', :conditions => { :company_id => 1 }) - - *2.2.1 [RC2] (November 14th, 2008)* * Ensure indices don't flip order in schema.rb #1266 [Jordi Bunster] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 9481c12b26..dcc8277849 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2016,16 +2016,6 @@ module ActiveRecord #:nodoc: @@subclasses[self] + extra = @@subclasses[self].inject([]) {|list, subclass| list + subclass.subclasses } end - # Sets the default options for the model. The format of the - # method_scoping argument is the same as in with_scope. - # - # class Person << ActiveRecord::Base - # default_scope :find => { :order => 'last_name, first_name' } - # end - def default_scope(options = {}) - self.scoped_methods << { :find => options, :create => options.is_a?(Hash) ? options[:conditions] : {} } - end - # Test whether the given method and optional key are scoped. def scoped?(method, key = nil) #:nodoc: if current_scoped_methods && (scope = current_scoped_methods[method]) diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 79b24cd4fd..ff10bfaf3e 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -522,44 +522,6 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase end -class DefaultScopingTest < ActiveRecord::TestCase - fixtures :developers - - def test_default_scope - expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } - assert_equal expected, received - end - - def test_method_scope - expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary } - assert_equal expected, received - end - - def test_nested_scope - expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.with_scope(:find => { :order => 'name DESC'}) do - DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } - end - assert_equal expected, received - end - - def test_nested_exclusive_scope - expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do - DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } - end - assert_equal expected, received - end - - def test_overwriting_default_scope - expected = Developer.find(:all, :order => 'salary').collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary } - assert_equal expected, received - end -end - =begin # We disabled the scoping for has_one and belongs_to as we can't think of a proper use case diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 1844014011..c08476f728 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -77,15 +77,3 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base raise if projects.empty? end end - -class DeveloperOrderedBySalary < ActiveRecord::Base - self.table_name = 'developers' - default_scope :order => "salary DESC" - - def self.all_ordered_by_name - with_scope(:find => { :order => "name DESC" }) do - find(:all) - end - end - -end -- cgit v1.2.3 From d9f460a39b73fd2cf0f17f523cc4810d0bf44cac Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 16 Nov 2008 22:21:05 +0530 Subject: Ensure @@already_loaded_fixtures is initialized before use --- activerecord/lib/active_record/fixtures.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index a09f58fc23..9a82ff2ed4 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -925,6 +925,7 @@ module ActiveRecord end @fixture_cache = {} + @@already_loaded_fixtures ||= {} # Load fixtures once and begin transaction. if use_transactional_fixtures? @@ -939,7 +940,6 @@ module ActiveRecord # Load fixtures for every test. else Fixtures.reset_cache - @@already_loaded_fixtures ||= {} @@already_loaded_fixtures[self.class] = nil load_fixtures end -- cgit v1.2.3 From 2530d0eea8eaecd2c61f99225f050ff47973e9a0 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 16 Nov 2008 23:36:41 +0530 Subject: =?UTF-8?q?Added=20default=5Fscope=20to=20Base=20[#1381=20state:co?= =?UTF-8?q?mmitted]=20(Pawe=C5=82=20Kondzior)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- activerecord/CHANGELOG | 16 +++++++ activerecord/lib/active_record/base.rb | 16 ++++++- activerecord/test/cases/method_scoping_test.rb | 61 ++++++++++++++++++++++++++ activerecord/test/models/developer.rb | 12 +++++ 4 files changed, 104 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index c2299b56ad..c1d7297260 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,3 +1,19 @@ +*2.3.0/3.0* + +* Added default_scope to Base #1381 [Paweł Kondzior]. Example: + + class Person < ActiveRecord::Base + default_scope :order => 'last_name, first_name' + end + + class Company < ActiveRecord::Base + has_many :people + end + + Person.all # => Person.find(:all, :order => 'last_name, first_name') + Company.find(1).people # => Person.find(:all, :order => 'last_name, first_name', :conditions => { :company_id => 1 }) + + *2.2.1 [RC2] (November 14th, 2008)* * Ensure indices don't flip order in schema.rb #1266 [Jordi Bunster] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index dcc8277849..d8b7a5a931 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -495,6 +495,10 @@ module ActiveRecord #:nodoc: superclass_delegating_accessor :store_full_sti_class self.store_full_sti_class = false + # Stores the default scope for the class + class_inheritable_accessor :default_scoping, :instance_writer => false + self.default_scoping = [] + class << self # Class methods # Find operates with four different retrieval approaches: # @@ -2016,6 +2020,16 @@ module ActiveRecord #:nodoc: @@subclasses[self] + extra = @@subclasses[self].inject([]) {|list, subclass| list + subclass.subclasses } end + # Sets the default options for the model. The format of the + # method_scoping argument is the same as in with_scope. + # + # class Person < ActiveRecord::Base + # default_scope :find => { :order => 'last_name, first_name' } + # end + def default_scope(options = {}) + self.default_scoping << { :find => options, :create => options.is_a?(Hash) ? options[:conditions] : {} } + end + # Test whether the given method and optional key are scoped. def scoped?(method, key = nil) #:nodoc: if current_scoped_methods && (scope = current_scoped_methods[method]) @@ -2031,7 +2045,7 @@ module ActiveRecord #:nodoc: end def scoped_methods #:nodoc: - Thread.current[:"#{self}_scoped_methods"] ||= [] + Thread.current[:"#{self}_scoped_methods"] ||= self.default_scoping end def current_scoped_methods #:nodoc: diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index ff10bfaf3e..4ac0018144 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -522,6 +522,67 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase end +class DefaultScopingTest < ActiveRecord::TestCase + fixtures :developers + + def test_default_scope + expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_default_scoping_with_threads + scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}] + + 2.times do + Thread.new { assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods) }.join + end + end + + def test_default_scoping_with_inheritance + scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}] + + # Inherit a class having a default scope and define a new default scope + klass = Class.new(DeveloperOrderedBySalary) + klass.send :default_scope, {} + + # Scopes added on children should append to parent scope + expected_klass_scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}, {:create=>nil, :find=>{}}] + assert_equal expected_klass_scope, klass.send(:scoped_methods) + + # Parent should still have the original scope + assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods) + end + + def test_method_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_nested_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_scope(:find => { :order => 'name DESC'}) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end + + def test_nested_exclusive_scope + expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end + + def test_overwriting_default_scope + expected = Developer.find(:all, :order => 'salary').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary } + assert_equal expected, received + end +end + =begin # We disabled the scoping for has_one and belongs_to as we can't think of a proper use case diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index c08476f728..0c20f97502 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -77,3 +77,15 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base raise if projects.empty? end end + +class DeveloperOrderedBySalary < ActiveRecord::Base + self.table_name = 'developers' + default_scope :order => "salary DESC" + + def self.all_ordered_by_name + with_scope(:find => { :order => "name DESC" }) do + find(:all) + end + end + +end -- cgit v1.2.3 From 8c197fb4ab4fa432a6e9421e0339a17a7ec296f1 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Sun, 16 Nov 2008 20:19:02 +0100 Subject: Add text/plain to the browser_generated_types array as webkit and gecko can submit them. For more information see: http://pseudo-flaw.net/content/web-browsers/form-data-encoding-roundup/ --- actionpack/lib/action_controller/mime_type.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_controller/mime_type.rb b/actionpack/lib/action_controller/mime_type.rb index 8ca3a70341..6923a13f3f 100644 --- a/actionpack/lib/action_controller/mime_type.rb +++ b/actionpack/lib/action_controller/mime_type.rb @@ -25,7 +25,7 @@ module Mime # These are the content types which browsers can generate without using ajax, flash, etc # i.e. following a link, getting an image or posting a form. CSRF protection # only needs to protect against these types. - @@browser_generated_types = Set.new [:html, :url_encoded_form, :multipart_form] + @@browser_generated_types = Set.new [:html, :url_encoded_form, :multipart_form, :text] cattr_reader :browser_generated_types @@ -177,7 +177,7 @@ module Mime end # Returns true if Action Pack should check requests using this Mime Type for possible request forgery. See - # ActionController::RequestForgerProtection. + # ActionController::RequestForgeryProtection. def verify_request? browser_generated? end -- cgit v1.2.3 From e6c51051e4bbc1483ecc9e0837bb893197bbca83 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 16 Nov 2008 13:51:04 -0600 Subject: Ensure shared default_scoping stack is duped before assigning to thread local --- activerecord/lib/active_record/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index d8b7a5a931..68f44ef0f6 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2045,7 +2045,7 @@ module ActiveRecord #:nodoc: end def scoped_methods #:nodoc: - Thread.current[:"#{self}_scoped_methods"] ||= self.default_scoping + Thread.current[:"#{self}_scoped_methods"] ||= self.default_scoping.dup end def current_scoped_methods #:nodoc: -- cgit v1.2.3 From 54c18564252e46bf2f270bf42c0be06033631d9b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 16 Nov 2008 21:29:48 +0100 Subject: The inflector is meant to work on words not phrases -- dont confuse people with a phrase example --- activesupport/lib/active_support/inflector.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index ba52e41c08..ad2660e6c8 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -134,7 +134,6 @@ module ActiveSupport # "octopus".pluralize # => "octopi" # "sheep".pluralize # => "sheep" # "words".pluralize # => "words" - # "the blue mailman".pluralize # => "the blue mailmen" # "CamelOctopus".pluralize # => "CamelOctopi" def pluralize(word) result = word.to_s.dup @@ -154,7 +153,6 @@ module ActiveSupport # "octopi".singularize # => "octopus" # "sheep".singluarize # => "sheep" # "word".singularize # => "word" - # "the blue mailmen".singularize # => "the blue mailman" # "CamelOctopi".singularize # => "CamelOctopus" def singularize(word) result = word.to_s.dup -- cgit v1.2.3 From 4b33fae1f52325d22083de2e83d827b924d1c616 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 17 Nov 2008 18:31:36 +0100 Subject: Fixed RedCloth and BlueCloth shouldn't preload. Instead just assume that they're available if you want to use textilize and markdown and let autoload require them [DHH] --- actionpack/CHANGELOG | 5 + actionpack/lib/action_view/helpers/text_helper.rb | 150 ++++++++++------------ 2 files changed, 74 insertions(+), 81 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index b7a824d559..4ed39133db 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,3 +1,8 @@ +*2.3.0/3.0* + +* Fixed RedCloth and BlueCloth shouldn't preload. Instead just assume that they're available if you want to use textilize and markdown and let autoload require them [DHH] + + *2.2.1 [RC2] (November 14th, 2008)* * Restore backwards compatible functionality for setting relative_url_root. Include deprecation diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 9bd3d63423..510c1a6a76 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -226,91 +226,79 @@ module ActionView end * "\n" end - begin - require_library_or_gem "redcloth" unless Object.const_defined?(:RedCloth) - - # Returns the text with all the Textile[http://www.textism.com/tools/textile] codes turned into HTML tags. - # - # You can learn more about Textile's syntax at its website[http://www.textism.com/tools/textile]. - # This method is only available if RedCloth[http://whytheluckystiff.net/ruby/redcloth/] - # is available. - # - # ==== Examples - # textilize("*This is Textile!* Rejoice!") - # # => "

This is Textile! Rejoice!

" - # - # textilize("I _love_ ROR(Ruby on Rails)!") - # # => "

I love ROR!

" - # - # textilize("h2. Textile makes markup -easy- simple!") - # # => "

Textile makes markup easy simple!

" - # - # textilize("Visit the Rails website "here":http://www.rubyonrails.org/.) - # # => "

Visit the Rails website here.

" - def textilize(text) - if text.blank? - "" - else - textilized = RedCloth.new(text, [ :hard_breaks ]) - textilized.hard_breaks = true if textilized.respond_to?(:hard_breaks=) - textilized.to_html - end + # Returns the text with all the Textile[http://www.textism.com/tools/textile] codes turned into HTML tags. + # + # You can learn more about Textile's syntax at its website[http://www.textism.com/tools/textile]. + # This method is only available if RedCloth[http://whytheluckystiff.net/ruby/redcloth/] + # is available. + # + # ==== Examples + # textilize("*This is Textile!* Rejoice!") + # # => "

This is Textile! Rejoice!

" + # + # textilize("I _love_ ROR(Ruby on Rails)!") + # # => "

I love ROR!

" + # + # textilize("h2. Textile makes markup -easy- simple!") + # # => "

Textile makes markup easy simple!

" + # + # textilize("Visit the Rails website "here":http://www.rubyonrails.org/.) + # # => "

Visit the Rails website here.

" + def textilize(text) + if text.blank? + "" + else + textilized = RedCloth.new(text, [ :hard_breaks ]) + textilized.hard_breaks = true if textilized.respond_to?(:hard_breaks=) + textilized.to_html end + end - # Returns the text with all the Textile codes turned into HTML tags, - # but without the bounding

tag that RedCloth adds. - # - # You can learn more about Textile's syntax at its website[http://www.textism.com/tools/textile]. - # This method is only available if RedCloth[http://whytheluckystiff.net/ruby/redcloth/] - # is available. - # - # ==== Examples - # textilize_without_paragraph("*This is Textile!* Rejoice!") - # # => "This is Textile! Rejoice!" - # - # textilize_without_paragraph("I _love_ ROR(Ruby on Rails)!") - # # => "I love ROR!" - # - # textilize_without_paragraph("h2. Textile makes markup -easy- simple!") - # # => "

Textile makes markup easy simple!

" - # - # textilize_without_paragraph("Visit the Rails website "here":http://www.rubyonrails.org/.) - # # => "Visit the Rails website here." - def textilize_without_paragraph(text) - textiled = textilize(text) - if textiled[0..2] == "

" then textiled = textiled[3..-1] end - if textiled[-4..-1] == "

" then textiled = textiled[0..-5] end - return textiled - end - rescue LoadError - # We can't really help what's not there + # Returns the text with all the Textile codes turned into HTML tags, + # but without the bounding

tag that RedCloth adds. + # + # You can learn more about Textile's syntax at its website[http://www.textism.com/tools/textile]. + # This method is requires RedCloth[http://whytheluckystiff.net/ruby/redcloth/] + # to be available. + # + # ==== Examples + # textilize_without_paragraph("*This is Textile!* Rejoice!") + # # => "This is Textile! Rejoice!" + # + # textilize_without_paragraph("I _love_ ROR(Ruby on Rails)!") + # # => "I love ROR!" + # + # textilize_without_paragraph("h2. Textile makes markup -easy- simple!") + # # => "

Textile makes markup easy simple!

" + # + # textilize_without_paragraph("Visit the Rails website "here":http://www.rubyonrails.org/.) + # # => "Visit the Rails website here." + def textilize_without_paragraph(text) + textiled = textilize(text) + if textiled[0..2] == "

" then textiled = textiled[3..-1] end + if textiled[-4..-1] == "

" then textiled = textiled[0..-5] end + return textiled end - begin - require_library_or_gem "bluecloth" unless Object.const_defined?(:BlueCloth) - - # Returns the text with all the Markdown codes turned into HTML tags. - # This method is only available if BlueCloth[http://www.deveiate.org/projects/BlueCloth] - # is available. - # - # ==== Examples - # markdown("We are using __Markdown__ now!") - # # => "

We are using Markdown now!

" - # - # markdown("We like to _write_ `code`, not just _read_ it!") - # # => "

We like to write code, not just read it!

" - # - # markdown("The [Markdown website](http://daringfireball.net/projects/markdown/) has more information.") - # # => "

The Markdown website - # # has more information.

" - # - # markdown('![The ROR logo](http://rubyonrails.com/images/rails.png "Ruby on Rails")') - # # => '

The ROR logo

' - def markdown(text) - text.blank? ? "" : BlueCloth.new(text).to_html - end - rescue LoadError - # We can't really help what's not there + # Returns the text with all the Markdown codes turned into HTML tags. + # This method requires BlueCloth[http://www.deveiate.org/projects/BlueCloth] + # to be available. + # + # ==== Examples + # markdown("We are using __Markdown__ now!") + # # => "

We are using Markdown now!

" + # + # markdown("We like to _write_ `code`, not just _read_ it!") + # # => "

We like to write code, not just read it!

" + # + # markdown("The [Markdown website](http://daringfireball.net/projects/markdown/) has more information.") + # # => "

The Markdown website + # # has more information.

" + # + # markdown('![The ROR logo](http://rubyonrails.com/images/rails.png "Ruby on Rails")') + # # => '

The ROR logo

' + def markdown(text) + text.blank? ? "" : BlueCloth.new(text).to_html end # Returns +text+ transformed into HTML using simple formatting rules. -- cgit v1.2.3