auto_decorator 0.1.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 +7 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +131 -0
- data/lib/auto_decorator/configuration.rb +12 -0
- data/lib/auto_decorator/loader.rb +35 -0
- data/lib/auto_decorator/railtie.rb +16 -0
- data/lib/auto_decorator/version.rb +5 -0
- data/lib/auto_decorator.rb +22 -0
- data/lib/generators/auto_decorator/decorator_generator.rb +26 -0
- data/lib/generators/auto_decorator/templates/decorator.rb.tt +10 -0
- metadata +125 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 711dd9d73ec942f2a199ab3151470494e23dc08a3b2116b615c748ea4dafe91b
|
|
4
|
+
data.tar.gz: fa65f54f123227f42f820748df620371dfee653e67b7fca04daeacb28111cfb1
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: df20999fee449028a65f637e6fa6c2fa0952327bf10ed08d1b2b71fee8870c636b7f030d6f93af0b0af2ebee789bdbd892ba10710ffe0de5ce4062b14f6cb7b4
|
|
7
|
+
data.tar.gz: 3b0b18150ebb2abbd65952e7060335eaa2258e8f88d177ea405a7888ee2cf303818d911f879345f640425b75ca7d24eba21cb72a1910ec578821d9474da2b2e4
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alexey Poimtsev
|
|
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,131 @@
|
|
|
1
|
+
# auto_decorator
|
|
2
|
+
|
|
3
|
+
Convention-based decorator autoloading for Rails models.
|
|
4
|
+
|
|
5
|
+
Decorators are plain Ruby modules that get automatically included into your model classes based on file naming convention — no configuration required.
|
|
6
|
+
|
|
7
|
+
> **Why not Draper?** Draper wraps objects in presenter classes. `auto_decorator` adds methods directly to the model. Less indirection, less boilerplate, same result for most use cases.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Add to your Gemfile:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
gem "auto_decorator"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### Convention
|
|
20
|
+
|
|
21
|
+
Place decorator files in `app/decorators/`. The gem discovers them automatically and includes each module into the matching model class:
|
|
22
|
+
|
|
23
|
+
| File | Module | Included into |
|
|
24
|
+
| ---------------------------------------------------- | ---------------------------------- | ------------------------- |
|
|
25
|
+
| `app/decorators/user_decorator.rb` | `UserDecorator` | `User` |
|
|
26
|
+
| `app/decorators/organization_decorator.rb` | `OrganizationDecorator` | `Organization` |
|
|
27
|
+
| `app/decorators/organizations/employee_decorator.rb` | `Organizations::EmployeeDecorator` | `Organizations::Employee` |
|
|
28
|
+
|
|
29
|
+
### Example
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
# app/decorators/user_decorator.rb
|
|
33
|
+
module UserDecorator
|
|
34
|
+
def full_name
|
|
35
|
+
[first_name, last_name].compact.join(" ")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def to_s
|
|
39
|
+
full_name.presence || email
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
# app/decorators/organizations/employee_decorator.rb
|
|
46
|
+
module Organizations
|
|
47
|
+
module EmployeeDecorator
|
|
48
|
+
def active?
|
|
49
|
+
status == "active"
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Since the modules are included directly into the model, decorated methods are available anywhere the model is used:
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
user = User.find(1)
|
|
59
|
+
user.full_name # => "Alice Smith"
|
|
60
|
+
user.to_s # => "Alice Smith"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Generator
|
|
64
|
+
|
|
65
|
+
Scaffold a new decorator with:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
rails g decorator User
|
|
69
|
+
# → creates app/decorators/user_decorator.rb
|
|
70
|
+
|
|
71
|
+
rails g decorator Organizations::Employee
|
|
72
|
+
# → creates app/decorators/organizations/employee_decorator.rb
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Generated file for `User`:
|
|
76
|
+
|
|
77
|
+
```ruby
|
|
78
|
+
# frozen_string_literal: true
|
|
79
|
+
|
|
80
|
+
module UserDecorator
|
|
81
|
+
end
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Generated file for `Organizations::Employee`:
|
|
85
|
+
|
|
86
|
+
```ruby
|
|
87
|
+
# frozen_string_literal: true
|
|
88
|
+
|
|
89
|
+
module Organizations
|
|
90
|
+
module EmployeeDecorator
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Configuration
|
|
96
|
+
|
|
97
|
+
The gem works with zero configuration. To override defaults:
|
|
98
|
+
|
|
99
|
+
```ruby
|
|
100
|
+
# config/initializers/auto_decorator.rb
|
|
101
|
+
AutoDecorator.configure do |config|
|
|
102
|
+
config.decorators_path = "app/decorators" # relative to Rails.root
|
|
103
|
+
config.decorator_suffix = "Decorator"
|
|
104
|
+
end
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## How it works
|
|
108
|
+
|
|
109
|
+
On every `config.to_prepare` (triggered on boot and in development on each request):
|
|
110
|
+
|
|
111
|
+
1. Glob all `*_decorator.rb` files under `decorators_path`
|
|
112
|
+
2. Derive the module name from the file path: `organizations/employee_decorator.rb` → `Organizations::EmployeeDecorator`
|
|
113
|
+
3. Derive the model name by stripping the suffix: `Organizations::EmployeeDecorator` → `Organizations::Employee`
|
|
114
|
+
4. Call `ModelClass.include(DecoratorModule)` — skips if already included
|
|
115
|
+
|
|
116
|
+
If the model class doesn't exist (e.g. a decorator for a non-existent model), it is silently skipped.
|
|
117
|
+
|
|
118
|
+
## Compatibility
|
|
119
|
+
|
|
120
|
+
| | |
|
|
121
|
+
| ----- | --------- |
|
|
122
|
+
| Ruby | ≥ 3.2 |
|
|
123
|
+
| Rails | 7.0 – 8.1 |
|
|
124
|
+
|
|
125
|
+
## Contributing
|
|
126
|
+
|
|
127
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/alec-c4/auto_decorator).
|
|
128
|
+
|
|
129
|
+
## License
|
|
130
|
+
|
|
131
|
+
[MIT](LICENSE.txt)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pathname"
|
|
4
|
+
|
|
5
|
+
module AutoDecorator
|
|
6
|
+
class Loader
|
|
7
|
+
def self.call(base_path:, suffix: "Decorator")
|
|
8
|
+
base = Pathname.new(base_path)
|
|
9
|
+
Dir.glob(base.join("**/*_#{suffix.downcase}.rb")).each do |file|
|
|
10
|
+
load_file(file, base, suffix)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.load_file(file, base_path, suffix)
|
|
15
|
+
relative = Pathname.new(file).relative_path_from(base_path).to_s.delete_suffix(".rb")
|
|
16
|
+
decorator_name = relative.split("/").map { |part| camelize(part) }.join("::")
|
|
17
|
+
model_name = decorator_name.delete_suffix(suffix)
|
|
18
|
+
|
|
19
|
+
load file
|
|
20
|
+
decorator_module = Object.const_get(decorator_name)
|
|
21
|
+
model_class = Object.const_get(model_name)
|
|
22
|
+
return if model_class.include?(decorator_module)
|
|
23
|
+
|
|
24
|
+
model_class.include(decorator_module)
|
|
25
|
+
rescue NameError
|
|
26
|
+
# Model or decorator constant not found — skip silently
|
|
27
|
+
end
|
|
28
|
+
private_class_method :load_file
|
|
29
|
+
|
|
30
|
+
def self.camelize(str)
|
|
31
|
+
str.split("_").map(&:capitalize).join
|
|
32
|
+
end
|
|
33
|
+
private_class_method :camelize
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails"
|
|
4
|
+
|
|
5
|
+
module AutoDecorator
|
|
6
|
+
class Railtie < Rails::Railtie
|
|
7
|
+
initializer "auto_decorator.load" do
|
|
8
|
+
config.to_prepare do
|
|
9
|
+
AutoDecorator::Loader.call(
|
|
10
|
+
base_path: Rails.root.join(AutoDecorator.configuration.decorators_path).to_s,
|
|
11
|
+
suffix: AutoDecorator.configuration.decorator_suffix
|
|
12
|
+
)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "auto_decorator/version"
|
|
4
|
+
require_relative "auto_decorator/configuration"
|
|
5
|
+
require_relative "auto_decorator/loader"
|
|
6
|
+
require_relative "auto_decorator/railtie"
|
|
7
|
+
|
|
8
|
+
module AutoDecorator
|
|
9
|
+
class << self
|
|
10
|
+
def configuration
|
|
11
|
+
@configuration ||= Configuration.new
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def configure
|
|
15
|
+
yield configuration
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def reset_configuration!
|
|
19
|
+
@configuration = Configuration.new
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/generators"
|
|
4
|
+
|
|
5
|
+
module AutoDecorator
|
|
6
|
+
module Generators
|
|
7
|
+
class DecoratorGenerator < Rails::Generators::NamedBase
|
|
8
|
+
source_root File.expand_path("templates", __dir__)
|
|
9
|
+
|
|
10
|
+
def create_decorator_file
|
|
11
|
+
template "decorator.rb.tt",
|
|
12
|
+
File.join("app/decorators", class_path.join("/"), "#{file_name}_decorator.rb")
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def decorator_name
|
|
18
|
+
"#{class_name.demodulize}Decorator"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def namespaces
|
|
22
|
+
class_name.deconstantize.split("::").reject(&:empty?)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
<% namespaces.each_with_index do |ns, i| -%>
|
|
4
|
+
<%= " " * i %>module <%= ns %>
|
|
5
|
+
<% end -%>
|
|
6
|
+
<%= " " * namespaces.size %>module <%= decorator_name %>
|
|
7
|
+
<%= " " * namespaces.size %>end
|
|
8
|
+
<% namespaces.reverse.each_with_index do |_ns, i| -%>
|
|
9
|
+
<%= " " * (namespaces.size - 1 - i) %>end
|
|
10
|
+
<% end -%>
|
metadata
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: auto_decorator
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Alexey Poimtsev
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: railties
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '7.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '7.0'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: rails
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '7.0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '7.0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: simplecov
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0.21'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0.21'
|
|
54
|
+
- !ruby/object:Gem::Dependency
|
|
55
|
+
name: appraisal
|
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: '0'
|
|
61
|
+
type: :development
|
|
62
|
+
prerelease: false
|
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: '0'
|
|
68
|
+
- !ruby/object:Gem::Dependency
|
|
69
|
+
name: lefthook
|
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
type: :development
|
|
76
|
+
prerelease: false
|
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '0'
|
|
82
|
+
description: Automatically includes decorator modules into Rails model classes based
|
|
83
|
+
on file naming convention. Zero configuration required.
|
|
84
|
+
email:
|
|
85
|
+
- alexey.poimtsev@gmail.com
|
|
86
|
+
executables: []
|
|
87
|
+
extensions: []
|
|
88
|
+
extra_rdoc_files: []
|
|
89
|
+
files:
|
|
90
|
+
- CHANGELOG.md
|
|
91
|
+
- LICENSE.txt
|
|
92
|
+
- README.md
|
|
93
|
+
- lib/auto_decorator.rb
|
|
94
|
+
- lib/auto_decorator/configuration.rb
|
|
95
|
+
- lib/auto_decorator/loader.rb
|
|
96
|
+
- lib/auto_decorator/railtie.rb
|
|
97
|
+
- lib/auto_decorator/version.rb
|
|
98
|
+
- lib/generators/auto_decorator/decorator_generator.rb
|
|
99
|
+
- lib/generators/auto_decorator/templates/decorator.rb.tt
|
|
100
|
+
homepage: https://github.com/alec-c4/auto_decorator
|
|
101
|
+
licenses:
|
|
102
|
+
- MIT
|
|
103
|
+
metadata:
|
|
104
|
+
homepage_uri: https://github.com/alec-c4/auto_decorator
|
|
105
|
+
source_code_uri: https://github.com/alec-c4/auto_decorator
|
|
106
|
+
changelog_uri: https://github.com/alec-c4/auto_decorator/blob/main/CHANGELOG.md
|
|
107
|
+
rubygems_mfa_required: 'true'
|
|
108
|
+
rdoc_options: []
|
|
109
|
+
require_paths:
|
|
110
|
+
- lib
|
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
|
+
requirements:
|
|
113
|
+
- - ">="
|
|
114
|
+
- !ruby/object:Gem::Version
|
|
115
|
+
version: 3.2.0
|
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
|
+
requirements:
|
|
118
|
+
- - ">="
|
|
119
|
+
- !ruby/object:Gem::Version
|
|
120
|
+
version: '0'
|
|
121
|
+
requirements: []
|
|
122
|
+
rubygems_version: 4.0.6
|
|
123
|
+
specification_version: 4
|
|
124
|
+
summary: Convention-based decorator autoloading for Rails models
|
|
125
|
+
test_files: []
|