Code Monkey home page Code Monkey logo

conventional_extensions's Introduction

  • ๐Ÿ‘‹ Hi, Iโ€™m @kaspth, long-time Ruby developer, served on @rails core 2016-2022
  • ๐Ÿ‘€ Iโ€™m interested in Ruby & composing a system with Domain Modeling
  • ๐ŸŒฑ Iโ€™m currently learning writing and publishing Gems again, plus consulting/marketing/what-not's/what-have-you's
  • ๐Ÿ’ž๏ธ Iโ€™m looking to collaborate on your Ruby problem, send me it on my Ruby community Discord
  • ๐Ÿ“ซ Reach me at [email protected] or @kaspth on Twitter

conventional_extensions's People

Contributors

kaspth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

natblow

conventional_extensions's Issues

Namespaced classes

Hiya ๐Ÿ‘‹

Extensions don't seem to be loading for namespaced classes.

As an example, here is a Organisation::User AR model and an extension...

# /app/models/organisation/user.rb
class Organisation::User < ApplicationRecord
end
# /app/models/organisation/user/extensions/first_name.rb
class Organisation::User
  def first_name
    'Joel'
  end
end

Calling the #first_name method results in a method_missing exception...

Organisation::User.new.first_name

# `method_missing': undefined method `first_name' for #<Organisation::User...

Test with Zeitwerk's reloading

Gotta make sure that everything reloads properly in a Rails app.

E.g. when Zeitwerk removes the old constants it'll be as if we've never called load_extensions, but then when Post gets loaded again, we'll call load_extensions again. However, our require use and $LOADED_FEATURES check (which is what require also does) might not comport with that.

So we either need to hook into Zeitwerk and clear our loaded extensions or something else.

NoMethodError: `private method 'load' called for nil:NilClass`

Hey @kaspth, thanks for creating this awesome gem!

I've encountered a weird issue on the latest 0.3.0 version. I have a minimal reproduction repo here: https://github.com/marcoroth/conventional_extensions-repro

I guess the main questions is: Am I even supposed to use load_exentions inside a model class under app/models/?

Versions

  • conventional_extensions: 0.3.0
  • Ruby: 3.1.2
  • Rails: 7.0.4

Relevant Files

Reproducion Steps

bundle install
rails db:create db:migrate 
rails c
Loading development environment (Rails 7.0.4)
irb(main):001:0> Post.new.mailroom
/Users/marcoroth/.anyenv/envs/rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/conventional_extensions-0.3.0/lib/conventional_extensions.rb:15:in `load_extensions': private method `load' called for nil:NilClass (NoMethodError)

    @loader.load(*extensions)
           ^^^^^

Possible fix?

This seems to fix it locally, but honestly I haven't fully grokked why @loader wouldn't need to be defined in some cases. This "fix" possibly also has some unwanted side-effects since @loader is not going to be cleaned up in the ensure block because of the loader_defined_before_entrance condition.

  def load_extensions(*extensions, from: Frame.previous.path)
-   @loader = Loader.new(self, from) unless loader_defined_before_entrance = defined?(@loader)
+   loader_defined_before_entrance = defined?(@loader)
+   @loader = Loader.new(self, from) unless loader_defined_before_entrance
+   @loader = Loader.new(self, from) if @loader.nil
    @loader.load(*extensions)
  ensure
    @loader = nil unless loader_defined_before_entrance
  end

I assume the defined?(...) is not doing the right thing, it returns a truthy value even if @loader is nil and thus not instantiating a loader.

irb(main):005:0> @loader = nil
=> nil
irb(main):006:0> defined?(@loader)
=> "instance-variable"

Either way, I'm happy to open a pull request if this fix makes sense.

Zeitwerk::NameError with Rails `eager_load` during tests

In config/environments/test.rb, I have this line:

config.eager_load = ENV['CI'].present?

When running my tests in CI, ENV['CI'] is true, thus setting eager_load to true. Which then results in Zeitwerk::NameError...

expected file app/models/client/extensions/stripe.rb to define constant Client::Extensions::Stripe, but didn't (Zeitwerk::NameError)

eager_load is true in prod, so I'm half expecting the same exception (not actually tested that yet). But I'm assuming something is done to avoid that?

Anyway, thx again for an amazing lib. Any help would be appreciated.

Disable auto-load in AR models?

Following on from #3, and now I've used this gem a little more, it has become apparent that sometimes a little more control is required as to when extensions are loaded.

For example, I may have included a module (a concern) in a model, which one of the model extensions requires. This will fail due to extensions being loaded before the inclusion of the module.

So would it be possible to disable this auto loading of extensions for a given class?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.