Class: CustomReport::Report

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#iteratorObject

Returns the value of attribute iterator.



12
13
14
# File 'app/models/custom_report/report.rb', line 12

def iterator
  @iterator
end

Instance Method Details

#columns_hashObject



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

def columns_hash
  columns.map do |c|
    if c.is_a?(Array)
      { :name => c[0], :format => c[2] }
    else
      { :name => c.keys.first, :format => nil }
    end
  end
end

#columns_yamlObject



56
57
58
# File 'app/models/custom_report/report.rb', line 56

def columns_yaml
  self.columns.to_yaml
end

#columns_yaml=(values) ⇒ Object



52
53
54
# File 'app/models/custom_report/report.rb', line 52

def columns_yaml=(values)
  self.columns = YAML::load(values)
end

#evaluate(entity, column) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/custom_report/report.rb', line 40

def evaluate(entity, column)
  accessor = if column.is_a?(Hash) and column.size == 1
    column.values.first
  else
    column[1]
  end

  unless accessor.include?("destroy")
    entity.instance_eval(accessor)
  end
end

#generate(options = {}, filter_args = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/custom_report/report.rb', line 14

def generate(options = {}, filter_args = {})
  result = []
  ActiveRecord::Base.transaction do
    # Create the scope
    begin
      @iterator = eval(self.scope) unless self.scope.include?("destroy")

      unless options[:dont_paginate]
        @iterator = @iterator.paginate :page => options[:page], :per_page => (options[:per_page] || 20)
      end

      # Return an array of hashes
      result = @iterator.map do |entity|
        self.columns.map do |column|
          evaluate(entity, column)
        end
      end

    ensure
      # Always roll back
      raise ActiveRecord::Rollback
    end
  end
  result
end

#unharmfulObject



70
71
72
73
74
# File 'app/models/custom_report/report.rb', line 70

def unharmful
  if scope.include?(".destroy")
    errors.add(:scope, 'Scope cannot destroy - unsafe!')
  end
end