rails-crud-tools 0.6.18 → 0.6.19

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,166 @@
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
+ config = CrudConfig.instance.config
16
+ if config.enabled
17
+ CrudConfig.instance.load_config
18
+ log_request_details
19
+ Thread.current[:crud_request] = request
20
+ end
21
+
22
+ yield
23
+
24
+ if config.enabled
25
+ key = "#{controller_path}##{action_name}"
26
+ method = request.request_method
27
+ if CrudOperations.instance.table_operations_present?(method, key)
28
+ CrudOperations.instance.log_operations(method, key)
29
+ log_and_write_operations(method, key)
30
+ end
31
+ end
32
+ ensure
33
+ Thread.current[:crud_request] = nil
34
+ end
35
+
36
+ # ジョブのCRUD操作をログ出力する
37
+ def log_crud_operations_for_job
38
+ config = CrudConfig.instance.config
39
+ if config.enabled
40
+ CrudConfig.instance.load_config
41
+ log_job_details
42
+ key = self.class.name
43
+ Thread.current[:crud_sidekiq_job_class] = key
44
+ end
45
+
46
+ yield
47
+
48
+ if config.enabled && CrudOperations.instance.table_operations_present?(Constants::DEFAULT_METHOD, key)
49
+ CrudOperations.instance.log_operations(Constants::DEFAULT_METHOD, key)
50
+ log_and_write_operations(Constants::DEFAULT_METHOD, key)
51
+ end
52
+ ensure
53
+ Thread.current[:crud_sidekiq_job_class] = nil
54
+ end
55
+
56
+ # xlsxファイルの最終更新者を更新する
57
+ def set_last_modified_by(file_path, modifier_name)
58
+ Zip::File.open(file_path) do |zip_file|
59
+ doc_props = zip_file.find_entry("docProps/core.xml")
60
+ if doc_props
61
+ content = doc_props.get_input_stream.read
62
+ updated_content = if content.include?("<cp:lastModifiedBy>")
63
+ content.sub(
64
+ %r{<cp:lastModifiedBy>.*?</cp:lastModifiedBy>},
65
+ "<cp:lastModifiedBy>#{modifier_name}</cp:lastModifiedBy>"
66
+ )
67
+ else
68
+ content.sub(
69
+ %r{</cp:coreProperties>},
70
+ "<cp:lastModifiedBy>#{modifier_name}</cp:lastModifiedBy></cp:coreProperties>"
71
+ )
72
+ end
73
+ zip_file.get_output_stream("docProps/core.xml") { |f| f.write(updated_content) }
74
+ CrudLogger.logger.info "Set the last modifier to #{modifier_name}."
75
+ else
76
+ CrudLogger.logger.warn "docProps/core.xml was not found."
77
+ end
78
+ end
79
+ end
80
+
81
+ private
82
+
83
+ # リクエストの詳細をログ出力する
84
+ def log_request_details
85
+ method = request.request_method
86
+ CrudLogger.logger.info "******************** Method: #{method}, Controller: #{controller_path}, Action: #{action_name}, Key: #{controller_path}##{action_name} ********************"
87
+ end
88
+
89
+ # ジョブの詳細をログ出力する
90
+ def log_job_details
91
+ job_name = self.class.name
92
+ CrudLogger.logger.info "******************** Job: #{job_name} ********************"
93
+ end
94
+
95
+ # ExcelファイルにCRUD操作を書き込む
96
+ def log_and_write_operations(method, key)
97
+ CrudData.instance.reload_if_needed
98
+ sheet = CrudData.instance.crud_sheet
99
+
100
+ # フラグを初期化
101
+ contents_changed = false
102
+
103
+ CrudOperations.instance.table_operations[method][key].each_key do |table_name|
104
+ row = CrudData.instance.crud_rows[method][key]
105
+ col = CrudData.instance.crud_cols[table_name]
106
+
107
+ # colまたはrowが存在しない場合にログ出力してスキップ
108
+ unless row && col
109
+ CrudLogger.logger.warn "Row or Column not found for table: #{table_name}, method: #{method}, key: #{key}, row: #{row}, col: #{col}"
110
+ next
111
+ end
112
+
113
+ # sheet[row][col]がnilの場合に警告文を出力し、空文字列として処理を進める
114
+ cell = sheet[row][col]
115
+ if cell.nil?
116
+ cell = sheet.add_cell(row, col, "")
117
+ CrudLogger.logger.warn "Cell not found at row: #{row}, col: #{col} for table: #{table_name}, method: #{method}, key: #{key}. Adding new cell."
118
+ existing_value = ""
119
+ else
120
+ existing_value = cell.value || ""
121
+ end
122
+
123
+ # 新しい値と既存の値を結合し、重複を排除
124
+ new_value = CrudOperations.instance.table_operations[method][key][table_name].join
125
+ merged_value = (existing_value.chars + new_value.chars).uniq
126
+
127
+ # CRUDの順序に並び替え
128
+ crud_order = %w[C R U D]
129
+ sorted_value = merged_value.sort_by { |char| crud_order.index(char) }.join
130
+
131
+ # 値が変化した場合のみ change_contents を実行
132
+ if cell.value != sorted_value
133
+ cell.change_contents(sorted_value)
134
+ contents_changed = true
135
+ end
136
+ end
137
+
138
+ return unless contents_changed
139
+
140
+ Thread.new do
141
+ update_crud_file
142
+ rescue StandardError => e
143
+ CrudLogger.logger.error "Failed to update #{CrudConfig.instance.config.crud_file_path}: #{e.message}"
144
+ end
145
+ end
146
+
147
+ def update_crud_file
148
+ File.open(CrudConfig.instance.config.crud_file_path, "r+") do |crud_file|
149
+ crud_file.flock(File::LOCK_EX)
150
+ begin
151
+ # Excelファイルを書き込む
152
+ CrudData.instance.workbook.write(crud_file)
153
+ set_last_modified_by(crud_file, CrudData.instance.process_id)
154
+ timestamp = File.mtime(crud_file)
155
+ # タイムスタンプを更新する
156
+ CrudData.instance.last_loaded_time = timestamp
157
+ CrudLogger.logger.info "Updated timestamp: #{timestamp}"
158
+ ensure
159
+ crud_file.flock(File::LOCK_UN)
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
165
+ end
166
+ 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.19"
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.config_path)
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.config.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.18
4
+ version: 0.6.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - yhijikata
@@ -55,17 +55,49 @@ 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
+ - ".devcontainer/devcontainer.json"
66
+ - ".devcontainer/docker-compose.yml"
67
+ - ".rspec"
68
+ - ".rubocop.yml"
69
+ - CHANGELOG.md
70
+ - Dockerfile
71
+ - Gemfile
72
+ - LICENSE
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - docker-compose.yml
77
+ - exe/console
78
+ - exe/crud
79
+ - exe/setup
80
+ - lib/rails/crud/tools.rb
81
+ - lib/rails/crud/tools/cli.rb
82
+ - lib/rails/crud/tools/constants.rb
83
+ - lib/rails/crud/tools/crud_config.rb
84
+ - lib/rails/crud/tools/crud_data.rb
85
+ - lib/rails/crud/tools/crud_logger.rb
86
+ - lib/rails/crud/tools/crud_notifications.rb
87
+ - lib/rails/crud/tools/crud_operations.rb
88
+ - lib/rails/crud/tools/crud_operations_logger.rb
89
+ - lib/rails/crud/tools/railtie.rb
90
+ - lib/rails/crud/tools/version.rb
91
+ - sig/rails/crud.rbs
92
+ - tools/crud_macro.xlsm
93
+ homepage: https://github.com/YamabikoLab/rails-crud-tools
63
94
  licenses:
64
95
  - MIT
65
96
  metadata:
66
- homepage_uri: https://github.com/YamabikoLab
97
+ homepage_uri: https://github.com/YamabikoLab/rails-crud-tools
67
98
  source_code_uri: https://github.com/YamabikoLab/rails-crud-tools
68
99
  changelog_uri: https://github.com/YamabikoLab/rails-crud-tools/blob/main/CHANGELOG.md
100
+ rubygems_mfa_required: 'true'
69
101
  post_install_message:
70
102
  rdoc_options: []
71
103
  require_paths:
@@ -74,7 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
106
  requirements:
75
107
  - - ">="
76
108
  - !ruby/object:Gem::Version
77
- version: 2.7.0
109
+ version: '3.1'
78
110
  required_rubygems_version: !ruby/object:Gem::Requirement
79
111
  requirements:
80
112
  - - ">="