diff --git a/src/breeze.cr b/src/breeze.cr index 945df0e..c0197c3 100644 --- a/src/breeze.cr +++ b/src/breeze.cr @@ -2,6 +2,7 @@ require "pulsar" require "avram" require "lucky" require "./charms/*" +require "./breeze/extension" require "./breeze/models/*" require "./breeze/operations/*" require "./breeze/queries/mixins/*" @@ -20,9 +21,14 @@ require "./breeze/pages/**" module Breeze VERSION = "0.1.0" + class_getter extensions = [] of Breeze::Extension Habitat.create do setting database : Avram::Database.class, example: "AppDatabase" setting enabled : Bool, example: "Lucky::Env.development?" end + + def self.register(extension : Breeze::Extension) + extensions << extension + end end diff --git a/src/breeze/components/breeze/navbar_links.cr b/src/breeze/components/breeze/navbar_links.cr index f6165e3..28546f4 100644 --- a/src/breeze/components/breeze/navbar_links.cr +++ b/src/breeze/components/breeze/navbar_links.cr @@ -1,24 +1,15 @@ class Breeze::NavbarLinks < Breeze::BreezeComponent needs context : HTTP::Server::Context - Habitat.create do - setting links : Array(Proc(Breeze::NavbarLinks, Nil)) = [ - ->(navbar : Breeze::NavbarLinks) { - navbar.mount_link("Requests", to: Breeze::Requests::Index) - }, - ->(navbar : Breeze::NavbarLinks) { - navbar.mount_link("Queries", to: Breeze::Queries::Index) - }, - ] - end - def render - settings.links.each do |component| - component.call(self) + mount_link("Requests", to: Breeze::Requests::Index) + mount_link("Queries", to: Breeze::Queries::Index) + Breeze.extensions.each do |extension| + mount extension.navbar_link(context) end end - def mount_link(link_text : String, to action : Lucky::Action.class) + private def mount_link(link_text : String, to action : Lucky::Action.class) mount(Breeze::NavbarLink, context: context, link_text: link_text, link_to: action.path) end end diff --git a/src/breeze/components/breeze_component.cr b/src/breeze/components/breeze_component.cr index c719a88..72a8adb 100644 --- a/src/breeze/components/breeze_component.cr +++ b/src/breeze/components/breeze_component.cr @@ -1,4 +1,24 @@ abstract class Breeze::BreezeComponent < Lucky::BaseComponent include Lucky::UrlHelpers alias HtmlProc = Proc(IO::Memory) | Proc(Nil) + + def mount(component : Lucky::BaseComponent, *args, **named_args) : Nil + print_component_comment(component) do + component.view(view).render + end + end + + def mount(component : Lucky::BaseComponent, *args, **named_args) : Nil + print_component_comment(component) do + component.view(view).render do |*yield_args| + yield *yield_args + end + end + end + + private def print_component_comment(component : Lucky::BaseComponent) : Nil + print_component_comment(component.class) do + yield + end + end end diff --git a/src/breeze/extension.cr b/src/breeze/extension.cr new file mode 100644 index 0000000..d184891 --- /dev/null +++ b/src/breeze/extension.cr @@ -0,0 +1,22 @@ +# To create an extension you must extend this module +# +# Example: +# ``` +# module BreezeCustom +# extend Breeze::Extension +# +# def self.navbar_link(context : HTTP::Server::Context) : Breeze::NavbarLink +# Breeze::NavbarLink.new(context: context, link_text: "Custom", link_to: BreezeCustom::Index.path) +# end +# end +# ``` +# +# Projects that want to use this extension will need to register it +# +# ``` +# Breeze.register BreezeCustom +# ``` +# +module Breeze::Extension + abstract def navbar_link(context : HTTP::Server::Context) : Breeze::NavbarLink +end diff --git a/src/extensions/breeze_carbon.cr b/src/extensions/breeze_carbon.cr index 5b97707..6274953 100644 --- a/src/extensions/breeze_carbon.cr +++ b/src/extensions/breeze_carbon.cr @@ -5,11 +5,17 @@ require "../breeze_carbon/actions/**" require "../breeze_carbon/pages/**" module BreezeCarbon + extend Breeze::Extension + Habitat.create do setting email_previews : Carbon::EmailPreviews.class, example: "Emails::Previews" end -end -Breeze::NavbarLinks.settings.links << ->(navbar : Breeze::NavbarLinks) { - navbar.mount_link("Emails", to: BreezeCarbon::Emails::Index) -} + def self.navbar_link(context : HTTP::Server::Context) : Breeze::NavbarLink + Breeze::NavbarLink.new( + context: context, + link_text: "Emails", + link_to: BreezeCarbon::Emails::Index.path + ) + end +end diff --git a/tasks/breeze/generators/templates/breeze_config.ecr b/tasks/breeze/generators/templates/breeze_config.ecr index b0977a9..9d589e7 100644 --- a/tasks/breeze/generators/templates/breeze_config.ecr +++ b/tasks/breeze/generators/templates/breeze_config.ecr @@ -16,6 +16,7 @@ end # # settings.email_previews = Emails::Previews # end +# Breeze.register BreezeCarbon # Configuration settings for Actions Breeze::ActionHelpers.configure do |settings|