Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails native view tags support #598

Open
sebyx07 opened this issue Jul 22, 2022 · 3 comments
Open

Rails native view tags support #598

sebyx07 opened this issue Jul 22, 2022 · 3 comments

Comments

@sebyx07
Copy link

sebyx07 commented Jul 22, 2022

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
Copy link
Member

@sebyx07
Copy link
Author

sebyx07 commented Jul 22, 2022

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
Copy link
Author

sebyx07 commented Jul 22, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants