Class: MagicReport::Report

Inherits:
Object
  • Object
show all
Includes:
Reflection
Defined in:
lib/magic_report/report.rb,
lib/magic_report/report/row.rb,
lib/magic_report/report/reflection.rb,
lib/magic_report/report/builder/field.rb,
lib/magic_report/report/builder/has_one.rb,
lib/magic_report/report/builder/has_many.rb

Defined Under Namespace

Modules: Reflection Classes: Builder, Row

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Reflection

add_field, add_has_many, add_has_one

Constructor Details

#initialize(model, row = nil) ⇒ Report

Returns a new instance of Report.



9
10
11
12
# File 'lib/magic_report/report.rb', line 9

def initialize(model, row = nil)
  @model = model
  @row = row || self.class.build_row
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/magic_report/report.rb', line 7

def model
  @model
end

#rowObject (readonly)

Returns the value of attribute row.



7
8
9
# File 'lib/magic_report/report.rb', line 7

def row
  @row
end

Class Method Details

.build_row(prefix = nil) ⇒ Object

Building empty row for current report This row doesn’t include outer reports



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/magic_report/report.rb', line 93

def build_row(prefix = nil)
  row = ::MagicReport::Report::Row.new

  _fields.each_value do |field|
    row.register_column(field.name, I18n.t!("#{i18n_scope}.#{i18n_key}.#{field.name}".tr("/", ".")), prefix)
  end

  _has_one.each_value do |has_one|
    row.add_nested_row(key: has_one.name, row: has_one.build_row)
  end

  _has_many.each_value do |has_many|
    row.add_nested_row(key: has_many.name, row: has_many.build_row)
  end

  row
end

.field(name, processor = nil) ⇒ Object



69
70
71
72
73
# File 'lib/magic_report/report.rb', line 69

def field(name, processor = nil)
  reflection = Builder::Field.build(self, name, processor)

  Reflection.add_field(self, name, reflection)
end

.fields(*names) ⇒ Object



75
76
77
# File 'lib/magic_report/report.rb', line 75

def fields(*names)
  names.each { |name| field(name) }
end

.has_many(name, **options, &extension) ⇒ Object



85
86
87
88
89
# File 'lib/magic_report/report.rb', line 85

def has_many(name, **options, &extension)
  reflection = Builder::HasMany.build(self, name, options, &extension)

  Reflection.add_has_many(self, name, reflection)
end

.has_one(name, **options, &extension) ⇒ Object



79
80
81
82
83
# File 'lib/magic_report/report.rb', line 79

def has_one(name, **options, &extension)
  reflection = Builder::HasOne.build(self, name, options, &extension)

  Reflection.add_has_one(self, name, reflection)
end

.i18n_keyObject



65
66
67
# File 'lib/magic_report/report.rb', line 65

def i18n_key
  name.underscore.to_sym
end

.i18n_scopeObject

Default i18n scope for locales

en:

magic_report:


61
62
63
# File 'lib/magic_report/report.rb', line 61

def i18n_scope
  :magic_report
end

Instance Method Details

#headingsObject



51
52
53
# File 'lib/magic_report/report.rb', line 51

def headings
  row.headings
end

#rowsObject



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
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/magic_report/report.rb', line 14

def rows
  @rows ||= begin
    rows = []

    _fields.each_value do |field|
      row.add_column_value(key: field.name, value: field.process(model))
    end

    _has_one.each_value do |has_one|
      simple_row = row.nested_rows[has_one.name]

      has_one.process_rows(model, simple_row)
    end

    rows.push(row)

    _has_many.each_value do |has_many|
      simple_row = row.nested_rows[has_many.name]

      resik = has_many.process_rows(model, simple_row)

      resik.shift.map do |resik_row|
        new_row = self.class.build_row

        # TODO: copy ID here
        # copy_primary_attributes(new_row, row)

        new_row.nested_rows[has_many.name] = resik_row

        rows.push(new_row)
      end
    end

    rows
  end
end