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 #599

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
matestack-ui-core (3.0.0)
matestack-ui-core (3.0.1)
rails (>= 5.2)

GEM
Expand Down
17 changes: 17 additions & 0 deletions lib/matestack/ui/core/plain_builder_wrapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Matestack
module Ui
module Core
module PlainBuilderWrapper
def initialize(ctx, builder)
@ctx = ctx; @builder = builder
end

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

@ctx.plain @builder.send(method, *args, **opts, &block)
end
end
end
end
end
62 changes: 62 additions & 0 deletions lib/matestack/ui/core/tag_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require_relative 'slots'
require_relative 'plain_builder_wrapper'

module Matestack
module Ui
module Core
Expand All @@ -11,6 +13,20 @@ module TagHelper

TAGS = [:a, :abbr, :acronym, :address, :applet, :area, :article, :aside, :audio, :b, :base, :basefont, :bdi, :bdo, :big, :blockquote, :body, :br, :button, :canvas, :caption, :center, :cite, :code, :col, :colgroup, :data, :datalist, :dd, :del, :details, :dfn, :dialog, :dir, :div, :dl, :dt, :em, :embed, :fieldset, :figcaption, :figure, :font, :footer, :form, :frame, :frameset, :h1, :h2, :h3, :h4, :h5, :h6, :head, :header, :hr, :html, :i, :iframe, :img, :input, :ins, :kbd, :label, :legend, :li, :link, :main, :map, :mark, :meta, :meter, :nav, :noframes, :noscript, :object, :ol, :optgroup, :option, :output, :paragraph, :param, :picture, :pre, :progress, :q, :rp, :rt, :ruby, :s, :samp, :script, :section, :select, :small, :source, :span, :strike, :strong, :style, :sub, :summary, :sup, :svg, :table, :tbody, :td, :template, :textarea, :tfoot, :th, :thead, :time, :title, :tr, :track, :tt, :u, :ul, :var, :video, :wbr]

DEFAULT_RAILS_PLAIN_METHODS_PATCH_MODULES = %w[ActionView::Helpers::UrlHelper ActionView::Helpers::AssetTagHelper]
SUPPORTED_GEMS_PLAIN_METHODS = {
simple: {
:Kaminari => %w[paginate]
},
block: {
:SimpleForm => %w[simple_form_for]
}
}

DEFAULT_RAILS_PLAIN_BUILDER_METHODS_PATCH_MODULES = %w[
form_with form_for
]

VOID_TAGS.each do |tag|
define_method tag do |options = {}|
Matestack::Ui::Core::Base.new(tag, nil, options)
Expand Down Expand Up @@ -93,6 +109,52 @@ def detached(text=nil, options=nil, &block)
def detached_to_s(text=nil, options=nil, &block)
detached(text, options, &block).to_str
end

def self.register_plain_method(method, block: false)
if block
define_method method do |*args, **opts, &b|
plain do
super(*args, **opts) do |builder|
matestack_to_s do
b.call(PlainBuilderWrapper.new(self, builder))
end
end
end
end
else
define_method method do |*args, **opts, &b|
if b
args.unshift(matestack_to_s { b.call })
end

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

DEFAULT_RAILS_PLAIN_METHODS_PATCH_MODULES.filter_map do |mod|
next unless Object.const_defined?(mod)
Object.const_get(mod).instance_methods(false)
end.flatten.each { |method| register_plain_method(method) }

DEFAULT_RAILS_PLAIN_BUILDER_METHODS_PATCH_MODULES.each do |method|
register_plain_method(method, block: true)
end

SUPPORTED_GEMS_PLAIN_METHODS[:simple].each do |mod, methods|
next unless Object.const_defined?(mod)
methods.each do |method|
register_plain_method(method)
end
end

SUPPORTED_GEMS_PLAIN_METHODS[:block].each do |mod, methods|
next unless Object.const_defined?(mod)
methods.each do |method|
register_plain_method(method, block: true)
end
end

alias matestack_to_s detached_to_s

end
Expand Down
30 changes: 15 additions & 15 deletions spec/core_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
# it.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
require 'simplecov'
SimpleCov.start do
enable_coverage :branch

add_filter "/spec"
add_filter %r{^/config/}


add_group "Concepts", "/app/concepts"
add_group "Helpers", "/app/helpers"
add_group "App Lib", "/app/lib"
add_group "Lib", %r{^/lib/}

track_files "{app,lib}/**/*.rb"
end
# require 'simplecov'
# SimpleCov.start do
# enable_coverage :branch
#
# add_filter "/spec"
# add_filter %r{^/config/}
#
#
# add_group "Concepts", "/app/concepts"
# add_group "Helpers", "/app/helpers"
# add_group "App Lib", "/app/lib"
# add_group "Lib", %r{^/lib/}
#
# track_files "{app,lib}/**/*.rb"
# end

require 'webmock/rspec'
WebMock.allow_net_connect!
Expand Down