Class: MagicReport::Report

Inherits:
Object
  • Object
show all
Includes:
Reflection
Defined in:
lib/magic_report/report.rb,
lib/magic_report/report/csv.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, Csv, 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, is_fill = false, row = nil) ⇒ Report

Returns a new instance of Report.



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

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

Instance Attribute Details

#is_fillObject (readonly)

Returns the value of attribute is_fill.



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

def is_fill
  @is_fill
end

#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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/magic_report/report.rb', line 117

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("/", ".")), field.is_primary, 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(*args) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/magic_report/report.rb', line 90

def field(*args)
  options = args.extract_options!
  name, processor = args

  reflection = Builder::Field.build(self, name, processor, options)

  Reflection.add_field(self, name, reflection)
end

.fields(*names) ⇒ Object



99
100
101
# File 'lib/magic_report/report.rb', line 99

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

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



109
110
111
112
113
# File 'lib/magic_report/report.rb', line 109

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



103
104
105
106
107
# File 'lib/magic_report/report.rb', line 103

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

  Reflection.add_has_one(self, name, reflection)
end

.i18n_keyObject



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

def i18n_key
  name.underscore.tr("/", ".").to_sym
end

.i18n_scopeObject

Default i18n scope for locales

en:

magic_report:


82
83
84
# File 'lib/magic_report/report.rb', line 82

def i18n_scope
  :magic_report
end

Instance Method Details

#copy_primary_fields(old_row, new_row) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/magic_report/report.rb', line 60

def copy_primary_fields(old_row, new_row)
  old_row.columns.each do |column|
    if column.is_primary || is_fill
      new_row.add_column_value(key: column.key, value: column.value)
    end
  end

  if is_fill
    old_row.nested_rows.each do |key, nested_row|
      nested_row.columns.each do |column|
        new_row.nested_rows[key].add_column_value(key: column.key, value: column.value)
      end
    end
  end
end

#headingsObject



56
57
58
# File 'lib/magic_report/report.rb', line 56

def headings
  row.headings
end

#rowsObject



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
50
51
52
53
54
# File 'lib/magic_report/report.rb', line 15

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, is_fill)
    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, is_fill)

      resik.map.with_index do |resik_row, index|
        if index.zero?
          # rows.push(new_row)
        else
          new_row = self.class.build_row

          copy_primary_fields(row, new_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
    end

    rows
  end
end