lib/lifeform/libraries/default/input.rb in lifeform-0.1.0 vs lib/lifeform/libraries/default/input.rb in lifeform-0.2.0

- old
+ new

@@ -20,10 +20,11 @@ def verify_attributes # rubocop:disable Metrics @model = attributes.delete(:model) @model = form.model if form.model && @model.nil? + @if = attributes.delete(:if) attributes[:value] ||= value_for_model if form.model attributes[:name] = "#{model_name}[#{attributes[:name]}]" if @model attributes[:id] ||= attributes[:name].parameterize(separator: "_") @label = handle_labels if attributes[:label] end @@ -48,29 +49,36 @@ ).tap do @attributes = attributes.filter_map { |k, v| [k, v] unless k == :label }.to_h end end - def render_in(view_context) + def render_in(view_context, &block) @view_context = view_context + @content = block + return "" if !@if.nil? && !@if + template end - def template - Papercraft.html do |wrapper_tag:, input_tag:, attributes:, field_type:, field_label:| + def template # rubocop:disable Metrics/AbcSize + Papercraft.html do |wrapper_tag:, input_tag:, attributes:, field_data:| field_body = proc { - emit(field_label) if field_label - send input_tag, type: field_type, **attributes + emit(field_data[:label]) if field_data[:label] + send input_tag, type: field_data[:type], **attributes + emit(field_data[:content]) if field_data[:content] } next field_body.call unless wrapper_tag send wrapper_tag, name: attributes[:name], &field_body end.render( wrapper_tag: self.class.const_get(:WRAPPER_TAG), input_tag: self.class.const_get(:INPUT_TAG), attributes: attributes, - field_type: @field_type, - field_label: @label + field_data: { + type: @field_type, + label: @label, + content: @content && @view_context.capture(&@content) + } ) end end end end