Class: UrlKeyedObject::ActiveRecord::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/url_keyed_object/active_record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, column, length) ⇒ Helper

Returns a new instance of Helper.



23
24
25
26
27
# File 'lib/url_keyed_object/active_record.rb', line 23

def initialize(klass, column, length)
  @klass = klass
  @column = column
  @length = length
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



21
22
23
# File 'lib/url_keyed_object/active_record.rb', line 21

def column
  @column
end

#klassObject (readonly)

Returns the value of attribute klass.



21
22
23
# File 'lib/url_keyed_object/active_record.rb', line 21

def klass
  @klass
end

#lengthObject (readonly)

Returns the value of attribute length.



21
22
23
# File 'lib/url_keyed_object/active_record.rb', line 21

def length
  @length
end

Instance Method Details

#generate_valid_url_key(instance) ⇒ Object



29
30
31
32
# File 'lib/url_keyed_object/active_record.rb', line 29

def generate_valid_url_key(instance)
  new_url_key = UrlKeyedObject.generate_checked_url_key(length) { |value| valid_url_key?(value) }
  instance.send(:write_attribute, column, new_url_key)
end

#valid_url_key?(url_key) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/url_keyed_object/active_record.rb', line 34

def valid_url_key?(url_key)
  klass.send("find_by_#{column}", url_key).nil?
end