From 846eb6da14fe794332c548b7c03d9290466221a1 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sat, 9 Nov 2024 02:05:04 +0100 Subject: [PATCH] Preliminary support for default Units import --- app/models/unit.rb | 18 ++++++++++++++++++ app/views/units/index.html.erb | 2 +- config/routes.rb | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/models/unit.rb b/app/models/unit.rb index 4e88fa2..28b38ba 100644 --- a/app/models/unit.rb +++ b/app/models/unit.rb @@ -14,6 +14,24 @@ class Unit < ApplicationRecord validates :multiplier, numericality: {other_than: 0}, if: :base scope :defaults, ->{ where(user: nil) } + scope :with_defaults, ->{ self.or(Unit.where(user: nil)) } + scope :default_diff, ->{ + other_units = Unit.arel_table.alias('other_units') + other_bases_units = Unit.arel_table.alias('other_bases_units') + constraints = other_bases_units[:id].eq(other_units[:base_id]) + .and(other_units[:symbol].eq(arel_table[:symbol])) + .and(other_units[:user_id].not_eq(arel_table[:user_id])) + + with_defaults + .joins( + arel_table.create_join( + arel_table.grouping([other_units, other_bases_units]), + arel_table.create_on(constraints), + Arel::Nodes::OuterJoin + ).to_sql + ) + .where({other_units: {id: nil}}) + } scope :ordered, ->{ left_outer_joins(:base) .order(arel_table.coalesce(Arel::Table.new(:bases_units)[:symbol], arel_table[:symbol]), diff --git a/app/views/units/index.html.erb b/app/views/units/index.html.erb index 19177c8..778b688 100644 --- a/app/views/units/index.html.erb +++ b/app/views/units/index.html.erb @@ -2,7 +2,7 @@ <% if current_user.at_least(:active) %> <%= image_link_to t('.add_unit'), 'plus-outline', new_unit_path, id: :add_unit, onclick: 'this.blur();', data: {turbo_stream: true} %> - <%= image_link_to t('.import_units'), 'import', new_unit_path, class: 'tools', + <%= image_link_to t('.import_units'), 'import', units_defaults_path, class: 'tools', data: {turbo_stream: true} %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 3f556c2..8695bcd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,7 +9,7 @@ Rails.application.routes.draw do end namespace :units do - get 'defaults/index' + resources :defaults, only: :index end resources :users, only: [:index, :show, :update] do