Module: Lookbook::TargetableConcern
- Extended by:
- ActiveSupport::Concern
- Included in:
- EmbedsController, InspectorController, PreviewsController
- Defined in:
- app/controllers/concerns/lookbook/targetable_concern.rb
Instance Method Summary collapse
- #inspector_data ⇒ Object
- #lookup_entities ⇒ Object
- #path_segments ⇒ Object
- #set_display_options ⇒ Object
- #set_params ⇒ Object
- #set_title ⇒ Object
Instance Method Details
#inspector_data ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'app/controllers/concerns/lookbook/targetable_concern.rb', line 94 def inspector_data return @inspector_data if @inspector_data.present? scenarios = @target ? @target.scenarios : [] rendered_scenarios = scenarios.map do |scenario| output = preview_controller.process(:render_scenario_to_string, @preview, scenario) RenderedScenarioEntity.new(scenario, output, preview_controller.params) end target = rendered_scenarios.find { |scenario| scenario.id == @target.id } || @target @inspector_data ||= Lookbook::Store.new({ context: Store.new({params: @params, path: params[:path]}), preview: @preview, scenarios: rendered_scenarios, examples: -> do Lookbook.logger.warn("The `examples` panel variable has been renamed to `scenarios`. The `examples` variable will be removed in the next major release.") rendered_scenarios end, target: target, data: Lookbook.data, app: Lookbook }) end |
#lookup_entities ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/controllers/concerns/lookbook/targetable_concern.rb', line 16 def lookup_entities @target = Engine.previews.find_scenario_by_path(params[:path]) if @target.present? @preview = @target.preview if params[:path] == @preview&.lookup_path redirect_to lookbook_inspect_path("#{params[:path]}/#{@preview.default_scenario.name}", params.permit!) end else @preview = Engine.previews.find_by_path(params[:path]) if @preview.present? default_scenario = @preview.default_scenario redirect_to lookbook_inspect_path(default_scenario.lookup_path, params.permit!) if default_scenario end end end |
#path_segments ⇒ Object
119 120 121 |
# File 'app/controllers/concerns/lookbook/targetable_concern.rb', line 119 def path_segments params[:path].split("/") end |
#set_display_options ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/controllers/concerns/lookbook/targetable_concern.rb', line 32 def @dynamic_display_options = [] @static_display_options = [] if @target.present? opts = @target. @dynamic_display_options = opts.select { _2.is_a?(Array) || _2.is_a?(Hash) } @static_display_options = opts.except(*@dynamic_display_options.keys) if params[:_display] display_params = SearchParamParser.call(params[:_display]) display_params.each do |name, value| if @dynamic_display_options.key?(name) ["lookbook-display-#{name}"] = value.is_a?(Array) ? value[1] : value end end end @dynamic_display_options.each do |name, opts| choices = opts.is_a?(Hash) ? opts[:choices].to_a : opts value = choices.first.is_a?(Array) ? choices.first[1] : choices.first @static_display_options[name] ||= .fetch("lookbook-display-#{name}", value) end unless params[:_display] display_params = @dynamic_display_options.each_with_object({}) do |(name, opts), hash| hash[name] = @static_display_options[name] end request.query_parameters[:_display] = SearchParamEncoder.call(display_params) end end end |
#set_params ⇒ Object
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 |
# File 'app/controllers/concerns/lookbook/targetable_concern.rb', line 65 def set_params @params = [] if @target = @target.("param").uniq(&:name) @params = .map do |param_tag| Param.from_tag( param_tag, value: preview_controller.params[param_tag.name] ) end # cast known param values to correct type @params.each do |param| if preview_controller.params.key?(param.name) preview_controller.params[param.name] = param.cast_value end end # set display and data params for use in preview layouts preview_controller.params[:lookbook] = { display: @static_display_options, data: Lookbook.data } end preview_controller.params.permit! end |
#set_title ⇒ Object
12 13 14 |
# File 'app/controllers/concerns/lookbook/targetable_concern.rb', line 12 def set_title @title = @target.present? ? [@target&.label, @preview&.label].compact.join(" :: ") : "Not found" end |