Sha256: 657cce898eddad032e8bb74f1239423f3cab36829c3ddf3e5f87da0ddb4dac11

Contents?: true

Size: 665 Bytes

Versions: 8

Compression:

Stored size: 665 Bytes

Contents

module BitCore
  # A section of an application.
  class Tool < ActiveRecord::Base
    has_many :content_modules,
             class_name: "BitCore::ContentModule",
             foreign_key: :bit_core_tool_id,
             inverse_of: :tool,
             dependent: :destroy

    validates :title, :position, presence: true
    validates :position,
              uniqueness: true,
              numericality: { greater_than_or_equal_to: 0 }

    def add_module(title)
      content_modules.create(title: title, position: last_position + 1)
    end

    private

    def last_position
      content_modules.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/tool.rb
bit_core-1.1.5 app/models/bit_core/tool.rb
bit_core-1.1.4 app/models/bit_core/tool.rb
bit_core-1.1.3 app/models/bit_core/tool.rb
bit_core-1.1.2 app/models/bit_core/tool.rb
bit_core-1.1.1 app/models/bit_core/tool.rb
bit_core-1.1.0 app/models/bit_core/tool.rb
bit_core-1.0.0 app/models/bit_core/tool.rb