Class: ActionBlocks::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/action_block_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Loader

Returns a new instance of Loader.



7
8
9
# File 'lib/action_block_loader.rb', line 7

def initialize(path)
  @load_paths = [File.expand_path(path, Rails.root)]
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



6
7
8
# File 'lib/action_block_loader.rb', line 6

def application
  @application
end

#load_pathsObject (readonly)

Returns the value of attribute load_paths.



6
7
8
# File 'lib/action_block_loader.rb', line 6

def load_paths
  @load_paths
end

Instance Method Details

#attach_reloaderObject

Hook into the Rails code reloading mechanism so that things are reloaded properly in development mode.

If any of the app files (e.g. models) has changed, we need to reload all the admin files. If the admin files themselves has changed, we need to regenerate the routes as well.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/action_block_loader.rb', line 62

def attach_reloader
  # ActiveSupport::Reloader.to_prepare(*args, &block)
  Rails.application.config.after_initialize do |app|
      if app.config.reload_classes_only_on_change
        # Rails is about to unload all the app files (e.g. models), so we
        # should first unload the classes generated by Active Admin, otherwise
        # they will contain references to the stale (unloaded) classes.
        ActiveSupport::Reloader.to_prepare(prepend: true) do
          ActionBlocks.unload!
        end
      else
        # If the user has configured the app to always reload app files after
        # each request, so we should unload the generated classes too.
        ActiveSupport::Reloader.to_complete() do
          puts "ActiveSupport::Reloader.to_complete()\n"
          # ActionBlocks.application.unload!
          # @@loaded = false
        end
      end

      # block_drs = {}
      #
      # load_paths.each do |path|
      #   block_drs[path] = [:rb]
      # end

      # routes_reloader = app.config.file_watcher.new([], block_drs) do
      #   app.reload_routes!
      # end
      #
      # app.reloaders << routes_reloader

      ActiveSupport::Reloader.to_prepare do
        # Rails.logger.debug("--> 1. ActionBlocks::Loader.new('app/blocks')")
        loader = ActionBlocks::Loader.new('app/blocks')
        loader.unload!
        loader.load!
        ActionBlocks.after_load

        # loader.attach_reloader
        # Rails might have reloaded the routes for other reasons (e.g.
        # routes.rb has changed), in which case Active Admin would have been
        # loaded via the `ActiveAdmin.routes` call in `routes.rb`.
        #
        # Otherwise, we should check if any of the admin files are changed
        # and force the routes to reload if necessary. This would again causes
        # Active Admin to load via `ActiveAdmin.routes`.
        #
        # Finally, if Active Admin is still not loaded at this point, then we
        # would need to load it manually.
        # unless ActionBlocks.application.loaded?
          # routes_reloader.execute_if_updated
          # self.load!
        # end
      end
    end
end

#filesObject

Returns ALL the files to be loaded



42
43
44
# File 'lib/action_block_loader.rb', line 42

def files
  load_paths.flatten.compact.uniq.flat_map{ |path| Dir["#{path}/**/*.rb"] }
end

#load(file) ⇒ Object



36
37
38
39
# File 'lib/action_block_loader.rb', line 36

def load(file)
  Rails.logger.debug "ActionBlocks::Loader load(#{file})"
  DatabaseHitDuringLoad.capture{ super }
end

#load!Object

Loads all ruby files that are within the load_paths setting. To reload everything simply call ‘ActionBlocks.unload!`



25
26
27
28
29
30
31
32
33
34
# File 'lib/action_block_loader.rb', line 25

def load!
  Rails.logger.debug "ActionBlocks::Loader load!()"
  Rails.logger.debug " loaded?:#{loaded?().inspect}"
  unless loaded?
    # ActiveSupport::Notifications.publish BeforeLoadEvent, self # before_load hook
    files.each{ |file| load file }                             # load files
    # ActiveSupport::Notifications.publish AfterLoadEvent, self  # after_load hook
    @@loaded = true
  end
end

#loaded?Boolean

Whether all configuration files have been loaded

Returns:

  • (Boolean)


12
13
14
# File 'lib/action_block_loader.rb', line 12

def loaded?
  @@loaded ||= false
end

#remove_active_admin_load_paths_from_rails_autoload_and_eager_loadObject

Since app/blocks is alphabetically before app/models, we have to remove it from the host app’s autoload_paths to prevent missing constant errors.

As well, we have to remove it from eager_load_paths to prevent the files from being loaded twice in production.



51
52
53
54
# File 'lib/action_block_loader.rb', line 51

def remove_active_admin_load_paths_from_rails_autoload_and_eager_load
  ActiveSupport::Dependencies.autoload_paths -= load_paths
  Rails.application.config.eager_load_paths  -= load_paths
end

#unload!Object

Removes all defined controllers from memory. Useful in development, where they are reloaded on each request.



18
19
20
21
# File 'lib/action_block_loader.rb', line 18

def unload!
  ActionBlocks.unload!
  @@loaded = false
end