Class: Permalink
- Inherits:
-
Object
- Object
- Permalink
- Includes:
- Mongoid::Document, Mongoid::Timestamps
- Defined in:
- app/models/permalink.rb
Class Method Summary collapse
-
.dispatch(path, options = {}) ⇒ Object
Returns a dispatcher object for given path.
-
.for_linkable(object) ⇒ Object
Scope method for finding Permalinks for given object.
- .for_scope(scope) ⇒ Object
-
.for_value(value, do_sanitize = true) ⇒ Object
Scope method for finding Permalinks for given value.
-
.remove_stopwords(string) ⇒ Object
Tries to remove stopwords from string.
-
.sanitize(string, keep_stopwords = false) ⇒ Object
Sanitizes string: Remove stopwords and format as permalink.
- .scope_list(scope) ⇒ Object
Instance Method Summary collapse
-
#current ⇒ Object
Returns the current permalink of the assigned linkable.
-
#current! ⇒ Object
Sets this permalink as the current one.
-
#current? ⇒ Boolean
Returns true if this permalink is the current one of the assigned linkable.
-
#sanitize_value! ⇒ Object
Sanitizes and increments string, if necessary.
- #scope=(scope) ⇒ Object
Class Method Details
.dispatch(path, options = {}) ⇒ Object
Returns a dispatcher object for given path.
90 91 92 |
# File 'app/models/permalink.rb', line 90 def dispatch(path, = {}) Vidibus::Permalink::Dispatcher.new(path, ) end |
.for_linkable(object) ⇒ Object
Scope method for finding Permalinks for given object.
67 68 69 |
# File 'app/models/permalink.rb', line 67 def for_linkable(object) where(linkable_id: object.id, linkable_type: object.class.to_s) end |
.for_scope(scope) ⇒ Object
84 85 86 87 |
# File 'app/models/permalink.rb', line 84 def for_scope(scope) return all unless scope all_in(scope: scope_list(scope)) end |
.for_value(value, do_sanitize = true) ⇒ Object
Scope method for finding Permalinks for given value. The value will be sanitized.
73 74 75 76 77 78 79 80 81 82 |
# File 'app/models/permalink.rb', line 73 def for_value(value, do_sanitize = true) if do_sanitize self.or([ {value: /^#{sanitize(value)}(-\d+)?$/}, {value: /^#{sanitize(value, true)}(-\d+)?$/} ]) else where(value: /^#{value}(-\d+)?$/) end end |
.remove_stopwords(string) ⇒ Object
Tries to remove stopwords from string. If the resulting string is blank, the original one will be returned. See Vidibus::Words for details.
127 128 129 130 131 |
# File 'app/models/permalink.rb', line 127 def self.remove_stopwords(string) words = Vidibus::Words.new(string) clean = words.keywords(10).join(" ") clean.blank? ? string : clean end |
.sanitize(string, keep_stopwords = false) ⇒ Object
Sanitizes string: Remove stopwords and format as permalink. See Vidibus::CoreExtensions::String for details.
96 97 98 99 100 |
# File 'app/models/permalink.rb', line 96 def sanitize(string, keep_stopwords = false) return if string.blank? string = remove_stopwords(string) unless keep_stopwords string.permalink end |
.scope_list(scope) ⇒ Object
102 103 104 105 106 |
# File 'app/models/permalink.rb', line 102 def scope_list(scope) return [] unless scope return scope if scope.kind_of?(Array) scope.map { |key, value| "#{key}:#{value}" } end |
Instance Method Details
#current ⇒ Object
Returns the current permalink of the assigned linkable.
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/models/permalink.rb', line 45 def current @current ||= begin if current? self else Permalink.where({ linkable_id: linkable.id, linkable_type: linkable.class.to_s, _current: true }).first end end end |
#current! ⇒ Object
Sets this permalink as the current one. All other permalinks of this linkable will be updated after saving.
61 62 63 |
# File 'app/models/permalink.rb', line 61 def current! self._current = true end |
#current? ⇒ Boolean
Returns true if this permalink is the current one of the assigned linkable.
40 41 42 |
# File 'app/models/permalink.rb', line 40 def current? !!_current end |
#sanitize_value! ⇒ Object
Sanitizes and increments string, if necessary.
20 21 22 23 24 25 26 27 28 |
# File 'app/models/permalink.rb', line 20 def sanitize_value! return true unless value_changed? || new_record? string = sanitize(value) if string != value_was string = increment(string) end self.value = string true end |
#scope=(scope) ⇒ Object
30 31 32 33 34 35 36 |
# File 'app/models/permalink.rb', line 30 def scope=(scope) if array = scope array = self.class.scope_list(scope) self.write_attribute(:scope, array) end array end |