activeadmin_decorator 0.2.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9066952cd77bd579269b0bf534618271ad135a974fdf9d837ea35e416c8508d6
4
- data.tar.gz: 93750f0e9c841dbf9408c5463d9453895c8f796e059290faf761fdad14123001
3
+ metadata.gz: c5fef3015a46bcf7b93b09a8a12018f117e4c301e208b24dba8b2f4405c5f81e
4
+ data.tar.gz: 89ef8d221f034e59303f563253cedbf3c6a49a49b5bf105ab5c550ec10822fe4
5
5
  SHA512:
6
- metadata.gz: 351b713fde7f9a5c868861cb75e1ed6d91646d5657f07955a1c6ab0b0ea6af8e45b13121cfeba35bfbe5d809cc07861dd6b688d4a78752c0fff6b036a983ad21
7
- data.tar.gz: f812a8602b9365810bc9d8da56c40d09b677e4622ee8bc102f11b4bbdbf4c660596e2309b90630cdcfd05f0c2f1e5f6407459dc54cf75dfbd65efa9d02b7f84f
6
+ metadata.gz: 2a0fa368c1a9acaf58cc87c76147fac4d5cdcc19404c0afbf137b6e0447eb8e8f89f927e492789b001e5d62a0435e6e6f7e5bb904975b8a2e1848065154ac5ea
7
+ data.tar.gz: aa450eea02e047f607b1552c8428585bada299f387d9f84900979a734e28f555febdcbef55775c2f4eee262fd20ee15917133ddb3b8983abff2f1521cf94716e
data/.rubocop.yml CHANGED
@@ -1,5 +1,7 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 3.2
3
+ NewCops: enable
4
+ SuggestExtensions: false
3
5
 
4
6
  Style/StringLiterals:
5
7
  Enabled: true
data/Gemfile CHANGED
@@ -8,3 +8,4 @@ gemspec
8
8
  gem "rake", "~> 13.0"
9
9
  gem "rspec", "~> 3.0"
10
10
  gem "rubocop", "~> 1.21"
11
+ gem "rubocop-rspec", "~> 1.42"
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:
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'decorator'
3
+ require_relative "decorator"
4
4
 
5
5
  module ActiveAdmin
6
6
  class ArbreDecorator < Decorator
@@ -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 'delegate'
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
- if object.is_a?(Enumerable)
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: "Decorators::#{association.to_s.singularize.classify}")
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
- associated =
22
- case relation
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
- associated = with.new(associated)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveadminDecorator
4
- VERSION = "0.2.0"
4
+ VERSION = "0.4.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.2.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-07 00:00:00.000000000 Z
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