1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-05 05:25:24 +02:00

Move merchants to top-level namespace (#895)

This commit is contained in:
Zach Gollwitzer 2024-06-20 08:38:59 -04:00 committed by GitHub
parent 2681dd96b1
commit dc3147c101
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
29 changed files with 153 additions and 145 deletions

View file

@ -0,0 +1,41 @@
class MerchantsController < ApplicationController
layout "with_sidebar"
before_action :set_merchant, only: %i[ edit update destroy ]
def index
@merchants = Current.family.merchants.alphabetically
end
def new
@merchant = Merchant.new
end
def create
Current.family.merchants.create!(merchant_params)
redirect_to merchants_path, notice: t(".success")
end
def edit
end
def update
@merchant.update!(merchant_params)
redirect_to merchants_path, notice: t(".success")
end
def destroy
@merchant.destroy!
redirect_to merchants_path, notice: t(".success")
end
private
def set_merchant
@merchant = Current.family.merchants.find(params[:id])
end
def merchant_params
params.require(:merchant).permit(:name, :color)
end
end

View file

@ -1,41 +0,0 @@
class Transactions::MerchantsController < ApplicationController
layout "with_sidebar"
before_action :set_merchant, only: %i[ edit update destroy ]
def index
@merchants = Current.family.transaction_merchants.alphabetically
end
def new
@merchant = Transaction::Merchant.new
end
def create
Current.family.transaction_merchants.create!(merchant_params)
redirect_to transaction_merchants_path, notice: t(".success")
end
def edit
end
def update
@merchant.update!(merchant_params)
redirect_to transaction_merchants_path, notice: t(".success")
end
def destroy
@merchant.destroy!
redirect_to transaction_merchants_path, notice: t(".success")
end
private
def set_merchant
@merchant = Current.family.transaction_merchants.find(params[:id])
end
def merchant_params
params.require(:transaction_merchant).permit(:name, :color)
end
end

View file

@ -6,7 +6,7 @@ class Family < ApplicationRecord
has_many :transactions, through: :accounts
has_many :imports, through: :accounts
has_many :categories, dependent: :destroy
has_many :transaction_merchants, dependent: :destroy, class_name: "Transaction::Merchant"
has_many :merchants, dependent: :destroy
def snapshot(period = Period.all)
query = accounts.active.joins(:balances)

View file

@ -1,4 +1,4 @@
class Transaction::Merchant < ApplicationRecord
class Merchant < ApplicationRecord
has_many :transactions, dependent: :nullify
belongs_to :family

View file

@ -21,7 +21,7 @@ class Transaction < ApplicationRecord
scope :with_categories, ->(categories) { joins(:category).where(categories: { name: categories }) }
scope :with_accounts, ->(accounts) { joins(:account).where(accounts: { name: accounts }) }
scope :with_account_ids, ->(account_ids) { joins(:account).where(accounts: { id: account_ids }) }
scope :with_merchants, ->(merchants) { joins(:merchant).where(transaction_merchants: { name: merchants }) }
scope :with_merchants, ->(merchants) { joins(:merchant).where(merchants: { name: merchants }) }
scope :on_or_after_date, ->(date) { where("transactions.date >= ?", date) }
scope :on_or_before_date, ->(date) { where("transactions.date <= ?", date) }
scope :with_converted_amount, ->(currency = Current.family.currency) {

View file

@ -23,6 +23,6 @@
<footer class="flex justify-between gap-4">
<%= previous_setting("Tags", tags_path) %>
<%= next_setting("Merchants", transaction_merchants_path) %>
<%= next_setting("Merchants", merchants_path) %>
</footer>
</section>

View file

@ -1,14 +1,14 @@
<% is_editing = @merchant.id.present? %>
<div data-controller="merchant-avatar">
<%= form_with model: @merchant, url: is_editing ? transaction_merchant_path(@merchant) : transaction_merchants_path, method: is_editing ? :patch : :post, scope: :transaction_merchant, data: { turbo: false } do |f| %>
<%= form_with model: @merchant, url: is_editing ? merchant_path(@merchant) : merchants_path, method: is_editing ? :patch : :post, scope: :merchant, data: { turbo: false } do |f| %>
<section class="space-y-4">
<div class="w-fit m-auto">
<%= render partial: "transactions/merchants/avatar", locals: { merchant: } %>
<%= render partial: "merchants/avatar", locals: { merchant: } %>
</div>
<div data-controller="select" data-select-active-class="bg-gray-200" data-select-selected-value="<%= @merchant&.color || Transaction::Merchant::COLORS[0] %>">
<div data-controller="select" data-select-active-class="bg-gray-200" data-select-selected-value="<%= @merchant&.color || Merchant::COLORS[0] %>">
<%= f.hidden_field :color, data: { select_target: "input", merchant_avatar_target: "color" } %>
<ul data-select-target="list" class="flex gap-2 items-center">
<% Transaction::Merchant::COLORS.each do |color| %>
<% Merchant::COLORS.each do |color| %>
<li tabindex="0" data-select-target="option" data-action="click->select#selectOption" data-value="<%= color %>" class="flex shrink-0 justify-center items-center w-6 h-6 cursor-pointer hover:bg-gray-200 rounded-full">
<div style="background-color: <%= color %>" class="shrink-0 w-4 h-4 rounded-full"></div>
</li>

View file

@ -2,7 +2,7 @@
<% merchants.each.with_index do |merchant, index| %>
<div class="flex justify-between items-center p-4 bg-white">
<div class="flex w-full items-center gap-2.5">
<%= render partial: "transactions/merchants/avatar", locals: { merchant: } %>
<%= render partial: "merchants/avatar", locals: { merchant: } %>
<p class="text-gray-900 text-sm truncate">
<%= merchant.name %>
</p>
@ -13,13 +13,13 @@
</button>
<div data-menu-target="content" class="absolute z-10 top-10 right-0 border border-alpha-black-25 bg-white rounded-lg shadow-xs w-48 hidden">
<div class="border-t border-b border-alpha-black-100 p-1">
<%= button_to edit_transaction_merchant_path(merchant),
<%= button_to edit_merchant_path(merchant),
method: :get,
class: "flex w-full gap-1 items-center text-sm hover:bg-gray-50 rounded-lg px-3 py-2",
data: { turbo_frame: "modal" } do %>
<%= lucide_icon("pencil-line", class: "w-5 h-5 mr-2") %> <%= t(".edit") %>
<% end %>
<%= button_to transaction_merchant_path(merchant),
<%= button_to merchant_path(merchant),
method: :delete,
class: "flex w-full gap-1 items-center text-sm text-red-600 hover:text-red-800 hover:bg-gray-50 rounded-lg px-3 py-2",
data: {

View file

@ -4,7 +4,7 @@
<div class="space-y-4">
<div class="flex items-center justify-between">
<h1 class="text-xl font-medium text-gray-900"><%= t(".title") %></h1>
<%= link_to new_transaction_merchant_path, class: "flex text-white text-sm font-medium items-center gap-1 bg-gray-900 rounded-lg p-2 pr-3", data: { turbo_frame: "modal" } do %>
<%= link_to new_merchant_path, class: "flex text-white text-sm font-medium items-center gap-1 bg-gray-900 rounded-lg p-2 pr-3", data: { turbo_frame: "modal" } do %>
<%= lucide_icon("plus", class: "w-5 h-5") %>
<span><%= t(".new_short") %></span>
<% end %>
@ -14,7 +14,7 @@
<div class="flex justify-center items-center py-20">
<div class="text-center flex flex-col items-center max-w-[300px]">
<p class="text-gray-900 mb-1 font-medium text-sm"><%= t(".empty") %></p>
<%= link_to new_transaction_merchant_path, class: "w-fit flex text-white text-sm font-medium items-center gap-1 bg-gray-900 rounded-lg p-2 pr-3", data: { turbo_frame: "modal" } do %>
<%= link_to new_merchant_path, class: "w-fit flex text-white text-sm font-medium items-center gap-1 bg-gray-900 rounded-lg p-2 pr-3", data: { turbo_frame: "modal" } do %>
<%= lucide_icon("plus", class: "w-5 h-5") %>
<span><%= t(".new_long") %></span>
<% end %>
@ -27,7 +27,7 @@
<span class="text-gray-400 mx-2">&middot;</span>
<p><%= @merchants.count %></p>
</div>
<%= render partial: "transactions/merchants/list", locals: { merchants: @merchants } %>
<%= render partial: "merchants/list", locals: { merchants: @merchants } %>
</div>
<% end %>
</div>

View file

@ -53,7 +53,7 @@
<%= sidebar_link_to t(".categories_label"), categories_path, icon: "tags" %>
</li>
<li>
<%= sidebar_link_to t(".merchants_label"), transaction_merchants_path, icon: "store" %>
<%= sidebar_link_to t(".merchants_label"), merchants_path, icon: "store" %>
</li>
<li>
<%= sidebar_link_to t(".rules_label"), transaction_rules_path, icon: "list-checks" %>

View file

@ -29,7 +29,7 @@
<div class="pb-6 space-y-2">
<%= form.date_field :date, label: t(".date_label"), max: Date.current %>
<%= form.collection_select :category_id, Current.family.categories.alphabetically, :id, :name, { prompt: t(".category_placeholder"), label: t(".category_label"), class: "text-gray-400" } %>
<%= form.collection_select :merchant_id, Current.family.transaction_merchants.alphabetically, :id, :name, { prompt: t(".merchant_placeholder"), label: t(".merchant_label"), class: "text-gray-400" } %>
<%= form.collection_select :merchant_id, Current.family.merchants.alphabetically, :id, :name, { prompt: t(".merchant_placeholder"), label: t(".merchant_label"), class: "text-gray-400" } %>
</div>
</details>

View file

@ -9,7 +9,7 @@
</div>
</div>
<div class="flex justify-between gap-4">
<%= previous_setting("Merchants", transaction_merchants_path) %>
<%= previous_setting("Merchants", merchants_path) %>
<%= next_setting("Imports", imports_path) %>
</div>
</div>

View file

@ -5,7 +5,7 @@
<%= lucide_icon("search", class: "w-5 h-5 text-gray-500 absolute inset-y-0 left-2 top-1/2 transform -translate-y-1/2") %>
</div>
<div class="my-2" id="list" data-list-filter-target="list">
<% Current.family.transaction_merchants.alphabetically.each do |merchant| %>
<% Current.family.merchants.alphabetically.each do |merchant| %>
<div class="filterable-item flex items-center gap-2 p-2" data-filter-name="<%= merchant.name %>">
<%= form.check_box :merchants,
{

View file

@ -30,7 +30,7 @@
<% unless @transaction.marked_as_transfer %>
<%= f.collection_select :category_id, Current.family.categories.alphabetically, :id, :name, { prompt: t(".category_placeholder"), label: t(".category_label"), class: "text-gray-400" }, "data-auto-submit-form-target": "auto" %>
<%= f.collection_select :merchant_id, Current.family.transaction_merchants.alphabetically, :id, :name, { prompt: t(".merchant_placeholder"), label: t(".merchant_label"), class: "text-gray-400" }, "data-auto-submit-form-target": "auto" %>
<%= f.collection_select :merchant_id, Current.family.merchants.alphabetically, :id, :name, { prompt: t(".merchant_placeholder"), label: t(".merchant_label"), class: "text-gray-400" }, "data-auto-submit-form-target": "auto" %>
<% end %>
<%= f.collection_select :account_id, Current.family.accounts.alphabetically, :id, :name, { prompt: t(".account_placeholder"), label: t(".account_label"), class: "text-gray-500" }, { class: "form-field__input cursor-not-allowed text-gray-400", disabled: "disabled" } %>