-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5099 from solidusio/waiting-for-dev/extending_com…
…ponents Use and extend view components in Solidus Admin
- Loading branch information
Showing
18 changed files
with
260 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require "solidus_admin/system/import" | ||
|
||
module SolidusAdmin | ||
# BaseComponent is the base class for all components in Solidus Admin. | ||
class BaseComponent < ViewComponent::Base | ||
include ViewComponent::InlineTemplate | ||
include SolidusAdmin::ContainerHelper | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusAdmin | ||
# Renders the main navigation of Solidus Admin. | ||
class MainNavComponent < BaseComponent | ||
include Import[ | ||
"main_nav_item_component", | ||
items: "main_nav_items" | ||
] | ||
|
||
erb_template <<~ERB | ||
<nav> | ||
<%= | ||
render main_nav_item_component.with_collection( | ||
sorted_items | ||
) | ||
%> | ||
</nav> | ||
ERB | ||
|
||
private | ||
|
||
def sorted_items | ||
items.sort_by(&:position) | ||
end | ||
end | ||
end |
21 changes: 21 additions & 0 deletions
21
admin/app/components/solidus_admin/main_nav_item_component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusAdmin | ||
# Menu item within a {MainNavComponent} | ||
class MainNavItemComponent < BaseComponent | ||
with_collection_parameter :item | ||
|
||
attr_reader :item | ||
|
||
def initialize(item:) | ||
@item = item | ||
super | ||
end | ||
|
||
erb_template <<~ERB | ||
<a href="#"> | ||
<%= item.title %> | ||
</a> | ||
ERB | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusAdmin | ||
# rubocop:disable Rails/ApplicationController | ||
class OrdersController < ActionController::Base | ||
layout 'solidus_admin/application' | ||
end | ||
# rubocop:enable Rails/ApplicationController | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require "solidus_admin/container" | ||
|
||
module SolidusAdmin | ||
module ContainerHelper | ||
def container | ||
SolidusAdmin::Container | ||
end | ||
|
||
def component(name) | ||
container.resolve("#{name}_component") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require "dry/system" | ||
require "dry/system/container" | ||
require "dry/system/component" | ||
require "view_component" | ||
require "solidus_admin/system/loaders/host_overridable_constant" | ||
|
||
module SolidusAdmin | ||
# Global registry for host-injectable components. | ||
# | ||
# We use this container to register all the components that can be | ||
# overridden by the host application. | ||
# | ||
# @api private | ||
class Container < Dry::System::Container | ||
configure do |config| | ||
config.root = Pathname(__FILE__).dirname.join("../..").realpath | ||
config.component_dirs.add("app/components") do |dir| | ||
dir.loader = System::Loaders::HostOverridableConstant.method(:call).curry["components"] | ||
dir.namespaces.add "solidus_admin", key: nil | ||
end | ||
end | ||
|
||
# Returns all the registered components for a given namespace. | ||
# | ||
# @api private | ||
def self.within_namespace(namespace) | ||
keys.filter_map do | ||
_1.start_with?("#{namespace}#{config.namespace_separator}") && resolve(_1) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusAdmin | ||
# Encapsulates the data for a main nav item. | ||
class MainNavItem | ||
attr_reader :title, :position | ||
|
||
# @param title [String] | ||
# @param position [Integer] | ||
def initialize(title:, position:) | ||
@title = title | ||
@position = position | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require "solidus_admin/container" | ||
|
||
module SolidusAdmin | ||
# Auto-imports container dependencies. | ||
# | ||
# @example | ||
# class Foo | ||
# # Foo.new will have a `#bar` instance method that returns the | ||
# # result of `Container["bar"]`. | ||
# include Import["bar"] | ||
# end | ||
Import = Container.injector | ||
end |
61 changes: 61 additions & 0 deletions
61
admin/lib/solidus_admin/system/loaders/host_overridable_constant.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusAdmin | ||
module System | ||
module Loaders | ||
# Loader that resolves constants that can be overriden by the host. | ||
# | ||
# For instance, when the loader is configured like this: | ||
# | ||
# ```ruby | ||
# config.component_dirs.add "app/components" do |dir| | ||
# dir.loader = SolidusAdmin::System::Loaders::HostOverridableConstant.method(:call).curry["components"] | ||
# end | ||
# ``` | ||
# | ||
# When `Container["foo"]` is given and the loader is used: | ||
# | ||
# - It will return a `MyApp::SolidusAdmin::Foo` constant if `app/components/my_app/solidus_admin/foo.rb` exists. | ||
# - Otherwise, it will return the resolved constant from the engine. | ||
# | ||
# @api private | ||
class HostOverridableConstant < Dry::System::Loader | ||
# @param [String] namespace | ||
def self.call(namespace, component, *_args) | ||
return override_constant(component) if override_path(namespace, component).exist? | ||
|
||
require!(component) | ||
constant(component) | ||
end | ||
|
||
class << self | ||
private | ||
|
||
def application | ||
Rails.application | ||
end | ||
|
||
def application_name | ||
Rails.application.class.module_parent.name | ||
end | ||
|
||
def override_path(namespace, component) | ||
application | ||
.root | ||
.join( | ||
"app", | ||
namespace, | ||
application_name.underscore, | ||
"solidus_admin", | ||
"#{component.identifier.key}.rb" | ||
) | ||
end | ||
|
||
def override_constant(component) | ||
"#{application_name}::SolidusAdmin::#{component.identifier.key.camelize}".constantize | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require "solidus_admin/container" | ||
require "solidus_admin/main_nav_item" | ||
|
||
module SolidusAdmin | ||
module Providers | ||
Container.register_provider("main_nav") do | ||
start do | ||
container.namespace("main_nav") do | ||
register("first_item", MainNavItem.new(title: "First item", position: "10")) | ||
register("second_item", MainNavItem.new(title: "Second item", position: "20")) | ||
register("third_item", MainNavItem.new(title: "Third item", position: "30")) | ||
end | ||
|
||
container.register("main_nav_items") do | ||
Container.within_namespace("main_nav") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters