Implement 'import' action

This commit is contained in:
cryptogopher 2024-11-17 03:39:39 +01:00
parent 41982e9dbc
commit f0e28deea2
3 changed files with 24 additions and 6 deletions

View File

@ -1,7 +1,8 @@
class Default::UnitsController < ApplicationController
navigation_tab :units
before_action :find_unit, only: [:import, :export, :destroy]
before_action -> { find_unit(current_user) }, only: :export
before_action -> { find_unit(nil) }, only: [:import, :destroy]
before_action except: :index do
case action_name.to_sym
@ -17,9 +18,16 @@ class Default::UnitsController < ApplicationController
end
def import
current_user.units
.find_or_initialize_by(symbol: @unit.symbol)
.update!(base: @base, **@unit.slice(:name, :multiplier))
run_and_render :index
end
def import_all
# From defaults_diff return not only portability, but reason for not being
# portable: missing_base and nesting_too_deep. Add portable and
# missing_base, if possible in one query
end
def export
@ -27,4 +35,11 @@ class Default::UnitsController < ApplicationController
def destroy
end
private
def find_unit(user)
@unit = Unit.find_by!(id: params[:id], user: user)
@base = Unit.find_by!(symbol: @unit.base.symbol, user: user? ? nil : user) if @unit.base
end
end

View File

@ -28,9 +28,9 @@ class Unit < ApplicationRecord
.outer_join(other_bases_units)
.on(other_units[:base_id].eq(other_bases_units[:id]))
.where(
other_bases_units[:symbol].eq(bases_units[:symbol])
other_bases_units[:symbol].is_not_distinct_from(bases_units[:symbol])
.and(other_units[:symbol].eq(arel_table[:symbol]))
.and(other_units[:user_id].not_eq(arel_table[:user_id]))
.and(other_units[:user_id].is_distinct_from(arel_table[:user_id]))
).exists
# Decide if Unit can be im-/exported based on existing hierarchy:
# * same base unit symbol has to exist
@ -43,14 +43,14 @@ class Unit < ApplicationRecord
.join(sub_units).on(other_units[:id].eq(sub_units[:base_id]))
.where(
other_units[:symbol].eq(arel_table[:symbol])
.and(other_units[:user_id].not_eq(arel_table[:user_id]))
.and(other_units[:user_id].is_distinct_from(arel_table[:user_id]))
)
.exists.not
).and(
Arel::SelectManager.new.project(1).from(other_bases_units)
.where(
other_bases_units[:symbol].eq(bases_units[:symbol])
.and(other_bases_units[:user_id].not_eq(bases_units[:user_id]))
other_bases_units[:symbol].is_not_distinct_from(bases_units[:symbol])
.and(other_bases_units[:user_id].is_distinct_from(bases_units[:user_id]))
)
.exists
)

View File

@ -0,0 +1,3 @@
<%= turbo_stream.update :units do %>
<%= render(@units) || render_no_items %>
<% end %>