Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_resource DSL now available for builders #419

Merged
merged 2 commits into from
Oct 16, 2021
Merged

Conversation

jaredcwhite
Copy link
Member

Summary

This provides a DSL for builder plugins to add resources, much like the past era doc DSL. For example:

def build
  add_resource :posts, "im-a-markdown-post.md" do
    title "I'm a Markdown post!"
    resolve_me from: -> { method_value }
    delayed -> { 123 }
    nested do
      val 456
    end
    content <<~MARKDOWN
      # Hello World!
    MARKDOWN
  end
end

There are a few added niceties compared to the previous doc as well as a few slight changes, but generally it should work and feel pretty much the same.

One cool thing now that we're in the resources era is if you add def self.resource_data_for_id(id) to your builder, you can recreate the resource via Bridgetown::Model::Find!

def build
  # creates id: builder://MyBuilder/later.html
  add_resource :page, "later.html" do
    title "Later, alligator!"
  end
end

def self.resource_data_for_id(id)
  { title: "After a while, crocodile!" } if id == "builder://MyBuilder/later.html"
end

later:

resource = Bridgetown::Model::Base.find("builder://MyBuilder/later.html").as_resource_in_collection

resource.data.title == "After a while, crocodile!" # true

(That would prove useful in any scenario in which you're adding a whole bunch of resources based on external API data.)

Tests have been added and passing, documentation will be forthcoming as part of the overall website revamp.

Context

Closes #332

@render
Copy link

render bot commented Oct 14, 2021

@render
Copy link

render bot commented Oct 14, 2021

@jaredcwhite jaredcwhite merged commit 65b8f4b into main Oct 16, 2021
@jaredcwhite jaredcwhite deleted the add_resource_dsl branch October 16, 2021 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add the Resource equivalent to the doc method in builders
1 participant