From 38f9218fd0fdcb5e17bd3c497ece41b4cd3205b4 Mon Sep 17 00:00:00 2001 From: sebi Date: Fri, 22 Jul 2022 14:10:54 +0100 Subject: [PATCH] initial --- Gemfile.lock | 2 +- .../ui/core/plain_builder_wrapper.rb | 17 +++++ lib/matestack/ui/core/tag_helper.rb | 62 +++++++++++++++++++ spec/core_spec_helper.rb | 30 ++++----- 4 files changed, 95 insertions(+), 16 deletions(-) create mode 100644 lib/matestack/ui/core/plain_builder_wrapper.rb diff --git a/Gemfile.lock b/Gemfile.lock index 1d03b7e6..6a1a5420 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - matestack-ui-core (3.0.0) + matestack-ui-core (3.0.1) rails (>= 5.2) GEM diff --git a/lib/matestack/ui/core/plain_builder_wrapper.rb b/lib/matestack/ui/core/plain_builder_wrapper.rb new file mode 100644 index 00000000..4b026bfa --- /dev/null +++ b/lib/matestack/ui/core/plain_builder_wrapper.rb @@ -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 \ No newline at end of file diff --git a/lib/matestack/ui/core/tag_helper.rb b/lib/matestack/ui/core/tag_helper.rb index b62f6247..213c234a 100644 --- a/lib/matestack/ui/core/tag_helper.rb +++ b/lib/matestack/ui/core/tag_helper.rb @@ -1,4 +1,6 @@ require_relative 'slots' +require_relative 'plain_builder_wrapper' + module Matestack module Ui module Core @@ -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) @@ -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 diff --git a/spec/core_spec_helper.rb b/spec/core_spec_helper.rb index de2963d8..5d4f8bab 100644 --- a/spec/core_spec_helper.rb +++ b/spec/core_spec_helper.rb @@ -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!