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 45
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 50
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_duplicate_url! unless settings.allow_duplicates
end
initialize_urls!(klass) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 28
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 35
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 58
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 64
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 71
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 93
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 101
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 105
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 111
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 116
def get_base_url_owner_conditions
  @url_owner_conditions = ["#{settings.url_attribute} LIKE ?", base_url + '%']
end
handle_duplicate_url!() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 120
def handle_duplicate_url!
  return if url_owners.none?{|owner| url_attribute_for(owner) == base_url}
  n = 1
  while url_owners.any?{|owner| url_attribute_for(owner) == 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 129
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 135
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 139
def is_blank?(object)
  object.blank?
end
is_new?(object) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 143
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 147
def is_present?(object)
  object.present?
end
klass_callback_method() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 75
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 83
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 79
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 151
def loadable?
  self.class.loadable?
end
modify_base_url() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 155
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 160
def orm_class
  self.class.orm_class
end
primary_key() click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 164
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 168
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 172
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 176
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 184
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 188
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
write_attribute(instance, attribute, value) click to toggle source
# File lib/stringex/acts_as_url/adapter/base.rb, line 198
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 202
def write_url_attribute(value)
  write_attribute instance, settings.url_attribute, value
end