Sha256: 7d2b8607480dfac773c9fe24cecdc0f16270e495f64961f0b9bd34b9ffd81bdf

Contents?: true

Size: 948 Bytes

Versions: 3

Compression:

Stored size: 948 Bytes

Contents

module BitCore
  # A section of an application.
  class Tool < ActiveRecord::Base
    belongs_to :arm

    has_many :content_modules,
             class_name: "BitCore::ContentModule",
             foreign_key: :bit_core_tool_id,
             inverse_of: :tool,
             dependent: :destroy

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

    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

3 entries across 3 versions & 1 rubygems

Version Path
bit_core-2.0.0.beta1 app/models/bit_core/tool.rb
bit_core-1.4.6 app/models/bit_core/tool.rb
bit_core-1.4.5 app/models/bit_core/tool.rb