module Vidibus::Permalink::Mongoid
Public Instance Methods
ensure_permalink_object()
click to toggle source
Ensure that a current permalink object exists for this record.
# File lib/vidibus/permalink/mongoid.rb, line 44 def ensure_permalink_object if permalink_repository permalink_object || begin obj = permalink_object_by_value(permalink) obj.sanitize_value! obj.current! obj.save end end end
permalink_object()
click to toggle source
Returns the current permalink object.
# File lib/vidibus/permalink/mongoid.rb, line 63 def permalink_object @permalink_object || if permalink_repository permalink_repository.for_linkable(self).where(_current: true).first end end
permalink_objects()
click to toggle source
Returns all permalink objects ordered by time of update.
# File lib/vidibus/permalink/mongoid.rb, line 70 def permalink_objects if permalink_repository permalink_repository.for_linkable(self).asc(:updated_at) end end
permalink_repository()
click to toggle source
Returns the defined permalink repository object.
# File lib/vidibus/permalink/mongoid.rb, line 56 def permalink_repository @permalink_repository ||= begin self.class.permalink_options[:repository] == false ? nil : ::Permalink end end
permalink_scope()
click to toggle source
Returns permalink scope.
# File lib/vidibus/permalink/mongoid.rb, line 77 def permalink_scope @permalink_scope ||= get_scope end
Private Instance Methods
destroy_permalink_objects()
click to toggle source
# File lib/vidibus/permalink/mongoid.rb, line 156 def destroy_permalink_objects if permalink_repository permalink_repository.where(linkable_id: id).delete end end
get_scope()
click to toggle source
# File lib/vidibus/permalink/mongoid.rb, line 98 def get_scope scope = self.class.permalink_options[:scope] return unless scope {}.tap do |hash| scope.each do |key, value| if value.kind_of?(String) hash[key] = value elsif value.kind_of?(Symbol) && respond_to?(value) hash[key] = send(value) else raise PermalinkConfigurationError.new( %Q{No scope value for key "#{key}" found.} ) end end end end
permalink_object_by_value(value)
click to toggle source
Returns a existing or new permalink object with wanted value. The permalink scope is also applied
# File lib/vidibus/permalink/mongoid.rb, line 85 def permalink_object_by_value(value) item = permalink_repository .for_linkable(self) .for_value(value) .for_scope(permalink_scope) .first item ||= permalink_repository.new({ value: value, scope: permalink_scope, linkable: self }) end
set_permalink(force = false)
click to toggle source
Initializes a new permalink object and sets permalink attribute.
# File lib/vidibus/permalink/mongoid.rb, line 117 def set_permalink(force = false) begin attribute_names = self.class.permalink_attributes rescue NoMethodError raise PermalinkConfigurationError.new("#{self.class}.permalink_attributes have not been assigned! Use #{self.class}.permalink(:my_field) to set it up.") end changed = false values = [] attribute_names.each do |name| changed ||= send("#{name}_changed?") values << send(name) end return unless permalink.blank? || changed || force value = values.join(' ') if permalink_repository @permalink_object = permalink_object_by_value(value) @permalink_object.sanitize_value! @permalink_object.current! self.permalink = @permalink_object.value else self.permalink = ::Permalink.sanitize(value) end if new_record? self.static_permalink = self.permalink else self.static_permalink ||= self.permalink end end
store_permalink_object()
click to toggle source
Stores current new permalink object or updates an existing one that matches.
# File lib/vidibus/permalink/mongoid.rb, line 149 def store_permalink_object return unless obj = @permalink_object @permalink_object = nil # reset to avoid storing it again obj.updated_at = Time.now obj.save! end