Class: Lookbook::Preview
- Inherits:
-
Object
- Object
- Lookbook::Preview
- Extended by:
- PreviewAfterRender
- Includes:
- ActionView::Helpers::AssetTagHelper, ActionView::Helpers::TagHelper
- Defined in:
- lib/lookbook/preview.rb
Class Method Summary collapse
-
.layout(layout_name) ⇒ Object
rubocop:disable Style/TrivialAccessors.
- .preview_name ⇒ Object
-
.render_args(scenario, params: {}) ⇒ Object
Returns the arguments for rendering of the component in its layout.
-
.scenario_template_path(scenario) ⇒ Object
Returns the relative path (from preview_path) to the scenario template if the template exists.
Instance Method Summary collapse
- #render(component = nil, **args, &block) ⇒ Object (also: #render_component)
- #render_with_template(template: nil, locals: nil) ⇒ Object
Methods included from PreviewAfterRender
Class Method Details
.layout(layout_name) ⇒ Object
rubocop:disable Style/TrivialAccessors
56 57 58 |
# File 'lib/lookbook/preview.rb', line 56 def layout(layout_name) @layout = layout_name end |
.preview_name ⇒ Object
40 41 42 |
# File 'lib/lookbook/preview.rb', line 40 def preview_name name.chomp("Preview").underscore end |
.render_args(scenario, params: {}) ⇒ Object
Returns the arguments for rendering of the component in its layout
45 46 47 48 49 50 51 52 53 |
# File 'lib/lookbook/preview.rb', line 45 def render_args(scenario, params: {}) scenario_params_names = instance_method(scenario).parameters.map(&:last) provided_params = params.slice(*scenario_params_names).to_h.symbolize_keys result = provided_params.empty? ? new.public_send(scenario) : new.public_send(scenario, **provided_params) result ||= {} result[:template] = scenario_template_path(scenario) if result[:template].nil? @layout = nil unless defined?(@layout) result.merge(layout: @layout) end |
.scenario_template_path(scenario) ⇒ Object
Returns the relative path (from preview_path) to the scenario template if the template exists
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/lookbook/preview.rb', line 62 def scenario_template_path(scenario) preview_name = name.chomp("Preview").underscore preview_path = Engine.preview_paths.detect do |path| Dir["#{path}/#{preview_name}_preview/#{scenario}.html.*"].first end if preview_path.nil? raise( PreviewTemplateError, "A preview template for scenario #{scenario} doesn't exist.\n\n" \ "To fix this issue, create a template for the scenario." ) end path = Dir["#{preview_path}/#{preview_name}_preview/#{scenario}.html.*"].first Pathname.new(path) .relative_path_from(Pathname.new(preview_path)) .to_s .sub(/\..*$/, "") end |
Instance Method Details
#render(component = nil, **args, &block) ⇒ Object Also known as: render_component
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/lookbook/preview.rb', line 7 def render(component = nil, **args, &block) if component.nil? { type: :view, template: args[:template] || Lookbook.config.preview_template, args: args, locals: args[:locals] || {}, assigns: args[:assigns] || {}, block: block } else { type: component.is_a?(String) ? :view : :component, args: args, block: block, component: component, locals: {}, template: Lookbook.config.preview_template } end end |
#render_with_template(template: nil, locals: nil) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/lookbook/preview.rb', line 29 def render_with_template(template: nil, locals: nil) { type: :template, template: template, locals: locals.to_h } end |