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, line 46
def self.ensure_loadable
  raise "The #{self} adapter cannot be loaded" unless loadable?
  Stringex::ActsAsUrl::Adapter.add_loaded_adapter self
end
loadable?() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 51
def self.loadable?
  orm_class
rescue NameError
  false
end
new(configuration) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 7
def initialize(configuration)
  ensure_loadable
  self.configuration = configuration
  self.settings = configuration.settings
end

Public Instance Methods

create_callbacks!(klass) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 13
def create_callbacks!(klass)
  self.klass = klass
  self.callback_options = {}
  create_method_to_callback
  create_callback
end
ensure_unique_url!(instance) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 20
def ensure_unique_url!(instance)
  @url_owners = nil
  self.instance = instance

  handle_url!
  handle_blacklisted_url!
  handle_duplicate_url! unless settings.allow_duplicates
end
initialize_urls!(klass) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 29
def initialize_urls!(klass)
  self.klass = klass
  klass_previous_instances do |instance|
    ensure_unique_url_for! instance
  end
end
url_attribute(instance) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 36
def url_attribute(instance)
  # Retrieve from database record if there are errors on attribute_to_urlify
  if !is_new?(instance) && is_present?(instance.errors[settings.attribute_to_urlify])
    self.instance = instance
    read_attribute instance_from_db, settings.url_attribute
  else
    read_attribute instance, settings.url_attribute
  end
end

Private Instance Methods

add_new_record_url_owner_conditions() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 59
def add_new_record_url_owner_conditions
  return if is_new?(instance)
  @url_owner_conditions.first << " and #{primary_key} != ?"
  @url_owner_conditions << instance.id
end
add_scoped_url_owner_conditions() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 65
def add_scoped_url_owner_conditions
  [settings.scope_for_url].flatten.compact.each do |scope|
    @url_owner_conditions.first << " and #{scope} = ?"
    @url_owner_conditions << instance.send(scope)
  end
end
create_callback() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 72
def create_callback
  klass.send klass_callback_method, :ensure_unique_url, callback_options
end
create_method_to_callback() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 94
def create_method_to_callback
  klass.class_eval <<-"END"
    def #{settings.url_attribute}
      acts_as_url_configuration.adapter.url_attribute self
    end
  END
end
duplicate_for_base_url(n) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 102
def duplicate_for_base_url(n)
  "#{base_url}#{settings.duplicate_count_separator}#{n}"
end
ensure_loadable() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 106
def ensure_loadable
  self.class.ensure_loadable
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, line 112
def ensure_unique_url_for!(instance)
  instance.send :ensure_unique_url
  instance.save
end
get_base_url_owner_conditions() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 117
def get_base_url_owner_conditions
  @url_owner_conditions = ["#{settings.url_attribute} LIKE ?", base_url + '%']
end
handle_blacklisted_url!() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 144
def handle_blacklisted_url!
  return unless settings.blacklist.to_set.include?(base_url)
  self.base_url = settings.blacklist_policy.call(instance, base_url)
  write_url_attribute base_url
end
handle_duplicate_url!() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 121
def handle_duplicate_url!
  return if !url_taken?(base_url)
  n = 1
  while url_taken?(duplicate_for_base_url(n))
    n = n.succ
  end
  write_url_attribute duplicate_for_base_url(n)
end
handle_url!() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 138
def handle_url!
  self.base_url = instance.send(settings.url_attribute)
  modify_base_url if is_blank?(base_url) || !settings.only_when_blank
  write_url_attribute base_url
end
instance_from_db() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 150
def instance_from_db
  instance.class.find(instance.id)
end
is_blank?(object) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 154
def is_blank?(object)
  object.blank?
end
is_new?(object) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 158
def is_new?(object)
  object.new_record?
end
is_present?(object) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 162
def is_present?(object)
  object.present?
end
klass_callback_method() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 76
def klass_callback_method
  settings.sync_url ? klass_sync_url_callback_method : klass_non_sync_url_callback_method
end
klass_non_sync_url_callback_method() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 84
def klass_non_sync_url_callback_method
  case configuration.settings.callback_method
  when :before_save
    :before_create
  else # :before_validation
    callback_options[:on] = :create
    configuration.settings.callback_method
  end
end
klass_sync_url_callback_method() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 80
def klass_sync_url_callback_method
  configuration.settings.callback_method
end
loadable?() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 166
def loadable?
  self.class.loadable?
end
modify_base_url() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 170
def modify_base_url
  root = instance.send(settings.attribute_to_urlify).to_s
  self.base_url = root.to_url(configuration.string_extensions_settings)
end
orm_class() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 175
def orm_class
  self.class.orm_class
end
primary_key() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 179
def primary_key
  instance.class.primary_key
end
read_attribute(instance, attribute) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 183
def read_attribute(instance, attribute)
  instance.read_attribute attribute
end
url_attribute_for(object) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 187
def url_attribute_for(object)
  object.send settings.url_attribute
end
url_owner_conditions() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 191
def url_owner_conditions
  get_base_url_owner_conditions
  add_new_record_url_owner_conditions
  add_scoped_url_owner_conditions

  @url_owner_conditions
end
url_owners() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 199
def url_owners
  @url_owners ||= url_owners_class.unscoped.where(url_owner_conditions).to_a
end
url_owners_class() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 203
def url_owners_class
  return instance.class unless settings.enforce_uniqueness_on_sti_base_class

  klass = instance.class
  while klass.superclass < orm_class
    klass = klass.superclass
  end
  klass
end
url_taken?(url) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 130
def url_taken?(url)
  if settings.url_taken_method
    instance.send(settings.url_taken_method, url)
  else
    url_owners.any?{|owner| url_attribute_for(owner) == url}
  end
end
write_attribute(instance, attribute, value) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 213
def write_attribute(instance, attribute, value)
  instance.send :write_attribute, attribute, value
end
write_url_attribute(value) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 217
def write_url_attribute(value)
  write_attribute instance, settings.url_attribute, value
end