Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Admin: Custom Domains management UI

Damian Legawiec354838062299
diff --git a/admin/app/controllers/spree/admin/custom_domains_controller.rb b/admin/app/controllers/spree/admin/custom_domains_controller.rb
new file mode 100644
index 0000000000..63b98ad5b9
--- /dev/null
+++ b/admin/app/controllers/spree/admin/custom_domains_controller.rb
@@ -0,0 +1,19 @@
+module Spree
+ module Admin
+ class CustomDomainsController < ResourceController
+ protected
+
+ def collection_url
+ spree.admin_custom_domains_path
+ end
+
+ def create_turbo_stream_enabled?
+ helpers.entri_enabled?
+ end
+
+ def location_after_save
+ spree.admin_custom_domains_path
+ end
+ end
+ end
+end
diff --git a/admin/app/helpers/spree/admin/custom_domains_helper.rb b/admin/app/helpers/spree/admin/custom_domains_helper.rb
new file mode 100644
index 0000000000..6b400a82b8
--- /dev/null
+++ b/admin/app/helpers/spree/admin/custom_domains_helper.rb
@@ -0,0 +1,9 @@
+module Spree
+ module Admin
+ module CustomDomainsHelper
+ def entri_enabled?
+ defined?(Entri) && Rails.application.credentials.dig(:entri, :app_id).present? && Rails.application.credentials.dig(:entri, :secret).present?
+ end
+ end
+ end
+end
diff --git a/admin/app/views/spree/admin/custom_domains/_custom_domain.html.erb b/admin/app/views/spree/admin/custom_domains/_custom_domain.html.erb
new file mode 100644
index 0000000000..1b3dabcd15
--- /dev/null
+++ b/admin/app/views/spree/admin/custom_domains/_custom_domain.html.erb
@@ -0,0 +1,13 @@
+<tr id="<%= spree_dom_id custom_domain %>">
+ <td><%= custom_domain.url %></td>
+ <td>
+ <%= active_badge(custom_domain.active?) %>
+ </td>
+
+ <% unless entri_enabled? %>
+ <td><%= active_badge(custom_domain.default?) %></td>
+ <td class="actions">
+ <%= link_to_edit(custom_domain, no_text: true, url: spree.edit_admin_custom_domain_path(custom_domain), data: { turbo: false }) %>
+ </td>
+ <% end %>
+</tr>
diff --git a/admin/app/views/spree/admin/custom_domains/_custom_domains.html.erb b/admin/app/views/spree/admin/custom_domains/_custom_domains.html.erb
new file mode 100644
index 0000000000..8785e78579
--- /dev/null
+++ b/admin/app/views/spree/admin/custom_domains/_custom_domains.html.erb
@@ -0,0 +1,24 @@
+<% if current_store.custom_domains.any? %>
+ <div class="table-responsive rounded mt-3 border">
+ <table class="table">
+ <thead class="text-muted">
+ <tr>
+ <th><%= Spree.t(:name) %></th>
+ <th>Active?</th>
+
+ <% unless entri_enabled? %>
+ <th>Default</th>
+ <th class="actions"></th>
+ <% end %>
+ </tr>
+ </thead>
+ <tbody>
+ <%= render collection: current_store.custom_domains, partial: 'spree/admin/custom_domains/custom_domain', cached: spree_base_cache_scope %>
+ </tbody>
+ </table>
+ </div>
+<% else %>
+ <div class="text-muted p-5 d-flex align-items-center w-100 justify-content-center">
+ You don't have any custom domains set yet.
+ </div>
+<% end %>
diff --git a/admin/app/views/spree/admin/custom_domains/_form.html.erb b/admin/app/views/spree/admin/custom_domains/_form.html.erb
new file mode 100644
index 0000000000..428eeea68b
--- /dev/null
+++ b/admin/app/views/spree/admin/custom_domains/_form.html.erb
@@ -0,0 +1,19 @@
+<div class="card mb-3">
+ <div class="card-body">
+ <div class="form-group">
+ <%= f.label :url, Spree.t(:domain) %>
+ <div class="form-control d-flex align-items-center py-0 focus-shadow-blue focus-border-blue">
+ <span class="text-muted">https://</span>
+ <%= f.text_field :url, class: 'form-control-plaintext pr-0 pl-1', autofocus: true, required: true %>
+ </div>
+ <%= f.error_message_on :url %>
+ </div>
+
+ <div class="custom-control custom-checkbox">
+ <%= f.check_box :default, class: 'custom-control-input' %>
+ <%= f.label :default, class: 'custom-control-label font-weight-semibold' %>
+ <p class="form-text mb-0">We will use this domain in emails to customers.</p>
+ <%= f.error_message_on :default %>
+ </div>
+ </div>
+</div>
diff --git a/admin/app/views/spree/admin/custom_domains/create.turbo_stream.erb b/admin/app/views/spree/admin/custom_domains/create.turbo_stream.erb
new file mode 100644
index 0000000000..a6ef94fe53
--- /dev/null
+++ b/admin/app/views/spree/admin/custom_domains/create.turbo_stream.erb
@@ -0,0 +1,3 @@
+<%= turbo_stream.replace('admin_custom_domains_index') do %>
+ <%= render partial: 'custom_domains' %>
+<% end %>
diff --git a/admin/app/views/spree/admin/custom_domains/edit.html.erb b/admin/app/views/spree/admin/custom_domains/edit.html.erb
new file mode 100644
index 0000000000..22275b01aa
--- /dev/null
+++ b/admin/app/views/spree/admin/custom_domains/edit.html.erb
@@ -0,0 +1,15 @@
+<% content_for :page_title do %>
+ <%= page_header_back_button collection_url %>
+ Edit custom domain
+<% end %>
+
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @object } %>
+
+<%= form_for @object, url: spree.admin_custom_domain_path(@object) do |f| %>
+ <div class="row">
+ <div class="col-lg-6">
+ <%= render partial: 'form', locals: { f: f } %>
+ <%= render partial: 'spree/admin/shared/edit_resource_links', locals: { f: f } %>
+ </div>
+ </div>
+<% end %>
diff --git a/admin/app/views/spree/admin/custom_domains/index.html.erb b/admin/app/views/spree/admin/custom_domains/index.html.erb
new file mode 100644
index 0000000000..6f3c14f469
--- /dev/null
+++ b/admin/app/views/spree/admin/custom_domains/index.html.erb
@@ -0,0 +1,50 @@
+<% content_for :head do %>
+ <script type="text/javascript" src="https://cdn.goentri.com/entri.js"></script>
+<% end %>
+<%= content_for(:page_title) do %>
+ <%= Spree.t(:domains) %>
+<% end %>
+<div class="card-lg">
+ <h5 class="mb-3">Internal URL</h5>
+ <div class="row mb-4">
+ <div class="col-lg-4">
+ <p class="text-muted">
+ This is your internal URL.
+ </p>
+ </div>
+ <div class="col-lg-7 offset-lg-1">
+ <%= form_for current_store, url: spree.admin_store_path(current_store), data: { turbo: false, controller: 'enable-button', 'enable-button-disable-when-not-changed-value': true } do |f| %>
+ <div class="form-control d-flex align-items-center py-0 focus-shadow-blue focus-border-blue mb-3 <% if current_store.custom_domains.any? %>disabled<% end %>">
+ <%= f.text_field :code, class: 'form-control-plaintext pl-0', data: { enable_button_target: 'input' }, required: true, disabled: current_store.custom_domains.any? %>
+ <span>.<%= Spree.root_domain %></span>
+ </div>
+ <% unless current_store.custom_domains.any? %>
+ <%= turbo_save_button_tag Spree.t('actions.update') %>
+ <% end %>
+ <% end %>
+ </div>
+ </div>
+ <hr class="my-5" />
+ <h5>Custom domains</h5>
+ <div class="row">
+ <div class="col-lg-4">
+ <p class="text-muted">
+ If you already own a domain name (like yourname.com), you can set your Vendo store up to live at that domain.
+ If you'd prefer, you can also use a subdomain on your site (for example, shop.yourname.com).
+ </p>
+ </div>
+ <div class="col-lg-7 offset-lg-1">
+ <% if entri_enabled? %>
+ <%= render 'setup_domain_entri' %>
+ <% else %>
+ <div class="text-right">
+ <%= link_to Spree.t(:new_domain), spree.new_admin_custom_domain_path, class: "btn btn-primary" %>
+ </div>
+ <% end %>
+
+ <%= turbo_frame_tag 'admin_custom_domains_index' do %>
+ <%= render 'custom_domains' %>
+ <% end %>
+ </div>
+ </div>
+</div>
diff --git a/admin/app/views/spree/admin/custom_domains/new.html.erb b/admin/app/views/spree/admin/custom_domains/new.html.erb
new file mode 100644
index 0000000000..9313076682
--- /dev/null
+++ b/admin/app/views/spree/admin/custom_domains/new.html.erb
@@ -0,0 +1,15 @@
+<% content_for :page_title do %>
+ <%= page_header_back_button collection_url %>
+ Setup a new custom domain for your store
+<% end %>
+
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @object } %>
+
+<%= form_for @object, url: spree.admin_custom_domains_path do |f| %>
+ <div class="row">
+ <div class="col-lg-6">
+ <%= render partial: 'form', locals: { f: f } %>
+ <%= render partial: 'spree/admin/shared/new_resource_links' %>
+ </div>
+ </div>
+<% end %>
diff --git a/admin/config/routes.rb b/admin/config/routes.rb
index c120ad9ae7..33f4171d92 100644
--- a/admin/config/routes.rb
+++ b/admin/config/routes.rb
@@ -128,6 +128,7 @@ Spree::Core::Engine.add_routes do
put :mark_as_default
end
end
+ resources :custom_domains, except: :show
# account management
resources :roles, except: :show
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index 43b952ed78..9ce0772110 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -256,7 +256,17 @@ module Spree
end
def formatted_url
- @formatted_url ||= Rails.env.development? || Rails.env.test? ? URI::Generic.build(scheme: Rails.application.routes.default_url_options[:protocol] || 'http', host: url.sub(%r{^https?://}, ''), port: Rails.application.routes.default_url_options[:port]).to_s : URI::HTTPS.build(host: url.sub(%r{^https?://}, '')).to_s
+ @formatted_url ||= if Rails.env.development? || Rails.env.test?
+ URI::HTTP.build(
+ scheme: Rails.application.routes.default_url_options[:protocol] || 'http',
+ host: url.to_s.sub(%r{^https?://}, ''),
+ port: Rails.application.routes.default_url_options[:port]
+ ).to_s
+ else
+ URI::HTTPS.build(
+ host: url.to_s.sub(%r{^https?://}, '')
+ ).to_s
+ end
end
def formatted_custom_domain