matestack / matestack-ui-core

Component based web UIs in pure Ruby for Rails. Boost your productivity & easily create component based web UIs in pure Ruby.
https://www.matestack.io
MIT License
547 stars 47 forks source link

Rails native view tags support #598

Open sebyx07 opened 2 years ago

sebyx07 commented 2 years ago

would be nice if matestack would support rails view tags by default

def response
  div do
    content_tag :div, class: 'bg-blue-500' do
      link_to root_path do
        image_tag 'logo.png'
      end
    end

    form_with url: posts_path do |form|
      form.text_field :title

      div do
         form.submit 'Save'
      end
    end
  end
end

I know matestack has it's own form way, but still

jonasjabari commented 2 years ago

there you go: https://docs.matestack.io/matestack-ui-core/html-rendering/integrating-action-view-helpers :)

sebyx07 commented 2 years ago

i came with a smth:

module MatestackCoreHelper
  [
    ActionView::Helpers::UrlHelper.instance_methods(false),
    ActionView::Helpers::AssetTagHelper.instance_methods(false),
  ].flatten.each do |rails_view_method|
    define_method rails_view_method do |*args, **opts, &block|
      if block
        args.unshift(matestack_to_s { block.call })
      end

      plain { super(*args, **opts) }
    end
  end

  %i[
    form_with form_for content_tag
  ].each do |rails_view_method|
    define_method rails_view_method do |*args, **opts, &block|
      plain do
        super(*args, **opts) do |form|
          matestack_to_s do
            block.call(PlainWrapper.new(self, form))
          end
        end
      end
    end
  end

  class PlainWrapper
    def initialize(ctx, form)
      @ctx = ctx; @form = form
    end

    private def method_missing(method, *args, **opts, &block)
      return super unless @form.respond_to?(method)

      @ctx.plain @form.send(method, *args, **opts, &block)
    end
  end
end

now the example above works w/o calling plain explicitly

sebyx07 commented 2 years ago

I also tried with simple_form and it works, just had to add simple_form_for to call PlainWrapper