Sha256: 81d75d7f791059b945b13a57e9c8be9b84bc8984d99b55920c851705180c18f6

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

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

    validates :title, :tool, :position, presence: true
    validates :position, numericality: { greater_than_or_equal_to: 1 }
    validates :position, uniqueness: { scope: :bit_player_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

4 entries across 4 versions & 1 rubygems

Version Path
bit_player-0.3.0 app/models/bit_player/content_module.rb
bit_player-0.2.5 app/models/bit_player/content_module.rb
bit_player-0.2.3 app/models/bit_player/content_module.rb
bit_player-0.2.2 app/models/bit_player/content_module.rb