Skip to content

Commit

Permalink
Remove draper dependency
Browse files Browse the repository at this point in the history
It's not really doing much, just add the bits that are used ourselves
  • Loading branch information
Earlopain committed Sep 20, 2023
1 parent b374e51 commit cddb167
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
source "https://rubygems.org"

gem "addressable"
gem "draper"
gem "good_job"
gem "httparty"
gem "kaminari"
Expand Down
14 changes: 0 additions & 14 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -326,7 +313,6 @@ PLATFORMS

DEPENDENCIES
addressable
draper
factory_bot_rails
good_job
httparty
Expand Down
26 changes: 19 additions & 7 deletions app/decorators/application_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 11 additions & 2 deletions app/decorators/paginated_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cddb167

Please sign in to comment.