Permalink
Browse files

Allow :use to accept a module

Closes #255
  • Loading branch information...
1 parent 0328e5e commit 087777ca15132697d3a0894772b16157244720e6 @norman norman committed Mar 13, 2012
Showing with 51 additions and 7 deletions.
  1. +1 −0 Changelog.md
  2. +25 −2 lib/friendly_id/base.rb
  3. +6 −5 lib/friendly_id/configuration.rb
  4. +19 −0 test/configuration_test.rb
View
@@ -8,6 +8,7 @@ suggestions, ideas and improvements to FriendlyId.
## 4.0.3 (NOT RELEASED YET)
+* Allow Configuration#use to accept a module (Norman Clarke)
* Fix bugs with History module + STI (Norman Clarke and [spetrunin](https://github.com/spetrunin))
## 4.0.2 (2012-03-12)
View
@@ -129,8 +129,31 @@ module Base
# end
# end
#
- # @option options [Symbol] :use The name of an addon to use. By default,
- # FriendlyId provides {FriendlyId::Slugged :slugged},
+ # === Including Your Own Modules
+ #
+ # Because :use can accept a name or a Module, {FriendlyId.defaults defaults}
+ # can be a convenient place to set up behavior common to all classes using
+ # FriendlyId. You can include any module, or more conveniently, define one
+ # on-the-fly. For example, let's say you want to make
+ # Babosa[http://github.com/norman/babosa] the default slugging library in
+ # place of Active Support, and transliterate all slugs from Russian Cyrillic
+ # to ASCII:
+ #
+ # require "babosa"
+ #
+ # FriendlyId.defaults do |config|
+ # config.base = :name
+ # config.use :slugged
+ # config.use Module.new {
+ # def normalize_friendly_id(text)
+ # text.to_slug.normalize(:transliterations => [:russian, :latin])
+ # end
+ # }
+ # end
+ #
+ #
+ # @option options [Symbol,Module] :use The addon or name of an addon to use.
+ # By default, FriendlyId provides {FriendlyId::Slugged :slugged},
# {FriendlyId::History :history}, {FriendlyId::Reserved :reserved}, and
# {FriendlyId::Scoped :scoped}.
#
@@ -51,12 +51,13 @@ def initialize(model_class, values = nil)
# extend FriendlyId
# friendly_id :name, :use => :slugged
# end
- # @param [#to_s] *modules Arguments should be a symbols or strings that
- # correspond with the name of a module inside the FriendlyId namespace. By
- # default FriendlyId provides +:slugged+, +:history+, +:simple_i18n+ and +:scoped+.
+ # @param [#to_s,Module] *modules Arguments should be Modules, or symbols or
+ # strings that correspond with the name of a module inside the FriendlyId
+ # namespace. By default FriendlyId provides +:slugged+, +:history+,
+ # +:simple_i18n+ and +:scoped+.
def use(*modules)
- modules.to_a.flatten.compact.map do |name|
- mod = FriendlyId.const_get(name.to_s.classify)
+ modules.to_a.flatten.compact.map do |object|
+ mod = object.kind_of?(Module) ? object : FriendlyId.const_get(object.to_s.classify)
model_class.send(:include, mod)
end
end
View
@@ -26,4 +26,23 @@ def setup
end
end
+ test "#use should accept a name that resolves to a module" do
+ refute @model_class < FriendlyId::Slugged
+ @model_class.class_eval do
+ extend FriendlyId
+ friendly_id :hello, :use => :slugged
+ end
+ assert @model_class < FriendlyId::Slugged
+ end
+
+ test "#use should accept a module" do
+ my_module = Module.new
+ refute @model_class < my_module
+ @model_class.class_eval do
+ extend FriendlyId
+ friendly_id :hello, :use => my_module
+ end
+ assert @model_class < my_module
+ end
+
end

0 comments on commit 087777c

Please sign in to comment.