Class: Effective::Report

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/effective/report.rb

Constant Summary collapse

DATATYPES =
[:boolean, :date, :decimal, :integer, :price, :string, :belongs_to, :belongs_to_polymorphic, :has_many, :has_one]
OPERATIONS =

Arel::Predications.instance_methods

[:eq, :not_eq, :matches, :does_not_match, :starts_with, :ends_with, :gt, :gteq, :lt, :lteq, :sql]

Instance Method Summary collapse

Instance Method Details

#collectionObject

The klass to base the collection from



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/effective/report.rb', line 72

def collection
  collection = reportable.all

  # Apply Scopes
  report_scopes.each do |scope|
    collection = (scope.value.nil? ? collection.send(scope.name) : collection.send(scope.name, scope.value))
  end

  # Apply Includes
  report_columns.select(&:as_associated?).each do |column|
    collection = collection.includes(column.name)
  end

  # Apply Filters
  report_columns.select(&:filter?).each do |column|
    collection = Resource.new(collection).search(column.name, column.value, operation: column.operation)
  end

  collection
end

#filtered_report_columnsObject



41
42
43
# File 'app/models/effective/report.rb', line 41

def filtered_report_columns
  report_columns.select(&:filter?)
end

#reportableObject



37
38
39
# File 'app/models/effective/report.rb', line 37

def reportable
  reportable_class_name.constantize if reportable_class_name.present?
end

#reportable_attributesObject

Used to build the Reports form { id: :integer, archived: :boolean }



47
48
49
50
51
52
53
54
55
56
# File 'app/models/effective/report.rb', line 47

def reportable_attributes
  attributes = Hash((reportable.new.reportable_attributes if reportable))

  attributes.each do |attribute, type|
    raise("#{reportable}.reportable_attributes #{attribute} => #{type || 'nil'} is invalid. Key must be a symbol") unless attribute.kind_of?(Symbol)
    raise("#{reportable}.reportable_attributes :#{attribute} => #{type || 'nil'} is invalid. Value must be one of #{DATATYPES.map { |s| ":#{s}"}.join(', ')}") unless DATATYPES.include?(type)
  end

  attributes
end

#reportable_scopesObject

{ active: nil, inactive: nil, with_first_name: :string, not_in_good_standing: :boolean }



59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/effective/report.rb', line 59

def reportable_scopes
  scopes = Hash((reportable.new.reportable_scopes if reportable))

  scopes.each do |scope, type|
    raise("#{reportable}.reportable_scopes #{scope} => #{type || 'nil'} is invalid. Key must be a symbol") unless scope.kind_of?(Symbol)
    raise("#{reportable}.reportable_scopes :#{scope} => #{type || 'nil'} is invalid. Value must be one of #{DATATYPES.map { |s| ":#{s}"}.join(', ')}") if type.present? && !DATATYPES.include?(type)
    raise("#{reportable} must respond to reportable scope :#{name}") unless reportable.respond_to?(scope)
  end

  scopes
end

#to_sObject



33
34
35
# File 'app/models/effective/report.rb', line 33

def to_s
  title.presence || 'report'
end