From cddb167260d549786e6799c68166298f2f94d29a Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Wed, 20 Sep 2023 19:38:45 +0200 Subject: [PATCH] Remove draper dependency It's not really doing much, just add the bits that are used ourselves --- Gemfile | 1 - Gemfile.lock | 14 ------------- app/decorators/application_decorator.rb | 26 ++++++++++++++++++------- app/decorators/paginated_decorator.rb | 13 +++++++++++-- app/models/application_record.rb | 16 +++++++++++++++ 5 files changed, 46 insertions(+), 24 deletions(-) diff --git a/Gemfile b/Gemfile index 7948a0b1..86143d5a 100644 --- a/Gemfile +++ b/Gemfile @@ -3,7 +3,6 @@ source "https://rubygems.org" gem "addressable" -gem "draper" gem "good_job" gem "httparty" gem "kaminari" diff --git a/Gemfile.lock b/Gemfile.lock index c1261935..76c35199 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -51,10 +51,6 @@ GEM globalid (>= 0.3.6) activemodel (7.0.8) activesupport (= 7.0.8) - activemodel-serializers-xml (1.0.2) - activemodel (> 5.x) - activesupport (> 5.x) - builder (~> 3.1) activerecord (7.0.8) activemodel (= 7.0.8) activesupport (= 7.0.8) @@ -91,13 +87,6 @@ GEM crass (1.0.6) date (3.3.3) diff-lcs (1.5.0) - draper (4.0.2) - actionpack (>= 5.0) - activemodel (>= 5.0) - activemodel-serializers-xml (>= 1.0) - activesupport (>= 5.0) - request_store (>= 1.0) - ruby2_keywords e2mmap (0.1.0) erubi (1.12.0) et-orbi (1.2.7) @@ -234,8 +223,6 @@ GEM ffi (~> 1.0) rbs (2.8.4) regexp_parser (2.8.1) - request_store (1.5.1) - rack (>= 1.4) responders (3.1.0) actionpack (>= 5.2) railties (>= 5.2) @@ -326,7 +313,6 @@ PLATFORMS DEPENDENCIES addressable - draper factory_bot_rails good_job httparty diff --git a/app/decorators/application_decorator.rb b/app/decorators/application_decorator.rb index 2329d915..66888223 100644 --- a/app/decorators/application_decorator.rb +++ b/app/decorators/application_decorator.rb @@ -1,14 +1,26 @@ # frozen_string_literal: true -class ApplicationDecorator < Draper::Decorator - def self.inherited(child_class) +class ApplicationDecorator + include ActiveModel::Serialization + include ActiveModel::Serializers::JSON + + # Delegate columns and instance methods + def self.inherited(decorator_class) super - child_class.class_eval do - def self.collection_decorator_class - PaginatedDecorator - end + model_class = decorator_class.name.delete_suffix("Decorator").constantize + decorator_class.class_eval do + delegate *model_class.column_names, *model_class.instance_methods(false), to: :@object # rubocop:disable Lint/AmbiguousOperator end end - delegate_all + delegate_missing_to :@object + delegate :to_param, :to_partial_path, to: :@object + + attr_reader :helpers + alias h helpers + + def initialize(object) + @object = object + @helpers = ApplicationController.helpers + end end diff --git a/app/decorators/paginated_decorator.rb b/app/decorators/paginated_decorator.rb index 6e613771..89f59c26 100644 --- a/app/decorators/paginated_decorator.rb +++ b/app/decorators/paginated_decorator.rb @@ -1,5 +1,14 @@ # frozen_string_literal: true -class PaginatedDecorator < Draper::CollectionDecorator - delegate :current_page, :total_pages, :limit_value, :entry_name, :total_count, :offset_value, :last_page? +class PaginatedDecorator + delegate :current_page, :total_pages, :limit_value, :entry_name, :total_count, :offset_value, :last_page?, to: :@object + delegate *Array.instance_methods(false), to: :collection # rubocop:disable Lint/AmbiguousOperator + + def initialize(object) + @object = object + end + + def collection + @collection ||= @object.map(&:decorate) + end end diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 06d81705..675c4662 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -114,4 +114,20 @@ def join_hash(input) end end end + + concerning :Decoration do + def decorate + self.class.decorator_class.new(self) + end + + class_methods do + def decorator_class + "#{model_name}Decorator".constantize + end + + def decorate + PaginatedDecorator.new(all) + end + end + end end