Module: Mongoid::Slug::ClassMethods
- Defined in:
- lib/mongoid/slug.rb
Instance Method Summary collapse
- #default_slug_url_builder ⇒ Object
-
#find_by_slug!(*args) ⇒ Array<Document>, Document
Find documents by slugs.
- #look_like_slugs?(*args) ⇒ Boolean
- #queryable ⇒ Object
- #slug(*fields, &block) ⇒ Object
-
#slug_scope_keys ⇒ Array<Document>, Document
Returns the scope keys for indexing, considering associations.
- #slug_scopes ⇒ Object
Instance Method Details
#default_slug_url_builder ⇒ Object
108 109 110 |
# File 'lib/mongoid/slug.rb', line 108 def default_slug_url_builder Mongoid::Slug.default_slug || ->(cur_object) { cur_object.slug_builder.to_url } end |
#find_by_slug!(*args) ⇒ Array<Document>, Document
Find documents by slugs.
A document matches if any of its slugs match one of the supplied params.
A document matching multiple supplied params will be returned only once.
If any supplied param does not match a document a Mongoid::Errors::DocumentNotFound will be raised.
152 153 154 |
# File 'lib/mongoid/slug.rb', line 152 def find_by_slug!(*args) with_default_scope.find_by_slug!(*args) end |
#look_like_slugs?(*args) ⇒ Boolean
112 113 114 |
# File 'lib/mongoid/slug.rb', line 112 def look_like_slugs?(*args) with_default_scope.look_like_slugs?(*args) end |
#queryable ⇒ Object
156 157 158 |
# File 'lib/mongoid/slug.rb', line 156 def queryable current_scope || Criteria.new(self) # Use Mongoid::Slug::Criteria for slugged documents. end |
#slug(*fields) { ... } ⇒ Object #slug(*fields, options) { ... } ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/mongoid/slug.rb', line 78 def slug(*fields, &block) = fields. self.slug_scope = [:scope] self.slug_index = [:index].nil? || [:index] self.slug_reserved_words = [:reserve] || Set.new(%w[new edit]) self.slugged_attributes = fields.map(&:to_s) self.slug_history = [:history] self.slug_by_model_type = [:by_model_type] self.slug_max_length = .key?(:max_length) ? [:max_length] : MONGO_INDEX_KEY_LIMIT_BYTES - 32 field :_slugs, type: Array, localize: [:localize] alias_attribute :slugs, :_slugs # Set indexes if slug_index && ! Mongoid::Slug::IndexBuilder.build_indexes(self, slug_scope_keys, slug_by_model_type, [:localize]) end self.slug_url_builder = block_given? ? block : default_slug_url_builder #-- always create slug on create #-- do not create new slug on update if the slug is permanent if [:permanent] set_callback :create, :before, :build_slug else set_callback :save, :before, :build_slug, if: :slug_should_be_rebuilt? end end |
#slug_scope_keys ⇒ Array<Document>, Document
Returns the scope keys for indexing, considering associations
125 126 127 128 129 130 131 132 133 |
# File 'lib/mongoid/slug.rb', line 125 def slug_scope_keys return nil unless slug_scope # If slug_scope is an array, we map over its elements to get each individual scope's key. slug_scopes.map do |individual_scope| # Attempt to find the association and get its key. If no association is found, use the scope as-is. reflect_on_association(individual_scope).try(:key) || individual_scope end end |
#slug_scopes ⇒ Object
116 117 118 119 120 |
# File 'lib/mongoid/slug.rb', line 116 def slug_scopes # If slug_scope is set (i.e., not nil), we convert it to an array to ensure we can handle it consistently. # If it's not set, we use an array with a single nil element, signifying no specific scope. slug_scope ? Array(slug_scope) : [nil] end |