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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/effective/report.rb', line 77

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

#email_report_columnObject



46
47
48
# File 'app/models/effective/report.rb', line 46

def email_report_column
  report_columns.find { |column| column.name == 'email' } || report_columns.find { |column| column.name.include?('email') }
end

#filtered_report_columnsObject



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

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

#reportableObject



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

def reportable
  reportable_class_name.constantize if reportable_class_name.present?
end

#reportable_attributesObject

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



52
53
54
55
56
57
58
59
60
61
# File 'app/models/effective/report.rb', line 52

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 }



64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/effective/report.rb', line 64

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 :#{scope}") unless reportable.respond_to?(scope)
  end

  scopes
end

#to_sObject



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

def to_s
  title.presence || 'report'
end