rails-crud-tools 0.6.7 → 0.6.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.
@@ -0,0 +1,164 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zip"
4
+ require_relative "crud_logger"
5
+ require_relative "constants"
6
+
7
+ module Rails
8
+ module Crud
9
+ module Tools
10
+ # The OperationsLogger module is responsible for logging CRUD operations in controllers and jobs.
11
+ # It provides methods to log request and job details, and to write CRUD operations to an Excel file.
12
+ module OperationsLogger
13
+ # コントローラのCRUD操作をログ出力する
14
+ def log_crud_operations
15
+ if CrudConfig.instance.enabled
16
+ CrudConfig.instance.load_config
17
+ log_request_details
18
+ Thread.current[:crud_request] = request
19
+ end
20
+
21
+ yield
22
+
23
+ if CrudConfig.instance.enabled
24
+ key = "#{controller_path}##{action_name}"
25
+ method = request.request_method
26
+ if CrudOperations.instance.table_operations_present?(method, key)
27
+ CrudOperations.instance.log_operations(method, key)
28
+ log_and_write_operations(method, key)
29
+ end
30
+ end
31
+ ensure
32
+ Thread.current[:crud_request] = nil
33
+ end
34
+
35
+ # ジョブのCRUD操作をログ出力する
36
+ def log_crud_operations_for_job
37
+ if CrudConfig.instance.enabled
38
+ CrudConfig.instance.load_config
39
+ log_job_details
40
+ key = self.class.name
41
+ Thread.current[:crud_sidekiq_job_class] = key
42
+ end
43
+
44
+ yield
45
+
46
+ if CrudConfig.instance.enabled && CrudOperations.instance.table_operations_present?(Constants::DEFAULT_METHOD, key)
47
+ CrudOperations.instance.log_operations(Constants::DEFAULT_METHOD, key)
48
+ log_and_write_operations(Constants::DEFAULT_METHOD, key)
49
+ end
50
+ ensure
51
+ Thread.current[:crud_sidekiq_job_class] = nil
52
+ end
53
+
54
+ # xlsxファイルの最終更新者を更新する
55
+ def set_last_modified_by(file_path, modifier_name)
56
+ Zip::File.open(file_path) do |zip_file|
57
+ doc_props = zip_file.find_entry("docProps/core.xml")
58
+ if doc_props
59
+ content = doc_props.get_input_stream.read
60
+ updated_content = if content.include?("<cp:lastModifiedBy>")
61
+ content.sub(
62
+ %r{<cp:lastModifiedBy>.*?</cp:lastModifiedBy>},
63
+ "<cp:lastModifiedBy>#{modifier_name}</cp:lastModifiedBy>"
64
+ )
65
+ else
66
+ content.sub(
67
+ %r{</cp:coreProperties>},
68
+ "<cp:lastModifiedBy>#{modifier_name}</cp:lastModifiedBy></cp:coreProperties>"
69
+ )
70
+ end
71
+ zip_file.get_output_stream("docProps/core.xml") { |f| f.write(updated_content) }
72
+ CrudLogger.logger.info "Set the last modifier to #{modifier_name}."
73
+ else
74
+ CrudLogger.logger.warn "docProps/core.xml was not found."
75
+ end
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ # リクエストの詳細をログ出力する
82
+ def log_request_details
83
+ method = request.request_method
84
+ CrudLogger.logger.info "******************** Method: #{method}, Controller: #{controller_path}, Action: #{action_name}, Key: #{controller_path}##{action_name} ********************"
85
+ end
86
+
87
+ # ジョブの詳細をログ出力する
88
+ def log_job_details
89
+ job_name = self.class.name
90
+ CrudLogger.logger.info "******************** Job: #{job_name} ********************"
91
+ end
92
+
93
+ # ExcelファイルにCRUD操作を書き込む
94
+ def log_and_write_operations(method, key)
95
+ CrudData.instance.reload_if_needed
96
+ sheet = CrudData.instance.crud_sheet
97
+
98
+ # フラグを初期化
99
+ contents_changed = false
100
+
101
+ CrudOperations.instance.table_operations[method][key].each_key do |table_name|
102
+ row = CrudData.instance.crud_rows[method][key]
103
+ col = CrudData.instance.crud_cols[table_name]
104
+
105
+ # colまたはrowが存在しない場合にログ出力してスキップ
106
+ unless row && col
107
+ CrudLogger.logger.warn "Row or Column not found for table: #{table_name}, method: #{method}, key: #{key}, row: #{row}, col: #{col}"
108
+ next
109
+ end
110
+
111
+ # sheet[row][col]がnilの場合に警告文を出力し、空文字列として処理を進める
112
+ cell = sheet[row][col]
113
+ if cell.nil?
114
+ cell = sheet.add_cell(row, col, "")
115
+ CrudLogger.logger.warn "Cell not found at row: #{row}, col: #{col} for table: #{table_name}, method: #{method}, key: #{key}. Adding new cell."
116
+ existing_value = ""
117
+ else
118
+ existing_value = cell.value || ""
119
+ end
120
+
121
+ # 新しい値と既存の値を結合し、重複を排除
122
+ new_value = CrudOperations.instance.table_operations[method][key][table_name].join
123
+ merged_value = (existing_value.chars + new_value.chars).uniq
124
+
125
+ # CRUDの順序に並び替え
126
+ crud_order = %w[C R U D]
127
+ sorted_value = merged_value.sort_by { |char| crud_order.index(char) }.join
128
+
129
+ # 値が変化した場合のみ change_contents を実行
130
+ if cell.value != sorted_value
131
+ cell.change_contents(sorted_value)
132
+ contents_changed = true
133
+ end
134
+ end
135
+
136
+ return unless contents_changed
137
+
138
+ Thread.new do
139
+ update_crud_file
140
+ rescue StandardError => e
141
+ CrudLogger.logger.error "Failed to update #{CrudConfig.instance.crud_file_path}: #{e.message}"
142
+ end
143
+ end
144
+
145
+ def update_crud_file
146
+ File.open(CrudConfig.instance.crud_file_path, "r+") do |crud_file|
147
+ crud_file.flock(File::LOCK_EX)
148
+ begin
149
+ # Excelファイルを書き込む
150
+ CrudData.instance.workbook.write(crud_file)
151
+ set_last_modified_by(crud_file, CrudData.instance.process_id)
152
+ timestamp = File.mtime(crud_file)
153
+ # タイムスタンプを更新する
154
+ CrudData.instance.last_loaded_time = timestamp
155
+ CrudLogger.logger.info "Updated timestamp: #{timestamp}"
156
+ ensure
157
+ crud_file.flock(File::LOCK_UN)
158
+ end
159
+ end
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/railtie"
4
+ require_relative "crud_operations_logger"
5
+
6
+ module Rails
7
+ module Crud
8
+ module Tools
9
+ # The Railtie class is responsible for adding custom initialization code to a Rails application.
10
+ # It includes filters for logging CRUD operations in controllers and jobs.
11
+ class Railtie < ::Rails::Railtie
12
+ initializer "rails-crud.add_after_action" do
13
+ ActiveSupport.on_load(:action_controller) do
14
+ include Rails::Crud::Tools::OperationsLogger
15
+
16
+ # 全てのコントローラにafter_actionフィルタを追加
17
+ ActionController::Base.class_eval do
18
+ around_action :log_crud_operations
19
+ end
20
+
21
+ # APIモードの場合はActionController::APIにも追加
22
+ ActionController::API.class_eval do
23
+ around_action :log_crud_operations
24
+ end
25
+ end
26
+
27
+ # ActiveJobにもフィルタを追加
28
+ ActiveSupport.on_load(:active_job) do
29
+ include Rails::Crud::Tools::OperationsLogger
30
+
31
+ # 全てのジョブにaround_performフィルタを追加
32
+ ActiveJob::Base.class_eval do
33
+ around_perform :log_crud_operations_for_job
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Crud
5
+ module Tools
6
+ VERSION = "0.6.9"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubyXL"
4
+ require "rubyXL/convenience_methods/cell"
5
+ require "rubyXL/convenience_methods/color"
6
+ require "rubyXL/convenience_methods/font"
7
+ require "rubyXL/convenience_methods/workbook"
8
+ require "rubyXL/convenience_methods/worksheet"
9
+ require_relative "tools/crud_operations"
10
+ require_relative "tools/crud_config"
11
+ require_relative "tools/crud_notifications"
12
+ require_relative "tools/railtie"
13
+ require_relative "tools/crud_data"
14
+
15
+ module Rails
16
+ module Crud
17
+ # The Tools module provides utility methods for setting up notifications and processing SQL queries.
18
+ # It includes methods to subscribe to ActiveSupport notifications and handle different types of SQL operations.
19
+ module Tools
20
+ def self.setup
21
+ unless File.exist?(CrudConfig.instance.config_file)
22
+ puts "The .crudconfig.yml file is required. Please run `bundle exec crud init`."
23
+ return
24
+ end
25
+
26
+ unless File.exist?(CrudConfig.instance.crud_file_path)
27
+ puts "The CRUD file is required. Please run `bundle exec crud gen crud`."
28
+ return
29
+ end
30
+
31
+ CrudData.instance.process_id = "rails-crud-tools-#{Time.now.strftime("%Y%m%d%H%M%S")}"
32
+ CrudData.instance.load_crud_data
33
+ setup_notifications
34
+ end
35
+
36
+ setup unless ENV["SKIP_CRUD_SETUP"] == "true"
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,6 @@
1
+ module Rails
2
+ module Crud
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-crud-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.7
4
+ version: 0.6.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - yhijikata
@@ -55,15 +55,44 @@ dependencies:
55
55
  description: This gem provides CRUD functionality for Rails applications.
56
56
  email:
57
57
  - yhijikata@systemlancer.com
58
- executables: []
58
+ executables:
59
+ - console
60
+ - crud
61
+ - setup
59
62
  extensions: []
60
63
  extra_rdoc_files: []
61
- files: []
62
- homepage: https://github.com/YamabikoLab
64
+ files:
65
+ - ".rspec"
66
+ - ".rubocop.yml"
67
+ - CHANGELOG.md
68
+ - Dockerfile
69
+ - Gemfile
70
+ - LICENSE
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - docker-compose.yml
75
+ - exe/console
76
+ - exe/crud
77
+ - exe/setup
78
+ - lib/rails/crud/tools.rb
79
+ - lib/rails/crud/tools/cli.rb
80
+ - lib/rails/crud/tools/constants.rb
81
+ - lib/rails/crud/tools/crud_config.rb
82
+ - lib/rails/crud/tools/crud_data.rb
83
+ - lib/rails/crud/tools/crud_logger.rb
84
+ - lib/rails/crud/tools/crud_notifications.rb
85
+ - lib/rails/crud/tools/crud_operations.rb
86
+ - lib/rails/crud/tools/crud_operations_logger.rb
87
+ - lib/rails/crud/tools/railtie.rb
88
+ - lib/rails/crud/tools/version.rb
89
+ - sig/rails/crud.rbs
90
+ - tools/crud_macro.xlsm
91
+ homepage: https://github.com/YamabikoLab/rails-crud-tools
63
92
  licenses:
64
93
  - MIT
65
94
  metadata:
66
- homepage_uri: https://github.com/YamabikoLab
95
+ homepage_uri: https://github.com/YamabikoLab/rails-crud-tools
67
96
  source_code_uri: https://github.com/YamabikoLab/rails-crud-tools
68
97
  changelog_uri: https://github.com/YamabikoLab/rails-crud-tools/blob/main/CHANGELOG.md
69
98
  rubygems_mfa_required: 'true'