Class: Props::Partialer
- Inherits:
-
Object
- Object
- Props::Partialer
- Defined in:
- lib/props_template/extensions/partial_renderer.rb
Constant Summary collapse
- INVALID_PARTIAL_MESSAGE =
"The partial name must be a string, but received (%s)."- OPTION_AS_ERROR_MESSAGE =
"The value (%s) of the option `as` is not a valid Ruby identifier; " \ "make sure it starts with lowercase letter, " \ "and is followed by any combination of letters, numbers and underscores."
- IDENTIFIER_ERROR_MESSAGE =
"The partial name (%s) is not a valid Ruby identifier; " \ "make sure your partial name starts with underscore."
Instance Method Summary collapse
- #block_opts_to_render_opts(builder, options) ⇒ Object
- #build_rendered_template(content, template, layout = nil) ⇒ Object
- #extract_details(options) ⇒ Object
- #find_template(partial_opts) ⇒ Object
- #handle(options, item = nil) ⇒ Object
-
#initialize(base, context, builder) ⇒ Partialer
constructor
A new instance of Partialer.
-
#instrument(name, **options) ⇒ Object
:doc:.
- #normalize_options(options, item = nil) ⇒ Object
- #raise_invalid_identifier(path) ⇒ Object
- #raise_invalid_option_as(as) ⇒ Object
- #refine_options(options, item = nil) ⇒ Object
- #registered_details ⇒ Object
- #render_partial(template, view, options) ⇒ Object
- #retrieve_template_keys(options) ⇒ Object
- #retrieve_variable(path) ⇒ Object
Constructor Details
#initialize(base, context, builder) ⇒ Partialer
Returns a new instance of Partialer.
26 27 28 29 30 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 26 def initialize(base, context, builder) @context = context @builder = builder @base = base end |
Instance Method Details
#block_opts_to_render_opts(builder, options) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 63 def block_opts_to_render_opts(builder, ) partial, pass_opts = [*[:partial]] pass_opts = pass_opts&.clone || {} pass_opts[:locals] ||= {} pass_opts[:partial] = partial pass_opts[:formats] = [:json] pass_opts[:handlers] = [:props] if !(String === partial) raise ArgumentError.new(INVALID_PARTIAL_MESSAGE % partial.inspect) end pass_opts end |
#build_rendered_template(content, template, layout = nil) ⇒ Object
103 104 105 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 103 def build_rendered_template(content, template, layout = nil) RenderedTemplate.new content, layout, template end |
#extract_details(options) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 32 def extract_details() registered_details.each_with_object({}) do |key, details| value = [key] details[key] = Array(value) if value end end |
#find_template(partial_opts) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 48 def find_template(partial_opts) partial = partial_opts[:partial] template_keys = retrieve_template_keys(partial_opts) details = extract_details(partial_opts) prefixes = partial.include?("/") ? [] : @context.lookup_context.prefixes @context.lookup_context.find_template(partial, prefixes, true, template_keys, details) end |
#handle(options, item = nil) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 78 def handle(, item = nil) return if ![:partial] = (, item) partial_opts = block_opts_to_render_opts(@builder, ) template = if [:_template] [:_template] else # mutate the original options to bypass find_template a second time. [:_template] = find_template(partial_opts) end render_partial(template, @context, partial_opts) end |
#instrument(name, **options) ⇒ Object
:doc:
107 108 109 110 111 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 107 def instrument(name, **) # :doc: ActiveSupport::Notifications.instrument("render_#{name}.action_view", ) do |payload| yield payload end end |
#normalize_options(options, item = nil) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 131 def (, item = nil) partial, rest = [*[:partial]] rest = (rest || {}).clone locals = (rest[:locals] || {}).clone rest[:locals] = locals if item as = if !rest[:as] retrieve_variable(partial) else rest[:as].to_sym end raise_invalid_option_as(as) unless /\A[a-z_]\w*\z/.match?(as.to_s) locals[as] = item end {partial: [partial, rest]} end |
#raise_invalid_identifier(path) ⇒ Object
117 118 119 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 117 def raise_invalid_identifier(path) raise ArgumentError.new(IDENTIFIER_ERROR_MESSAGE % path) end |
#raise_invalid_option_as(as) ⇒ Object
113 114 115 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 113 def raise_invalid_option_as(as) raise ArgumentError.new(OPTION_AS_ERROR_MESSAGE % as) end |
#refine_options(options, item = nil) ⇒ Object
127 128 129 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 127 def (, item = nil) .clone end |
#registered_details ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 40 def registered_details if ActionView.version.to_s >= "7" ActionView::LookupContext.registered_details else @context.lookup_context.registered_details end end |
#render_partial(template, view, options) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 93 def render_partial(template, view, ) instrument(:partial, identifier: template.identifier) do |payload| locals = [:locals] content = template.render(view, locals) payload[:cache_hit] = view.view_renderer.cache_hits[template.virtual_path] build_rendered_template(content, template) end end |
#retrieve_template_keys(options) ⇒ Object
57 58 59 60 61 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 57 def retrieve_template_keys() template_keys = [:locals].keys template_keys << [:as] if [:as] template_keys end |
#retrieve_variable(path) ⇒ Object
121 122 123 124 125 |
# File 'lib/props_template/extensions/partial_renderer.rb', line 121 def retrieve_variable(path) base = (path[-1] == "/") ? "" : File.basename(path) raise_invalid_identifier(path) unless base =~ /\A_?(.*?)(?:\.\w+)*\z/ $1.to_sym end |