Class: Lookbook::Engine
- Inherits:
-
Rails::Engine
- Object
- Rails::Engine
- Lookbook::Engine
- Defined in:
- lib/lookbook/engine.rb
Class Method Summary collapse
- .auto_refresh? ⇒ Boolean
- .component_paths ⇒ Object
- .files_changed(modified, added, removed) ⇒ Object
- .hooks ⇒ Object
- .host_app_path ⇒ Object
- .host_config ⇒ Object
- .inputs ⇒ Object
- .load_pages(changes = nil) ⇒ Object
- .load_previews(changes = nil) ⇒ Object
- .mount_path ⇒ Object
- .mounted? ⇒ Boolean
- .notify_clients(changes = nil) ⇒ Object
- .page_paths ⇒ Object (also: page_watch_paths)
- .pages ⇒ Object
- .panels ⇒ Object
- .preview_controller ⇒ Object
- .preview_embeds_allowed? ⇒ Boolean
- .preview_paths ⇒ Object
- .preview_watch_paths ⇒ Object
- .previews ⇒ Object
- .reloading? ⇒ Boolean
- .run_hooks(event_name, *args) ⇒ Object
- .runtime_context ⇒ Object
- .tags ⇒ Object
- .theme ⇒ Object
- .view_paths ⇒ Object
- .websocket ⇒ Object
Instance Method Summary collapse
Class Method Details
.auto_refresh? ⇒ Boolean
112 113 114 115 116 117 |
# File 'lib/lookbook/engine.rb', line 112 def auto_refresh? opts.live_updates == true && reloading? && runtime_context.web? && FileWatcher.evented? end |
.component_paths ⇒ Object
175 176 177 178 179 180 |
# File 'lib/lookbook/engine.rb', line 175 def component_paths @_component_paths ||= begin paths = [*opts.component_paths, *view_paths, host_app_path] PathUtils.normalize_paths(paths) end end |
.files_changed(modified, added, removed) ⇒ Object
260 261 262 263 264 |
# File 'lib/lookbook/engine.rb', line 260 def files_changed(modified, added, removed) changes = {modified: modified, added: added, removed: removed} reloaders.register_changes(changes) notify_clients(changes) end |
.hooks ⇒ Object
147 148 149 |
# File 'lib/lookbook/engine.rb', line 147 def hooks @_hooks ||= HookStore.new end |
.host_app_path ⇒ Object
157 158 159 |
# File 'lib/lookbook/engine.rb', line 157 def host_app_path Rails.application.root.join("app") end |
.host_config ⇒ Object
161 162 163 |
# File 'lib/lookbook/engine.rb', line 161 def host_config Rails.application.config end |
.inputs ⇒ Object
139 140 141 |
# File 'lib/lookbook/engine.rb', line 139 def inputs @_inputs ||= InputStore.init_from_config end |
.load_pages(changes = nil) ⇒ Object
246 247 248 249 250 251 |
# File 'lib/lookbook/engine.rb', line 246 def load_pages(changes = nil) pages.load(Engine.page_paths, changes) rescue => e Lookbook.logger.error(e) raise e end |
.load_previews(changes = nil) ⇒ Object
236 237 238 239 240 241 242 243 244 |
# File 'lib/lookbook/engine.rb', line 236 def load_previews(changes = nil) changed_files = [*changes[:added], *changes[:modified]] if changes parser.parse(changed_files) do |code_objects| previews.load(code_objects.all(:class), changes) end rescue => e Lookbook.logger.error(e) raise e end |
.mount_path ⇒ Object
100 101 102 |
# File 'lib/lookbook/engine.rb', line 100 def mount_path routes.find_script_name({}) end |
.mounted? ⇒ Boolean
104 105 106 |
# File 'lib/lookbook/engine.rb', line 104 def mounted? mount_path.present? end |
.notify_clients(changes = nil) ⇒ Object
253 254 255 256 257 258 |
# File 'lib/lookbook/engine.rb', line 253 def notify_clients(changes = nil) return unless changes.present? websocket.broadcast(:reload) run_hooks(:after_change, changes.to_h) end |
.page_paths ⇒ Object Also known as: page_watch_paths
182 183 184 |
# File 'lib/lookbook/engine.rb', line 182 def page_paths @_page_paths ||= PathUtils.normalize_paths(opts.page_paths) end |
.pages ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/lookbook/engine.rb', line 199 def pages @_pages ||= PageCollection.new if opts.lazy_load_previews_and_pages && !@_loaded_pages @_loaded_pages = true if reloading? reloaders.execute(:pages) else load_pages end end @_pages end |
.panels ⇒ Object
135 136 137 |
# File 'lib/lookbook/engine.rb', line 135 def panels @_panels ||= PanelStore.init_from_config end |
.preview_controller ⇒ Object
231 232 233 234 |
# File 'lib/lookbook/engine.rb', line 231 def preview_controller @_preview_controller = opts.preview_controller.constantize @_preview_controller.include PreviewControllerActions end |
.preview_embeds_allowed? ⇒ Boolean
119 120 121 |
# File 'lib/lookbook/engine.rb', line 119 def opts..enabled == true && opts..policy != "DENY" end |
.preview_paths ⇒ Object
188 189 190 |
# File 'lib/lookbook/engine.rb', line 188 def preview_paths @_preview_paths ||= PathUtils.normalize_paths(opts.preview_paths) end |
.preview_watch_paths ⇒ Object
192 193 194 195 196 197 |
# File 'lib/lookbook/engine.rb', line 192 def preview_watch_paths @_preview_watch_paths ||= begin paths = [*opts.preview_paths, *opts.component_paths, *opts.listen_paths, *view_paths].uniq PathUtils.normalize_paths(paths) end end |
.previews ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/lookbook/engine.rb', line 215 def previews @_previews ||= PreviewCollection.new if opts.lazy_load_previews_and_pages && !@_loaded_previews @_loaded_previews = true if reloading? reloaders.execute(:previews) else load_previews end end @_previews end |
.reloading? ⇒ Boolean
108 109 110 |
# File 'lib/lookbook/engine.rb', line 108 def reloading? opts.reload_on_change end |
.run_hooks(event_name, *args) ⇒ Object
151 152 153 154 155 |
# File 'lib/lookbook/engine.rb', line 151 def run_hooks(event_name, *args) hooks.for_event(event_name).each do |hook| hook.call(Lookbook, *args) end end |
.runtime_context ⇒ Object
127 128 129 |
# File 'lib/lookbook/engine.rb', line 127 def runtime_context @_runtime_context ||= RuntimeContext.new(env: Rails.env) end |
.tags ⇒ Object
143 144 145 |
# File 'lib/lookbook/engine.rb', line 143 def @_tags ||= TagStore.init_from_config end |
.theme ⇒ Object
131 132 133 |
# File 'lib/lookbook/engine.rb', line 131 def theme @_theme ||= Lookbook::Theme.new(opts.ui_theme, opts.ui_theme_overrides) end |
.view_paths ⇒ Object
165 166 167 168 169 170 171 172 173 |
# File 'lib/lookbook/engine.rb', line 165 def view_paths # handle view path registry changes in Rails 7.1 paths = if defined?(ActionView::PathRegistry) ActionView::PathRegistry.all_file_system_resolvers.map(&:path) else ActionView::ViewPaths.all_view_paths.flat_map(&paths) end paths.map { |path| Pathname(path.to_s) } end |
.websocket ⇒ Object
123 124 125 |
# File 'lib/lookbook/engine.rb', line 123 def websocket @_websocket ||= auto_refresh? ? Websocket.new(mount_path, logger: Lookbook.logger) : NullWebsocket.new end |
Instance Method Details
#parser ⇒ Object
89 90 91 |
# File 'lib/lookbook/engine.rb', line 89 def parser @_parser ||= PreviewParser.new(opts.preview_paths, Engine.) end |