magic-presenter 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -0
- data/{lib → app/models/concerns}/magic/presentable.rb +8 -0
- data/config/initializers/action_view.rb +19 -0
- data/lib/magic/presenter/base.rb +19 -0
- data/lib/magic/presenter/global_id.rb +35 -0
- data/lib/magic/presenter/version.rb +1 -1
- data/lib/magic/presenter.rb +14 -2
- metadata +10 -9
- data/config/initializers/loading.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17e4d44c0c2d9a9e09e7e099a4067b9e6aafe27e1b389cf01808ed0a77d0b9ae
|
4
|
+
data.tar.gz: 7d492486c2f4edb2407af461794e1f0c7e53c7f6b93e65595a618369fb845250
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb2609c3dbba1cec025bca22aaa196c718b32084b2954b8184f9b5ce896e7a9289c0b7eb0660469edaf3571d27d99026f6e588f58004e395cee07a75820e38d7
|
7
|
+
data.tar.gz: 1a87908713cb7dbcb029687dd1d4b9006432e8ba9703ef1dbe813088060c471ada70f8d7d1f5de4cdc9210ba602bd0457b1bf061f6fd3d9a25c5a420003198e0
|
data/README.md
CHANGED
@@ -103,6 +103,12 @@ When in doubt, one can use `Magic::Presenter.name_for`:
|
|
103
103
|
Magic::Presenter.name_for Person # => "PersonPresenter"
|
104
104
|
```
|
105
105
|
|
106
|
+
### In views
|
107
|
+
|
108
|
+
> [!IMPORTANT]
|
109
|
+
> Every object passed to views is decorated automagically.
|
110
|
+
> This involves both implicit instance variables and `locals` passed explicitly.
|
111
|
+
|
106
112
|
## Development
|
107
113
|
|
108
114
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
ActiveSupport.on_load :action_view do
|
4
|
+
concerning :DecoratedAssignments, prepend: true do
|
5
|
+
def assign(assignments, ...)
|
6
|
+
assignments
|
7
|
+
.transform_values! &:decorated
|
8
|
+
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
def _run(method, template, locals, ...)
|
13
|
+
locals
|
14
|
+
.transform_values! &:decorated
|
15
|
+
|
16
|
+
super
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/magic/presenter/base.rb
CHANGED
@@ -17,6 +17,8 @@ module Magic
|
|
17
17
|
# If not found, it looks for presenters of its ancestor classes,
|
18
18
|
# up to `ObjectPresenter`.
|
19
19
|
class Base < Decorator::Base
|
20
|
+
include GlobalID if defined? ::GlobalID
|
21
|
+
|
20
22
|
class << self
|
21
23
|
def name_for object_class
|
22
24
|
object_class
|
@@ -25,6 +27,23 @@ module Magic
|
|
25
27
|
.delete_suffix('Record')
|
26
28
|
.then { "#{_1}Presenter" }
|
27
29
|
end
|
30
|
+
|
31
|
+
def model_class
|
32
|
+
Presentable.classes
|
33
|
+
.select { Presenter.for(_1) == self }
|
34
|
+
.sole
|
35
|
+
rescue Enumerable::SoleItemExpectedError => error
|
36
|
+
raise Lookup::Error, "#{error.message
|
37
|
+
.sub('items', 'model classes')
|
38
|
+
.sub('item', 'model class')
|
39
|
+
} for #{self}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def descendants
|
43
|
+
Magic.eager_load :presenters
|
44
|
+
|
45
|
+
super
|
46
|
+
end
|
28
47
|
end
|
29
48
|
end
|
30
49
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Magic
|
4
|
+
module Presenter
|
5
|
+
# = Active Job integration
|
6
|
+
#
|
7
|
+
# Active Job [accepts Active Record objects](
|
8
|
+
# http://edgeguides.rubyonrails.org/active_job_basics.html#globalid
|
9
|
+
# ).
|
10
|
+
# An object passed to a background job must implement [Global ID](
|
11
|
+
# https://github.com/rails/globalid
|
12
|
+
# ).
|
13
|
+
#
|
14
|
+
# This module implements Global ID for decorated models by defining
|
15
|
+
# `.find` method that uses the one from a model class and decorates
|
16
|
+
# the result.
|
17
|
+
# This means one can pass decorated objects to background jobs and
|
18
|
+
# get them decorated when deserialized.
|
19
|
+
module GlobalID
|
20
|
+
extend ActiveSupport::Concern
|
21
|
+
|
22
|
+
included do
|
23
|
+
include ::GlobalID::Identification
|
24
|
+
end
|
25
|
+
|
26
|
+
class_methods do
|
27
|
+
def find(...)
|
28
|
+
model_class
|
29
|
+
.find(...)
|
30
|
+
.decorated
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/magic/presenter.rb
CHANGED
@@ -6,11 +6,10 @@ require_relative 'presenter/version'
|
|
6
6
|
require_relative 'presenter/engine'
|
7
7
|
|
8
8
|
module Magic # :nodoc:
|
9
|
-
autoload :Presentable, 'magic/presentable'
|
10
|
-
|
11
9
|
# Presentation layer for Rails models
|
12
10
|
module Presenter
|
13
11
|
autoload :Base, 'magic/presenter/base'
|
12
|
+
autoload :GlobalID, 'magic/presenter/global_id'
|
14
13
|
autoload :Generator, 'generators/magic/presenter/generator'
|
15
14
|
|
16
15
|
module_function
|
@@ -18,4 +17,17 @@ module Magic # :nodoc:
|
|
18
17
|
def for(...) = Base.for(...)
|
19
18
|
def name_for(...) = Base.name_for(...)
|
20
19
|
end
|
20
|
+
|
21
|
+
module_function
|
22
|
+
|
23
|
+
def eager_load *scopes
|
24
|
+
return if Rails.application.config.eager_load
|
25
|
+
|
26
|
+
scopes
|
27
|
+
.map(&:to_s)
|
28
|
+
.map(&:pluralize)
|
29
|
+
.map { Rails.root / 'app' / _1 }
|
30
|
+
.select(&:exist?)
|
31
|
+
.each { Rails.autoloaders.main.eager_load_dir _1 }
|
32
|
+
end
|
21
33
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magic-presenter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Senko
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2024-10-
|
10
|
+
date: 2024-10-27 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: rails
|
@@ -33,16 +33,16 @@ dependencies:
|
|
33
33
|
name: magic-decorator
|
34
34
|
requirement: !ruby/object:Gem::Requirement
|
35
35
|
requirements:
|
36
|
-
- - "
|
36
|
+
- - "~>"
|
37
37
|
- !ruby/object:Gem::Version
|
38
|
-
version: 0.3
|
38
|
+
version: '0.3'
|
39
39
|
type: :runtime
|
40
40
|
prerelease: false
|
41
41
|
version_requirements: !ruby/object:Gem::Requirement
|
42
42
|
requirements:
|
43
|
-
- - "
|
43
|
+
- - "~>"
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
version: 0.3
|
45
|
+
version: '0.3'
|
46
46
|
description: Based on Magic Decorator, it’s meant to replace Draper.
|
47
47
|
email:
|
48
48
|
- Alexander.Senko@gmail.com
|
@@ -53,7 +53,8 @@ files:
|
|
53
53
|
- MIT-LICENSE
|
54
54
|
- README.md
|
55
55
|
- Rakefile
|
56
|
-
-
|
56
|
+
- app/models/concerns/magic/presentable.rb
|
57
|
+
- config/initializers/action_view.rb
|
57
58
|
- config/initializers/presentable.rb
|
58
59
|
- config/initializers/rspec.rb
|
59
60
|
- lib/generators/magic/presenter/generator.rb
|
@@ -67,11 +68,11 @@ files:
|
|
67
68
|
- lib/generators/rspec/presenter/templates/presenter_spec.rb.tt
|
68
69
|
- lib/generators/test_unit/presenter/presenter_generator.rb
|
69
70
|
- lib/generators/test_unit/presenter/templates/presenter_test.rb.tt
|
70
|
-
- lib/magic/presentable.rb
|
71
71
|
- lib/magic/presenter.rb
|
72
72
|
- lib/magic/presenter/authors.rb
|
73
73
|
- lib/magic/presenter/base.rb
|
74
74
|
- lib/magic/presenter/engine.rb
|
75
|
+
- lib/magic/presenter/global_id.rb
|
75
76
|
- lib/magic/presenter/version.rb
|
76
77
|
- lib/tasks/magic/presenter_tasks.rake
|
77
78
|
homepage: https://github.com/Alexander-Senko/magic-presenter
|
@@ -80,7 +81,7 @@ licenses:
|
|
80
81
|
metadata:
|
81
82
|
homepage_uri: https://github.com/Alexander-Senko/magic-presenter
|
82
83
|
source_code_uri: https://github.com/Alexander-Senko/magic-presenter
|
83
|
-
changelog_uri: https://github.com/Alexander-Senko/magic-presenter/blob/v0.
|
84
|
+
changelog_uri: https://github.com/Alexander-Senko/magic-presenter/blob/v0.3.0/CHANGELOG.md
|
84
85
|
rdoc_options: []
|
85
86
|
require_paths:
|
86
87
|
- lib
|