Sha256: efa286632c228ad063a9727ac7ca3ad70530b47709162e3c45f033c310be761a

Contents?: true

Size: 847 Bytes

Versions: 5

Compression:

Stored size: 847 Bytes

Contents

module Binda
  class Category < ApplicationRecord

  	# Associations
  	belongs_to :structure
  	has_and_belongs_to_many :components

		# Validations
		validates :name, presence: true

  	# Slug
		extend FriendlyId
		friendly_id :default_slug, use: [:slugged, :finders]


		# Friendly id preference on slug generation
		#
		# Method inherited from friendly id 
		# @see https://github.com/norman/friendly_id/issues/436
	  def should_generate_new_friendly_id?
	    slug.blank? || name_changed?
	  end

		# Set slug name
		#
		# It generates 4 possible slugs before falling back to FriendlyId default behaviour
	  def default_slug
	  	[ "#{ self.structure.name }-#{ self.name }",
	  		"#{ self.structure.name }-#{ self.name }-1",
	  		"#{ self.structure.name }-#{ self.name }-2",
	  		"#{ self.structure.name }-#{ self.name }-3" ]
	  end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
binda-0.1.3 app/models/binda/category.rb
binda-0.1.2 app/models/binda/category.rb
binda-0.1.1 app/models/binda/category.rb
binda-0.1.0 app/models/binda/category.rb
binda-0.0.7 app/models/binda/category.rb