Skip to content

Commit

Permalink
Merge pull request #25 from luckyframework/matthewmcgarvey/extension-…
Browse files Browse the repository at this point in the history
…registration

First pass at extension registration
  • Loading branch information
matthewmcgarvey authored Mar 23, 2021
2 parents 7739d60 + a58acf5 commit 6860b07
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 18 deletions.
6 changes: 6 additions & 0 deletions src/breeze.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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/*"
Expand All @@ -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
19 changes: 5 additions & 14 deletions src/breeze/components/breeze/navbar_links.cr
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions src/breeze/components/breeze_component.cr
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions src/breeze/extension.cr
Original file line number Diff line number Diff line change
@@ -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
14 changes: 10 additions & 4 deletions src/extensions/breeze_carbon.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions tasks/breeze/generators/templates/breeze_config.ecr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ end
#
# settings.email_previews = Emails::Previews
# end
# Breeze.register BreezeCarbon

# Configuration settings for Actions
Breeze::ActionHelpers.configure do |settings|
Expand Down

0 comments on commit 6860b07

Please sign in to comment.