-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- #2189 Lil' quick sketch through the workflow. Only interesting things of note here are I am using `scenario` syntax (which feels better to me, and was validated by @ExMember in antoher context yesterday.) I am also doing something "weird" where I overload the `visit` method to rely on our `Record#location` method to interject a `polymorphic_path` call; saving us the burden of including the `polymorphic_path` call on our own every time we use `visit`; but adding the burden of a layer of indirection. I also did this with `within`, because omg that awkward `"##{dom_id(model)"` drives me nuts every time I type it. It makes me want to scream. I can't stand it. * ✨ `Marketplace`: Adds `Product#tags` Products can now be tagged with things like `Gluten Free` or `Vegan`, and will show up as such under Product listings * ✨ `Tag` labels are case insensitive
- Loading branch information
Showing
22 changed files
with
235 additions
and
2 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
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 |
---|---|---|
|
@@ -136,3 +136,8 @@ en: | |
payment_settings: | ||
index: | ||
link_to: "Payment Settings" | ||
tags: | ||
index: | ||
link_to: "Tags" | ||
new: | ||
link_to: "Add Tag" |
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
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,8 @@ | ||
class Marketplace | ||
class ProductTag < Record | ||
self.table_name = :marketplace_product_tags | ||
|
||
belongs_to :product, inverse_of: :product_tags | ||
belongs_to :tag, inverse_of: :product_tags | ||
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,14 @@ | ||
class Marketplace | ||
class Tag < Record | ||
self.table_name = "marketplace_tags" | ||
|
||
belongs_to :bazaar, inverse_of: :tags | ||
has_many :product_tags, inverse_of: :tag, dependent: :destroy | ||
has_many :products, through: :product_tags, inverse_of: :tags | ||
|
||
validates :label, uniqueness: {case_sensitive: false, scope: :bazaar_id} | ||
|
||
attr_accessor :marketplace | ||
location(parent: :marketplace) | ||
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,32 @@ | ||
# frozen_string_literal: true | ||
|
||
class Marketplace | ||
class TagPolicy < Policy | ||
alias_method :tag, :object | ||
def space | ||
tag.bazaar | ||
end | ||
|
||
def permitted_attributes(_params = nil) | ||
%i[label] | ||
end | ||
|
||
def update? | ||
return false unless current_person.authenticated? | ||
|
||
super | ||
end | ||
|
||
alias_method :create?, :update? | ||
|
||
def show? | ||
true | ||
end | ||
|
||
class Scope < ApplicationScope | ||
def resolve | ||
scope.all | ||
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,6 @@ | ||
<%= form_with(model: tag.location) do |form| %> | ||
<%= render "text_field", attribute: :label, form: form %> | ||
<%= form.submit %> | ||
<%- 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,20 @@ | ||
<%- breadcrumb :marketplace_tags, marketplace %> | ||
<%= render CardComponent.new do |card| %> | ||
<%- marketplace.tags.each do |tag| %> | ||
<%- tag.marketplace = marketplace %> | ||
<div id="<%= dom_id(tag)%>" class="flex flex-row gap-3"> | ||
<span class="flex-grow"> | ||
<%= tag.label %> | ||
</span> | ||
</div> | ||
<%- end %> | ||
<%- card.with_footer(variant: :action_bar) do %> | ||
<%- new_tag = bazaar.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 %> | ||
<%- 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,2 @@ | ||
<%- breadcrumb :new_marketplace_tag, mtag %> | ||
<%= render "form", tag: mtag %> |
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,31 @@ | ||
# frozen_string_literal: true | ||
|
||
class Marketplace | ||
class TagsController < Controller | ||
# Apparently, `tag` is used inside of `turbo_frame_tag`, so if we define a | ||
# helper_method named `tag` our method gets called when it really shouldn't | ||
# be... so `mtag` it is. For now. | ||
expose :mtag, scope: -> { tags }, model: Tag | ||
expose :tags, -> { policy_scope(bazaar.tags.create_with(marketplace: marketplace)) } | ||
|
||
def new | ||
authorize(mtag) | ||
end | ||
|
||
def create | ||
if authorize(mtag).save | ||
redirect_to marketplace.location(child: :tags) | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
def index | ||
skip_authorization | ||
end | ||
|
||
def mtag_params | ||
policy(Tag).permit(params.require(:tag)) | ||
end | ||
end | ||
end |
17 changes: 17 additions & 0 deletions
17
db/migrate/20240208025354_create_marketplace_product_tags.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,17 @@ | ||
class CreateMarketplaceProductTags < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :marketplace_tags, id: :uuid do |t| | ||
t.references :bazaar, type: :uuid | ||
t.string :label | ||
|
||
t.timestamps | ||
end | ||
|
||
create_table :marketplace_product_tags, id: :uuid do |t| | ||
t.references :product, type: :uuid, foreign_key: {to_table: :marketplace_products} | ||
t.references :tag, type: :uuid, foreign_key: {to_table: :marketplace_tags} | ||
|
||
t.timestamps | ||
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,49 @@ | ||
require "rails_helper" | ||
|
||
describe "Product Tags", type: :system do | ||
let(:space) { create(:space, :with_entrance, :with_members) } | ||
let(:marketplace) { create(:marketplace, :ready_for_shopping, room: space.entrance) } | ||
|
||
before do | ||
sign_in(space.members.first, space) | ||
end | ||
|
||
scenario "Adding Tags to a Product" do # rubocop:disable RSpec/Capybara/FeatureMethods,RSpec/ExampleLength | ||
muffins = create(:marketplace_product, marketplace:, name: "Mazin' Muffins", description: "Buttery corn muffins") | ||
|
||
visit(marketplace) | ||
click_link("Tags") | ||
|
||
click_link("Add Tag") | ||
|
||
fill_in("Label", with: "🚫🌾 Gluten Free") | ||
|
||
click_button("Create") | ||
|
||
click_link("Products") | ||
within(muffins) do | ||
click_link("⚙️ Edit") | ||
end | ||
|
||
check("🚫🌾 Gluten Free") | ||
click_button("Save") | ||
|
||
visit(marketplace) | ||
|
||
within(muffins) do | ||
expect(page).to have_content("🚫🌾 Gluten Free") | ||
end | ||
end | ||
|
||
def visit(object_or_path) | ||
if object_or_path.respond_to?(:location) | ||
super(polymorphic_path(object_or_path.location)) | ||
else | ||
super | ||
end | ||
end | ||
|
||
def within(model, *, **, &block) | ||
page.within("##{dom_id(model)}", *, **, &block) | ||
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,5 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Marketplace::Tag, type: :model do | ||
it { is_expected.to validate_uniqueness_of(:label).case_insensitive.scoped_to(:bazaar_id) } | ||
end |