activeadmin_decorator 0.1.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0dbd1ee7dba87fb7db7df96d74b0c9f7ad65f228d49501eeb0dc6dcb375c543d
4
- data.tar.gz: a6857c139caad372f9c969aacd1dd4c635f3517580c6958b657203a71f3644a5
3
+ metadata.gz: 1aa3d5c6a44294b75d34898242260a457d281767f805e171b34f96fa0c22a441
4
+ data.tar.gz: ed311ac3f20aebac9af7e5a10e4e0ac576c2da717edaa0df8e061bedd56ab0b4
5
5
  SHA512:
6
- metadata.gz: 61a35efbc1eea4be20001404d3a3ff3789c25493d7e61b08c3a962d0987d94e5a659e2dc7303670d5316c3d63ca4b74e9fba0f8c58460fdeb206db9cc201e505
7
- data.tar.gz: 49c9d04c49fd4d6c2471d5f65c530b91268885e70048ac3388f454a085539a233f69bc2185f2ca9862de615d1ee961e5d2763b880cd6e7a5bd3594f7a59a32bd
6
+ metadata.gz: d39774cfbfafec1065209ce73b89ba98caeb2221385f70f4ecf1628a6466a225834b03bc5fc83a4ee38d45098b15710953cab11b0342de38204b6c5d4d6053d9
7
+ data.tar.gz: 9a06ba36f44a9476eed1e7a4241d3d5eba2fd4ec4ff06632a3f5ca6949ad7b43117d795171b5afaa86693a609038ad085dc7e78bc0b17641b7418f0a408f984b
data/README.md CHANGED
@@ -39,21 +39,25 @@ 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:
45
48
  ```ruby
46
49
  class UserDecorator < ActiveAdmin::ArbreDecorator
47
50
  def full_name
48
- ul do |list|
49
- list.li first_name
50
- list.li last_name
51
+ ul do
52
+ li first_name
53
+ li last_name
51
54
  end
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
- Also included: `ActiveAdmin::ViewHelpers`, `ActionView::Helpers` and `Rails.application.routes.url_helpers`,
60
+ Also included: `ActionView::Helpers` and `Rails.application.routes.url_helpers`,
57
61
  so you can:
58
62
  ```ruby
59
63
  class CommentDecorator < ActiveAdmin::ArbreDecorator
@@ -4,43 +4,16 @@ require_relative 'decorator'
4
4
 
5
5
  module ActiveAdmin
6
6
  class ArbreDecorator < Decorator
7
- include ActiveAdmin::ViewHelpers
8
7
  include ActionView::Helpers
9
-
10
- CONFLICTING_METHODS = %i[display_name title].freeze
8
+ include Arbre::Element::BuilderMethods
11
9
 
12
10
  def initialize(obj)
13
11
  super
14
12
  singleton_class.include Rails.application.routes.url_helpers
15
- CONFLICTING_METHODS.each do |method|
16
- define_singleton_method(method) { obj.send(method) } if !custom_method?(method) && obj.respond_to?(method)
17
- end
18
- end
19
-
20
- ruby2_keywords def method_missing(method, *args, &block)
21
- if cached_arbre_element.respond_to?(method) && !model.respond_to?(method)
22
- Arbre::Context.new(model:) do
23
- __send__(method, *args, &block)
24
- end
25
- else
26
- super
27
- end
28
- end
29
-
30
- def respond_to?(method, include_private = false)
31
- return false if CONFLICTING_METHODS.include?(method) && !model.respond_to?(method) && !custom_method?(method)
32
-
33
- super
34
13
  end
35
14
 
36
15
  private
37
16
 
38
- def cached_arbre_element
39
- @cached_arbre_element ||= Arbre::Element.new
40
- end
41
-
42
- def custom_method?(method_name)
43
- methods.include?(method_name) && method(method_name).owner < ArbreDecorator
44
- end
17
+ def arbre_context = @arbre_context ||= Arbre::Context.new
45
18
  end
46
19
  end
@@ -16,7 +16,7 @@ module ActiveAdmin
16
16
  end
17
17
 
18
18
  # use in decorator to decorate association
19
- def decorates_association(association, relation: association, with: "Decorators::#{association.to_s.singularize.classify}")
19
+ def decorates_association(association, relation: association, with: nil)
20
20
  define_method(association) do
21
21
  associated =
22
22
  case relation
@@ -24,10 +24,13 @@ module ActiveAdmin
24
24
  when Proc then relation.call(model)
25
25
  else raise ArgumentError, "relation must be a Symbol or Proc"
26
26
  end
27
- with = with.constantize if with.is_a?(String)
28
27
  if associated.is_a?(ActiveRecord::Relation)
28
+ with ||= decorator_class_name_for(associated.klass)
29
+ with = with.constantize if with.is_a?(String)
29
30
  associated = associated.map { |item| with.new(item) }
30
31
  else
32
+ with ||= decorator_class_name_for(associated.class)
33
+ with = with.constantize if with.is_a?(String)
31
34
  associated = with.new(associated)
32
35
  end
33
36
  associated
@@ -38,5 +41,14 @@ module ActiveAdmin
38
41
  def model = __getobj__
39
42
 
40
43
  def nil? = model.nil?
44
+
45
+ private
46
+
47
+ # autodetect decorator class name for association
48
+ def decorator_class_name_for(klass)
49
+ @prefix ||= self.class.name.split('::')[0...-1].freeze
50
+ @suffix ||= self.class.name.split('::')[-1].sub(/^#{model.class.name}/, '').freeze
51
+ [*@prefix, klass].join('::').concat(@suffix)
52
+ end
41
53
  end
42
54
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveadminDecorator
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  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.1.0
4
+ version: 0.3.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-05 00:00:00.000000000 Z
11
+ date: 2023-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeadmin