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.

Open source Post and Post Category admin views (#12368)

Łukasz Adamczykc023fbfcbb97
diff --git a/admin/app/helpers/spree/admin/posts_helper.rb b/admin/app/helpers/spree/admin/posts_helper.rb
new file mode 100644
index 0000000000..6113753b22
--- /dev/null
+++ b/admin/app/helpers/spree/admin/posts_helper.rb
@@ -0,0 +1,9 @@
+module Spree
+ module Admin
+ module PostsHelper
+ def post_authors_select_options
+ Spree.admin_user_class.spree_admin.map { |user| [user.name, user.id] }
+ end
+ end
+ end
+end
diff --git a/admin/app/helpers/spree/admin/tags_helper.rb b/admin/app/helpers/spree/admin/tags_helper.rb
index 990c50b2ae..2fafec71a6 100644
--- a/admin/app/helpers/spree/admin/tags_helper.rb
+++ b/admin/app/helpers/spree/admin/tags_helper.rb
@@ -8,6 +8,17 @@ module Spree
def tags_json_array
@tags_json_array ||= tags_scope.pluck(:id, :name).map { |id, name| { id: id, name: name } }.as_json
end
+
+ def post_tags_scope
+ @post_tags_scope ||= ActsAsTaggableOn::Tag.
+ joins(:taggings).
+ where('taggings.taggable_type = ?', 'Spree::Post').
+ for_context(:tags)
+ end
+
+ def post_tags_json_array
+ @post_tags_json_array ||= post_tags_scope.pluck(:id, :name).map { |id, name| { id: id, name: name } }.as_json
+ end
end
end
end
diff --git a/admin/app/views/spree/admin/post_categories/_form.html.erb b/admin/app/views/spree/admin/post_categories/_form.html.erb
new file mode 100644
index 0000000000..d2eae80e99
--- /dev/null
+++ b/admin/app/views/spree/admin/post_categories/_form.html.erb
@@ -0,0 +1,19 @@
+<div class="row">
+ <div class="col-lg-12">
+ <div class="card mb-4">
+ <div class="card-body">
+ <div class="form-group">
+ <%= f.label :title, Spree.t(:title) %>
+ <%= f.text_field :title, class: "form-control", required: true %>
+ </div>
+
+ <div class="form-group">
+ <%= f.label :description, Spree.t(:description) %>
+ <div class="trix-container">
+ <%= f.rich_text_area :description %>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+</div>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/post_categories/_post_category.html.erb b/admin/app/views/spree/admin/post_categories/_post_category.html.erb
new file mode 100644
index 0000000000..a58851a3ca
--- /dev/null
+++ b/admin/app/views/spree/admin/post_categories/_post_category.html.erb
@@ -0,0 +1,6 @@
+<tr data-controller="row-link" id="<%= dom_id(post_category) %>">
+ <td><%= link_to post_category.title, spree.edit_admin_post_category_path(post_category), class: 'd-flex', data: { row_link_target: :link } %></td>
+ <td class="actions cursor-pointer" data-action="click->row-link#openLink">
+ <%= link_to_edit(post_category, class: 'btn btn-light btn-sm') if can? :edit, post_category %>
+ </td>
+</tr>
diff --git a/admin/app/views/spree/admin/post_categories/edit.html.erb b/admin/app/views/spree/admin/post_categories/edit.html.erb
new file mode 100644
index 0000000000..256d47ddb7
--- /dev/null
+++ b/admin/app/views/spree/admin/post_categories/edit.html.erb
@@ -0,0 +1,15 @@
+<% content_for :page_title do %>
+ <%= page_header_back_button spree.admin_post_categories_path, @post_category %>
+ <%= @post_category.title %>
+<% end %>
+
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @post_category } %>
+
+<%= form_for [:admin, @post_category] do |f| %>
+ <div class="row">
+ <div class="col-lg-6">
+ <%= render 'form', f: f %>
+ <%= render 'spree/admin/shared/edit_resource_links', f: f %>
+ </div>
+ </div>
+<% end %>
diff --git a/admin/app/views/spree/admin/post_categories/index.html.erb b/admin/app/views/spree/admin/post_categories/index.html.erb
new file mode 100644
index 0000000000..f07449db76
--- /dev/null
+++ b/admin/app/views/spree/admin/post_categories/index.html.erb
@@ -0,0 +1,27 @@
+<% content_for :page_actions do %>
+ <%= link_to_with_icon 'plus', Spree.t(:new_post_category), new_object_url, class: 'btn btn-primary' if can?(:create, Spree::PostCategory) %>
+<% end %>
+
+<%= render 'spree/admin/shared/posts_tabs' %>
+
+<% if @post_categories.any? %>
+ <div class="card-lg p-0">
+ <div class="table-responsive rounded-lg">
+ <table class="table">
+ <thead class="text-muted">
+ <tr>
+ <th class="w-90"><%= Spree.t(:title) %></th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody>
+ <%= render partial: 'spree/admin/post_categories/post_category', collection: @post_categories, cached: spree_base_cache_scope %>
+ </tbody>
+ </table>
+ </div>
+ </div>
+<% else %>
+ <div class="text-muted p-5 d-flex align-items-center w-100 justify-content-center">
+ <%= Spree.t(:no_resource_found, resource: Spree.t(:post_categories)) %>
+ </div>
+<% end %>
diff --git a/admin/app/views/spree/admin/post_categories/new.html.erb b/admin/app/views/spree/admin/post_categories/new.html.erb
new file mode 100644
index 0000000000..78a5bb6f9f
--- /dev/null
+++ b/admin/app/views/spree/admin/post_categories/new.html.erb
@@ -0,0 +1,15 @@
+<% content_for :page_title do %>
+ <%= page_header_back_button spree.admin_post_categories_path, @post_category %>
+ <%= Spree.t(:new_post_category) %>
+<% end %>
+
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @post_category } %>
+
+<%= form_for [:admin, @post_category] do |f| %>
+ <div class="row">
+ <div class="col-lg-6">
+ <%= render 'form', f: f %>
+ <%= render 'spree/admin/shared/new_resource_links' %>
+ </div>
+ </div>
+<% end %>
diff --git a/admin/app/views/spree/admin/posts/_extra_actions.html.erb b/admin/app/views/spree/admin/posts/_extra_actions.html.erb
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/admin/app/views/spree/admin/posts/_filters.html.erb b/admin/app/views/spree/admin/posts/_filters.html.erb
new file mode 100644
index 0000000000..65359a1dae
--- /dev/null
+++ b/admin/app/views/spree/admin/posts/_filters.html.erb
@@ -0,0 +1,40 @@
+<%= search_form_for [:admin, @search], class: "filter-wrap border-bottom", data: {controller: "filters reveal", reveal_hidden_class: "d-none"} do |f| %>
+ <div class="d-flex flex-column flex-lg-row gap-2">
+ <%= render 'spree/admin/shared/filters_search_bar', param: :search_by_title, label: Spree.t(:title) %>
+
+ <%= render 'spree/admin/shared/filters_button' %>
+ </div>
+ <div data-reveal-target="item" class="d-none mt-4" id="table-filter">
+ <div class="row">
+ <div class="col-lg-6">
+ <div class="form-group">
+ <%= f.label :author_id_eq, Spree.t(:author) %>
+ <%= f.select :author_id_eq,
+ options_for_select(post_authors_select_options, params.dig(:q, :author_id_eq)),
+ { include_blank: true },
+ { class: "custom-select", data: { filters_target: :input } } %>
+ </div>
+ </div>
+
+ <div class="col-lg-6">
+ <div class="form-group">
+ <%= f.label :post_category_id_eq, Spree.t(:category) %>
+ <%= f.select :post_category_id_eq,
+ options_for_select(Spree::PostCategory.all.pluck(:title, :id), params.dig(:q, :post_category_id_eq)),
+ { include_blank: true },
+ { class: "custom-select", data: { filters_target: :input } } %>
+ </div>
+ </div>
+ </div>
+ <div class="form-actions">
+ <%= turbo_save_button_tag Spree.t(:filter_results) do %>
+ <%= icon("search") %>
+ <%= Spree.t(:filter_results) %>
+ <% end %>
+ </div>
+ </div>
+
+ <%= render "spree/admin/shared/filter_badge_template" %>
+
+ <div data-filters-target="badgesContainer" class="filter-badges-container"></div>
+<% end %>
diff --git a/admin/app/views/spree/admin/posts/_form.html.erb b/admin/app/views/spree/admin/posts/_form.html.erb
new file mode 100644
index 0000000000..13c8fedb52
--- /dev/null
+++ b/admin/app/views/spree/admin/posts/_form.html.erb
@@ -0,0 +1,80 @@
+<div class="row" data-controller="slug-form seo-form">
+ <div class="col-lg-8">
+ <div class="card mb-4">
+ <div class="card-body">
+ <div class="form-group">
+ <%= f.label :title, Spree.t(:title) %>
+ <%= f.text_field :title,
+ class: "form-control",
+ required: true,
+ data: {
+ seo_form_target: 'sourceTitleInput',
+ slug_form_target: 'name',
+ action: 'slug-form#updateUrlFromName'
+ } %>
+ </div>
+
+ <div class="form-group">
+ <%= f.label :content, Spree.t(:content) %>
+ <div class="trix-container mb-0">
+ <%= f.rich_text_area :content, class: 'overflow-auto', style: 'height: 300px', data: { seo_form_target: 'sourceDescriptionInput' } %>
+ </div>
+ </div>
+
+ <div class="form-group mb-0">
+ <%= f.label :excerpt, Spree.t(:excerpt) %>
+ <%= help_bubble('Add a summary of the post') %>
+
+ <div class="trix-container mb-0">
+ <%= f.rich_text_area :excerpt, class: 'overflow-auto', style: 'height: 100px', data: { seo_form_target: 'sourceExcerptInput' } %>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+
+ <div class="col-lg-4">
+ <div class="card mb-4">
+ <div class="card-body">
+ <div class="form-group mb-0">
+ <%= f.label :image, Spree.t(:featured_image) %>
+ <%= render 'active_storage/upload_form', form: f, field_name: :image, width: 1200, height: 600, crop: true, css: 'flex-col' %>
+ </div>
+ </div>
+ </div>
+
+ <div class="card mb-4">
+ <div class="card-body">
+ <div class="form-group">
+ <%= f.label :author_id, Spree.t(:author) %>
+ <%= f.select :author_id, options_for_select(post_authors_select_options, @post.author_id || try_spree_current_user.id), {}, { class: 'custom-select' } %>
+ </div>
+
+ <div class="form-group">
+ <%= f.label :post_category_id, Spree.t(:category) %>
+ <%= f.select :post_category_id, options_for_select(current_store.post_categories.pluck(:title, :id), @post.post_category_id), { include_blank: true }, { class: 'custom-select' } %>
+ </div>
+
+ <div class="form-group">
+ <%= f.label :published_at, Spree.t(:published_at) %>
+ <%= help_bubble('Marks when the post will be published, leave it blank to hide it') %>
+ <%= f.datetime_field :published_at, class: 'form-control' %>
+ </div>
+
+ <div class="form-group mb-0">
+ <%= f.label :tag_list, Spree.t(:tags) %>
+ <%= tom_select_tag 'post[tag_list]', multiple: true, class: 'w-100', options: post_tags_json_array, active_option: @post.tag_list, value_field: :name, create: true %>
+ </div>
+ </div>
+ </div>
+
+ <%= render 'spree/admin/shared/seo',
+ f: f,
+ title: @post.title,
+ meta_title: @post.meta_title,
+ description: @post.content,
+ slug: @post.slug,
+ slug_path: 'posts',
+ placeholder: 'Add a title and content to see how this post might appear in a search engine listing' %>
+ </div>
+</div>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/posts/_post.html.erb b/admin/app/views/spree/admin/posts/_post.html.erb
new file mode 100644
index 0000000000..55a6f2adca
--- /dev/null
+++ b/admin/app/views/spree/admin/posts/_post.html.erb
@@ -0,0 +1,32 @@
+<tr data-controller="row-link" id="<%= dom_id(post) %>">
+ <td>
+ <%= link_to spree.edit_admin_post_path(post), class: 'text-decoration-none d-flex align-items-center font-weight-bold text-dark', data: { row_link_target: :link, 'turbo-frame': '_top' } do %>
+ <% if post.image.attached? && post.image.variable? %>
+ <div class="admin-product-image-container mr-3">
+ <%= image_tag(
+ main_app.cdn_image_url(
+ post.image.variant(
+ spree_image_variant_options(resize_to_fill: [200, 130])
+ )
+ ),
+ class: 'img-fluid',
+ loading: :lazy,
+ width: 100,
+ height: 65
+ ) %>
+ </div>
+ <% else %>
+ <%= render 'spree/admin/shared/no_image', width: 100, height: 65 %>
+ <% end %>
+ <%= post.title %>
+ <% end %>
+ </td>
+
+ <td class="cursor-pointer" data-action="click->row-link#openLink"><%= post.post_category_title || '-' %></td>
+ <td class="cursor-pointer" data-action="click->row-link#openLink"><%= post.author_name %></td>
+ <td class="cursor-pointer" data-action="click->row-link#openLink"><%= post.published_at.present? ? local_date(post.published_at) : '-' %></td>
+
+ <td class="actions">
+ <%= link_to_edit(post, class: 'btn btn-light btn-sm', data: { 'turbo-frame': '_top' }) if can? :edit, post %>
+ </td>
+</tr>
diff --git a/admin/app/views/spree/admin/posts/edit.html.erb b/admin/app/views/spree/admin/posts/edit.html.erb
new file mode 100644
index 0000000000..55cc0c7be3
--- /dev/null
+++ b/admin/app/views/spree/admin/posts/edit.html.erb
@@ -0,0 +1,15 @@
+<% content_for :page_title do %>
+ <%= page_header_back_button spree.admin_posts_path, @post %>
+ <%= @post.title %>
+<% end %>
+
+<% content_for :page_actions_dropdown do %>
+ <%= external_page_preview_link(@post) %>
+<% end %>
+
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @post } %>
+
+<%= form_for [:admin, @post] do |f| %>
+ <%= render 'form', f: f %>
+ <%= render 'spree/admin/shared/edit_resource_links', f: f %>
+<% end %>
diff --git a/admin/app/views/spree/admin/posts/index.html.erb b/admin/app/views/spree/admin/posts/index.html.erb
new file mode 100644
index 0000000000..b643830a56
--- /dev/null
+++ b/admin/app/views/spree/admin/posts/index.html.erb
@@ -0,0 +1,36 @@
+<% content_for :page_actions do %>
+ <%= render 'extra_actions' %>
+
+ <%= link_to_with_icon 'plus', Spree.t(:new_post), new_object_url, class: 'btn btn-primary ml-2' if can?(:create, Spree::Post) %>
+<% end %>
+
+<%= render 'spree/admin/shared/posts_tabs' %>
+
+<div class="card-lg p-0">
+ <%= render "spree/admin/posts/filters" %>
+
+ <% if @posts.any? %>
+ <div class="table-responsive rounded-lg">
+ <table class="table">
+ <thead class="text-muted">
+ <tr>
+ <th><%= Spree.t(:title) %></th>
+ <th><%= Spree.t(:category) %></th>
+ <th><%= Spree.t(:author) %></th>
+ <th><%= Spree.t(:published_at) %></th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody>
+ <%= render partial: "spree/admin/posts/post", collection: @posts, cached: spree_base_cache_scope %>
+ </tbody>
+ </table>
+ </div>
+
+ <%= render partial: "spree/admin/shared/index_table_options", locals: { collection: @posts } %>
+ <% else %>
+ <div class="text-muted p-5 d-flex align-items-center w-100 justify-content-center">
+ <%= Spree.t(:no_resource_found, resource: Spree.t(:posts)) %>
+ </div>
+ <% end %>
+</div>
diff --git a/admin/app/views/spree/admin/posts/new.html.erb b/admin/app/views/spree/admin/posts/new.html.erb
new file mode 100644
index 0000000000..013d734312
--- /dev/null
+++ b/admin/app/views/spree/admin/posts/new.html.erb
@@ -0,0 +1,11 @@
+<% content_for :page_title do %>
+ <%= page_header_back_button spree.admin_posts_path, @post %>
+ <%= Spree.t(:new_post) %>
+<% end %>
+
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @post } %>
+
+<%= form_for [:admin, @post] do |f| %>
+ <%= render 'form', f: f %>
+ <%= render 'spree/admin/shared/new_resource_links' %>
+<% end %>
diff --git a/admin/app/views/spree/admin/shared/_posts_tabs.html.erb b/admin/app/views/spree/admin/shared/_posts_tabs.html.erb
new file mode 100644
index 0000000000..c762cc855e
--- /dev/null
+++ b/admin/app/views/spree/admin/shared/_posts_tabs.html.erb
@@ -0,0 +1,8 @@
+<% content_for :page_title do %>
+ <%= Spree.t(:posts) %>
+<% end %>
+
+<% content_for(:page_tabs) do %>
+ <%= nav_pill_list_item(:posts) %>
+ <%= nav_pill_list_item(:post_categories, label: Spree.t(:categories)) %>
+<% end %>
\ No newline at end of file
diff --git a/admin/spec/controllers/spree/admin/post_categories_controller_spec.rb b/admin/spec/controllers/spree/admin/post_categories_controller_spec.rb
new file mode 100644
index 0000000000..81938a8d85
--- /dev/null
+++ b/admin/spec/controllers/spree/admin/post_categories_controller_spec.rb
@@ -0,0 +1,30 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::PostCategoriesController, type: :controller do
+ stub_authorization!
+ render_views
+
+ let(:store) { Spree::Store.default }
+
+ describe 'GET #index' do
+ subject(:index) { get :index }
+
+ it 'renders the list of post categories' do
+ index
+
+ expect(response).to render_template(:index)
+ expect(assigns[:collection]).to contain_exactly(*store.post_categories)
+ end
+ end
+
+ describe 'GET #edit' do
+ subject(:edit) { get :edit, params: { id: post_category.id } }
+
+ let(:post_category) { create(:post_category) }
+
+ it 'renders the edit page' do
+ edit
+ expect(response).to render_template(:edit)
+ end
+ end
+end
diff --git a/admin/spec/controllers/spree/admin/posts_controller_spec.rb b/admin/spec/controllers/spree/admin/posts_controller_spec.rb
new file mode 100644
index 0000000000..19451881a4
--- /dev/null
+++ b/admin/spec/controllers/spree/admin/posts_controller_spec.rb
@@ -0,0 +1,58 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Admin::PostsController, type: :controller do
+ stub_authorization!
+ render_views
+
+ let(:store) { Spree::Store.default }
+
+ describe 'GET #index' do
+ subject(:index) { get :index }
+
+ let!(:posts) { create_list(:post, 3) }
+ let!(:other_posts) { create_list(:post, 2, store: create(:store)) }
+
+ it 'renders the list of posts' do
+ index
+
+ expect(response).to render_template(:index)
+ expect(assigns[:collection]).to contain_exactly(*store.posts)
+ end
+ end
+
+ describe 'GET #select_options' do
+ subject(:select_options) { get :select_options }
+
+ let!(:published_posts) { create_list(:post, 2, published_at: 1.day.ago) }
+ let!(:draft_posts) { create_list(:post, 2, published_at: nil) }
+
+ it 'lists published posts for select options' do
+ select_options
+
+ expect(JSON.parse(response.body)).to contain_exactly(
+ { 'id' => published_posts[0].id, 'name' => published_posts[0].title },
+ { 'id' => published_posts[1].id, 'name' => published_posts[1].title }
+ )
+ end
+ end
+
+ describe 'GET #new' do
+ subject(:new) { get :new }
+
+ it 'renders the new post page' do
+ new
+ expect(response).to render_template(:new)
+ end
+ end
+
+ describe 'GET #edit' do
+ subject(:edit) { get :edit, params: { id: post.id } }
+
+ let(:post) { create(:post) }
+
+ it 'renders the edit page' do
+ edit
+ expect(response).to render_template(:edit)
+ end
+ end
+end
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 357e1f9cdd..664518eb15 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -1349,6 +1349,7 @@ en:
please_enter_reasonable_quantity: Please enter a reasonable quantity.
populate_get_error: Something went wrong. Please try adding the item again.
posts: Posts
+ post_categories: Post categories
powered_by: Powered by
pre_tax_refund_amount: Pre-Tax Refund Amount
pre_tax_amount: Pre-Tax Amount
@@ -1813,6 +1814,7 @@ en:
taxonomy_tree_error: The requested change has not been accepted and the tree has been returned to its previous state, please try again.
taxonomy_tree_instruction: ! '* Right click (or double tap on iOS device) a child in the tree to access the menu for adding, deleting or sorting a child.'
taxons: Taxons
+ title: Title
true_label: "TRUE"
automatic_taxon_names:
on_sale: On sale