Skip to content

Commit

Permalink
Delete, rename, and more
Browse files Browse the repository at this point in the history
  • Loading branch information
rosschapman committed Jun 2, 2024
1 parent 02ceafa commit 335fe51
Show file tree
Hide file tree
Showing 28 changed files with 227 additions and 77 deletions.
27 changes: 27 additions & 0 deletions app/components/marketplace/tag/admin_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class Marketplace::Tag::AdminComponent < ApplicationComponent
with_collection_parameter :tag
attr_accessor :tag
delegate :label, to: :tag

def initialize(tag:, data: {}, classes: "")
super(data: data, classes: classes)

self.tag = tag
end

def edit_button?
tag.persisted? && policy(tag).edit?
end

def destroy_button?
tag.persisted? && policy(tag).destroy?
end

def assigned_product_count
# Consider optimizing this query with a counter cache or other method
# if it becomes a performance bottleneck.
@assigned_product_count ||= tag.product_tags.count
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<%= render CardComponent.new(dom_id: dom_id(tag)) do |card| %>
<div class="flex flex-col gap-2">
<h3><%= tag.label %></h3>
<span class="text-xs">Assigned to <%= assigned_product_count %> <%= "Product".pluralize(assigned_product_count) %></span>
</div>
<%- card.with_footer(variant: :action_bar) do %>
<% if edit_button? %>
<%= render ButtonComponent.new(
label: "#{t("edit.link_to")}",
title: t("marketplace.tags.edit.link_to", label: tag.label),
href: tag.location(:edit),
method: :get,
scheme: :secondary
) %>
<%- end %>
<% if destroy_button? %>
<%= render ButtonComponent.new(
label: "#{t("destroy.link_to")}",
title: t("marketplace.tags.destroy.link_to", label: tag.label),
href: tag.location,
turbo_stream: true,
method: :delete,
confirm: t("destroy.confirm"),
scheme: :secondary
) %>
<%- end %>
<%- end %>
<%- end %>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
13 changes: 13 additions & 0 deletions app/components/marketplace/tag/display_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class Marketplace::Tag::DisplayComponent < ApplicationComponent
with_collection_parameter :tag
attr_accessor :tag
delegate :label, to: :tag

def initialize(tag:, data: {}, classes: "")
super(data: data, classes: classes)

self.tag = tag
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span class="text-xs px-2 py-1 rounded-full bg-gray-200/50 text-gray-700"><%= tag.label %></span>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
5 changes: 4 additions & 1 deletion app/furniture/marketplace/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ en:
success: "Notification Method '%{contact_location}' Saved!"
destroy:
success: "Notification Method '%{contact_location}' Removed!"
link_to: "Remove Notification Method '%{contact_location}'"
order:
placed_mailer:
notification:
Expand Down Expand Up @@ -142,6 +143,8 @@ en:
new:
link_to: "Add Product Tag"
edit:
link_to: "Edit Product Tag"
link_to: "Edit Product Tag '%{label}'"
update:
success: "Product Tag saved!"
destroy:
link_to: "Remove Product Tag '%{label}'"
6 changes: 3 additions & 3 deletions app/furniture/marketplace/menu_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<%- marketplace.tags.group_tag.by_position.each do |tag| %>
<%- marketplace.tags.menu_tag.by_position.each do |tag| %>
<%- unless tag.products.empty? %>
<h1><%= tag.label %></h1>
<div class="grid lg:grid-cols-3 gap-3">
<%- tag.products.with_all_rich_text.unarchived.each do |product| %>
<%- tag.products.with_all_rich_text.unarchived.sort_alpha.each do |product| %>
<%= render Marketplace::Menu::ProductComponent.new(product:, cart:) %>
<%- end %>
</div>
<%- end %>
<%- end %>
<h1>Other</h1>
<div class="grid lg:grid-cols-3 gap-3">
<%- marketplace.products.unarchived.without_group_tag.each do |product| %>
<%- marketplace.products.unarchived.without_menu_tag.sort_alpha.each do |product| %>
<%= render Marketplace::Menu::ProductComponent.new(product:, cart:) %>
<%- end %>
</div>
5 changes: 3 additions & 2 deletions app/furniture/marketplace/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ class Product < Record

before_commit :standardize_attachment_name

scope :with_group_tag, -> { joins(:tags).merge(Tag.group_tag) }
scope :without_group_tag, -> { where.not(id: with_group_tag) }
scope :with_menu_tag, -> { joins(:tags).merge(Tag.menu_tag) }
scope :without_menu_tag, -> { where.not(id: with_menu_tag) }
scope :sort_alpha, -> { order(name: :asc) }

def standardize_attachment_name
return unless photo.attached? && photo.blob.persisted?
Expand Down
9 changes: 6 additions & 3 deletions app/furniture/marketplace/product/title_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<h3><%= name %></h3>
<div class="mb-2">
<h3><%= name %></h3>
</div>
<%- if servings.present? %>
<p class="text-sm">Serves <%= servings %></p>
<%- end %>

<%- if product.tags.present? %>
<p>(<%= product.tags.pluck(:label).join(", ") %>)</p>
<div class="flex gap-1">
<%= render Marketplace::Tag::DisplayComponent.with_collection(product.tags.not_menu) %>
</div>
<%- end %>
7 changes: 2 additions & 5 deletions app/furniture/marketplace/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<%- breadcrumb :marketplace_products, marketplace%>

<div class="grid grid-cols-1 md:grid-cols-3 gap-3 sm:gap-5 mt-3 mx-auto">
<%- if params[:archive] %>
<%= render Marketplace::ProductComponent.with_collection(marketplace.products.archived) %>
<%= render Marketplace::ProductComponent.with_collection(marketplace.products.archived.sort_alpha) %>
<%- else %>
<%= render Marketplace::ProductComponent.with_collection(marketplace.products.unarchived) %>
<%= render Marketplace::ProductComponent.with_collection(marketplace.products.unarchived.sort_alpha) %>
<%- end %>
</div>

<div class="text-center mt-3">
<%- new_product = marketplace.products.new %>
<%- if policy(new_product).create? %>
Expand All @@ -19,7 +17,6 @@
scheme: :secondary) %>
<%- end %>
</div>

<div class="text-center mt-3">
<%= render Marketplace::Archivable::IndexLinkComponent.new(marketplace: marketplace, resource: :products, to_archive: !params[:archive]) %>
</div>
4 changes: 2 additions & 2 deletions app/furniture/marketplace/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Tag < Record

# Tacking `_tag` onto the end of this scope name solely to avoid
# collisions with ActiveRecord `groups`
scope :group_tag, -> { where(is_group: true) }
scope :not_group, -> { where(is_group: false) }
scope :menu_tag, -> { where(is_menu: true) }
scope :not_menu, -> { where(is_menu: false) }
scope :by_position, -> { order(position: :asc) }
end
end
2 changes: 1 addition & 1 deletion app/furniture/marketplace/tag_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def space
end

def permitted_attributes(_params = nil)
%i[label is_group position]
%i[label is_menu position]
end

def update?
Expand Down
4 changes: 2 additions & 2 deletions app/furniture/marketplace/tags/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<div class="flex flex-col gap-5">
<div>
<%= render "text_field", attribute: :label, form: form %>
<%= form.label :is_group, "Is this a menu group?" %>
<%= form.check_box :is_group %>
<%= form.label :is_menu, "Make this a menu" %>
<%= form.check_box :is_menu %>
</div>
<%= form.submit %>
</div>
Expand Down
40 changes: 19 additions & 21 deletions app/furniture/marketplace/tags/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<%- breadcrumb :marketplace_tags, marketplace %>
<%= render CardComponent.new do |card| %>
<section class="mt-3">
<div class="flex flex-col gap-4">
<div>
<h1>Menu Groups</h1>
<%- if marketplace.tags.group_tag.empty? %>
There are currently no tags marked as Menu Groups
<h1>Menus</h1>
<%- if marketplace.tags.menu_tag.empty? %>
There are currently no tags marked as Menus
<% else %>
<p>Drag and drop to change the order in which groups will display on your Marketplace</p>
<ul data-tag-list-test data-controller="sortable" data-sortable-animation-value="150" data-sortable-resource-name-value="tag" class="flex flex-col gap-2 p-0">
<%- marketplace.tags.group_tag.by_position.each do |tag| %>
<li data-sortable-update-url="<%= polymorphic_path(tag.location) %>" id="<%= dom_id(tag)%>" class="items-center rounded-md bg-gray-50 px-2 py-1 font-medium text-gray-600 ring-1 ring-inset ring-gray-500/10 list-none cursor-move">
<%= render SvgComponent.new(icon: "bars_3", classes: "w-6 h-6 inline-block") %>
<%= link_to tag.label, tag.location(:edit) %>
<%- marketplace.tags.menu_tag.by_position.each do |tag| %>
<li data-sortable-update-url="<%= polymorphic_path(tag.location) %>" id="<%= dom_id(tag)%>" class="list-none cursor-move">
<%= render Marketplace::Tag::AdminComponent.new(tag: tag) %>
</li>
<%- end %>
<%- end%>
Expand All @@ -20,21 +19,20 @@
<div>
<h1>Tags</h1>
<p>Adding tags to your products like "vegan" or "discounted" will help shoppers more easily find what they want</p>
<div class="flex space-x-2">
<%- marketplace.tags.not_group.each do |tag| %>
<span id="<%= dom_id(tag)%>" class="rounded-md bg-gray-50 px-2 py-1 font-medium text-gray-600 ring-1 ring-inset ring-gray-500/10">
<%= link_to tag.label, tag.location(:edit) %>
</span>
<%- end %>
<div class="grid grid-cols-1 gap-3 sm:gap-5 sm:grid-cols-3">
<%= render Marketplace::Tag::AdminComponent.with_collection(marketplace.tags.not_menu) %>
</div>
</div>
<div>
<%- card.with_footer(variant: :action_bar) do %>
<%- new_tag = marketplace.tags.new %>
<%- if policy(new_tag).create? %>
<%= link_to t("marketplace.tags.new.link_to"), marketplace.location(:new, child: :tag), class: "button w-full" %>
<%- end %>
<div class="text-center mt-3">
<%- new_tag = marketplace.tags.new %>
<%- if policy(new_tag).create? %>
<%= render ButtonComponent.new(
label: "#{t('marketplace.tags.new.link_to')}",
title: t('marketplace.tags.new.link_to'),
href: marketplace.location(:new, child: :tag),
method: :get,
scheme: :secondary) %>
<%- end %>
</div>
</div>
<%- end %>
</div>
14 changes: 14 additions & 0 deletions app/furniture/marketplace/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ def index
skip_authorization
end

def destroy
authorize(mtag).destroy

respond_to do |format|
format.turbo_stream do
if mtag.destroyed?
render turbo_stream: turbo_stream.remove(mtag)
else
render turbo_stream: turbo_stream.replace(mtag)
end
end
end
end

def mtag_params
policy(Tag).permit(params.require(:tag))
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class AddIsGroupToMarketplaceTag < ActiveRecord::Migration[7.1]
def change
add_column :marketplace_tags, :is_group, :boolean, default: false, null: false
add_column :marketplace_tags, :is_menu, :boolean, default: false, null: false
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameMarketplaceTagIsGroupToIsMenu < ActiveRecord::Migration[7.1]
def change
safety_assured { rename_column :marketplace_tags, :is_group, :is_menu }
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_05_24_021224) do
ActiveRecord::Schema[7.1].define(version: 2024_05_31_200256) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down Expand Up @@ -244,7 +244,7 @@
t.string "label"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "is_group", default: false, null: false
t.boolean "is_menu", default: false, null: false
t.integer "position", default: 0, null: false
t.uuid "marketplace_id", null: false
t.index ["marketplace_id", "position"], name: "index_marketplace_tags_on_marketplace_id_and_position", unique: true
Expand Down
2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
hero_image: FactoryBot.create(:media)
)
marketplace = FactoryBot.create(:marketplace, :ready_for_shopping, product_quantity: 16, room: marketplace_section)
magic_menu_group = FactoryBot.create(:marketplace_tag, :group, marketplace: marketplace, label: "Magic")
magic_menu_group = FactoryBot.create(:marketplace_tag, :menu, marketplace: marketplace, label: "Magic")
fire_tag = FactoryBot.create(:marketplace_tag, marketplace: marketplace, label: "🔥")
marketplace.products.sample(8).each do |product|
product.tags << magic_menu_group
Expand Down
15 changes: 15 additions & 0 deletions spec/components/marketplace/tag/admin_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Marketplace::Tag::AdminComponent, type: :component do
pending "add some examples to (or delete) #{__FILE__}"

# it "renders something useful" do
# expect(
# render_inline(described_class.new(attr: "value")) { "Hello, components!" }.css("p").to_html
# ).to include(
# "Hello, components!"
# )
# end
end
15 changes: 15 additions & 0 deletions spec/components/marketplace/tag/display_component_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe Marketplace::Tag::DisplayComponent, type: :component do
pending "add some examples to (or delete) #{__FILE__}"

# it "renders something useful" do
# expect(
# render_inline(described_class.new(attr: "value")) { "Hello, components!" }.css("p").to_html
# ).to include(
# "Hello, components!"
# )
# end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class Marketplace::Tag::AdminComponentPreview < ViewComponent::Preview
def default
render(Marketplace::Tag::AdminComponent.new)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class Marketplace::Tag::DisplayComponentPreview < ViewComponent::Preview
def default
render(Marketplace::Tag::DisplayComponent.new)
end
end
4 changes: 2 additions & 2 deletions spec/factories/furniture/marketplace/marketplace_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
sequence(:label) { |n| "#{Faker::Food.allergen} #{n}" }
sequence(:position) { |n| n }

trait :group do
is_group { true }
trait :menu do
is_menu { true }
end
end
end
Loading

0 comments on commit 335fe51

Please sign in to comment.