Module: Roda::RodaPlugins::BridgetownRoutes::InstanceMethods

Defined in:
lib/roda/plugins/bridgetown_routes.rb

Instance Method Summary collapse

Instance Method Details

#front_matter(&block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/roda/plugins/bridgetown_routes.rb', line 75

def front_matter(&block)
  b = block.binding
  denylisted = %i(r argv)
  data = b.local_variables.filter_map do |key|
    next if denylisted.any? key

    [key, b.local_variable_get(key)]
  end.to_h

  Bridgetown::FrontMatter::RubyFrontMatter.new(data:).tap { _1.instance_exec(&block) }.to_h
end

#renderObject



118
119
120
# File 'lib/roda/plugins/bridgetown_routes.rb', line 118

def render(...)
  view.render(...)
end

#render_with(data: {}) ⇒ Object

rubocop:todo Metrics/AbcSize, Metrics/MethodLength



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/roda/plugins/bridgetown_routes.rb', line 87

def render_with(data: {}, &) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  data = front_matter(&) if data.empty? && block_given?
  path = Kernel.caller_locations(1, 1).first.path
  source_path = Pathname.new(path).relative_path_from(
    bridgetown_site.in_source_dir("_routes")
  )
  code = response._route_file_code

  unless code.present?
    raise Bridgetown::Errors::FatalException,
          "`render_with' method must be called from a template-based file in `src/_routes'"
  end

  data = Bridgetown::Model::BuilderOrigin.new(
    Bridgetown::Model::BuilderOrigin.id_for_builder_path(
      self, Addressable::URI.encode(source_path.to_s)
    )
  ).read do
    data[:_collection_] = bridgetown_site.collections.pages
    data[:_original_path_] = path
    data[:_relative_path_] = source_path
    data[:_front_matter_line_count_] = response._front_matter_line_count
    data[:_content_] = code
    data
  end

  Bridgetown::Model::Base.new(data).to_resource.tap do |resource|
    resource.roda_app = self
  end.read!
end

#routes_manifestObject



50
51
52
# File 'lib/roda/plugins/bridgetown_routes.rb', line 50

def routes_manifest
  self.class.opts[:routes_manifest]
end

#run_file_route(file, slug:) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/roda/plugins/bridgetown_routes.rb', line 54

def run_file_route(file, slug:)
  response["X-Bridgetown-Routes"] = "1"
  # eval_route_file caches when Bridgetown.env.production?
  Bridgetown::Routes::CodeBlocks.eval_route_file file, slug, self

  # set route locale
  locale = routes_manifest.locale_for(slug)
  I18n.locale = request.params[:locale] = locale

  # get the route block extracted from the file at slug
  route_block = Bridgetown::Routes::CodeBlocks.route_block(slug)
  response.instance_variable_set(
    :@_route_file_code, route_block.instance_variable_get(:@_route_file_code)
  ) # could be nil
  response.instance_variable_set(
    :@_front_matter_line_count,
    route_block.instance_variable_get(:@_front_matter_line_count)
  ) # could be nil
  instance_exec(request, &route_block)
end

#view(view_class: Bridgetown::ERBView) ⇒ Object



122
123
124
125
126
127
# File 'lib/roda/plugins/bridgetown_routes.rb', line 122

def view(view_class: Bridgetown::ERBView)
  # TODO: support user choosing templates by extension rather than class
  response._fake_resource_view(
    view_class:, roda_app: self, bridgetown_site:
  )
end