Class: UrlKeyedObject::ActiveRecord::Helper
- Inherits:
-
Object
- Object
- UrlKeyedObject::ActiveRecord::Helper
- Defined in:
- lib/url_keyed_object/active_record.rb
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
Instance Method Summary collapse
- #generate_valid_url_key(instance) ⇒ Object
-
#initialize(klass, column, length) ⇒ Helper
constructor
A new instance of Helper.
- #valid_url_key?(url_key) ⇒ Boolean
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
#column ⇒ Object (readonly)
Returns the value of attribute column.
21 22 23 |
# File 'lib/url_keyed_object/active_record.rb', line 21 def column @column end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
21 22 23 |
# File 'lib/url_keyed_object/active_record.rb', line 21 def klass @klass end |
#length ⇒ Object (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
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 |