Sha256: fff3eabcb9d8af0d92cb958c7544df67d2a082c63b7176c358dc25c077df8cc9

Contents?: true

Size: 907 Bytes

Versions: 1

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

    private

    def next_position
      last_position + 1
    end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bit_core-1.2.0 app/models/bit_core/tool.rb