activeadmin_decorator 0.2.0 → 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 +4 -4
- data/.rubocop.yml +2 -0
- data/Gemfile +1 -0
- data/README.md +4 -0
- data/lib/activeadmin/arbre_decorator.rb +1 -1
- data/lib/activeadmin/decorator/association.rb +45 -0
- data/lib/activeadmin/decorator.rb +13 -18
- data/lib/activeadmin_decorator/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c5fef3015a46bcf7b93b09a8a12018f117e4c301e208b24dba8b2f4405c5f81e
|
4
|
+
data.tar.gz: 89ef8d221f034e59303f563253cedbf3c6a49a49b5bf105ab5c550ec10822fe4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a0fa368c1a9acaf58cc87c76147fac4d5cdcc19404c0afbf137b6e0447eb8e8f89f927e492789b001e5d62a0435e6e6f7e5bb904975b8a2e1848065154ac5ea
|
7
|
+
data.tar.gz: aa450eea02e047f607b1552c8428585bada299f387d9f84900979a734e28f555febdcbef55775c2f4eee262fd20ee15917133ddb3b8983abff2f1521cf94716e
|
data/.rubocop.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -39,6 +39,9 @@ end
|
|
39
39
|
Each decorated association will be available as a method on the decorator,
|
40
40
|
you can still access the original association with `model.association_name`.
|
41
41
|
|
42
|
+
The association decorator class name will be auto-detected from the relation result and current decorator name if not given.
|
43
|
+
Example for `:comments` association on `Decorators::UserDecorator` it will be `Decorators::CommentDecorator`.
|
44
|
+
|
42
45
|
### ArbreDecorator
|
43
46
|
|
44
47
|
With `ActiveAdmin::ArbreDecorator` you can keep your show/index blocks in AA clean and use Arbre DSL in decorator:
|
@@ -52,6 +55,7 @@ class UserDecorator < ActiveAdmin::ArbreDecorator
|
|
52
55
|
end
|
53
56
|
end
|
54
57
|
```
|
58
|
+
This is done by using including `Arbre::Element::BuilderMethods` and new `arbre_context`.
|
55
59
|
|
56
60
|
Also included: `ActionView::Helpers` and `Rails.application.routes.url_helpers`,
|
57
61
|
so you can:
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveAdmin
|
4
|
+
class Decorator < SimpleDelegator
|
5
|
+
module Association
|
6
|
+
class << self
|
7
|
+
def decorate(association, with: nil, parent: nil)
|
8
|
+
raise ArgumentError, "parent or with required" if parent.nil? && with.nil?
|
9
|
+
|
10
|
+
if association.nil?
|
11
|
+
nil
|
12
|
+
elsif association.respond_to?(:each)
|
13
|
+
decorate_many(association, with, parent)
|
14
|
+
else
|
15
|
+
decorate_one(association, with, parent)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def decorate_many(association, with, parent)
|
20
|
+
with ||=
|
21
|
+
if association.is_a?(ActiveRecord::Relation)
|
22
|
+
decorator_class_name_for(parent, association.klass)
|
23
|
+
else
|
24
|
+
decorator_class_name_for(parent, association.first.class)
|
25
|
+
end
|
26
|
+
with = with.constantize if with.is_a?(String)
|
27
|
+
association.map { |item| with.new(item) }
|
28
|
+
end
|
29
|
+
|
30
|
+
def decorate_one(element, with, parent)
|
31
|
+
with ||= decorator_class_name_for(parent, element.class)
|
32
|
+
with = with.constantize if with.is_a?(String)
|
33
|
+
with.new(element)
|
34
|
+
end
|
35
|
+
|
36
|
+
def decorator_class_name_for(parent, klass)
|
37
|
+
parent_class_elements = parent.class.name.split("::")
|
38
|
+
prefix = parent_class_elements[0...-1]
|
39
|
+
suffix = parent_class_elements[-1].sub(/^#{parent.model.class.name}/, "")
|
40
|
+
[*prefix, klass].join("::").concat(suffix)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "delegate"
|
4
|
+
require_relative "decorator/association"
|
4
5
|
|
5
6
|
module ActiveAdmin
|
6
7
|
class Decorator < SimpleDelegator
|
@@ -8,29 +9,23 @@ module ActiveAdmin
|
|
8
9
|
# Utility method for ActiveAdmin
|
9
10
|
def decorate(*args)
|
10
11
|
object = args[0]
|
11
|
-
|
12
|
-
object.map { |o| new(o) }
|
13
|
-
else
|
14
|
-
new(object)
|
15
|
-
end
|
12
|
+
Association.decorate(object, with: self)
|
16
13
|
end
|
17
14
|
|
18
15
|
# use in decorator to decorate association
|
19
|
-
def decorates_association(association, relation: association, with:
|
16
|
+
def decorates_association(association, relation: association, with: nil) # rubocop:disable Metrics/MethodLength
|
17
|
+
raise ArgumentError, "relation must be a Symbol or Proc" unless relation.is_a?(Symbol) || relation.is_a?(Proc)
|
18
|
+
|
20
19
|
define_method(association) do
|
21
|
-
|
22
|
-
|
23
|
-
when Symbol then model.send(relation)
|
24
|
-
when Proc then relation.call(model)
|
25
|
-
else raise ArgumentError, "relation must be a Symbol or Proc"
|
26
|
-
end
|
27
|
-
with = with.constantize if with.is_a?(String)
|
28
|
-
if associated.is_a?(ActiveRecord::Relation)
|
29
|
-
associated = associated.map { |item| with.new(item) }
|
20
|
+
if instance_variable_defined?("@#{association}_decorated")
|
21
|
+
then instance_variable_get("@#{association}_decorated")
|
30
22
|
else
|
31
|
-
|
23
|
+
result =
|
24
|
+
if relation.is_a?(Proc) then relation.call(model)
|
25
|
+
elsif relation.is_a?(Symbol) then model.send(relation)
|
26
|
+
end
|
27
|
+
instance_variable_set("@#{association}_decorated", Association.decorate(result, with:, parent: self))
|
32
28
|
end
|
33
|
-
associated
|
34
29
|
end
|
35
30
|
end
|
36
31
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activeadmin_decorator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michal Papis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-11-
|
11
|
+
date: 2023-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activeadmin
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- Rakefile
|
70
70
|
- lib/activeadmin/arbre_decorator.rb
|
71
71
|
- lib/activeadmin/decorator.rb
|
72
|
+
- lib/activeadmin/decorator/association.rb
|
72
73
|
- lib/activeadmin_decorator.rb
|
73
74
|
- lib/activeadmin_decorator/version.rb
|
74
75
|
- sig/activeadmin_decorator.rbs
|