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(
74     klass_callback_method,
75     :ensure_unique_url,
76     **callback_options
77   )
78 end
create_method_to_callback() click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
 98         def create_method_to_callback
 99           klass.class_eval <<-"END"
100             def #{settings.url_attribute}
101               acts_as_url_configuration.adapter.url_attribute self
102             end
103           END
104         end
duplicate_for_base_url(n) click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
106 def duplicate_for_base_url(n)
107   "#{base_url}#{settings.duplicate_count_separator}#{n}"
108 end
duplicate_url_sequence() click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
136 def duplicate_url_sequence
137   settings.duplicate_sequence ||
138     Enumerator.new do |enum|
139       n = 1
140       loop do
141         enum.yield n
142         n += 1
143       end
144     end
145 end
ensure_loadable() click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
110 def ensure_loadable
111   self.class.ensure_loadable
112 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
116 def ensure_unique_url_for!(instance)
117   instance.send :ensure_unique_url
118   instance.save
119 end
get_base_url_owner_conditions() click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
121 def get_base_url_owner_conditions
122   @url_owner_conditions = ["#{settings.url_attribute} LIKE ?", base_url + '%']
123 end
handle_blacklisted_url!() click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
161 def handle_blacklisted_url!
162   return unless settings.blacklist.to_set.include?(base_url)
163   self.base_url = settings.blacklist_policy.call(instance, base_url)
164   write_url_attribute base_url
165 end
handle_duplicate_url!() click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
125 def handle_duplicate_url!
126   return if !url_taken?(base_url)
127   n = nil
128   sequence = duplicate_url_sequence.tap(&:rewind)
129   loop do
130     n = sequence.next
131     break unless url_taken?(duplicate_for_base_url(n))
132   end
133   write_url_attribute duplicate_for_base_url(n)
134 end
handle_url!() click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
155 def handle_url!
156   self.base_url = instance.send(settings.url_attribute)
157   modify_base_url if is_blank?(base_url) || !settings.only_when_blank
158   write_url_attribute base_url
159 end
instance_from_db() click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
167 def instance_from_db
168   instance.class.find(instance.id)
169 end
is_blank?(object) click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
171 def is_blank?(object)
172   object.blank?
173 end
is_new?(object) click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
175 def is_new?(object)
176   object.new_record?
177 end
is_present?(object) click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
179 def is_present?(object)
180   object.present?
181 end
klass_callback_method() click to toggle source
   # File lib/stringex/acts_as_url/adapter/base.rb
80 def klass_callback_method
81   settings.sync_url ? klass_sync_url_callback_method : klass_non_sync_url_callback_method
82 end
klass_non_sync_url_callback_method() click to toggle source
   # File lib/stringex/acts_as_url/adapter/base.rb
88 def klass_non_sync_url_callback_method
89   case configuration.settings.callback_method
90   when :before_save
91     :before_create
92   else # :before_validation
93     callback_options[:on] = :create
94     configuration.settings.callback_method
95   end
96 end
klass_sync_url_callback_method() click to toggle source
   # File lib/stringex/acts_as_url/adapter/base.rb
84 def klass_sync_url_callback_method
85   configuration.settings.callback_method
86 end
loadable?() click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
183 def loadable?
184   self.class.loadable?
185 end
modify_base_url() click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
187 def modify_base_url
188   root = instance.send(settings.attribute_to_urlify).to_s
189   self.base_url = root.to_url(configuration.string_extensions_settings)
190 end
orm_class() click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
192 def orm_class
193   self.class.orm_class
194 end
primary_key() click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
196 def primary_key
197   instance.class.primary_key
198 end
read_attribute(instance, attribute) click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
200 def read_attribute(instance, attribute)
201   instance.read_attribute attribute
202 end
url_attribute_for(object) click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
204 def url_attribute_for(object)
205   object.send settings.url_attribute
206 end
url_owner_conditions() click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
208 def url_owner_conditions
209   get_base_url_owner_conditions
210   add_new_record_url_owner_conditions
211   add_scoped_url_owner_conditions
212 
213   @url_owner_conditions
214 end
url_owners() click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
216 def url_owners
217   @url_owners ||= url_owners_class.unscoped.where(url_owner_conditions).to_a
218 end
url_owners_class() click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
220 def url_owners_class
221   return instance.class unless settings.enforce_uniqueness_on_sti_base_class
222 
223   klass = instance.class
224   while klass.superclass < orm_class
225     klass = klass.superclass
226   end
227   klass
228 end
url_taken?(url) click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
147 def url_taken?(url)
148   if settings.url_taken_method
149     instance.send(settings.url_taken_method, url)
150   else
151     url_owners.any?{|owner| url_attribute_for(owner) == url}
152   end
153 end
write_attribute(instance, attribute, value) click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
230 def write_attribute(instance, attribute, value)
231   instance.send :write_attribute, attribute, value
232 end
write_url_attribute(value) click to toggle source
    # File lib/stringex/acts_as_url/adapter/base.rb
234 def write_url_attribute(value)
235   write_attribute instance, settings.url_attribute, value
236 end