Sha256: 14f4debc1b29109ca412636635224cfe2b9eeff26594e335b1b4b4fbf2bf3673

Contents?: true

Size: 1.11 KB

Versions: 8

Compression:

Stored size: 1.11 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 }

    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.create(type: type, position: last_position + 1)
    end

    private

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
bit_core-1.1.6 app/models/bit_core/content_module.rb
bit_core-1.1.5 app/models/bit_core/content_module.rb
bit_core-1.1.4 app/models/bit_core/content_module.rb
bit_core-1.1.3 app/models/bit_core/content_module.rb
bit_core-1.1.2 app/models/bit_core/content_module.rb
bit_core-1.1.1 app/models/bit_core/content_module.rb
bit_core-1.1.0 app/models/bit_core/content_module.rb
bit_core-1.0.0 app/models/bit_core/content_module.rb