Module: Hc::Permalink
- Defined in:
- lib/hc/permalink.rb,
lib/hc/permalink/version.rb
Constant Summary collapse
- VERSION =
"0.1.3"[]
Class Method Summary collapse
- .from_string(value, remove_spaces: false) ⇒ Object
- .generate_unique(value:, target_class:, target_field: 'permalink', ignore_id: nil, constraint: nil) ⇒ Object
- .shared_resource_classes ⇒ Object
- .shared_resource_classes=(class_list) ⇒ Object
- .simple(value) ⇒ Object
Class Method Details
.from_string(value, remove_spaces: false) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/hc/permalink.rb', line 40 def self.from_string(value, remove_spaces: false) # Replace spaces and underscores with hyphens, then replace all # other common punctuation and symbols, then downcase the result # value = value.to_s.gsub(/[\s_]+/,'-').gsub(/[!"#$%&'\(\)*+,\.\/:;<=>?@\[\\\]\^_`{}¦|£¬~]+/, '').gsub(/[-]{2,}/, '-') value = value.gsub('-','') if remove_spaces # Attempt to Transliterate Russian and latin-based characters # with accents into their latin(ish) equivalents # value = I18n.transliterate(Russian::transliterate(value).gsub('ș', 's').gsub('ț', 't').gsub('ồ', 'o').gsub('ế', 'e').gsub('ộ', 'o').gsub('ạ', 'a')).downcase.gsub("?", "") # Return the resulting slug, but remove any trailing hyphens # return value[/[\w-]*\w/] end |
.generate_unique(value:, target_class:, target_field: 'permalink', ignore_id: nil, constraint: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/hc/permalink.rb', line 19 def self.generate_unique(value:, target_class:, target_field: 'permalink', ignore_id: nil, constraint: nil) # Create a base name which we will later increment # base_value = self.from_string(value, remove_spaces: true).to_s[0..63] current_value = base_value counter = 0 # Loop on finding objects with these parameters until no object found # while self.locate_object(target_class: target_class, target_field: target_field, target_value: current_value, ignore_id: ignore_id, constraint: constraint) counter += 1 current_value = "#{base_value[0..(63 - counter.to_s.length)]}#{counter}" end # Return the current generated value # return current_value end |
.shared_resource_classes ⇒ Object
15 16 17 |
# File 'lib/hc/permalink.rb', line 15 def self.shared_resource_classes @@shared_resource_classes.collect(&:constantize) end |
.shared_resource_classes=(class_list) ⇒ Object
10 11 12 13 |
# File 'lib/hc/permalink.rb', line 10 def self.shared_resource_classes=(class_list) raise "Class list must be an array of classes" unless class_list.is_a?(Array) @@shared_resource_classes = class_list.dup end |
.simple(value) ⇒ Object
59 60 61 |
# File 'lib/hc/permalink.rb', line 59 def self.simple(value) value.to_s.gsub(/[\s-]+/,'_').gsub(/[^\w]+/, '').downcase end |