Sha256: 61a96e62cefc0814905d6bed5c2b7b15cc7fe85b28f611517738f4d866a9356b

Contents?: true

Size: 1.53 KB

Versions: 11

Compression:

Stored size: 1.53 KB

Contents

module ForemanStatistics
  class FactTrend < Trend
    validates :trendable_id, :presence => true, :uniqueness => { :scope => %i[trendable_type fact_value] }, :allow_blank => false

    before_save :update_fact_name

    def to_label
      name.presence || fact_value || fact_name
    end

    def type_name
      if fact_value.blank?
        name.presence || fact_name
      else
        fact_name
      end
    end

    def create_values
      self.class.create_values(trendable_id)
    end

    def self.create_values(fact_name_id)
      FactValue.select('fact_name_id, value').group(:fact_name_id, :value).where(:fact_name_id => fact_name_id).includes(:fact_name).map do |fact|
        create(:trendable_type => 'FactName',
               :trendable_id => fact.fact_name.id,
               :fact_name => fact.fact_name.name,
               :fact_value => fact.value,
               :name => fact.value)
      end
    end

    def destroy_values
      ids = FactTrend.where(:trendable_id => trendable_id, :trendable_type => trendable_type).pluck(:id)
      super(ids)
    end

    def values
      return FactTrend.where(:id => self) if fact_value
      FactTrend.has_value.where(:trendable_type => trendable_type, :trendable_id => trendable_id)
    end

    def self.model_name
      Trend.model_name
    end

    def find_hosts
      Host.joins(:fact_values).where(:fact_values => { :value => fact_value }).order(:name)
    end

    private

    def update_fact_name
      self.fact_name = FactName.find(trendable_id).name if trendable_id
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
foreman_statistics-2.1.0 app/models/foreman_statistics/fact_trend.rb
foreman_statistics-2.0.1 app/models/foreman_statistics/fact_trend.rb
foreman_statistics-2.0.0 app/models/foreman_statistics/fact_trend.rb
foreman_statistics-1.2.0 app/models/foreman_statistics/fact_trend.rb
foreman_statistics-1.1.1 app/models/foreman_statistics/fact_trend.rb
foreman_statistics-1.1.0 app/models/foreman_statistics/fact_trend.rb
foreman_statistics-1.0.0 app/models/foreman_statistics/fact_trend.rb
foreman_statistics-0.1.3 app/models/foreman_statistics/fact_trend.rb
foreman_statistics-0.1.2 app/models/foreman_statistics/fact_trend.rb
foreman_statistics-0.1.1 app/models/foreman_statistics/fact_trend.rb
foreman_statistics-0.1.0 app/models/foreman_statistics/fact_trend.rb