Add progress bar model
This commit is contained in:
@@ -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
|
||||
|
||||
28
app/models/progress_bar.rb
Normal file
28
app/models/progress_bar.rb
Normal 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
|
||||
3
app/models/progress_bar/translation.rb
Normal file
3
app/models/progress_bar/translation.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class ProgressBar::Translation < Globalize::ActiveRecord::Translation
|
||||
delegate :primary?, to: :globalized_model
|
||||
end
|
||||
Reference in New Issue
Block a user