Skip to content

Commit

Permalink
feat: Added meta tags component
Browse files Browse the repository at this point in the history
  • Loading branch information
joelmoss committed Feb 1, 2024
1 parent 13aaac0 commit b1b6424
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
1 change: 0 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
AllCops:
TargetRubyVersion: 3.0
SuggestExtensions: false
NewCops: enable

Expand Down
2 changes: 2 additions & 0 deletions lib/phlexible/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 27 additions & 0 deletions lib/phlexible/rails/action_controller/meta_tags.rb
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions lib/phlexible/rails/meta_tags_component.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b1b6424

Please sign in to comment.