Module: BetterSeo::Rails::Helpers::StructuredDataHelper

Defined in:
lib/better_seo/rails/helpers/structured_data_helper.rb

Constant Summary collapse

TYPE_MAPPING =
{
  organization: BetterSeo::StructuredData::Organization,
  article: BetterSeo::StructuredData::Article,
  person: BetterSeo::StructuredData::Person,
  product: BetterSeo::StructuredData::Product,
  breadcrumb_list: BetterSeo::StructuredData::BreadcrumbList,
  local_business: BetterSeo::StructuredData::LocalBusiness,
  event: BetterSeo::StructuredData::Event,
  faq_page: BetterSeo::StructuredData::FAQPage,
  how_to: BetterSeo::StructuredData::HowTo,
  recipe: BetterSeo::StructuredData::Recipe
}.freeze

Instance Method Summary collapse

Instance Method Details

#article_sd(**properties, &block) ⇒ Object



42
43
44
# File 'lib/better_seo/rails/helpers/structured_data_helper.rb', line 42

def article_sd(**properties, &block)
  structured_data_tag(:article, **properties, &block)
end


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/better_seo/rails/helpers/structured_data_helper.rb', line 54

def breadcrumb_list_sd(items: nil, &block)
  if block_given?
    structured_data_tag(:breadcrumb_list, &block)
  elsif items
    structured_data_tag(:breadcrumb_list) do |breadcrumb|
      breadcrumb.add_items(items)
    end
  else
    structured_data_tag(:breadcrumb_list)
  end
end

#event_sd(**properties, &block) ⇒ Object



70
71
72
# File 'lib/better_seo/rails/helpers/structured_data_helper.rb', line 70

def event_sd(**properties, &block)
  structured_data_tag(:event, **properties, &block)
end

#faq_page_sd(questions: nil, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/better_seo/rails/helpers/structured_data_helper.rb', line 74

def faq_page_sd(questions: nil, &block)
  if block_given?
    structured_data_tag(:faq_page, &block)
  elsif questions
    structured_data_tag(:faq_page) do |faq|
      faq.add_questions(questions)
    end
  else
    structured_data_tag(:faq_page)
  end
end

#how_to_sd(steps: nil, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/better_seo/rails/helpers/structured_data_helper.rb', line 86

def how_to_sd(steps: nil, &block)
  if block_given?
    structured_data_tag(:how_to, &block)
  elsif steps
    structured_data_tag(:how_to) do |how_to|
      how_to.add_steps(steps)
    end
  else
    structured_data_tag(:how_to)
  end
end

#local_business_sd(**properties, &block) ⇒ Object



66
67
68
# File 'lib/better_seo/rails/helpers/structured_data_helper.rb', line 66

def local_business_sd(**properties, &block)
  structured_data_tag(:local_business, **properties, &block)
end

#organization_sd(**properties, &block) ⇒ Object



38
39
40
# File 'lib/better_seo/rails/helpers/structured_data_helper.rb', line 38

def organization_sd(**properties, &block)
  structured_data_tag(:organization, **properties, &block)
end

#person_sd(**properties, &block) ⇒ Object



46
47
48
# File 'lib/better_seo/rails/helpers/structured_data_helper.rb', line 46

def person_sd(**properties, &block)
  structured_data_tag(:person, **properties, &block)
end

#product_sd(**properties, &block) ⇒ Object



50
51
52
# File 'lib/better_seo/rails/helpers/structured_data_helper.rb', line 50

def product_sd(**properties, &block)
  structured_data_tag(:product, **properties, &block)
end

#recipe_sd(ingredients: nil, instructions: nil, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/better_seo/rails/helpers/structured_data_helper.rb', line 98

def recipe_sd(ingredients: nil, instructions: nil, &block)
  if block_given?
    structured_data_tag(:recipe, &block)
  else
    structured_data_tag(:recipe) do |recipe|
      recipe.add_ingredients(ingredients) if ingredients
      recipe.add_instructions(instructions) if instructions
      yield(recipe) if block_given?
    end
  end
end

#structured_data_tag(type_or_object, **properties, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/better_seo/rails/helpers/structured_data_helper.rb', line 20

def structured_data_tag(type_or_object, **properties, &block)
  sd_object = if type_or_object.is_a?(Symbol)
                create_structured_data(type_or_object, properties, &block)
              else
                type_or_object
              end

  raw(sd_object.to_script_tag)
end

#structured_data_tags(objects = nil) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/better_seo/rails/helpers/structured_data_helper.rb', line 30

def structured_data_tags(objects = nil)
  array = block_given? ? yield : objects
  return "" if array.nil? || array.empty?

  tags = array.map(&:to_script_tag).join("\n\n")
  raw(tags)
end