Class: Stringex::ActsAsUrl::Adapter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/stringex/acts_as_url/adapter/base.rb

Direct Known Subclasses

ActiveRecord, DataMapper, Mongoid

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Base

Returns a new instance of Base.


7
8
9
10
11
# File 'lib/stringex/acts_as_url/adapter/base.rb', line 7

def initialize(configuration)
  ensure_loadable
  self.configuration = configuration
  self.settings = configuration.settings
end

Instance Attribute Details

#base_urlObject

Returns the value of attribute base_url


5
6
7
# File 'lib/stringex/acts_as_url/adapter/base.rb', line 5

def base_url
  @base_url
end

#callback_optionsObject

Returns the value of attribute callback_options


5
6
7
# File 'lib/stringex/acts_as_url/adapter/base.rb', line 5

def callback_options
  @callback_options
end

#configurationObject

Returns the value of attribute configuration


5
6
7
# File 'lib/stringex/acts_as_url/adapter/base.rb', line 5

def configuration
  @configuration
end

#instanceObject

Returns the value of attribute instance


5
6
7
# File 'lib/stringex/acts_as_url/adapter/base.rb', line 5

def instance
  @instance
end

#klassObject

Returns the value of attribute klass


5
6
7
# File 'lib/stringex/acts_as_url/adapter/base.rb', line 5

def klass
  @klass
end

#settingsObject

Returns the value of attribute settings


5
6
7
# File 'lib/stringex/acts_as_url/adapter/base.rb', line 5

def settings
  @settings
end

Class Method Details

.ensure_loadableObject


46
47
48
49
# 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?Boolean

Returns:

  • (Boolean)

51
52
53
54
55
# File 'lib/stringex/acts_as_url/adapter/base.rb', line 51

def self.loadable?
  orm_class
rescue NameError
  false
end

Instance Method Details

#create_callbacks!(klass) ⇒ Object


13
14
15
16
17
18
# 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) ⇒ Object


20
21
22
23
24
25
26
27
# 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) ⇒ Object


29
30
31
32
33
34
# 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) ⇒ Object


36
37
38
39
40
41
42
43
44
# 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