GithubHelp home page GithubHelp logo

rails-autoloading's Introduction

Rails constants and autoloading

Autoload

In a rails application

  1. We have model PointTransaction inside module Ps
  module Ps
    class PointTransaction
      ...
    end
  end
  1. We have an api controller Api::V1::Ps::DailyLoginsController
class Api::V1::Ps::DailyLoginsController
  # I1
  def index
    @txs = Ps::PointTransaction.all
  end

  # I2
  def index
    @txs = ::Ps::PointTransaction.all
  end
end
  • I1

    • Parent namepsace as the algorithm here, we have

      • Api::V1::Ps::DailyLoginsController
      • Api::V1::Ps
      • Api::V1
      • Api
      • top namespace
    • Resolve Ps::PointTransaction processes by this procedure follows the above parent namespaces in order:

      • First run Api::V1::Ps::DailyLoginsController
        Cannot found api/v1/ps/daily_logins_controller/ps.rb
        Cannot found api/v1/ps/daily_logins_controller/ps
        Ps is existed on the parent Api::V1::Ps => raise NameError`
        Following the logic

        If none of the parent namespaces of the class or module has the missing constant then Rails assumes the reference is relative. Otherwise qualified.

        That above logics force Ps must be defined on direct parent namespace Api::V1::Ps::DailyLoginsController => Finally the NameError is: uninitialized constant Api::V1::Ps::DailyLoginsController::Ps

    • Solution

      • Use top namespace ::Ps::PointTransaction
      • Make Ps load first out side the moduel. E.gs puts Ps
      • Use require_dependency
      • Change module controller different from model or otherwise
        • Api::V1::Ps::DailyLoginsController to Api::V1::Cs::DailyLoginsController
          OR
        • Ps::PointTransaction to Cs::PointTransaction
  • I2

    • ::Ps::PointTransaction.all will force Ps was found on top namesapce => It'll run fine

Relative References vs Qualified References

  1. Load Top namespace User before
  puts User

  module Admin
    class UsersController
      puts Admin::User
    end
  end

Bonus Api::V1::Ps::DailyLoginsController was defined (also as the procedure)

  1. Find Api

  2. Find Api::V1

  3. Find Api::V1::Ps

  4. Find Api::V1::Ps::DailyLoginsController

  5. Normal load

  module Admin
  class UsersController
      puts Admin::User
    end
  end
  • 1 will get error because when puts Admin::User execute, User's already existed. User here is a quanlified references => force find user in admin/user.rb as describe here
  • 2 will run as normal because when puts Admin::User run, User constant is still not existed, Rails will consider User as relative refernece => load top namespace User.
  • Note: This above is only true when these code run only (run first)

rails-autoloading's People

Contributors

phuocph avatar

Watchers

 avatar

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.