Sha256: 3bbcd3bc5ab6146c8dae385090791c79c57d0403409e917b1302801cec671cd4

Contents?: true

Size: 1.03 KB

Versions: 3

Compression:

Stored size: 1.03 KB

Contents

module KirguduBase
	module Concerns::Models::Slug
		extend ActiveSupport::Concern

		included do
			############################ FILTERS ###############################
			before_query :query_add_slug
			before_validation :normalize_slug

			########################## VALIDATIONS ###############################

			validates_presence_of :slug
			validates_length_of :slug, maximum: 128, allow_blank: true
			validates_slug_format_of :slug, allow_blank: true

			########################## SCOPES ###############################

			scope :with_slug, lambda {
				|value|
				like_value = "%#{value.gsub('%', ' ').strip}%" rescue nil
				where { (slug.like like_value) } if like_value
			}
		end


		########################## INSTANCE METHODS ###############################

		def normalize_slug
			self.slug = self.slug.downcase.strip if self.slug
		end

		########################## CLASS METHODS ###############################

		module ClassMethods
			def query_add_slug(query, filters)
				query.with_slug(filters[:slug])
			end
		end

	end
end


Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kirgudu_base-0.0.25 app/models/kirgudu_base/concerns/models/slug.rb
kirgudu_base-0.0.23 app/models/kirgudu_base/concerns/models/slug.rb
kirgudu_base-0.0.22 app/models/kirgudu_base/concerns/models/slug.rb