Permalink
Browse files

make loading more private and less presumptive

  • Loading branch information...
1 parent 1b4480f commit f23f3255905ef16ea536ebf903db0fa3af035cc8 @rsl committed Dec 6, 2012
View
@@ -3,10 +3,9 @@
require 'stringex/configuration'
require 'stringex/string_extensions'
require 'stringex/unidecoder'
+require 'stringex/acts_as_url'
String.send :include, Stringex::StringExtensions::PublicInstanceMethods
String.send :extend, Stringex::StringExtensions::PublicClassMethods
-require 'stringex/acts_as_url' if defined?(ActiveRecord) || defined?(Mongoid::Document)
-
-ActiveRecord::Base.send :include, Stringex::ActsAsUrl if defined?(ActiveRecord)
+Stringex::ActsAsUrl.load_adapters
@@ -4,10 +4,20 @@
module Stringex
module ActsAsUrl # :nodoc:
+ ADAPTERS = [
+ Adapter::ActiveRecord
+ ]
+
def self.included(base)
base.extend ClassMethods
end
+ def self.load_adapters
+ ADAPTERS.each do |adapter|
+ adapter.load if adapter.loadable?
+ end
+ end
+
module ClassMethods # :doc:
# Creates a callback to automatically create an url-friendly representation
# of the <tt>attribute</tt> argument. Example:
@@ -28,6 +28,15 @@ def url_attribute(instance)
end
end
+ def self.loadable?
+ defined? ActiveRecord
+ end
+
+ def self.load
+ ensure_loadable
+ ::ActiveRecord::Base.send :include, Stringex::ActsAsUrl
+ end
+
private
def add_new_record_url_owner_conditions
@@ -5,6 +5,7 @@ class Base
attr_accessor :base_url, :configuration, :instance, :klass, :settings
def initialize(configuration)
+ ensure_loadable
self.configuration = configuration
self.settings = configuration.settings
end
@@ -24,14 +25,24 @@ def initialize_urls!(klass)
end
end
- private
+ def self.available?
+ false
+ end
- # Override any of these you need within your adapter
+ def self.ensure_loadable
+ raise "#{self} is not an loadable adapter" unless loadable?
+ end
+
+ private
def duplicate_for_base_url(n)
"#{base_url}#{settings.duplicate_count_separator}#{n}"
end
+ def ensure_loadable
+ self.class.ensure_loadable
+ end
+
def handle_duplicate_url!
return if url_owners.none?{|owner| url_attribute_for(owner) == base_url}
n = 1
@@ -47,6 +58,10 @@ def handle_url!
write_url_attribute base_url
end
+ def loadable?
+ self.class.loadable?
+ end
+
def modify_base_url
root = instance.send(settings.attribute_to_urlify).to_s
self.base_url = root.to_url(configuration.string_extensions_settings)

0 comments on commit f23f325

Please sign in to comment.