rails-crud-tools 0.4.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 768af8c4b3d9581a61568c0817ae477ef8d2f30552825ba1c69fa7daa4d748dc
4
+ data.tar.gz: aab060f00958cfa1c1969f7bb87692854b8a5c6d638cc7b31b4c540b6071e871
5
+ SHA512:
6
+ metadata.gz: 0e58858f11d6fa4aab47fa791881ee52e2d714f347abee711eaeedfd57f780f51c63f0f8fae3964eb897b454e93fe116a0fb1cbcf57892d5f22612b3e7bd5500
7
+ data.tar.gz: 96a3967933bf721e59d1007a2fe12a3103f52866197401436523355282cc72367417c62a462b9490395b0f8ade376b8d8988b574df0178edb1ba012668085193
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,61 @@
1
+ ## [0.4.0] - 2025-01-03
2
+ - セルの値が変化する場合のみ保存するよう修正
3
+ - 設定ファイルにシート名を追加
4
+ - 設定ファイルにフォント名を追加
5
+ - テストコード作成
6
+ - 初期化コマンド追加
7
+ - 設定ファイル作成コマンド追加
8
+
9
+ ## [0.3.2] - 2025-01-01
10
+
11
+ - 複数スレッド対応
12
+
13
+ ## [0.3.1] - 2024-12-31
14
+
15
+ - rails-crud-toolsに変更
16
+
17
+ ## [0.3.0] - 2024-12-30
18
+
19
+ - ジョブ対応
20
+ - クラッド図生成コマンドの追加
21
+
22
+ ## [0.2.2] - 2024-12-25
23
+
24
+ - SQL出力のON/OFFを追加
25
+ - サマリーログの改善
26
+
27
+ ## [0.2.1] - 2024-12-25
28
+
29
+ - リファクタリング
30
+
31
+ ## [0.2.0] - 2024-12-25
32
+
33
+ - api対応
34
+
35
+ ## [0.1.6] - 2024-12-25
36
+
37
+ - ログ改善
38
+
39
+ ## [0.1.5] - 2024-12-25
40
+
41
+ - マルチスレッド改善
42
+
43
+ ## [0.1.4] - 2024-12-24
44
+
45
+ - リファクタリング
46
+
47
+ ## [0.1.3] - 2024-12-24
48
+
49
+ - bug fix
50
+
51
+ ## [0.1.2] - 2024-12-24
52
+
53
+ - bug fix
54
+
55
+ ## [0.1.1] - 2024-12-24
56
+
57
+ - プレビュー版のリリース
58
+
59
+ ## [0.1.0] - 2024-12-23
60
+
61
+ - Initial release
data/Dockerfile ADDED
@@ -0,0 +1,21 @@
1
+ FROM ruby:3.1.4
2
+
3
+ ARG IDEURL=https://download.jetbrains.com/ruby/RubyMine-2024.3.1.tar.gz
4
+
5
+ RUN apt-get update -qq && apt-get install -y \
6
+ build-essential \
7
+ vim && \
8
+ rm -rf /var/lib/apt/lists/*
9
+
10
+ ENV EDITOR=vim
11
+
12
+ WORKDIR /root/app
13
+
14
+ RUN curl -fsSL -o ide.tar.gz $IDEURL && \
15
+ mkdir ide && \
16
+ tar xfz ide.tar.gz --strip-components=1 -C ide && \
17
+ rm ide.tar.gz
18
+
19
+ CMD yes '' | ide/bin/remote-dev-server.sh run $APP_ROOT --listenOn 0.0.0.0 --port 5995
20
+
21
+ EXPOSE 5995
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in rails-crud-tools.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ group :test do
15
+ gem "rails"
16
+ gem "simplecov"
17
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 yamabiko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 yhijikata
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # Rails::Crud::Tools
2
+
3
+ Welcome to Rails::Crud::Tools!
4
+ This gem provides a tool to automatically update CRUD diagrams as you interact with your application.
5
+ It simplifies logging and managing CRUD operations seamlessly within a Rails application.
6
+
7
+ ## Installation
8
+
9
+ Add the gem to the `development` group in your application's Gemfile by including the following lines:
10
+
11
+ ```ruby
12
+ gem 'rails-crud-tools', git: 'https://github.com/YamabikoLab/rails-crud-tools', tag: 'v*.*.*'
13
+ ```
14
+
15
+ Then execute:
16
+
17
+ ```sh
18
+ $ bundle install
19
+ ```
20
+
21
+ If you are not using Bundler, you can install the gem manually:
22
+
23
+ ```sh
24
+ $ gem install rails-crud-tools
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ### Setup
30
+ This command will generate the **doc/crud.xlsx** file and the **.crudconfig** file.
31
+
32
+ ```sh
33
+ $ bundle exec crud init
34
+ ```
35
+
36
+ .crudconfig
37
+ ```yaml
38
+ enabled: true # Enables or disables the CRUD tools functionality
39
+ base_dir: doc # The base directory where the CRUD files will be stored
40
+ crud_file: crud.xlsx # The name of the CRUD Excel file
41
+ sheet_name: CRUD # The name of the sheet in the CRUD Excel file
42
+ method_col: Verb # Column indicating the HTTP method
43
+ action_col: Controller#Action # Column indicating the controller and action
44
+ table_start_col: your_first_table # Column where the table starts
45
+ header_bg_color: 00FFCC # The background color for the header in the CRUD Excel file
46
+ sql_logging_enabled: true # Enables or disables SQL logging for CRUD operations
47
+ font_name: Arial # The font name used in the CRUD Excel file
48
+ ```
49
+
50
+ ### How It Works
51
+
52
+ Once integrated, the gem automatically tracks CRUD operations (Create, Read, Update, Delete) performed in your application.
53
+ The diagrams will update dynamically based on these operations, providing you with real-time insights into your application's data flow.
54
+
55
+ ## Logs
56
+
57
+ Please refer to the log file at `log/crud.log`.
58
+
59
+ ## CRUD Macro Workbook
60
+
61
+ The `tools/crud_macro.xlsm` file is a macro-enabled workbook used for manipulating CRUD diagrams.
62
+ This workbook contains macros that help in managing and visualizing CRUD operations within your application.
63
+
64
+ ### Download
65
+
66
+ You can download the `crud_macro.xlsm` file from the following link:
67
+
68
+ [Download crud_macro.xlsm](https://github.com/YamabikoLab/rails-crud/raw/main/tools/crud_macro.xlsm)
69
+
70
+ ## Development
71
+
72
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run the following command to execute the tests:
73
+
74
+ ```sh
75
+ $ rake spec
76
+ ```
77
+
78
+ You can also use `bin/console` for an interactive prompt to experiment with the gem’s functionality.
79
+
80
+ To install this gem onto your local machine for development purposes, run:
81
+
82
+ ```sh
83
+ $ bundle exec rake install
84
+ ```
85
+
86
+ To release a new version:
87
+ 1. Update the version number in `version.rb`.
88
+ 2. Run:
89
+
90
+ ```sh
91
+ $ bundle exec rake release
92
+ ```
93
+
94
+ This will create a git tag for the new version, push the git commits and tag, and upload the `.gem` file to [RubyGems](https://rubygems.org).
95
+
96
+ ## Contributing
97
+
98
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/YamabikoLab/rails-crud](https://github.com/YamabikoLab/rails-crud).
99
+
100
+ When contributing, please:
101
+ - Fork the repository.
102
+ - Create a feature branch.
103
+ - Submit a pull request with a clear description of your changes.
104
+
105
+ ## License
106
+
107
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
108
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,11 @@
1
+ version: '3.8'
2
+ services:
3
+ app:
4
+ build: .
5
+ container_name: rails-crud-tools
6
+ volumes:
7
+ - .:/root/app
8
+ - /root/app/ide
9
+ ports:
10
+ - '5995:5995'
11
+ tty: true
data/exe/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "rails/crud/tools"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/exe/crud ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ require "rails/crud/tools/cli"
3
+
4
+ def display_help
5
+ puts <<~HELP
6
+ Usage: crud [command]
7
+
8
+ Commands:
9
+ init Initialize by generating CRUD and config files
10
+ gen_crud Generate CRUD
11
+ gen_config Generate config
12
+ help Display this help message
13
+ HELP
14
+ end
15
+
16
+ if ARGV.empty? || ARGV.include?("help")
17
+ display_help
18
+ elsif ARGV.include?("init")
19
+ RailsCrudTools::CLI.init
20
+ elsif ARGV.include?("gen_crud")
21
+ RailsCrudTools::CLI.generate_crud_file
22
+ elsif ARGV.include?("gen_config")
23
+ RailsCrudTools::CLI.generate_crudconfig
24
+ else
25
+ puts "Unknown command: #{ARGV.join(' ')}"
26
+ display_help
27
+ end
data/exe/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,178 @@
1
+ require "rubyXL"
2
+ require 'rubyXL/convenience_methods'
3
+ require 'active_record'
4
+ require 'yaml'
5
+ require 'erb'
6
+
7
+ module RailsCrudTools
8
+ class CLI
9
+ @application_loaded = false
10
+ class << self
11
+ def generate_crud_file
12
+ load_application unless @application_loaded
13
+
14
+ # 1. `bundle exec rails routes --expanded`の結果を取得
15
+ routes_output = `bundle exec rails routes --expanded`
16
+ config = Rails::Crud::Tools::CrudConfig.instance
17
+ font_name = config.font_name
18
+
19
+ # 2. 取得した結果を区切り文字で分割
20
+ routes_lines = routes_output.split("\n").reject(&:empty?)
21
+ routes_data = []
22
+ current_route = {}
23
+
24
+ routes_lines.each do |line|
25
+ if line.start_with?("--[ Route")
26
+ routes_data << current_route unless current_route.empty?
27
+ current_route = {}
28
+ else
29
+ key, value = line.split("|").map(&:strip)
30
+ current_route[key] = value
31
+ end
32
+ end
33
+ routes_data << current_route unless current_route.empty?
34
+
35
+ # 3. 全テーブル名を取得し、アルファベット順にソート
36
+ table_names = ActiveRecord::Base.connection.tables.sort
37
+
38
+ # 4. `rubyXL`を使って`xlsx`ファイルに書き込み
39
+ workbook = RubyXL::Workbook.new
40
+ sheet = workbook[0]
41
+ sheet.sheet_name = config.sheet_name
42
+
43
+ # ヘッダー行を追加
44
+ headers = %w[Prefix Verb URI Controller#Action crud_count] + table_names
45
+
46
+ headers.each_with_index do |header, index|
47
+ cell = sheet.add_cell(0, index, header)
48
+ cell.change_fill("00FFCC")
49
+ cell.change_font_name(font_name)
50
+ cell.change_font_bold(true)
51
+ apply_borders(cell)
52
+ end
53
+
54
+ start_col = "F"
55
+ end_col = ("A".."ZZ").to_a[table_names.length + 4] # 'F'から始まる列の範囲を計算
56
+
57
+ # データ行を追加
58
+ routes_data.each_with_index do |route, row_index|
59
+ headers.each_with_index do |header, col_index|
60
+ cell = sheet.add_cell(row_index + 1, col_index, route[header])
61
+ cell.change_font_name(font_name)
62
+ apply_borders(cell)
63
+ end
64
+
65
+ # 追加: crud_count列に式を設定
66
+ crud_count_formula = "=SUMPRODUCT(LEN(#{start_col}#{row_index + 2}:#{end_col}#{row_index + 2}))"
67
+ crud_count_cell = sheet.add_cell(row_index + 1, 4, "", crud_count_formula)
68
+ crud_count_cell.change_font_name(font_name)
69
+ apply_borders(crud_count_cell)
70
+ end
71
+
72
+ # app/jobs ディレクトリ内のジョブ名を取得
73
+ job_files = Dir.glob("app/jobs/**/*.rb")
74
+ job_classes = job_files.map do |file|
75
+ File.basename(file, ".rb").camelize
76
+ end.reject { |job| job == "ApplicationJob" }.sort
77
+
78
+ # ジョブ名を Controller#Action 列に追加
79
+ job_classes.each_with_index do |job, index|
80
+ headers.each_with_index do |header, col_index|
81
+ if header == "Controller#Action"
82
+ cell = sheet.add_cell(routes_data.length + 1 + index, col_index, job)
83
+ cell.change_font_name(font_name)
84
+ else
85
+ cell = sheet.add_cell(routes_data.length + 1 + index, col_index, nil)
86
+ end
87
+ apply_borders(cell)
88
+ end
89
+
90
+ # 追加: crud_count列に式を設定
91
+ crud_count_formula = "=SUMPRODUCT(LEN(#{start_col}#{routes_data.length + 2 + index}:#{end_col}#{routes_data.length + 2 + index}))"
92
+ crud_count_cell = sheet.add_cell(routes_data.length + 1 + index, 4, "", crud_count_formula)
93
+ crud_count_cell.change_font_name(font_name)
94
+ apply_borders(crud_count_cell)
95
+ end
96
+
97
+ # ヘッダーの背景色を設定
98
+ (0..headers.length - 1).each do |col_index|
99
+ sheet[0][col_index].change_fill(config.header_bg_color)
100
+ end
101
+
102
+ # 列幅を設定
103
+ headers.each_with_index do |header, col_index|
104
+ max_length = header.length
105
+ (1..routes_data.length).each do |row_index|
106
+ cell_value = sheet[row_index][col_index].value.to_s
107
+ max_length = [max_length, cell_value.length].max
108
+ end
109
+ sheet.change_column_width(col_index, max_length + 2)
110
+ end
111
+
112
+ # ファイルを保存
113
+ crud_file = config.crud_file_path
114
+ workbook.write(crud_file)
115
+
116
+ puts "Output: #{crud_file}"
117
+ end
118
+
119
+ def generate_crudconfig
120
+ load_application unless @application_loaded
121
+
122
+ table_names = ActiveRecord::Base.connection.tables.sort
123
+ table_start_col = table_names.any? ? table_names.first : "active_admin_comments"
124
+
125
+ config_content = <<~CONFIG
126
+ enabled: true
127
+ base_dir: doc
128
+ crud_file: crud.xlsx
129
+ sheet_name: CRUD
130
+ method_col: Verb
131
+ action_col: Controller#Action
132
+ table_start_col: #{table_start_col}
133
+ sql_logging_enabled: true
134
+ header_bg_color: 00FFCC
135
+ font_name: Arial
136
+ CONFIG
137
+
138
+ File.write('.crudconfig', config_content)
139
+ puts "Generated .crudconfig file"
140
+ end
141
+
142
+ def init
143
+ generate_crudconfig
144
+ generate_crud_file
145
+ end
146
+
147
+ private
148
+
149
+ def load_application
150
+ return if @application_loaded
151
+
152
+ path = Dir.pwd
153
+ $stderr.puts "Loading application in '#{File.basename(path)}'..."
154
+ environment_path = "#{path}/config/environment.rb"
155
+ require environment_path
156
+
157
+ if defined? Rails
158
+ Rails.application.eager_load!
159
+ Rails.application.config.eager_load_namespaces.each(&:eager_load!) if Rails.application.config.respond_to?(:eager_load_namespaces)
160
+ end
161
+
162
+ @application_loaded = true
163
+ rescue ::LoadError
164
+ error_message = <<~EOS
165
+ Tried to load your application environment from '#{environment_path}'.
166
+ EOS
167
+ puts error_message
168
+ rescue TypeError
169
+ end
170
+
171
+ def apply_borders(cell)
172
+ %i[top bottom left right].each do |side|
173
+ cell.change_border(side, "thin")
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,9 @@
1
+ module Rails
2
+ module Crud
3
+ module Tools
4
+ module Constants
5
+ DEFAULT_METHOD = "default_method"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,50 @@
1
+ require "yaml"
2
+
3
+ module Rails
4
+ module Crud
5
+ module Tools
6
+ class CrudConfig
7
+ include Singleton
8
+
9
+ attr_accessor :enabled, :base_dir, :crud_file, :sheet_name, :method_col, :action_col, :table_start_col, :sql_logging_enabled, :header_bg_color, :font_name
10
+
11
+ def initialize
12
+ @config_file = ".crudconfig"
13
+ @last_loaded = nil
14
+ load_config
15
+ end
16
+
17
+ def load_config
18
+ if @last_loaded.nil? || File.mtime(@config_file) > @last_loaded
19
+ unless File.exist?(@config_file)
20
+ raise "Config file not found: #{@config_file}. Please generate it using `bundle exec crud gen_config`."
21
+ end
22
+
23
+ config = YAML.load_file(@config_file)
24
+
25
+ @enabled = config["enabled"]
26
+ @base_dir = config["base_dir"]
27
+ @crud_file = config["crud_file"]
28
+ @sheet_name = config["sheet_name"]
29
+ @method_col = config["method_col"]
30
+ @action_col = config["action_col"]
31
+ @table_start_col = config["table_start_col"]
32
+ @sql_logging_enabled = config["sql_logging_enabled"]
33
+ @header_bg_color = config["header_bg_color"]
34
+ @font_name = config["font_name"]
35
+
36
+ @last_loaded = File.mtime(@config_file)
37
+ end
38
+ end
39
+
40
+ def crud_file_path
41
+ File.join(@base_dir, @crud_file)
42
+ end
43
+
44
+ def crud_log_path
45
+ File.join(@base_dir, @crud_log)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,80 @@
1
+ require_relative "crud_logger"
2
+ require_relative "constants"
3
+
4
+ module Rails
5
+ module Crud
6
+ module Tools
7
+ # このクラスは、CRUDファイルからデータを読み込むためのクラスです。
8
+ class CrudData
9
+ include Singleton
10
+
11
+ attr_accessor :crud_rows, :crud_cols, :workbook, :last_loaded_time
12
+
13
+ def initialize
14
+ @crud_rows = {}
15
+ @crud_cols = {}
16
+ @last_loaded_time = nil
17
+ end
18
+
19
+ def load_crud_data
20
+ config = CrudConfig.instance
21
+ return unless config.enabled
22
+
23
+ unless File.exist?(config.crud_file_path)
24
+ CrudLogger.logger.warn "CRUD file not found: #{config.crud_file_path}"
25
+ return false
26
+ end
27
+
28
+ @workbook = RubyXL::Parser.parse(config.crud_file_path)
29
+ @last_loaded_time = File.mtime(config.crud_file_path)
30
+ sheet = get_crud_sheet
31
+ headers = sheet[0].cells.map(&:value)
32
+
33
+ method_col_index = headers.index(config.method_col)
34
+ action_col_index = headers.index(config.action_col)
35
+ table_start_col_index = headers.index(config.table_start_col)
36
+
37
+ raise "Method column not found" unless method_col_index
38
+ raise "Action column not found" unless action_col_index
39
+ raise "Table start column not found" unless table_start_col_index
40
+
41
+ headers[table_start_col_index..].each_with_index do |table_name, index|
42
+ @crud_cols[table_name] = table_start_col_index + index
43
+ end
44
+
45
+ sheet.each_with_index do |row, index|
46
+ next if index.zero?
47
+
48
+ method = row[method_col_index]&.value.to_s.strip
49
+ method = Constants::DEFAULT_METHOD if method.empty?
50
+ action = row[action_col_index]&.value&.split&.first
51
+ next if action.nil?
52
+
53
+ @crud_rows[method] ||= {}
54
+ @crud_rows[method][action] = index
55
+ end
56
+ end
57
+
58
+ # CRUDデータが更新された場合に再読み込みする
59
+ def reload_if_needed
60
+ config = CrudConfig.instance
61
+ return unless config.enabled
62
+
63
+ return unless @last_loaded_time.nil? || File.mtime(config.crud_file_path) > @last_loaded_time
64
+ CrudLogger.logger.info "Reloading CRUD data due to file modification. last_loaded_time = #{@last_loaded_time}"
65
+ load_crud_data
66
+
67
+ end
68
+
69
+ # CRUDシートを取得する
70
+ def get_crud_sheet
71
+ sheet_name = CrudConfig.instance.sheet_name
72
+ sheet = @workbook[sheet_name]
73
+ raise "CRUD sheet '#{sheet_name}' not found" if sheet.nil?
74
+
75
+ sheet
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,24 @@
1
+ require 'logger'
2
+ require 'singleton'
3
+
4
+ module Rails
5
+ module Crud
6
+ module Tools
7
+ class CrudLogger
8
+ include Singleton
9
+
10
+ def initialize
11
+ @logger = Logger.new("log/crud.log")
12
+ end
13
+
14
+ def self.logger
15
+ instance.logger
16
+ end
17
+
18
+ def logger
19
+ @logger
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,77 @@
1
+ require 'active_support/notifications'
2
+
3
+ # Notification を使用して SQL クエリを監視するためのモジュール
4
+ module Rails
5
+ module Crud
6
+ module Tools
7
+ def self.setup_notifications
8
+ # 初回呼び出し時に @subscribed を false に設定
9
+ @subscribed ||= false
10
+ # 既に通知が登録されている場合は処理を中断
11
+ return if @subscribed
12
+
13
+ config = CrudConfig.instance
14
+ if config.enabled
15
+ # SQL クエリを監視する
16
+ ActiveSupport::Notifications.subscribe(/sql.active_record/) do |name, started, finished, unique_id, data|
17
+ # INSERT, UPDATE, DELETE, SELECT のみを対象とする
18
+ if data[:sql] =~ /(INSERT|UPDATE|DELETE|SELECT)/
19
+ operation = case $1
20
+ when "INSERT" then "C"
21
+ when "UPDATE" then "U"
22
+ when "DELETE" then "D"
23
+ when "SELECT" then "R"
24
+ else "Unknown"
25
+ end
26
+
27
+ match_data = data[:sql].match(/(?:INSERT INTO|UPDATE|DELETE FROM|FROM)\s+`?(\w+)`?/i)
28
+ if match_data
29
+ # テーブル名を取得して CRUD 操作に追加
30
+ table_name = match_data[1]
31
+ key, method = determine_key_and_method
32
+ next if key.nil? || method.nil?
33
+
34
+ CrudOperations.instance.add_operation(method, key, table_name, operation)
35
+
36
+ next unless config.sql_logging_enabled
37
+
38
+ # SQL ログを出力
39
+ CrudLogger.logger.info "#{data[:name]} - #{data[:sql]}"
40
+ else
41
+ # テーブル名が見つからない場合は警告を出力
42
+ CrudLogger.logger.warn "Table name not found in SQL: #{data[:sql]}"
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ # 通知の登録が完了した後に @subscribed を true に設定
49
+ @subscribed = true
50
+ end
51
+
52
+ private
53
+
54
+ # キーとメソッドを決定する
55
+ def self.determine_key_and_method
56
+ request = Thread.current[:crud_request]
57
+ sidekiq_job_class = Thread.current[:crud_sidekiq_job_class]
58
+
59
+ if request
60
+ method = request.request_method
61
+ controller = request.params['controller']
62
+ action = request.params['action']
63
+ key = "#{controller}##{action}"
64
+ elsif sidekiq_job_class
65
+ key = sidekiq_job_class
66
+ method = Constants::DEFAULT_METHOD
67
+ else
68
+ CrudLogger.logger.warn "Unknown method and key detected"
69
+ return nil
70
+ end
71
+
72
+ [key, method]
73
+ end
74
+
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,50 @@
1
+ require_relative 'crud_logger'
2
+ require_relative 'constants'
3
+
4
+ module Rails
5
+ module Crud
6
+ module Tools
7
+ class CrudOperations
8
+ include Singleton
9
+
10
+ attr_accessor :table_operations, :logs
11
+
12
+ def initialize
13
+ @table_operations = Hash.new do |hash, method|
14
+ hash[method] = Hash.new do |h, key|
15
+ h[key] = Hash.new do |hh, table|
16
+ hh[table] = []
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ def add_operation(method, key, table_name, operation)
23
+ # @table_operations[method]が存在しない場合は初期化
24
+ @table_operations[method] ||= {}
25
+ # @table_operations[method][key]が存在しない場合は初期化
26
+ @table_operations[method][key] ||= {}
27
+ # @table_operations[method][key][table_name]が存在しない場合は初期化
28
+ @table_operations[method][key][table_name] ||= []
29
+
30
+ @table_operations[method][key][table_name] << operation unless @table_operations[method][key][table_name].include?(operation)
31
+ end
32
+
33
+ def log_operations(method, key)
34
+ CrudLogger.logger.info "\nSummary: Method: #{method}, Key: #{key}"
35
+
36
+ @table_operations[method][key].each do |table_name, operations|
37
+ CrudLogger.logger.info "#{table_name} - #{operations.join(', ')}"
38
+ end
39
+
40
+ end
41
+
42
+ def table_operations_present?(method, key)
43
+ return false if @table_operations[method].nil?
44
+ return false if @table_operations[method][key].nil?
45
+ !@table_operations[method][key].empty?
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,122 @@
1
+ require_relative 'crud_logger'
2
+ require_relative 'constants'
3
+
4
+ # ログ出力を行うモジュール
5
+ module Rails
6
+ module Crud
7
+ module Tools
8
+ module OperationsLogger
9
+
10
+ # コントローラのCRUD操作をログ出力する
11
+ def log_crud_operations
12
+ if CrudConfig.instance.enabled
13
+ CrudConfig.instance.load_config
14
+ log_request_details
15
+ Thread.current[:crud_request] = request
16
+ end
17
+
18
+ yield
19
+
20
+ if CrudConfig.instance.enabled
21
+ key = "#{controller_path}##{action_name}"
22
+ method = request.request_method
23
+ if CrudOperations.instance.table_operations_present?(method, key)
24
+ CrudOperations.instance.log_operations(method, key)
25
+ log_and_write_operations(method, key)
26
+ end
27
+ end
28
+ ensure
29
+ Thread.current[:crud_request] = nil
30
+ end
31
+
32
+ # ジョブのCRUD操作をログ出力する
33
+ def log_crud_operations_for_job
34
+ if CrudConfig.instance.enabled
35
+ CrudConfig.instance.load_config
36
+ log_job_details
37
+ key = self.class.name
38
+ Thread.current[:crud_sidekiq_job_class] = key
39
+ end
40
+
41
+ yield
42
+
43
+ if CrudConfig.instance.enabled
44
+ if CrudOperations.instance.table_operations_present?(Constants::DEFAULT_METHOD, key)
45
+ CrudOperations.instance.log_operations(Constants::DEFAULT_METHOD, key)
46
+ log_and_write_operations(Constants::DEFAULT_METHOD, key)
47
+ end
48
+ end
49
+ ensure
50
+ Thread.current[:crud_sidekiq_job_class] = nil
51
+ end
52
+
53
+ private
54
+
55
+ # リクエストの詳細をログ出力する
56
+ def log_request_details
57
+ method = request.request_method
58
+ CrudLogger.logger.info "******************** Method: #{method}, Controller: #{controller_path}, Action: #{action_name}, Key: #{controller_path}##{action_name} ********************"
59
+ end
60
+
61
+ # ジョブの詳細をログ出力する
62
+ def log_job_details
63
+ job_name = self.class.name
64
+ CrudLogger.logger.info "******************** Job: #{job_name} ********************"
65
+ end
66
+
67
+ # ExcelファイルにCRUD操作を書き込む
68
+ def log_and_write_operations(method, key)
69
+ CrudData.instance.reload_if_needed
70
+ sheet = CrudData.instance.get_crud_sheet
71
+
72
+ # フラグを初期化
73
+ contents_changed = false
74
+
75
+ CrudOperations.instance.table_operations[method][key].each_key do |table_name|
76
+ row = CrudData.instance.crud_rows[method][key]
77
+ col = CrudData.instance.crud_cols[table_name]
78
+
79
+ # colまたはrowが存在しない場合にログ出力してスキップ
80
+ unless row && col
81
+ CrudLogger.logger.warn "Row or Column not found for table: #{table_name}, method: #{method}, key: #{key}, row: #{row}, col: #{col}"
82
+ next
83
+ end
84
+
85
+ # sheet[row][col]がnilの場合に警告文を出力し、空文字列として処理を進める
86
+ cell = sheet[row][col]
87
+ if cell.nil?
88
+ cell = sheet.add_cell(row, col, "")
89
+ CrudLogger.logger.warn "Cell not found at row: #{row}, col: #{col} for table: #{table_name}, method: #{method}, key: #{key}. Adding new cell."
90
+ existing_value = ""
91
+ else
92
+ existing_value = cell.value || ""
93
+ end
94
+
95
+ # 新しい値と既存の値を結合し、重複を排除
96
+ new_value = CrudOperations.instance.table_operations[method][key][table_name].join
97
+ merged_value = (existing_value.chars + new_value.chars).uniq
98
+
99
+ # CRUDの順序に並び替え
100
+ crud_order = %w[C R U D]
101
+ sorted_value = merged_value.sort_by { |char| crud_order.index(char) }.join
102
+
103
+ # 値が変化した場合のみ change_contents を実行
104
+ if cell.value != sorted_value
105
+ cell.change_contents(sorted_value)
106
+ contents_changed = true
107
+ end
108
+ end
109
+
110
+ if contents_changed
111
+ # Excelファイルを書き込む
112
+ CrudData.instance.workbook.write(CrudConfig.instance.crud_file_path)
113
+ timestamp = File.mtime(CrudConfig.instance.crud_file_path)
114
+ CrudLogger.logger.debug "Updated timestamp: #{timestamp}"
115
+ # タイムスタンプを更新する
116
+ CrudData.instance.last_loaded_time = timestamp
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,36 @@
1
+ require 'rails/railtie'
2
+ require_relative 'crud_operations_logger'
3
+
4
+ module Rails
5
+ module Crud
6
+ module Tools
7
+ class Railtie < ::Rails::Railtie
8
+ initializer 'rails-crud.add_after_action' do
9
+ ActiveSupport.on_load(:action_controller) do
10
+ include Rails::Crud::Tools::OperationsLogger
11
+
12
+ # 全てのコントローラにafter_actionフィルタを追加
13
+ ActionController::Base.class_eval do
14
+ around_action :log_crud_operations
15
+ end
16
+
17
+ # APIモードの場合はActionController::APIにも追加
18
+ ActionController::API.class_eval do
19
+ around_action :log_crud_operations
20
+ end
21
+ end
22
+
23
+ # ActiveJobにもフィルタを追加
24
+ ActiveSupport.on_load(:active_job) do
25
+ include Rails::Crud::Tools::OperationsLogger
26
+
27
+ # 全てのジョブにaround_performフィルタを追加
28
+ ActiveJob::Base.class_eval do
29
+ around_perform :log_crud_operations_for_job
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Crud
5
+ module Tools
6
+ VERSION = "0.4.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,34 @@
1
+ require "rubyXL"
2
+ require "rubyXL/convenience_methods/cell"
3
+ require "rubyXL/convenience_methods/color"
4
+ require "rubyXL/convenience_methods/font"
5
+ require "rubyXL/convenience_methods/workbook"
6
+ require "rubyXL/convenience_methods/worksheet"
7
+ require_relative "tools/crud_operations"
8
+ require_relative "tools/crud_config"
9
+ require_relative "tools/crud_notifications"
10
+ require_relative "tools/railtie"
11
+ require_relative "tools/crud_data"
12
+
13
+ module Rails
14
+ module Crud
15
+ module Tools
16
+ def self.setup()
17
+ unless File.exist?(".crudconfig")
18
+ puts "The .crudconfig file is required. Please run `bundle exec crud init`."
19
+ return
20
+ end
21
+
22
+ unless File.exist?(CrudConfig.instance.crud_file_path)
23
+ puts "The CRUD file is required. Please run `bundle exec crud gen_crud`."
24
+ return
25
+ end
26
+
27
+ CrudData.instance.load_crud_data
28
+ setup_notifications
29
+ end
30
+
31
+ setup
32
+ end
33
+ end
34
+ 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 ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-crud-tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - yhijikata
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-01-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubyXL
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '7.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '7.0'
41
+ description: This gem provides CRUD functionality for Rails applications.
42
+ email:
43
+ - yhijikata@systemlancer.com
44
+ executables:
45
+ - console
46
+ - crud
47
+ - setup
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - ".rspec"
52
+ - ".rubocop.yml"
53
+ - CHANGELOG.md
54
+ - Dockerfile
55
+ - Gemfile
56
+ - LICENSE
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - docker-compose.yml
61
+ - exe/console
62
+ - exe/crud
63
+ - exe/setup
64
+ - lib/rails/crud/tools.rb
65
+ - lib/rails/crud/tools/cli.rb
66
+ - lib/rails/crud/tools/constants.rb
67
+ - lib/rails/crud/tools/crud_config.rb
68
+ - lib/rails/crud/tools/crud_data.rb
69
+ - lib/rails/crud/tools/crud_logger.rb
70
+ - lib/rails/crud/tools/crud_notifications.rb
71
+ - lib/rails/crud/tools/crud_operations.rb
72
+ - lib/rails/crud/tools/crud_operations_logger.rb
73
+ - lib/rails/crud/tools/railtie.rb
74
+ - lib/rails/crud/tools/version.rb
75
+ - sig/rails/crud.rbs
76
+ - tools/crud_macro.xlsm
77
+ homepage: https://github.com/YamabikoLab
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ homepage_uri: https://github.com/YamabikoLab
82
+ source_code_uri: https://github.com/YamabikoLab/rails-crud-tools
83
+ changelog_uri: https://github.com/YamabikoLab/rails-crud-tools/blob/main/CHANGELOG.md
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 2.6.0
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubygems_version: 3.3.26
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: A crud diagram is created simply by manipulating the screen.
103
+ test_files: []