activeadmin_decorator 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1aa3d5c6a44294b75d34898242260a457d281767f805e171b34f96fa0c22a441
4
- data.tar.gz: ed311ac3f20aebac9af7e5a10e4e0ac576c2da717edaa0df8e061bedd56ab0b4
3
+ metadata.gz: c5fef3015a46bcf7b93b09a8a12018f117e4c301e208b24dba8b2f4405c5f81e
4
+ data.tar.gz: 89ef8d221f034e59303f563253cedbf3c6a49a49b5bf105ab5c550ec10822fe4
5
5
  SHA512:
6
- metadata.gz: d39774cfbfafec1065209ce73b89ba98caeb2221385f70f4ecf1628a6466a225834b03bc5fc83a4ee38d45098b15710953cab11b0342de38204b6c5d4d6053d9
7
- data.tar.gz: 9a06ba36f44a9476eed1e7a4241d3d5eba2fd4ec4ff06632a3f5ca6949ad7b43117d795171b5afaa86693a609038ad085dc7e78bc0b17641b7418f0a408f984b
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"
@@ -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,32 +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: nil)
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
- if associated.is_a?(ActiveRecord::Relation)
28
- with ||= decorator_class_name_for(associated.klass)
29
- with = with.constantize if with.is_a?(String)
30
- associated = associated.map { |item| with.new(item) }
20
+ if instance_variable_defined?("@#{association}_decorated")
21
+ then instance_variable_get("@#{association}_decorated")
31
22
  else
32
- with ||= decorator_class_name_for(associated.class)
33
- with = with.constantize if with.is_a?(String)
34
- 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))
35
28
  end
36
- associated
37
29
  end
38
30
  end
39
31
  end
@@ -41,14 +33,5 @@ module ActiveAdmin
41
33
  def model = __getobj__
42
34
 
43
35
  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
53
36
  end
54
37
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveadminDecorator
4
- VERSION = "0.3.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.3.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