Skip to content

Commit

Permalink
fix: base resource overriding (#3077)
Browse files Browse the repository at this point in the history
* fix: base resource overriding

* fix: base resource extension

* lint

* .

* mv base

* .

* .

* .

* .

* .

* .

* require base resource

* Revert "."

This reverts commit cb5ed40.

* lint

* fix warning

* fix test

* lint

* require instead require_relative

* just require

* proper load
  • Loading branch information
Paul-Bob committed Aug 5, 2024
1 parent bc0c2f6 commit 4c50e08
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
File renamed without changes.
12 changes: 8 additions & 4 deletions lib/avo/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ class Engine < ::Rails::Engine
# This undoes Rails' previous nested directories behavior in the `app` dir.
# More on this: https://github.com/fxn/zeitwerk/issues/250
avo_directory = Rails.root.join("app", "avo").to_s
ActiveSupport::Dependencies.autoload_paths.delete(avo_directory)
engine_avo_directory = Avo::Engine.root.join("app", "avo").to_s

if Dir.exist?(avo_directory)
Rails.autoloaders.main.push_dir(avo_directory, namespace: Avo)
app.config.watchable_dirs[avo_directory] = [:rb]
[avo_directory, engine_avo_directory].each do |directory_path|
ActiveSupport::Dependencies.autoload_paths.delete(directory_path)

if Dir.exist?(directory_path)
Rails.autoloaders.main.push_dir(directory_path, namespace: Avo)
app.config.watchable_dirs[directory_path] = [:rb]
end
end
end

Expand Down
36 changes: 36 additions & 0 deletions spec/features/avo/resource_extension_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "rails_helper"

RSpec.feature "Resource extension", type: :feature do
before_all do
Avo::Resources::CourseLink.define_method :test_base_resource_extension do
some_extended_method
end
end

it "no extension" do
expect(TestBuddy).not_to receive(:hi).with("Extension successfully")
expect { Avo::Resources::CourseLink.new.test_base_resource_extension }.to raise_error(NameError)
end

it "with extension" do
file_path = Rails.root.join("app/avo/base_resource.rb")
FileUtils.mkdir_p(File.dirname(file_path))
File.write file_path,
<<~RUBY
module Avo
class BaseResource < Avo::Resources::Base
def some_extended_method
TestBuddy.hi("Extension successfully")
end
end
end
RUBY

load file_path

expect(TestBuddy).to receive(:hi).with("Extension successfully").at_least :once
expect { Avo::Resources::CourseLink.new.test_base_resource_extension }.not_to raise_error

File.delete(file_path) if File.exist?(file_path)
end
end

0 comments on commit 4c50e08

Please sign in to comment.