# File lib/stringex/acts_as_url/adapter/base.rb 192 def primary_key 193 instance.class.primary_key 194 end
class Stringex::ActsAsUrl::Adapter::Base
Attributes
base_url[RW]
callback_options[RW]
configuration[RW]
instance[RW]
klass[RW]
settings[RW]
Public Class Methods
ensure_loadable()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 46 def self.ensure_loadable 47 raise "The #{self} adapter cannot be loaded" unless loadable? 48 Stringex::ActsAsUrl::Adapter.add_loaded_adapter self 49 end
loadable?()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 51 def self.loadable? 52 orm_class 53 rescue NameError 54 false 55 end
new(configuration)
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 7 def initialize(configuration) 8 ensure_loadable 9 self.configuration = configuration 10 self.settings = configuration.settings 11 end
Public Instance Methods
create_callbacks!(klass)
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 13 def create_callbacks!(klass) 14 self.klass = klass 15 self.callback_options = {} 16 create_method_to_callback 17 create_callback 18 end
ensure_unique_url!(instance)
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 20 def ensure_unique_url!(instance) 21 @url_owners = nil 22 self.instance = instance 23 24 handle_url! 25 handle_blacklisted_url! 26 handle_duplicate_url! unless settings.allow_duplicates 27 end
initialize_urls!(klass)
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 29 def initialize_urls!(klass) 30 self.klass = klass 31 klass_previous_instances do |instance| 32 ensure_unique_url_for! instance 33 end 34 end
url_attribute(instance)
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 36 def url_attribute(instance) 37 # Retrieve from database record if there are errors on attribute_to_urlify 38 if !is_new?(instance) && is_present?(instance.errors[settings.attribute_to_urlify]) 39 self.instance = instance 40 read_attribute instance_from_db, settings.url_attribute 41 else 42 read_attribute instance, settings.url_attribute 43 end 44 end
Private Instance Methods
add_new_record_url_owner_conditions()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 59 def add_new_record_url_owner_conditions 60 return if is_new?(instance) 61 @url_owner_conditions.first << " and #{primary_key} != ?" 62 @url_owner_conditions << instance.id 63 end
add_scoped_url_owner_conditions()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 65 def add_scoped_url_owner_conditions 66 [settings.scope_for_url].flatten.compact.each do |scope| 67 @url_owner_conditions.first << " and #{scope} = ?" 68 @url_owner_conditions << instance.send(scope) 69 end 70 end
create_callback()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 72 def create_callback 73 klass.send klass_callback_method, :ensure_unique_url, callback_options 74 end
create_method_to_callback()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 94 def create_method_to_callback 95 klass.class_eval <<-"END" 96 def #{settings.url_attribute} 97 acts_as_url_configuration.adapter.url_attribute self 98 end 99 END 100 end
duplicate_for_base_url(n)
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 102 def duplicate_for_base_url(n) 103 "#{base_url}#{settings.duplicate_count_separator}#{n}" 104 end
duplicate_url_sequence()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 132 def duplicate_url_sequence 133 settings.duplicate_sequence || 134 Enumerator.new do |enum| 135 n = 1 136 loop do 137 enum.yield n 138 n += 1 139 end 140 end 141 end
ensure_loadable()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 106 def ensure_loadable 107 self.class.ensure_loadable 108 end
ensure_unique_url_for!(instance)
click to toggle source
NOTE: The instance
here is not the cached instance but a block variable passed from klass_previous_instances
, just to be clear
# File lib/stringex/acts_as_url/adapter/base.rb 112 def ensure_unique_url_for!(instance) 113 instance.send :ensure_unique_url 114 instance.save 115 end
get_base_url_owner_conditions()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 117 def get_base_url_owner_conditions 118 @url_owner_conditions = ["#{settings.url_attribute} LIKE ?", base_url + '%'] 119 end
handle_blacklisted_url!()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 157 def handle_blacklisted_url! 158 return unless settings.blacklist.to_set.include?(base_url) 159 self.base_url = settings.blacklist_policy.call(instance, base_url) 160 write_url_attribute base_url 161 end
handle_duplicate_url!()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 121 def handle_duplicate_url! 122 return if !url_taken?(base_url) 123 n = nil 124 sequence = duplicate_url_sequence.tap(&:rewind) 125 loop do 126 n = sequence.next 127 break unless url_taken?(duplicate_for_base_url(n)) 128 end 129 write_url_attribute duplicate_for_base_url(n) 130 end
handle_url!()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 151 def handle_url! 152 self.base_url = instance.send(settings.url_attribute) 153 modify_base_url if is_blank?(base_url) || !settings.only_when_blank 154 write_url_attribute base_url 155 end
instance_from_db()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 163 def instance_from_db 164 instance.class.find(instance.id) 165 end
is_blank?(object)
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 167 def is_blank?(object) 168 object.blank? 169 end
is_new?(object)
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 171 def is_new?(object) 172 object.new_record? 173 end
is_present?(object)
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 175 def is_present?(object) 176 object.present? 177 end
klass_callback_method()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 76 def klass_callback_method 77 settings.sync_url ? klass_sync_url_callback_method : klass_non_sync_url_callback_method 78 end
klass_non_sync_url_callback_method()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 84 def klass_non_sync_url_callback_method 85 case configuration.settings.callback_method 86 when :before_save 87 :before_create 88 else # :before_validation 89 callback_options[:on] = :create 90 configuration.settings.callback_method 91 end 92 end
klass_sync_url_callback_method()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 80 def klass_sync_url_callback_method 81 configuration.settings.callback_method 82 end
loadable?()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 179 def loadable? 180 self.class.loadable? 181 end
modify_base_url()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 183 def modify_base_url 184 root = instance.send(settings.attribute_to_urlify).to_s 185 self.base_url = root.to_url(configuration.string_extensions_settings) 186 end
orm_class()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 188 def orm_class 189 self.class.orm_class 190 end
primary_key()
click to toggle source
read_attribute(instance, attribute)
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 196 def read_attribute(instance, attribute) 197 instance.read_attribute attribute 198 end
url_attribute_for(object)
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 200 def url_attribute_for(object) 201 object.send settings.url_attribute 202 end
url_owner_conditions()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 204 def url_owner_conditions 205 get_base_url_owner_conditions 206 add_new_record_url_owner_conditions 207 add_scoped_url_owner_conditions 208 209 @url_owner_conditions 210 end
url_owners()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 212 def url_owners 213 @url_owners ||= url_owners_class.unscoped.where(url_owner_conditions).to_a 214 end
url_owners_class()
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 216 def url_owners_class 217 return instance.class unless settings.enforce_uniqueness_on_sti_base_class 218 219 klass = instance.class 220 while klass.superclass < orm_class 221 klass = klass.superclass 222 end 223 klass 224 end
url_taken?(url)
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 143 def url_taken?(url) 144 if settings.url_taken_method 145 instance.send(settings.url_taken_method, url) 146 else 147 url_owners.any?{|owner| url_attribute_for(owner) == url} 148 end 149 end
write_attribute(instance, attribute, value)
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 226 def write_attribute(instance, attribute, value) 227 instance.send :write_attribute, attribute, value 228 end
write_url_attribute(value)
click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb 230 def write_url_attribute(value) 231 write_attribute instance, settings.url_attribute, value 232 end