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

Getting Rails support to work #22

Open
fkchang opened this issue Feb 21, 2021 · 7 comments
Open

Getting Rails support to work #22

fkchang opened this issue Feb 21, 2021 · 7 comments

Comments

@fkchang
Copy link

fkchang commented Feb 21, 2021

I'm using 1.4.2 which still has Rails support, but I get this error when a wunderbar _html template

uninitialized constant Wunderbar::HtmlMarkup

I did include it in the Gemfile as

gem 'wunderbar', '1.4.2', require: 'wunderbar/rails'

Is there another step?

@rubys
Copy link
Owner

rubys commented Feb 21, 2021

It looks like there is two problems. First, wunderbar/rails doesn't require wunderbar, which is why you are getting that error. Second, it looks like the Rails API has changed and default_format is now expected to be a symbol, not a mime type.

For now, change your gemfile to read:

gem 'wunderbar', '~ 1.4.5'

And drop the following file into your config/initializers directory, perhaps with the filename of wunderbar.rb:

module Wunderbar
  module Rails
    class HtmlHandler
      cattr_accessor :default_format
      self.default_format = :html

      def self.call(template, source=nil)
        %{
          compiled = Proc.new {#{template.source}}
          x = Wunderbar::HtmlMarkup.new(self);
          instance_variables.each do |var|
            x.instance_variable_set var, instance_variable_get(var)
          end
          x.instance_eval(&compiled)
          x._.target!
        }.strip # take care to preserve line numbers in original source
      end
    end

    class JsonHandler
      cattr_accessor :default_format
      self.default_format = :json

      def self.call(template, source=nil)
        %{
          compiled = Proc.new {#{template.source}}
          x = Wunderbar::JsonBuilder.new(self);
          instance_variables.each do |var|
            x.instance_variable_set var, instance_variable_get(var)
          end
          x.instance_eval(&compiled)
          x.target!
        }.strip # take care to preserve line numbers in original source
      end
    end

    ActionView::Template.register_template_handler :_html, HtmlHandler
    ActionView::Template.register_template_handler :_json, JsonHandler
  end
end

With that, you should be able to serve files with an _html extension. For example:

rails generate controller hello test
rm app/views/hello/test.html.erb

create a file named app/views/test._html with the following contents:

_h1 "wunderbar"
_p 'it worked'

@fkchang
Copy link
Author

fkchang commented Feb 22, 2021

That did the trick, thanks

@fkchang
Copy link
Author

fkchang commented Feb 22, 2021

Can I render a haml partial from my _html template? render 'partial_name' isn't working

@fkchang
Copy link
Author

fkchang commented Feb 23, 2021

I see I can't really reuse the normal render or helpers, they must output elsewhere and not to a string

@rubys
Copy link
Owner

rubys commented Feb 23, 2021

I'm pretty sure that one can have haml partials, so it must be possible. I'll try to investigate in a bit.

@rubys
Copy link
Owner

rubys commented Feb 24, 2021

An example of rendering a partial from with a wunderbar view. For illustrative purposes, 'bin/rails generate scaffold book title author'. Now you will have a directory named app/views/books with a file named new.html.erb. Rename that file to new._html, and replace its contents with:

_h1 'New Book'
  
_ { render 'form', book: @book }

_ { link_to 'Back', books_path }

A modification to the class initializer described above will allow wunderbar partials to be included from other sources (erb, haml, wunderbar, whatever).

In class HtmlHandler, modify

x._.target!

to read:

x._.target!.html_safe

@fkchang
Copy link
Author

fkchang commented Feb 25, 2021

That's working for me, thanks

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