Add progress bar model

This commit is contained in:
Javi Martín
2019-01-04 14:42:46 +01:00
committed by decabeza
parent c3c9b02220
commit eaea95ccfb
9 changed files with 205 additions and 1 deletions

View File

@@ -5,5 +5,7 @@ module Milestoneable
has_many :milestones, as: :milestoneable, dependent: :destroy
scope :with_milestones, -> { joins(:milestones).distinct }
has_many :progress_bars, as: :progressable
end
end

View File

@@ -0,0 +1,28 @@
class ProgressBar < ActiveRecord::Base
self.inheritance_column = nil
RANGE = 0..100
enum kind: %i[primary secondary]
belongs_to :progressable, polymorphic: true
translates :title, touch: true
include Globalizable
validates :progressable, presence: true
validates :kind, presence: true,
uniqueness: {
scope: [:progressable_type, :progressable_id],
conditions: -> { primary }
}
validates :percentage, presence: true, inclusion: RANGE, numericality: { only_integer: true }
before_validation :assign_progress_bar_to_translations
validates_translation :title, presence: true, unless: :primary?
private
def assign_progress_bar_to_translations
translations.each { |translation| translation.globalized_model = self }
end
end

View File

@@ -0,0 +1,3 @@
class ProgressBar::Translation < Globalize::ActiveRecord::Translation
delegate :primary?, to: :globalized_model
end