Class: Lookbook::Engine

Inherits:
Rails::Engine
  • Object
show all
Defined in:
lib/lookbook/engine.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.auto_refresh?Boolean

Returns:

  • (Boolean)


136
137
138
139
140
141
# File 'lib/lookbook/engine.rb', line 136

def auto_refresh?
  opts.live_updates == true &&
    reloading? &&
    runtime_context.web? &&
    FileWatcher.evented?
end

.component_pathsObject



199
200
201
202
203
204
# File 'lib/lookbook/engine.rb', line 199

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



284
285
286
287
288
# File 'lib/lookbook/engine.rb', line 284

def files_changed(modified, added, removed)
  changes = {modified: modified, added: added, removed: removed}
  reloaders.register_changes(changes)
  notify_clients(changes)
end

.hooksObject



171
172
173
# File 'lib/lookbook/engine.rb', line 171

def hooks
  @_hooks ||= HookStore.new
end

.host_app_pathObject



181
182
183
# File 'lib/lookbook/engine.rb', line 181

def host_app_path
  Rails.application.root.join("app")
end

.host_configObject



185
186
187
# File 'lib/lookbook/engine.rb', line 185

def host_config
  Rails.application.config
end

.inputsObject



163
164
165
# File 'lib/lookbook/engine.rb', line 163

def inputs
  @_inputs ||= InputStore.init_from_config
end

.load_pages(changes = nil) ⇒ Object



270
271
272
273
274
275
# File 'lib/lookbook/engine.rb', line 270

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



260
261
262
263
264
265
266
267
268
# File 'lib/lookbook/engine.rb', line 260

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_pathObject



124
125
126
# File 'lib/lookbook/engine.rb', line 124

def mount_path
  routes.find_script_name({})
end

.mounted?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/lookbook/engine.rb', line 128

def mounted?
  mount_path.present?
end

.notify_clients(changes = nil) ⇒ Object



277
278
279
280
281
282
# File 'lib/lookbook/engine.rb', line 277

def notify_clients(changes = nil)
  return unless changes.present?

  websocket.broadcast(:reload)
  run_hooks(:after_change, changes.to_h)
end

.page_pathsObject Also known as: page_watch_paths



206
207
208
# File 'lib/lookbook/engine.rb', line 206

def page_paths
  @_page_paths ||= PathUtils.normalize_paths(opts.page_paths)
end

.pagesObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/lookbook/engine.rb', line 223

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

.panelsObject



159
160
161
# File 'lib/lookbook/engine.rb', line 159

def panels
  @_panels ||= PanelStore.init_from_config
end

.preview_controllerObject



255
256
257
258
# File 'lib/lookbook/engine.rb', line 255

def preview_controller
  @_preview_controller = opts.preview_controller.constantize
  @_preview_controller.include PreviewControllerActions
end

.preview_embeds_allowed?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/lookbook/engine.rb', line 143

def preview_embeds_allowed?
  opts.preview_embeds.enabled == true && opts.preview_embeds.policy != "DENY"
end

.preview_pathsObject



212
213
214
# File 'lib/lookbook/engine.rb', line 212

def preview_paths
  @_preview_paths ||= PathUtils.normalize_paths(opts.preview_paths)
end

.preview_watch_pathsObject



216
217
218
219
220
221
# File 'lib/lookbook/engine.rb', line 216

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

.previewsObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/lookbook/engine.rb', line 239

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

Returns:

  • (Boolean)


132
133
134
# File 'lib/lookbook/engine.rb', line 132

def reloading?
  opts.reload_on_change
end

.run_hooks(event_name, *args) ⇒ Object



175
176
177
178
179
# File 'lib/lookbook/engine.rb', line 175

def run_hooks(event_name, *args)
  hooks.for_event(event_name).each do |hook|
    hook.call(Lookbook, *args)
  end
end

.runtime_contextObject



151
152
153
# File 'lib/lookbook/engine.rb', line 151

def runtime_context
  @_runtime_context ||= RuntimeContext.new(env: Rails.env)
end

.tagsObject



167
168
169
# File 'lib/lookbook/engine.rb', line 167

def tags
  @_tags ||= TagStore.init_from_config
end

.themeObject



155
156
157
# File 'lib/lookbook/engine.rb', line 155

def theme
  @_theme ||= Lookbook::Theme.new(opts.ui_theme, opts.ui_theme_overrides)
end

.view_pathsObject



189
190
191
192
193
194
195
196
197
# File 'lib/lookbook/engine.rb', line 189

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

.websocketObject



147
148
149
# File 'lib/lookbook/engine.rb', line 147

def websocket
  @_websocket ||= auto_refresh? ? Websocket.new(mount_path, logger: Lookbook.logger) : NullWebsocket.new
end

Instance Method Details

#optsObject



109
110
111
# File 'lib/lookbook/engine.rb', line 109

def opts
  Lookbook.config
end

#parserObject



113
114
115
# File 'lib/lookbook/engine.rb', line 113

def parser
  @_parser ||= PreviewParser.new(opts.preview_paths, Engine.tags)
end

#reloadersObject



117
118
119
# File 'lib/lookbook/engine.rb', line 117

def reloaders
  @_reloaders ||= Reloaders.new
end