-
-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ Marketplace
: Add Product#tags
#2190
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Marketplace | ||
class Tag < Record | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a blocker, but one way to help users keep their tags tidy would be to add a case-insensitive uniqueness constraint here, e.g: validates :label, uniqueness: true, scope: { :bazaar, case_insensitive: true } This would prevent adding "Gluten-Free" if "gluten-free" already exists. (Also a good excuse to start a smol There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like enforcing a case insensitive uniqueness scoped to bazaar! |
||
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 |
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 |
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 %> |
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 %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<%- breadcrumb :new_marketplace_tag, mtag %> | ||
<%= render "form", tag: mtag %> |
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 |
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 |
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) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two lines are repeated in pretty much every There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have a strong opinion either way. For tests like these, I don't mind duplicating a couple of lines, and I like seeing at a glance how data is being setup for the test. An intermediate solution would be to have a trait like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the solution of a |
||
|
||
before do | ||
sign_in(space.members.first, space) | ||
end | ||
|
||
scenario "Adding Tags to a Product" do # rubocop:disable RSpec/Capybara/FeatureMethods,RSpec/ExampleLength | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should disable both of these cops in system specs. Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that makes sense. |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically we don't need this |
||
end | ||
|
||
def within(model, *, **, &block) | ||
page.within("##{dom_id(model)}", *, **, &block) | ||
end | ||
end |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allows us to have
Models
within theMarketplace
that do not reference aMarketplace
directly, such asTaxRate
andTag