arerd 0.1.0 → 0.2.1

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: 9a3364c03a39be5bf9ac0727652516999efb340d3208fd87621556021c28de20
4
- data.tar.gz: c84c46b6e3e2fec66857e74c087e5ba6f03b2fe4cc42c3ed99d9997dc5f69038
3
+ metadata.gz: ac4f8c25a6b90c5fd67ae251935edfaf44bdba168cf9057ba19d957996aac6b9
4
+ data.tar.gz: 905a00ce024e271515432af6b457e1ee661dc68f382172785ef5f3f577d44283
5
5
  SHA512:
6
- metadata.gz: 8a634f01c34f49fe14acfcfc9e9d37407723852857b05ab16ecef35ac5f5dfb420a6eeaf5695e156d0ca44e998a8f8df7032d8e3f987ab7b51b77b66c4035dc4
7
- data.tar.gz: 46d6acb3f3be56edb6630df0c97ff1a318d73b6aba02b2e009272ae0761bc616896040f6dd12ebc7beb8918845632274f7df9f70044f6ba8f4b5822f8305b1ee
6
+ metadata.gz: 63dad4d9eee0ce49073f2e3afc4d3fc6697b9d421d209d19db2fa6fe55cdc8e528251ee0c40ca9f0cee32adbb72a44e72d55192a68c142c97d8df7baa9404957
7
+ data.tar.gz: a07a13531800f47018a60ee69cbfcd31f4fb38b771d3b1b55060eefdb005268e2584313eddc3e2be0418df4569c14c0f6ae34e1a308e86de53e991931b9cef72
data/CLAUDE.md ADDED
@@ -0,0 +1,78 @@
1
+ # CLAUDE.md
2
+
3
+ This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
+
5
+ ## Project Overview
6
+
7
+ Arerd is a Ruby gem that generates Entity-Relationship (ER) diagrams from ActiveRecord models in Mermaid format. It provides rake tasks (`db:erd:mermaid` and `db:erd:markdown`) that can be used in Rails applications.
8
+
9
+ ## Common Commands
10
+
11
+ ### Development Commands
12
+ - `bundle exec rake test` - Run all tests (Minitest)
13
+ - `bundle exec rake standard` - Run StandardRB linter
14
+ - `bundle exec rake standard:fix` - Auto-fix StandardRB violations
15
+ - `bundle exec rake` - Run both tests and linter (default task)
16
+
17
+ ### Testing in a Rails Application
18
+ To test the gem in a Rails app during development:
19
+ - `bin/rails db:erd:mermaid` - Generate Mermaid diagram to stdout
20
+ - `bin/rails db:erd:markdown` - Generate Markdown-wrapped diagram to stdout
21
+
22
+ ### Running a Single Test
23
+ ```bash
24
+ bundle exec ruby -Ilib:test test/test_association.rb
25
+ ```
26
+
27
+ ## Code Architecture
28
+
29
+ ### Core Components
30
+
31
+ #### 1. **Association Model** ([lib/arerd/association.rb](lib/arerd/association.rb))
32
+ The heart of the gem. Represents a bidirectional relationship between two ActiveRecord models. Key characteristics:
33
+ - Stores left/right model pairs with their association names and multiplicities
34
+ - `build` class method converts ActiveRecord reflection objects into Association instances
35
+ - Filters out unsupported association types (`:through`, polymorphic)
36
+ - **Important**: Only creates one Association object per bidirectional relationship by comparing model names lexicographically (line 85) to avoid duplicates in has_and_belongs_to_many
37
+
38
+ #### 2. **ERD Generator** ([lib/arerd/erd_generator.rb](lib/arerd/erd_generator.rb))
39
+ Orchestrates the diagram generation:
40
+ - `collect_models_and_associations`: Eager loads Rails app, collects all ApplicationRecord descendants, builds Association objects
41
+ - `generate_mermaid`/`generate_markdown`: Renders ERB templates with collected data
42
+ - Templates use Rails I18n (`model_name.human`, `human_attribute_name`) for internationalization
43
+
44
+ #### 3. **Templates** ([lib/arerd/templates/](lib/arerd/templates/))
45
+ - `erd.mmd.erb`: Generates Mermaid erDiagram syntax
46
+ - Introspects database for index information to mark FK/UK columns
47
+ - Maps association multiplicities to Mermaid relationship symbols (||, |o, }o, o{)
48
+ - `erd.md.erb`: Wraps Mermaid output in Markdown code fence
49
+
50
+ #### 4. **Railtie Integration** ([lib/arerd/railtie.rb](lib/arerd/railtie.rb))
51
+ Minimal Rails integration that loads rake tasks when Rails is present.
52
+
53
+ ### Association Building Logic
54
+
55
+ The `Association.build` method handles different ActiveRecord association types:
56
+ - **has_many/has_one**: Creates Association from the "owning" side, requires `inverse_of`
57
+ - **has_and_belongs_to_many**: Creates only one Association per pair (avoids duplicates via name comparison)
58
+ - **belongs_to**: Only validates `inverse_of` presence, doesn't create Association (parent association handles it)
59
+ - **Filtered out**: `:through` associations, polymorphic associations
60
+
61
+ ### Test Structure
62
+
63
+ Tests use Minitest with an in-memory SQLite database:
64
+ - [test/test_helper.rb](test/test_helper.rb): Sets up ActiveRecord connection and I18n
65
+ - [test/support/schema.rb](test/support/schema.rb): Defines test database schema
66
+ - [test/support/models.rb](test/support/models.rb): Defines test ActiveRecord models
67
+ - All test models must include `inverse_of` option per gem requirements
68
+
69
+ ## Important Constraints
70
+
71
+ 1. **All associations must specify `inverse_of` option** - The gem warns and skips associations without it
72
+ 2. **has_many :through associations are ignored** - They're considered "virtual" associations
73
+ 3. **Polymorphic associations are not supported** - The gem warns and skips them
74
+ 4. **ApplicationRecord descendants only** - The gem specifically looks for `ApplicationRecord.descendants`
75
+
76
+ ## StandardRB Linting
77
+
78
+ This project uses StandardRB for code style. Configuration is minimal (see [.standard.yml](.standard.yml)). Follow StandardRB conventions for all code changes.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- arerd (0.1.0)
4
+ arerd (0.2.1)
5
5
  rails (>= 6.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -109,3 +109,7 @@ erDiagram
109
109
  User |o--o{ Notification : "sent_notifications / sender"
110
110
  Community }o--o{ User : "users / communities"
111
111
  ```
112
+
113
+ ## License
114
+
115
+ Arerd is available as open source under the terms of the [MIT License](LICENSE).
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Arerd
4
4
  class Association
5
+ include Comparable
6
+
5
7
  attr_reader :left_model, :left_key, :left_association_name,
6
8
  :right_model, :right_key, :right_association_name,
7
9
  :left_side_multiplicity, :right_side_multiplicity
@@ -26,6 +28,13 @@ module Arerd
26
28
  @right_side_multiplicity = right_side_multiplicity
27
29
  end
28
30
 
31
+ def <=>(other)
32
+ return nil unless other.is_a?(Association)
33
+
34
+ [left_model.name, right_model.name, left_association_name.to_s, right_association_name.to_s] <=>
35
+ [other.left_model.name, other.right_model.name, other.left_association_name.to_s, other.right_association_name.to_s]
36
+ end
37
+
29
38
  def self.build(association)
30
39
  case association.macro
31
40
  when :has_many
@@ -22,9 +22,11 @@ module Arerd
22
22
  def self.collect_models_and_associations
23
23
  Rails.application.eager_load!
24
24
 
25
- models = ApplicationRecord.descendants
25
+ models = ApplicationRecord.descendants.sort_by(&:name)
26
26
 
27
- associations = Arerd::Association.build_associations_from_models(models)
27
+ associations = Arerd::Association
28
+ .build_associations_from_models(models)
29
+ .sort
28
30
 
29
31
  {models:, associations:}
30
32
  end
data/lib/arerd/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Arerd
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arerd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shuhei YOSHIDA
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-07-21 00:00:00.000000000 Z
11
+ date: 2026-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,8 +38,8 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: Provides a Rake task (db:erd) that extracts Entity-Relationship information
42
- from ActiveRecord and outputs an E-R diagram in Mermaid notation.
41
+ description: Provides a Rake task that extracts entity-relationship information from
42
+ ActiveRecord models and generates an ER diagram in Mermaid notation.
43
43
  email:
44
44
  - contact@yantene.net
45
45
  executables: []
@@ -55,6 +55,7 @@ files:
55
55
  - ".editorconfig"
56
56
  - ".standard.yml"
57
57
  - ".tool-versions"
58
+ - CLAUDE.md
58
59
  - Gemfile
59
60
  - Gemfile.lock
60
61
  - LICENSE
@@ -69,7 +70,8 @@ files:
69
70
  - lib/arerd/templates/erd.mmd.erb
70
71
  - lib/arerd/version.rb
71
72
  homepage: https://github.com/yantene/arerd
72
- licenses: []
73
+ licenses:
74
+ - MIT
73
75
  metadata:
74
76
  allowed_push_host: https://rubygems.org
75
77
  homepage_uri: https://github.com/yantene/arerd
@@ -92,5 +94,5 @@ requirements: []
92
94
  rubygems_version: 3.3.27
93
95
  signing_key:
94
96
  specification_version: 4
95
- summary: Rails gem for generating ERD from ActiveRecord in Mermaid format
97
+ summary: A Rails gem for generating an ERD from ActiveRecord models in Mermaid format
96
98
  test_files: []