Sha256: bff0ac66589379d55d740310cca717bd900cbf9bad30eb44e46c54cd92c6a962

Contents?: true

Size: 1.12 KB

Versions: 15

Compression:

Stored size: 1.12 KB

Contents

module ClarkKent
	class ReportColumn < ActiveRecord::Base
	  include ClarkKent::Cloneable

		SummaryMethods = ['total','average']

		belongs_to :report

		scope :sorted, -> { order("clark_kent_report_columns.column_order") }

		def report_sort_pretty
			{'ascending' => 'A->Z','descending' => 'Z->A'}[self.report_sort]
		end

		def calculate_summary(values)
			return nil unless self.summary_method.present?
			calculator.new(values).calculate
		end

		def summarizable?
			report.column_options_for(self.column_name.to_sym).summarizable
		end

		def calculator
			('ClarkKent::' + summary_method.camelcase + 'Calculator').constantize
		end

    def name
      column_name
    end

    def link
      report.column_options_for(self.name.to_sym).link
    end
	end

	class AbstractCalculator
    def initialize values
    	@values = values
    end
	end

	class TotalCalculator < AbstractCalculator
    def calculate
    	@values.sum
    end
	end

	class AverageCalculator < AbstractCalculator
    def calculate
    	if @values.length < 1
        return nil
      else
        return (@values.sum.to_f / @values.length)
      end
    end
	end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
clark_kent-0.3.2 app/models/clark_kent/report_column.rb
clark_kent-0.3.1 app/models/clark_kent/report_column.rb
clark_kent-0.3.0 app/models/clark_kent/report_column.rb
clark_kent-0.2.10 app/models/clark_kent/report_column.rb
clark_kent-0.2.8 app/models/clark_kent/report_column.rb
clark_kent-0.2.7 app/models/clark_kent/report_column.rb
clark_kent-0.2.6 app/models/clark_kent/report_column.rb
clark_kent-0.2.5 app/models/clark_kent/report_column.rb
clark_kent-0.2.4 app/models/clark_kent/report_column.rb
clark_kent-0.2.3 app/models/clark_kent/report_column.rb
clark_kent-0.2.2 app/models/clark_kent/report_column.rb
clark_kent-0.2.1 app/models/clark_kent/report_column.rb
clark_kent-0.2.0 app/models/clark_kent/report_column.rb
clark_kent-0.1.7 app/models/clark_kent/report_column.rb
clark_kent-0.1.6 app/models/clark_kent/report_column.rb