Sha256: d355abf98937bd03e1238cb74e2b91e6a07d41699e44311923d978aa5da9074f

Contents?: true

Size: 907 Bytes

Versions: 2

Compression:

Stored size: 907 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_or_module)
      if title_or_module.class == String
        content_modules.build(title: title_or_module, position: next_position)
      else
        title_or_module.tap do |m|
          m.tool = self
          m.position = next_position
        end
      end
    end

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

    private

    def next_position
      last_position + 1
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bit_core-1.2.2 app/models/bit_core/tool.rb
bit_core-1.2.1 app/models/bit_core/tool.rb