Sha256: 2169b6a5a93763526ceebe0b544d511c34f892f7cd87160e1453c40e62bb51a7

Contents?: true

Size: 1.25 KB

Versions: 14

Compression:

Stored size: 1.25 KB

Contents

module Ecm::Courses
  class CourseCategory < ApplicationRecord
    module Icalendar
      def to_icalendar
        calendar = ::Icalendar::Calendar.new
        courses.map { |c| c.to_icalendar_events(calendar) }
        calendar
      end
    end

    include Icalendar

    # associations
    has_many :courses, ->() { order(:position) },
             dependent: :destroy

    # awesome nested set
    acts_as_nested_set
    default_scope { order('lft ASC')  }

    # friendly id
    extend FriendlyId
    friendly_id :name, use: [:slugged]

    # validations
    validates :name,   presence: true, uniqueness: { scope: [:parent_id] }
    validates :locale, presence: true, if: :root?
    validates :locale, inclusion: { in: I18n.available_locales.map(&:to_s) }, allow_blank: true
    validates :locale, absence: true, unless: :root?

    def index_name
      if root?
        #{ }"[#{locale}] #{name}"
      else
        "#{'&#160;&#160;&#160;&#160;' * level} |--&#160;#{name}".html_safe
      end
    end

    def to_s
      name
    end

    def courses_count
      courses.count
    end

    def tree_name
      # root_prefix = root? ? "[#{locale}] " : ''

      if courses_count < 1
        self
      else
        "#{self} (#{courses_count})"
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
ecm_courses2-1.1.4 app/models/ecm/courses/course_category.rb
ecm_courses2-1.1.3 app/models/ecm/courses/course_category.rb
ecm_courses2-1.1.2 app/models/ecm/courses/course_category.rb
ecm_courses2-1.1.1 app/models/ecm/courses/course_category.rb
ecm_courses2-1.1.0 app/models/ecm/courses/course_category.rb
ecm_courses2-1.0.3 app/models/ecm/courses/course_category.rb
ecm_courses2-1.0.2 app/models/ecm/courses/course_category.rb
ecm_courses2-1.0.1 app/models/ecm/courses/course_category.rb
ecm_courses2-1.0.0 app/models/ecm/courses/course_category.rb
ecm_courses2-0.2.4 app/models/ecm/courses/course_category.rb
ecm_courses2-0.2.3 app/models/ecm/courses/course_category.rb
ecm_courses2-0.2.2 app/models/ecm/courses/course_category.rb
ecm_courses2-0.2.1 app/models/ecm/courses/course_category.rb
ecm_courses2-0.2.0 app/models/ecm/courses/course_category.rb