Sha256: a84eabb628338b021d7e98318c73cb844a1fee6c7cc5193318178c4744a5efc3

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module BitCore
  # A logical unit of content, possibly containing mixed provider types.
  class ContentModule < ActiveRecord::Base
    belongs_to :tool,
               class_name: "BitCore::Tool",
               foreign_key: :bit_core_tool_id
    has_many :content_providers,
             class_name: "BitCore::ContentProvider",
             foreign_key: :bit_core_content_module_id,
             inverse_of: :content_module,
             dependent: :destroy

    validates :title, :tool, :position, presence: true
    validates :position,
              numericality: { greater_than_or_equal_to: 1 },
              uniqueness: { scope: :bit_core_tool_id }

    # Returns the `ContentProvider` at the given position, or a `Null`
    # `ContentProvider` if none exists.
    def provider(position)
      content_providers.where(position: position).first ||
        ContentProviders::Null.new(self, position)
    end

    def provider_exists?(position)
      content_providers.exists?(position: position)
    end

    def add_content_provider(type)
      content_providers.build(
        type: type,
        position: next_position,
        show_next_nav: true
      )
    end

    private

    def next_position
      last_position + 1
    end

    def last_position
      content_providers.order(:position).last.try(:position) || 0
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bit_core-1.2.0 app/models/bit_core/content_module.rb