Sha256: c178aec5907482ec65c1f5a1275187916aaf8135465c92025b4bedcd610b2f89

Contents?: true

Size: 1017 Bytes

Versions: 47

Compression:

Stored size: 1017 Bytes

Contents

# frozen_string_literal: true

require 'active_support'

# Preload all the LHS::Records that are defined in app/models/*
# in order to collect record endpoints and to be able to identify records from hrefs
# and not only from model constant names (which is different to ActiveRecord)
module AutoloadRecords
  extend ActiveSupport::Concern

  included do
    class Engine < Rails::Engine
      initializer 'Load all LHS::Records from app/models/**' do |app|
        Middleware.require_records
        next if app.config.cache_classes

        app.config.middleware.use Middleware
      end

      class Middleware
        def initialize(app)
          @app = app
        end

        def call(env)
          self.class.require_records
          @app.call(env)
        end

        def self.require_records
          Dir.glob(Rails.root.join('app', 'models', '**', '*.rb')).each do |file|
            require_dependency file if File.read(file).match('LHS::Record')
          end
        end
      end
    end
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
lhs-21.2.2 lib/lhs/concerns/autoload_records.rb
lhs-21.2.1 lib/lhs/concerns/autoload_records.rb
lhs-21.2.0 lib/lhs/concerns/autoload_records.rb
lhs-21.1.4 lib/lhs/concerns/autoload_records.rb
lhs-21.1.3 lib/lhs/concerns/autoload_records.rb
lhs-21.1.2 lib/lhs/concerns/autoload_records.rb
lhs-21.1.2.pre.keyfix.2 lib/lhs/concerns/autoload_records.rb
lhs-21.1.2.pre.keyfix.1 lib/lhs/concerns/autoload_records.rb
lhs-21.1.2.pre.fix.pre.cant.pre.add.pre.new.pre.key.pre.during.pre.iteration.1 lib/lhs/concerns/autoload_records.rb
lhs-21.1.1.pre.pre.fix.pre.cant.pre.add.pre.new.pre.key.pre.during.pre.iteration.1 lib/lhs/concerns/autoload_records.rb
lhs-21.1.0 lib/lhs/concerns/autoload_records.rb
lhs-21.0.0 lib/lhs/concerns/autoload_records.rb
lhs-20.1.4 lib/lhs/concerns/autoload_records.rb
lhs-20.1.3.pre.paginationfix.3 lib/lhs/concerns/autoload_records.rb
lhs-20.1.3.pre.paginationfix.2 lib/lhs/concerns/autoload_records.rb
lhs-20.1.3.pre.paginationfix.1 lib/lhs/concerns/autoload_records.rb
lhs-20.1.2 lib/lhs/concerns/autoload_records.rb
lhs-20.1.1 lib/lhs/concerns/autoload_records.rb
lhs-20.0.0 lib/lhs/concerns/autoload_records.rb
lhs-19.10.0 lib/lhs/concerns/autoload_records.rb