breathing 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 61e525b923beadee13acfa8b5dc2708b88a3d4e4add63ea910d4ee3fd67d42e4
4
- data.tar.gz: 4ab65c2f8a026145dc121641922204bcd373f3ef1f379ced10b6575f1229e0a9
3
+ metadata.gz: 78f7bd77e2aacd87fe91b8e42eeb8dd590fac1a067c90c092a8069fa9d2a391f
4
+ data.tar.gz: cb6802aa877aa675fe62170d7d3fde082b1262439c33409a5a38169c90a05c8e
5
5
  SHA512:
6
- metadata.gz: c41a580a29c80ffeae3318a71153a6c6004a6a5f17bd02db91ecf3dbb8687dba7dd36318f5d3abbe4c86912a4103b4592aa9f6f4da446187deee072eaf84bccb
7
- data.tar.gz: 5ffc9e9e41a1fa92ed44503b2df61dd6306fb2acff8ca390fc024eaa9468a1f65953d1de3c982e7d5a6e1b18f17a950f2c76075156a7af960ff8d84323d5c124
6
+ metadata.gz: 1610bf010d660a7f0c1f00ed61b3235e75497220ef2b21c0f9a5c200b2c670c8495cbf905b782c7202f0236549e086296141d6e3d1b5fd1c24684d3a3af69335
7
+ data.tar.gz: 22d2262edfd99554875fbbaec0edac113c5e970a41f32126f7ebf18cc7b141987f9c58b1da1c02f4c1257e8f07a07e4a9a269c8876f6957bcc10cbac3767b161
@@ -2,7 +2,7 @@ $:.push File.expand_path('lib', __dir__)
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'breathing'
5
- s.version = '0.0.8'
5
+ s.version = '0.0.9'
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.authors = ['Akira Kusumoto']
8
8
  s.email = ['akirakusumo10@gmail.com']
@@ -18,12 +18,33 @@ module Breathing
18
18
  end
19
19
 
20
20
  def data_attributes
21
- data_column_names.each.with_object("change_logs.id" => id,
22
- "change_logs.created_at" => created_at.to_s(:db),
23
- "action" => action,
24
- "id" => transaction_id) do |name, hash|
21
+ data_column_names.each.with_object('change_logs.id' => id,
22
+ 'change_logs.created_at' => created_at.to_s(:db),
23
+ 'action' => action,
24
+ 'id' => transaction_id) do |name, hash|
25
25
  hash[name] = data[name]
26
26
  end
27
27
  end
28
+
29
+ def diff
30
+ return nil if action != 'UPDATE'
31
+
32
+ changed_attribute_columns.each.with_object({}) do |column_name, diff_hash|
33
+ diff_hash[column_name] = {before_data[column_name] => after_data[column_name]}
34
+ end
35
+ end
36
+
37
+ def attributes_for_excel
38
+ {
39
+ 'change_logs.id' => id,
40
+ 'created_at' => created_at.to_s(:db),
41
+ 'table_name' => table_name,
42
+ 'action' => action,
43
+ 'id' => transaction_id,
44
+ 'diff' => diff.to_s,
45
+ 'before_data' => before_data.to_s,
46
+ 'after_data' => after_data.to_s,
47
+ }
48
+ end
28
49
  end
29
50
  end
@@ -11,42 +11,38 @@ module Breathing
11
11
  def create(id: 1, file_name: 'breathing.xlsx')
12
12
  sheet = @workbook[0]
13
13
  table_names = Breathing::ChangeLog.where('id >= ?', id).group(:table_name).pluck(:table_name)
14
- table_names.each do |table_name|
14
+ table_names.sort.each do |table_name|
15
15
  if sheet.sheet_name == 'Sheet1'
16
16
  sheet.sheet_name = table_name
17
17
  else
18
18
  sheet = @workbook.add_worksheet(table_name)
19
19
  end
20
20
 
21
- rows = Breathing::ChangeLog.where(table_name: table_name).where('id >= ?', id).order(:id)
22
- column_widths = []
21
+ rows = Breathing::ChangeLog.where(table_name: table_name).where('id >= ?', id).order(:id).to_a
23
22
 
24
- if first_row = rows.first
25
- add_header_row(sheet, first_row, column_widths)
26
- end
27
- add_body_rows(sheet, rows, column_widths)
28
-
29
- column_widths.each.with_index { |size, i| sheet.change_column_width(i, size + 2) }
23
+ next if rows.size.zero?
30
24
 
25
+ add_header_row(sheet, rows.first)
26
+ add_body_rows(sheet, rows)
31
27
  add_style(sheet)
32
28
  end
33
29
 
30
+ add_change_logs_sheet(id) if table_names.size.positive?
31
+
34
32
  @workbook.write(file_name)
35
33
  end
36
34
 
37
35
  private
38
36
 
39
- def add_header_row(sheet, row, column_widths)
37
+ def add_header_row(sheet, row)
40
38
  header_color = 'ddedf3' # blue
41
39
  row.data_attributes.keys.each.with_index do |header_column, column_index|
42
40
  cell = sheet.add_cell(0, column_index, header_column)
43
41
  cell.change_fill(header_color)
44
-
45
- column_widths << header_column.size
46
42
  end
47
43
  end
48
44
 
49
- def add_body_rows(sheet, rows, column_widths)
45
+ def add_body_rows(sheet, rows)
50
46
  rows.each.with_index(1) do |row, row_number|
51
47
  row.data_attributes.each.with_index do |(column_name, value), column_index|
52
48
  cell = sheet.add_cell(row_number, column_index, value)
@@ -55,23 +51,42 @@ module Breathing
55
51
  elsif row.action == 'DELETE'
56
52
  cell.change_fill('d9d9d9') # color: grey
57
53
  end
58
-
59
- column_widths[column_index] = value.to_s.size if column_widths[column_index] < value.to_s.size
60
54
  end
61
55
  end
62
56
  end
63
57
 
64
58
  def add_style(sheet)
65
- sheet.sheet_data.rows.each.with_index do |row, i|
66
- row.cells.each do |cell|
59
+ sheet.sheet_data.rows.each.with_index do |row, row_index|
60
+ row.cells.each.with_index do |cell, column_index|
67
61
  %i[top bottom left right].each do |direction|
68
62
  cell.change_border(direction, 'thin')
69
63
  end
64
+ cell.change_border(:bottom, 'double') if row_index.zero?
70
65
 
71
- cell.change_border(:bottom, 'double') if i.zero?
66
+ cell_width = cell.value.to_s.size + 2
67
+ sheet.change_column_width(column_index, cell_width) if cell_width > sheet.get_column_width(column_index)
72
68
  end
73
69
  end
70
+
74
71
  sheet.change_row_horizontal_alignment(0, 'center')
75
72
  end
73
+
74
+ def add_change_logs_sheet(id)
75
+ sheet = @workbook.add_worksheet(Breathing::ChangeLog.table_name)
76
+
77
+ change_logs = Breathing::ChangeLog.where('id >= ?', id).order(:id)
78
+ change_logs.first.attributes_for_excel.keys.each.with_index do |header_column, column_index|
79
+ cell = sheet.add_cell(0, column_index, header_column)
80
+ cell.change_fill('ddedf3') # blue
81
+ end
82
+
83
+ change_logs.each.with_index(1) do |change_log, row_number|
84
+ change_log.attributes_for_excel.each.with_index do |(_column_name, value), column_index|
85
+ sheet.add_cell(row_number, column_index, value)
86
+ end
87
+ end
88
+
89
+ add_style(sheet)
90
+ end
76
91
  end
77
92
  end
@@ -40,8 +40,8 @@ module Breathing
40
40
  ActiveRecord::Schema.define version: 0 do
41
41
  create_table table_name, if_not_exists: true do |t|
42
42
  t.datetime :created_at, null: false, index: true
43
- t.string :action, null: false
44
43
  t.string :table_name, null: false
44
+ t.string :action, null: false
45
45
  t.string :transaction_id, null: false
46
46
  t.json :before_data, null: false
47
47
  t.json :after_data, null: false
@@ -23,13 +23,18 @@ describe Breathing::Excel do
23
23
  end
24
24
 
25
25
  it 'multi sheets' do
26
- User.create!(name: 'a', age: 20)
26
+ user = User.create!(name: 'a', age: 20)
27
+ user.update!(age: 21)
28
+ user.destroy!
27
29
  Department.create!(name: 'a')
28
30
 
29
31
  Tempfile.open(['tmp', '.xlsx']) do |file|
30
32
  Breathing::Excel.new.create(file_name: file.path)
31
33
  workbook = RubyXL::Parser.parse(file.path)
32
- expect(workbook.sheets.size).to eq(2)
34
+ expect(workbook.sheets.map(&:name)).to eq(%w[departments users change_logs])
35
+ change_logs_sheet = workbook.worksheets.last
36
+
37
+ expect(change_logs_sheet.sheet_data.size).to eq(Breathing::ChangeLog.count + 1)
33
38
  end
34
39
  end
35
40
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: breathing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Kusumoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-22 00:00:00.000000000 Z
11
+ date: 2020-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor