From b1b64249c7b2d716948d87a6915018a3472ac8b0 Mon Sep 17 00:00:00 2001 From: Joel Moss Date: Thu, 1 Feb 2024 19:35:46 +0000 Subject: [PATCH] feat: Added meta tags component --- .rubocop.yml | 1 - lib/phlexible/rails.rb | 2 ++ .../rails/action_controller/meta_tags.rb | 27 +++++++++++++++++++ lib/phlexible/rails/meta_tags_component.rb | 13 +++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 lib/phlexible/rails/action_controller/meta_tags.rb create mode 100644 lib/phlexible/rails/meta_tags_component.rb diff --git a/.rubocop.yml b/.rubocop.yml index d7108f4..03e8d2f 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,5 +1,4 @@ AllCops: - TargetRubyVersion: 3.0 SuggestExtensions: false NewCops: enable diff --git a/lib/phlexible/rails.rb b/lib/phlexible/rails.rb index dad02d7..3e5296a 100644 --- a/lib/phlexible/rails.rb +++ b/lib/phlexible/rails.rb @@ -8,11 +8,13 @@ module Rails autoload :Responder, 'phlexible/rails/responder' autoload :AElement, 'phlexible/rails/a_element' + autoload :MetaTagsComponent, 'phlexible/rails/meta_tags_component' autoload :ButtonTo, 'phlexible/rails/button_to' autoload :ButtonToConcerns, 'phlexible/rails/button_to' module ActionController autoload :ImplicitRender, 'phlexible/rails/action_controller/implicit_render' + autoload :MetaTags, 'phlexible/rails/action_controller/meta_tags' end end end diff --git a/lib/phlexible/rails/action_controller/meta_tags.rb b/lib/phlexible/rails/action_controller/meta_tags.rb new file mode 100644 index 0000000..6326736 --- /dev/null +++ b/lib/phlexible/rails/action_controller/meta_tags.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module Phlexible + module Rails + module ActionController + module MetaTags + extend ActiveSupport::Concern + + def meta_tags + @meta_tags ||= {} + end + + def meta_tag(name, content) + meta_tags[name] = content + end + + module ClassMethods + def meta_tag(name, content, **kwargs) + before_action(**kwargs) do |ctrl| + ctrl.meta_tag name, content.is_a?(Proc) ? ctrl.instance_exec(&content) : content + end + end + end + end + end + end +end diff --git a/lib/phlexible/rails/meta_tags_component.rb b/lib/phlexible/rails/meta_tags_component.rb new file mode 100644 index 0000000..9e342f4 --- /dev/null +++ b/lib/phlexible/rails/meta_tags_component.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Phlexible + module Rails + class MetaTagsComponent < Phlex::HTML + def template + controller.instance_variable_get(:@meta_tags)&.each do |name, content| + meta name: name, content: content.is_a?(String) ? content : content.to_json + end + end + end + end +end