BindaCMS/binda

View on GitHub
app/models/binda/board.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Binda
  class Board < ApplicationRecord

      include FieldableAssociations

        belongs_to :structure, required: true

        validates :name, presence: true
        validates :slug, uniqueness: true

        # Slug
        extend FriendlyId
        friendly_id :name, 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?
        end

        def self.remove_orphans
            Board
                .includes(:structure)
                .where(binda_structures: {id: nil})
                .each do |b|
                b.destroy!
                puts "Binda::Board with id ##{b.id} successfully destroyed"
            end
    end

    # Create field instances for the current component
    def create_field_instances
      CreateFieldInstancesJob.perform_later self
    end

  end
end