Sha256: 2059f08e7790b76415c5793bcf0775dcc9435dcff8c20494c22da5be2ec1ebe4

Contents?: true

Size: 889 Bytes

Versions: 8

Compression:

Stored size: 889 Bytes

Contents

module Binda
  class Category < ApplicationRecord

  	# Associations
  	belongs_to :structure
  	has_and_belongs_to_many :components

		# Validations
		validates :name, presence: true
		validates :structure_id, 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

8 entries across 8 versions & 1 rubygems

Version Path
binda-0.1.11 app/models/binda/category.rb
binda-0.1.10 app/models/binda/category.rb
binda-0.1.9 app/models/binda/category.rb
binda-0.1.8 app/models/binda/category.rb
binda-0.1.7 app/models/binda/category.rb
binda-0.1.6 app/models/binda/category.rb
binda-0.1.5 app/models/binda/category.rb
binda-0.1.4 app/models/binda/category.rb