Class: Effective::Report
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Effective::Report
- 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
-
#col(name, atts = {}) ⇒ Object
Find or build.
-
#collection ⇒ Object
The klass to base the collection from.
- #email_report_column ⇒ Object
- #filtered_report_columns ⇒ Object
- #notification(atts = {}) ⇒ Object
- #reportable ⇒ Object
-
#reportable_attributes ⇒ Object
Used to build the Reports form { id: :integer, archived: :boolean }.
-
#reportable_scopes ⇒ Object
{ active: nil, inactive: nil, with_first_name: :string, not_in_good_standing: :boolean }.
- #scope(name, atts = {}) ⇒ Object
- #to_s ⇒ Object
- #user_report_column ⇒ Object
Instance Method Details
#col(name, atts = {}) ⇒ Object
Find or build
48 49 50 51 52 53 |
# File 'app/models/effective/report.rb', line 48 def col(name, atts = {}) atts[:name] ||= name.to_sym atts[:as] ||= reportable_attributes[name] report_columns.find { |col| atts.all? { |k, v| col.send(k).to_s == v.to_s } } || report_columns.build(atts) end |
#collection ⇒ Object
The klass to base the collection from
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'app/models/effective/report.rb', line 105 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_column ⇒ Object
68 69 70 |
# File 'app/models/effective/report.rb', line 68 def email_report_column report_columns.find { |column| column.name == 'email' } || report_columns.find { |column| column.name.include?('email') } end |
#filtered_report_columns ⇒ Object
64 65 66 |
# File 'app/models/effective/report.rb', line 64 def filtered_report_columns report_columns.select(&:filter?) end |
#notification(atts = {}) ⇒ Object
60 61 62 |
# File 'app/models/effective/report.rb', line 60 def notification(atts = {}) notifications.find { |col| atts.all? { |k, v| col.send(k).to_s == v.to_s } } || notifications.build(atts) end |
#reportable ⇒ Object
43 44 45 |
# File 'app/models/effective/report.rb', line 43 def reportable reportable_class_name.constantize if reportable_class_name.present? end |
#reportable_attributes ⇒ Object
Used to build the Reports form { id: :integer, archived: :boolean }
80 81 82 83 84 85 86 87 88 89 |
# File 'app/models/effective/report.rb', line 80 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_scopes ⇒ Object
{ active: nil, inactive: nil, with_first_name: :string, not_in_good_standing: :boolean }
92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/models/effective/report.rb', line 92 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 |
#scope(name, atts = {}) ⇒ Object
55 56 57 58 |
# File 'app/models/effective/report.rb', line 55 def scope(name, atts = {}) atts[:name] ||= name.to_sym report_scopes.find { |scope| scope.name == name.to_s } || report_scopes.build(atts) end |
#to_s ⇒ Object
39 40 41 |
# File 'app/models/effective/report.rb', line 39 def to_s title.presence || 'report' end |
#user_report_column ⇒ Object
72 73 74 75 76 |
# File 'app/models/effective/report.rb', line 72 def user_report_column report_columns.find { |column| column.name == 'user' } || report_columns.find { |column| column.name == 'owner' } || report_columns.find { |column| column.name.include?('user') } end |