darthjee-active_ext 1.3.2 → 2.0.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 +5 -5
- data/.circleci/config.yml +77 -9
- data/.claude/agents/architect.md +46 -0
- data/.claude/agents/ruby.md +49 -0
- data/.claude/configuration/arcanum-repo-config.json +8 -0
- data/.claude/scripts/check_ruby.sh +7 -0
- data/.claude/settings.json +10 -0
- data/.github/active_ext-usage.md +137 -0
- data/.github/commit_message_template-2.0.md +32 -0
- data/.github/commit_message_template.md +10 -0
- data/.github/copilot-instructions.md +1 -0
- data/.github/pull_request_template.md +17 -0
- data/.gitignore +6 -0
- data/.rubocop.yml +12 -0
- data/.rubocop_todo.yml +8 -0
- data/AGENTS.md +139 -0
- data/CLAUDE.md +1 -0
- data/Dockerfile +21 -0
- data/Gemfile +25 -0
- data/Makefile +6 -0
- data/README.md +15 -3
- data/Rakefile +8 -0
- data/active_ext.gemspec +7 -14
- data/active_ext.jpg +0 -0
- data/config/check_specs.yml +6 -0
- data/config/rubycritc.rb +12 -0
- data/config/yardstick.rb +13 -0
- data/config/yardstick.yml +39 -0
- data/docker-compose.yml +14 -9
- data/docs/agents/arcanum-split-issue.md +9 -0
- data/docs/agents/architecture.md +59 -0
- data/docs/agents/contributing.md +182 -0
- data/docs/agents/flow.md +5 -0
- data/docs/agents/folder-structure.md +18 -0
- data/docs/agents/issue-enhancement.md +13 -0
- data/docs/agents/issues/.gitkeep +1 -0
- data/docs/agents/plans/.gitkeep +1 -0
- data/lib/active_record/base_ext.rb +39 -2
- data/lib/active_record/relation_ext.rb +108 -10
- data/lib/darthjee/active_ext/version.rb +3 -1
- data/lib/darthjee/active_ext.rb +3 -0
- data/lib/darthjee.rb +3 -1
- data/scripts/bump_version.sh +62 -0
- data/spec/lib/active_record/relation_spec.rb +5 -3
- data/spec/spec_helper.rb +12 -8
- data/spec/support/models/document.rb +2 -0
- data/spec/support/schema.rb +3 -1
- data/spec/support/shared_examples/active_relation.rb +4 -2
- metadata +43 -121
- data/spec/lib/active_record/base_spec.rb +0 -13
data/README.md
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
ActiveExt
|
|
2
2
|
==========
|
|
3
3
|
|
|
4
|
-
[](https://dl.circleci.com/status-badge/redirect/gh/darthjee/active_ext/tree/main)
|
|
5
|
+
[](https://app.codacy.com/gh/darthjee/active_ext/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
|
|
6
|
+
[](https://app.codacy.com/gh/darthjee/active_ext/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
|
|
7
|
+
[](https://badge.fury.io/rb/darthjee-active_ext)
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
**Current Release**: [2.0.0](https://github.com/darthjee/active_ext/tree/2.0.0)
|
|
12
|
+
|
|
13
|
+
**Next release**: [2.1.0](https://github.com/darthjee/active_ext/compare/2.0.0...main)
|
|
14
|
+
|
|
15
|
+
Yard Documentation
|
|
16
|
+
-------------------
|
|
17
|
+
[https://www.rubydoc.info/gems/darthjee-active_ext/2.0.0](https://www.rubydoc.info/gems/darthjee-active_ext/2.0.0)
|
|
18
|
+
|
|
7
19
|
|
|
8
20
|
# Usage
|
|
9
21
|
This project adds some new methods to the core active_record classes
|
data/Rakefile
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'bundler/gem_tasks'
|
|
2
4
|
require 'rspec/core/rake_task'
|
|
5
|
+
require 'yardstick/rake/measurement'
|
|
6
|
+
require './config/yardstick'
|
|
7
|
+
require './config/rubycritc'
|
|
3
8
|
|
|
4
9
|
RSpec::Core::RakeTask.new
|
|
5
10
|
|
|
11
|
+
desc 'run rspec'
|
|
6
12
|
task default: :spec
|
|
13
|
+
|
|
14
|
+
desc 'run rspec'
|
|
7
15
|
task test: :spec
|
data/active_ext.gemspec
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
5
|
require 'darthjee/active_ext/version'
|
|
5
6
|
|
|
@@ -11,21 +12,13 @@ Gem::Specification.new do |gem|
|
|
|
11
12
|
gem.summary = 'Active Extensions'
|
|
12
13
|
gem.homepage = 'https://github.com/darthjee/active_ext'
|
|
13
14
|
gem.description = 'Extension of active support classes with usefull methods'
|
|
15
|
+
gem.required_ruby_version = '>= 3.3.1'
|
|
14
16
|
|
|
15
17
|
gem.files = `git ls-files -z`.split("\x0")
|
|
16
18
|
gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
17
|
-
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
18
19
|
gem.require_paths = ['lib']
|
|
19
20
|
|
|
20
|
-
gem.
|
|
21
|
-
gem.
|
|
22
|
-
|
|
23
|
-
gem.add_development_dependency 'activerecord', '~> 5.x'
|
|
24
|
-
gem.add_development_dependency 'sqlite3'
|
|
25
|
-
|
|
26
|
-
gem.add_development_dependency 'bundler', '>= 1.6'
|
|
27
|
-
gem.add_development_dependency 'rake', '>= 11.3.0'
|
|
28
|
-
gem.add_development_dependency 'rspec', '>= 2.14'
|
|
29
|
-
gem.add_development_dependency 'rspec-mocks', '>= 2.99.4'
|
|
30
|
-
gem.add_development_dependency 'simplecov', '>= 0.14.1'
|
|
21
|
+
gem.add_dependency 'activesupport', '~> 7.2.x'
|
|
22
|
+
gem.add_dependency 'darthjee-core_ext', '>= 3.2.0'
|
|
23
|
+
gem.metadata['rubygems_mfa_required'] = 'false'
|
|
31
24
|
end
|
data/active_ext.jpg
ADDED
|
Binary file
|
data/config/rubycritc.rb
ADDED
data/config/yardstick.rb
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'yardstick/rake/measurement'
|
|
4
|
+
require 'yardstick/rake/verify'
|
|
5
|
+
require 'yaml'
|
|
6
|
+
|
|
7
|
+
options = YAML.load_file('config/yardstick.yml')
|
|
8
|
+
|
|
9
|
+
Yardstick::Rake::Measurement.new(:yardstick_measure, options) do |measurement|
|
|
10
|
+
measurement.output = 'measurement/report.txt'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
Yardstick::Rake::Verify.new(:verify_measurements, options)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
threshold: 10.0
|
|
2
|
+
require_exact_threshold: false
|
|
3
|
+
rules:
|
|
4
|
+
ApiTag::Presence:
|
|
5
|
+
enabled: true
|
|
6
|
+
exclude: []
|
|
7
|
+
ApiTag::Inclusion:
|
|
8
|
+
enabled: true
|
|
9
|
+
exclude: []
|
|
10
|
+
ApiTag::ProtectedMethod:
|
|
11
|
+
enabled: true
|
|
12
|
+
exclude: []
|
|
13
|
+
ApiTag::PrivateMethod:
|
|
14
|
+
enabled: true
|
|
15
|
+
exclude: []
|
|
16
|
+
ExampleTag:
|
|
17
|
+
enabled: true
|
|
18
|
+
exclude:
|
|
19
|
+
- ActiveRecord::Base.percentage
|
|
20
|
+
- ActiveRecord::Base.pluck_as_json
|
|
21
|
+
- ActiveRecord::Base.scopped
|
|
22
|
+
ReturnTag:
|
|
23
|
+
enabled: true
|
|
24
|
+
exclude:
|
|
25
|
+
- ActiveRecord::Base.percentage
|
|
26
|
+
- ActiveRecord::Base.pluck_as_json
|
|
27
|
+
- ActiveRecord::Base.scopped
|
|
28
|
+
Summary::Presence:
|
|
29
|
+
enabled: true
|
|
30
|
+
exclude: []
|
|
31
|
+
Summary::Length:
|
|
32
|
+
enabled: true
|
|
33
|
+
exclude: []
|
|
34
|
+
Summary::Delimiter:
|
|
35
|
+
enabled: true
|
|
36
|
+
exclude: []
|
|
37
|
+
Summary::SingleLine:
|
|
38
|
+
enabled: true
|
|
39
|
+
exclude: []
|
data/docker-compose.yml
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
|
-
version: '
|
|
1
|
+
version: '3'
|
|
2
2
|
services:
|
|
3
3
|
base: &base
|
|
4
|
-
image:
|
|
5
|
-
working_dir: /home/app/
|
|
4
|
+
image: active_ext
|
|
5
|
+
working_dir: /home/app/app
|
|
6
6
|
volumes:
|
|
7
|
-
- .:/home/app/
|
|
8
|
-
- active_ext_gems_2_4_0:/usr/local/bundle
|
|
7
|
+
- .:/home/app/app
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
base_build:
|
|
10
|
+
<<: *base
|
|
11
|
+
build: .
|
|
12
|
+
command: echo done
|
|
11
13
|
|
|
12
14
|
active_ext:
|
|
13
15
|
<<: *base
|
|
14
16
|
container_name: active_ext
|
|
15
|
-
|
|
17
|
+
depends_on: [base_build]
|
|
18
|
+
command: /bin/bash -c 'rspec'
|
|
16
19
|
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
test_all:
|
|
21
|
+
<<: *base
|
|
22
|
+
depends_on: [base_build]
|
|
23
|
+
command: /bin/bash -c 'rspec && yard && rake yardstick_measure && rake verify_measurements'
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Arcanum Split Issue
|
|
2
|
+
|
|
3
|
+
A checklist of concerns to consider when splitting a broad issue into sub-issues via `/arcanum-split-issue`. Not exhaustive — adjust or extend the list for this project's needs.
|
|
4
|
+
|
|
5
|
+
- **Sub-issue granularity** — is each sub-issue independently workable, or does it still depend on another sub-issue landing first?
|
|
6
|
+
- **Standalone clarity** — does each sub-issue stand on its own, without requiring the reader to have the parent issue open to understand it?
|
|
7
|
+
- **Shared contracts** — interfaces, schemas, or config keys touched by more than one sub-issue, and who owns getting them right first.
|
|
8
|
+
- **Sequencing** — is there a natural order sub-issues should be implemented/merged in, or can they proceed in parallel?
|
|
9
|
+
- **Responsible agents** — which specialist agent(s) each sub-issue is likely to fall to.
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Architecture
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
`darthjee-active_ext` is a small Ruby gem that reopens ActiveRecord classes to add
|
|
6
|
+
query helpers. It has no runtime of its own: requiring `darthjee/active_ext` loads
|
|
7
|
+
`darthjee/core_ext` (from the `darthjee-core_ext` gem) and the ActiveRecord
|
|
8
|
+
extensions, and the new methods are then available on every model and relation.
|
|
9
|
+
|
|
10
|
+
The logic lives on `ActiveRecord::Relation`. `ActiveRecord::Base` only delegates
|
|
11
|
+
the same methods to `all`, so the helpers work both on a model class
|
|
12
|
+
(`Document.percentage(...)`) and on any scope chain (`Document.active.percentage(...)`).
|
|
13
|
+
|
|
14
|
+
## Source Code Layout
|
|
15
|
+
|
|
16
|
+
All gem source code lives under `lib/`.
|
|
17
|
+
|
|
18
|
+
### `lib/darthjee.rb` and `lib/darthjee/`
|
|
19
|
+
|
|
20
|
+
Gem namespace and entry point.
|
|
21
|
+
|
|
22
|
+
- `darthjee/active_ext.rb`: requires `darthjee/core_ext` and the ActiveRecord
|
|
23
|
+
extensions, and defines the `Darthjee::ActiveExt` module.
|
|
24
|
+
- `darthjee/active_ext/version.rb`: `Darthjee::ActiveExt::VERSION`, which the
|
|
25
|
+
gemspec reads.
|
|
26
|
+
|
|
27
|
+
### `lib/active_record/`
|
|
28
|
+
|
|
29
|
+
Reopens the ActiveRecord classes.
|
|
30
|
+
|
|
31
|
+
- `relation_ext.rb`: adds instance methods to `ActiveRecord::Relation`:
|
|
32
|
+
- `percentage(*filters)` (public): the fraction (0.0–1.0) of records in the
|
|
33
|
+
relation that match the filters. Returns `0.0` for an empty relation.
|
|
34
|
+
- `scopped(*filters)` (private API): applies the filters. A list of Symbols is
|
|
35
|
+
chained as named scopes; anything else (a Hash or SQL String) goes to `where`.
|
|
36
|
+
Do not mix the two forms in one call.
|
|
37
|
+
- `pluck_as_json(*keys)` (public): like `pluck`, but returns an array of
|
|
38
|
+
Symbol-keyed hashes (via `Array#as_hash` from `darthjee-core_ext`). With no
|
|
39
|
+
keys it returns `as_json` for each record.
|
|
40
|
+
- `base_ext.rb`: delegates `percentage`, `pluck_as_json` and `scopped` from the
|
|
41
|
+
`ActiveRecord::Base` class to `all`. It holds only YARD `@method` docstrings
|
|
42
|
+
that point at the Relation docs.
|
|
43
|
+
|
|
44
|
+
## Tests
|
|
45
|
+
|
|
46
|
+
- RSpec, against an in-memory SQLite database (`spec/spec_helper.rb`).
|
|
47
|
+
- `spec/support/schema.rb` and `spec/support/models/document.rb` define the
|
|
48
|
+
`Document` test model and its scopes (`with_error`, `with_success`, `active`).
|
|
49
|
+
- `spec/support/shared_examples/active_relation.rb` holds shared examples that run
|
|
50
|
+
against both the model class and relations.
|
|
51
|
+
- The `lib/` files are listed in `config/check_specs.yml` because their behaviour
|
|
52
|
+
is covered by `spec/lib/active_record/relation_spec.rb`, not by a spec per file.
|
|
53
|
+
|
|
54
|
+
## Quality Tooling
|
|
55
|
+
|
|
56
|
+
- YARD docs, with coverage measured by yardstick (`config/yardstick.yml`,
|
|
57
|
+
`rake yardstick_measure`, `rake verify_measurements`).
|
|
58
|
+
- RuboCop (`.rubocop.yml`) and RubyCritic (`config/rubycritc.rb`).
|
|
59
|
+
- SimpleCov coverage, with an LCOV report on CI (CircleCI and Codacy).
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
## Commit Guidelines
|
|
4
|
+
|
|
5
|
+
- **Atomic and Unitary:** Each commit must represent a single logical change.
|
|
6
|
+
*Example:*
|
|
7
|
+
- Good: `Add pluck_as_json to ActiveRecord::Relation`
|
|
8
|
+
- Bad: `Add pluck_as_json and refactor scopped logic`
|
|
9
|
+
- **No Unrelated Changes:** Do not mix unrelated changes in the same commit.
|
|
10
|
+
- **Separate Refactoring:** Whenever possible, separate refactoring commits from new feature or bugfix commits.
|
|
11
|
+
- Follow `.github/commit_message_template.md`.
|
|
12
|
+
|
|
13
|
+
## Pull Requests
|
|
14
|
+
|
|
15
|
+
- **Descriptive Summary:** Every PR must include a clear and descriptive summary of its purpose and changes (see `.github/pull_request_template.md`).
|
|
16
|
+
- **PR Description Files:** If a description cannot be provided directly in the PR, generate a file with the PR description (e.g., `docs/agents/issues/<pr_number>_description.md`), but do not commit this file.
|
|
17
|
+
|
|
18
|
+
## Definition of Done for PRs
|
|
19
|
+
|
|
20
|
+
A PR is considered complete when:
|
|
21
|
+
|
|
22
|
+
- The stated objective has been achieved.
|
|
23
|
+
- All specs are passing (`bundle exec rspec`).
|
|
24
|
+
- RuboCop passes without offenses (`rubocop`).
|
|
25
|
+
- Every public class, module and method has a YARD docstring, and yardstick passes (`bundle exec rake verify_measurements`).
|
|
26
|
+
- Code coverage is as high as reasonably possible (SimpleCov).
|
|
27
|
+
- Code is not overly complex:
|
|
28
|
+
- Classes and methods should have clear, focused responsibilities.
|
|
29
|
+
- If a class or method is taking on too many responsibilities, refactor to simplify.
|
|
30
|
+
- Methods should be small and do exactly one thing. If a method is growing, extract parts into private helper methods or separate classes.
|
|
31
|
+
- *Example:*
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
# Good: each method does one thing
|
|
35
|
+
class Worker
|
|
36
|
+
def fetch_job; end
|
|
37
|
+
|
|
38
|
+
def process_job(job); end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Bad: method does too much
|
|
42
|
+
class Worker
|
|
43
|
+
def run
|
|
44
|
+
fetch_job
|
|
45
|
+
process_job
|
|
46
|
+
send_metrics
|
|
47
|
+
cleanup
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
- This requirement applies primarily to source code. For specs, refactor only if there is excessive duplication.
|
|
53
|
+
|
|
54
|
+
### CI Checks
|
|
55
|
+
|
|
56
|
+
Before a PR is considered complete, all CI checks must pass locally. This project has a single gem at the root, so both CircleCI jobs apply to any change in `lib/`, `spec/`, `config/` or the root files:
|
|
57
|
+
|
|
58
|
+
| Modified folder(s) | CircleCI job | Local commands |
|
|
59
|
+
| --- | --- | --- |
|
|
60
|
+
| `lib/`, `spec/`, `config/`, root files | `test` | `bundle exec rspec` |
|
|
61
|
+
| `lib/`, `spec/`, `config/`, root files | `checks` | `rubocop`<br>`bundle exec rake verify_measurements`<br>`check_specs` (all files under `lib/` need a spec or an entry in `config/check_specs.yml`)<br>`rubycritic.sh` and `check_readme.sh` (README must reference the current version; these helpers ship in the CI image `darthjee/circleci_rails_gems`) |
|
|
62
|
+
|
|
63
|
+
`docker-compose run test_all` runs rspec, yard and the yardstick checks inside the dev container.
|
|
64
|
+
|
|
65
|
+
Run these same checks when **planning how to resolve an issue**: end the plan with a step that lists the affected folders and the CI commands to run before opening a PR.
|
|
66
|
+
|
|
67
|
+
## Code Organization
|
|
68
|
+
|
|
69
|
+
### File Responsibility: Class Declarers vs Scripts
|
|
70
|
+
|
|
71
|
+
Every source file (excluding specs) must act as a **class declarer**: it defines or reopens one or more classes or modules. Files must not act as **scripts**, meaning they must not run logic or side effects when they are required (beyond `require` statements and class/module definitions).
|
|
72
|
+
|
|
73
|
+
The only entrypoints are the require files:
|
|
74
|
+
|
|
75
|
+
| Purpose | Entrypoint |
|
|
76
|
+
| --- | --- |
|
|
77
|
+
| Gem require (`require 'darthjee/active_ext'`) | `lib/darthjee/active_ext.rb` |
|
|
78
|
+
| Namespace require (`require 'darthjee'`) | `lib/darthjee.rb` |
|
|
79
|
+
|
|
80
|
+
*Example:*
|
|
81
|
+
|
|
82
|
+
```ruby
|
|
83
|
+
# Good: class declarer — reopens a class to add behaviour
|
|
84
|
+
module ActiveRecord
|
|
85
|
+
class Relation
|
|
86
|
+
def percentage(*filters); end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Bad: script — executes logic at load time
|
|
91
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
|
92
|
+
Document.create(status: :error)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Specs and `spec/support/` files are exempt from this rule and may run setup code freely.
|
|
96
|
+
|
|
97
|
+
### File Naming: snake_case Matching the Class
|
|
98
|
+
|
|
99
|
+
Files must use **snake_case** and match the class or module they define (Zeitwerk-style). Files that extend an existing class end in `_ext`.
|
|
100
|
+
|
|
101
|
+
*Examples:*
|
|
102
|
+
|
|
103
|
+
- `lib/darthjee/active_ext.rb` for `Darthjee::ActiveExt`
|
|
104
|
+
- `lib/active_record/relation_ext.rb` for extensions to `ActiveRecord::Relation`
|
|
105
|
+
|
|
106
|
+
Spec files mirror the `lib/` path and end in `_spec.rb`:
|
|
107
|
+
|
|
108
|
+
- `lib/active_record/relation_ext.rb` → `spec/lib/active_record/relation_spec.rb`
|
|
109
|
+
|
|
110
|
+
### Method Order: Public Before Private
|
|
111
|
+
|
|
112
|
+
Within a class, **public methods must be declared before private methods**. Private helpers go at the end of the class body, after a single `private` keyword.
|
|
113
|
+
|
|
114
|
+
```ruby
|
|
115
|
+
# Good: public methods first, private methods last
|
|
116
|
+
class Worker
|
|
117
|
+
def run
|
|
118
|
+
prepare
|
|
119
|
+
execute
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def status; end
|
|
123
|
+
|
|
124
|
+
private
|
|
125
|
+
|
|
126
|
+
def prepare; end
|
|
127
|
+
|
|
128
|
+
def execute; end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Bad: private methods mixed in with or before public methods
|
|
132
|
+
class Worker
|
|
133
|
+
private def prepare; end
|
|
134
|
+
|
|
135
|
+
def run; end
|
|
136
|
+
|
|
137
|
+
private def execute; end
|
|
138
|
+
end
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Dependency Injection
|
|
142
|
+
|
|
143
|
+
Classes must receive their dependencies (data, configuration, collaborators) as `initialize` arguments. A class must never load files, read `ENV` or fetch configuration on its own.
|
|
144
|
+
|
|
145
|
+
**Only the entrypoint, or the host application, loads configuration.** It then passes the loaded data to the classes that need it. Extensions to ActiveRecord must work with whatever connection and models the host app provides, and must not open connections or define models themselves.
|
|
146
|
+
|
|
147
|
+
This makes every class testable on its own: specs instantiate it with the data they need, without touching the filesystem or environment.
|
|
148
|
+
|
|
149
|
+
```ruby
|
|
150
|
+
# Good: receives data as an argument — easy to test
|
|
151
|
+
class Router
|
|
152
|
+
def initialize(data)
|
|
153
|
+
@data = data
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def build; end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# Bad: loads its own config — couples to the filesystem
|
|
160
|
+
class Router
|
|
161
|
+
def build
|
|
162
|
+
data = YAML.load_file('./data.yml') # ❌
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Refactoring Guidelines
|
|
168
|
+
|
|
169
|
+
When refactoring, aim to:
|
|
170
|
+
|
|
171
|
+
- **Reduce Code Duplication:** move repeated spec setup into `let`, `before` blocks, helper methods in `spec/support/`, or shared examples (`spec/support/shared_examples/`).
|
|
172
|
+
|
|
173
|
+
```ruby
|
|
174
|
+
# Good
|
|
175
|
+
shared_examples 'a method that returns the percentage of objects found' do
|
|
176
|
+
# ...
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
it_behaves_like 'a method that returns the percentage of objects found'
|
|
180
|
+
|
|
181
|
+
# Bad: the same setup and expectations copied into many spec files
|
|
182
|
+
```
|
data/docs/agents/flow.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Folder Structure
|
|
2
|
+
|
|
3
|
+
## Project Root
|
|
4
|
+
|
|
5
|
+
| Directory / File | Description |
|
|
6
|
+
|-----------------|-------------|
|
|
7
|
+
| `lib/` | Gem source code: the `darthjee/active_ext` entry point and version, plus the `active_record/` extensions (`relation_ext.rb`, `base_ext.rb`). |
|
|
8
|
+
| `spec/` | RSpec test suite. `spec/lib/` mirrors `lib/`; `spec/support/` holds the test schema, models and shared examples. |
|
|
9
|
+
| `config/` | Tooling config: `check_specs.yml` (files exempt from spec coverage), `yardstick.yml`/`yardstick.rb` (YARD docs quality), `rubycritc.rb` (RubyCritic). |
|
|
10
|
+
| `docs/agents/` | Agent docs: architecture, flow, folder structure, issue specs and implementation plans. |
|
|
11
|
+
| `.github/` | Copilot instructions, PR and commit message templates, and `active_ext-usage.md` (a usage guide for projects that use the gem). |
|
|
12
|
+
| `.circleci/` | CircleCI pipeline config. |
|
|
13
|
+
| `active_ext.gemspec` | Gem spec (published as `darthjee-active_ext`). |
|
|
14
|
+
| `Gemfile` | Development dependencies. |
|
|
15
|
+
| `Rakefile` | Rake tasks (YARD/yardstick measurement, etc.). |
|
|
16
|
+
| `Dockerfile` / `docker-compose.yml` / `Makefile` | Docker dev environment; `make dev` opens a shell, and the `test_all` service runs specs and the docs checks. |
|
|
17
|
+
| `.rubocop.yml` / `.rubocop_todo.yml` | RuboCop lint config. |
|
|
18
|
+
| `AGENTS.md` / `CLAUDE.md` | Agent instructions (`CLAUDE.md` points to `AGENTS.md`). |
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Issue Enhancement
|
|
2
|
+
|
|
3
|
+
A checklist of concerns to consider when fleshing out a vague issue idea (tagged `Idea`/`Writting`) before it reaches the `Created` stage. Not exhaustive — adjust or extend the list for this project's needs.
|
|
4
|
+
|
|
5
|
+
- **Scope boundaries** — what's explicitly in scope and what's explicitly out.
|
|
6
|
+
- **Alternative solutions** — other ways to solve the same problem, and why this one was chosen.
|
|
7
|
+
- **Edge cases** — inputs, states, or timing the happy path doesn't cover.
|
|
8
|
+
- **Backward compatibility** — whether this breaks existing behavior, data, or integrations.
|
|
9
|
+
- **Testing strategy** — how the change will be verified.
|
|
10
|
+
- **Performance & security considerations** — anything relevant to load, latency, or attack surface.
|
|
11
|
+
- **Rails/ActiveRecord compatibility** — whether the method is chainable, works on both `ActiveRecord::Base` and `ActiveRecord::Relation`, and supports the Ruby/Rails versions declared in the gemspec and Gemfile.
|
|
12
|
+
- **Public API & versioning** — whether the gem's public API changes, which semver bump that needs, and which README and YARD examples must be updated.
|
|
13
|
+
- **SQL safety & DB portability** — whether user input can reach raw SQL, and whether the query behaves the same across database adapters (SQLite in specs; Postgres/MySQL in host apps).
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -1,10 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'active_record'
|
|
2
4
|
|
|
3
5
|
module ActiveRecord
|
|
6
|
+
# Extends +ActiveRecord::Base+ with additional class methods.
|
|
7
|
+
|
|
8
|
+
# These methods are delegated to the `all` scope of the model,
|
|
9
|
+
# allowing you to call them directly on the model class.
|
|
4
10
|
class Base
|
|
5
11
|
class << self
|
|
6
|
-
delegate :percentage, :pluck_as_json, to: :all
|
|
12
|
+
delegate :percentage, :pluck_as_json, :scopped, to: :all
|
|
13
|
+
|
|
14
|
+
# @method percentage
|
|
15
|
+
# @api public
|
|
16
|
+
#
|
|
17
|
+
# Calculates the percentage of records that match a given condition
|
|
18
|
+
# @overload (see ActiveRecord::Relation#percentage)
|
|
19
|
+
#
|
|
20
|
+
# @see ActiveRecord::Relation#percentage
|
|
21
|
+
# @param (see ActiveRecord::Relation#percentage)
|
|
22
|
+
# @return (see ActiveRecord::Relation#percentage)
|
|
23
|
+
# @example (see ActiveRecord::Relation#percentage)
|
|
24
|
+
|
|
25
|
+
# @method scopped
|
|
26
|
+
# @api public
|
|
27
|
+
#
|
|
28
|
+
# Applies the given filters to the relation, returning a new relation
|
|
29
|
+
# @overload (see ActiveRecord::Relation#scopped)
|
|
30
|
+
# @see ActiveRecord::Relation#scopped
|
|
31
|
+
# @param (see ActiveRecord::Relation#scopped)
|
|
32
|
+
# @return (see ActiveRecord::Relation#scopped)
|
|
33
|
+
# @example (see ActiveRecord::Relation#scopped)
|
|
34
|
+
|
|
35
|
+
# @method pluck_as_json
|
|
36
|
+
# @api public
|
|
37
|
+
#
|
|
38
|
+
# Plucks specified columns and returns an array of hashes
|
|
39
|
+
#
|
|
40
|
+
# @overload (see ActiveRecord::Relation#pluck_as_json)
|
|
41
|
+
# @see ActiveRecord::Relation#pluck_as_json
|
|
42
|
+
# @param (see ActiveRecord::Relation#pluck_as_json)
|
|
43
|
+
# @return (see ActiveRecord::Relation#pluck_as_json)
|
|
44
|
+
# @example (see ActiveRecord::Relation#pluck_as_json)
|
|
7
45
|
end
|
|
8
46
|
end
|
|
9
47
|
end
|
|
10
|
-
|
|
@@ -1,23 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require 'active_record'
|
|
2
4
|
|
|
3
5
|
module ActiveRecord
|
|
6
|
+
# Extends +ActiveRecord::Relation+ with additional instance methods.
|
|
7
|
+
#
|
|
8
|
+
# These methods are available on all ActiveRecord relations.
|
|
4
9
|
class Relation
|
|
10
|
+
# @api public
|
|
11
|
+
#
|
|
12
|
+
# Returns the percentage of records that match the given filter
|
|
13
|
+
#
|
|
14
|
+
# @overload percentage(**conditions)
|
|
15
|
+
# @param conditions [Hash] a hash of column names and values to match.
|
|
16
|
+
# @overload percentage(sql_condition)
|
|
17
|
+
# @param sql_condition [String] a raw SQL condition to evaluate.
|
|
18
|
+
# @overload percentage(*scopes)
|
|
19
|
+
# @param scopes [Array<Symbol>] one or more named scopes to chain.
|
|
20
|
+
#
|
|
21
|
+
# @return [Float] fraction of matching records (0.0–1.0), or +0.0+
|
|
22
|
+
# when the relation is empty (to avoid division by zero).
|
|
23
|
+
#
|
|
24
|
+
# @note Do *not* mix Symbol scope names with Hash or String conditions in a
|
|
25
|
+
# single call. Use Symbols together, or a single Hash/String — never both
|
|
26
|
+
# forms at once.
|
|
27
|
+
#
|
|
28
|
+
# @example Using a named scope
|
|
29
|
+
# Document.percentage(:with_error) #=> 0.75
|
|
30
|
+
#
|
|
31
|
+
# @example Using a hash condition
|
|
32
|
+
# Document.percentage(status: :error) #=> 0.75
|
|
33
|
+
#
|
|
34
|
+
# @example Using a raw SQL string
|
|
35
|
+
# Document.percentage("status = 'error'") #=> 0.75
|
|
36
|
+
#
|
|
37
|
+
# @example Chaining multiple named scopes
|
|
38
|
+
# Document.percentage(:active, :with_error) #=> 0.25
|
|
39
|
+
#
|
|
40
|
+
# @example Within a nested scope
|
|
41
|
+
# Document.active.percentage(:with_error) #=> 0.5
|
|
42
|
+
#
|
|
43
|
+
# @example Empty relation returns 0.0, not a Float
|
|
44
|
+
# Document.where(id: nil).percentage(:with_error) #=> 0.0
|
|
45
|
+
#
|
|
46
|
+
# @see #scopped for the underlying implementation of filter application.
|
|
5
47
|
def percentage(*filters)
|
|
6
|
-
return 0 if count
|
|
48
|
+
return 0.0 if count.zero?
|
|
7
49
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
relation.public_send(scope)
|
|
11
|
-
end
|
|
12
|
-
else
|
|
13
|
-
filtered = where(*filters)
|
|
14
|
-
end
|
|
50
|
+
scopped(*filters).count / count.to_f
|
|
51
|
+
end
|
|
15
52
|
|
|
16
|
-
|
|
53
|
+
# @api private
|
|
54
|
+
#
|
|
55
|
+
# Applies the given filters to the relation, returning a new relation
|
|
56
|
+
#
|
|
57
|
+
# This is a helper method for +percentage+ to handle both named scopes and
|
|
58
|
+
# arbitrary conditions.
|
|
59
|
+
#
|
|
60
|
+
# @overload scopped(**conditions)
|
|
61
|
+
# @param conditions [Hash] a hash of column names and values to match.
|
|
62
|
+
# @overload scopped(sql_condition)
|
|
63
|
+
# @param sql_condition [String] a raw SQL condition to evaluate.
|
|
64
|
+
# @overload scopped(*scopes)
|
|
65
|
+
# @param scopes [Array<Symbol>] one or more named scopes to chain.
|
|
66
|
+
#
|
|
67
|
+
# @return [ActiveRecord::Relation] a new relation with the filters applied.
|
|
68
|
+
#
|
|
69
|
+
# @example Using a named scope
|
|
70
|
+
# Document.scopped(:with_error) #=> returns relation with :with_error scope applied
|
|
71
|
+
#
|
|
72
|
+
# @example Using a hash condition
|
|
73
|
+
# Document.scopped(status: :error) #=> returns relation where status is error
|
|
74
|
+
#
|
|
75
|
+
# @example Using a raw SQL string
|
|
76
|
+
# Document.scopped("status = 'error'") #=> returns relation where status is error
|
|
77
|
+
#
|
|
78
|
+
# @example Chaining multiple named scopes
|
|
79
|
+
# Document.scopped(:active, :with_error) #=> returns relation with both scopes applied
|
|
80
|
+
def scopped(*filters)
|
|
81
|
+
return where(*filters) unless filters.first.is_a?(Symbol)
|
|
82
|
+
|
|
83
|
+
filters.inject(self) do |relation, scope|
|
|
84
|
+
relation.public_send(scope)
|
|
85
|
+
end
|
|
17
86
|
end
|
|
18
87
|
|
|
88
|
+
# @api public
|
|
89
|
+
#
|
|
90
|
+
# Returns an array of hashes for the selected columns, one hash per record
|
|
91
|
+
#
|
|
92
|
+
# This is similar to +pluck+, but each row is represented as a Hash keyed
|
|
93
|
+
# by column name (as a Symbol) rather than a plain Array.
|
|
94
|
+
#
|
|
95
|
+
# When called with no arguments, returns the full +as_json+ representation
|
|
96
|
+
# of every record in the relation.
|
|
97
|
+
#
|
|
98
|
+
# @overload pluck_as_json(*keys)
|
|
99
|
+
# @param keys [Array<Symbol>] column names to include in each hash.
|
|
100
|
+
# @overload pluck_as_json()
|
|
101
|
+
#
|
|
102
|
+
# @return [Array<Hash>] array of hashes mapping column name to value.
|
|
103
|
+
#
|
|
104
|
+
# @example Selecting specific columns
|
|
105
|
+
# Document.pluck_as_json(:id, :status)
|
|
106
|
+
# #=> [{ id: 1, status: "error" }, { id: 2, status: "success" }]
|
|
107
|
+
#
|
|
108
|
+
# @example No arguments returns all columns
|
|
109
|
+
# Document.pluck_as_json
|
|
110
|
+
# #=> [{ id: 1, status: "error", active: false, ... }, ...]
|
|
111
|
+
#
|
|
112
|
+
# @example Works with scope chains
|
|
113
|
+
# Document.active.pluck_as_json(:id, :status)
|
|
114
|
+
# #=> [{ id: 2, status: "success" }]
|
|
19
115
|
def pluck_as_json(*keys)
|
|
20
|
-
|
|
116
|
+
return map(&:as_json) if keys.empty?
|
|
117
|
+
|
|
118
|
+
pluck(*keys).map { |entry| entry.as_hash(keys) }
|
|
21
119
|
end
|
|
22
120
|
end
|
|
23
121
|
end
|