Sha256: 23a91c0a08de7460e738adf132cf93221eb21448fb4e526b4245d578acbe2b5d

Contents?: true

Size: 1.32 KB

Versions: 5

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

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

    private

    def next_position
      last_position + 1
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
bit_core-2.0.0.beta1 app/models/bit_core/content_module.rb
bit_core-1.4.6 app/models/bit_core/content_module.rb
bit_core-1.4.5 app/models/bit_core/content_module.rb
bit_core-1.2.2 app/models/bit_core/content_module.rb
bit_core-1.2.1 app/models/bit_core/content_module.rb