Skip to content

Commit

Permalink
make loading more private and less presumptive
Browse files Browse the repository at this point in the history
  • Loading branch information
rsl committed Dec 6, 2012
1 parent 1b4480f commit f23f325
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
5 changes: 2 additions & 3 deletions lib/stringex.rb
Expand Up @@ -3,10 +3,9 @@
require 'stringex/configuration' require 'stringex/configuration'
require 'stringex/string_extensions' require 'stringex/string_extensions'
require 'stringex/unidecoder' require 'stringex/unidecoder'
require 'stringex/acts_as_url'


String.send :include, Stringex::StringExtensions::PublicInstanceMethods String.send :include, Stringex::StringExtensions::PublicInstanceMethods
String.send :extend, Stringex::StringExtensions::PublicClassMethods String.send :extend, Stringex::StringExtensions::PublicClassMethods


require 'stringex/acts_as_url' if defined?(ActiveRecord) || defined?(Mongoid::Document) Stringex::ActsAsUrl.load_adapters

ActiveRecord::Base.send :include, Stringex::ActsAsUrl if defined?(ActiveRecord)
10 changes: 10 additions & 0 deletions lib/stringex/acts_as_url.rb
Expand Up @@ -4,10 +4,20 @@


module Stringex module Stringex
module ActsAsUrl # :nodoc: module ActsAsUrl # :nodoc:
ADAPTERS = [
Adapter::ActiveRecord
]

def self.included(base) def self.included(base)
base.extend ClassMethods base.extend ClassMethods
end end


def self.load_adapters
ADAPTERS.each do |adapter|
adapter.load if adapter.loadable?
end
end

module ClassMethods # :doc: module ClassMethods # :doc:
# Creates a callback to automatically create an url-friendly representation # Creates a callback to automatically create an url-friendly representation
# of the <tt>attribute</tt> argument. Example: # of the <tt>attribute</tt> argument. Example:
Expand Down
9 changes: 9 additions & 0 deletions lib/stringex/acts_as_url/adapter/active_record.rb
Expand Up @@ -28,6 +28,15 @@ def url_attribute(instance)
end end
end end


def self.loadable?
defined? ActiveRecord
end

def self.load
ensure_loadable
::ActiveRecord::Base.send :include, Stringex::ActsAsUrl
end

private private


def add_new_record_url_owner_conditions def add_new_record_url_owner_conditions
Expand Down
19 changes: 17 additions & 2 deletions lib/stringex/acts_as_url/adapter/base.rb
Expand Up @@ -5,6 +5,7 @@ class Base
attr_accessor :base_url, :configuration, :instance, :klass, :settings attr_accessor :base_url, :configuration, :instance, :klass, :settings


def initialize(configuration) def initialize(configuration)
ensure_loadable
self.configuration = configuration self.configuration = configuration
self.settings = configuration.settings self.settings = configuration.settings
end end
Expand All @@ -24,14 +25,24 @@ def initialize_urls!(klass)
end end
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) def duplicate_for_base_url(n)
"#{base_url}#{settings.duplicate_count_separator}#{n}" "#{base_url}#{settings.duplicate_count_separator}#{n}"
end end


def ensure_loadable
self.class.ensure_loadable
end

def handle_duplicate_url! def handle_duplicate_url!
return if url_owners.none?{|owner| url_attribute_for(owner) == base_url} return if url_owners.none?{|owner| url_attribute_for(owner) == base_url}
n = 1 n = 1
Expand All @@ -47,6 +58,10 @@ def handle_url!
write_url_attribute base_url write_url_attribute base_url
end end


def loadable?
self.class.loadable?
end

def modify_base_url def modify_base_url
root = instance.send(settings.attribute_to_urlify).to_s root = instance.send(settings.attribute_to_urlify).to_s
self.base_url = root.to_url(configuration.string_extensions_settings) self.base_url = root.to_url(configuration.string_extensions_settings)
Expand Down

0 comments on commit f23f325

Please sign in to comment.