From f0e28deea2af45ee403dbb8dd69799ee1b864f2e Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 17 Nov 2024 03:39:39 +0100 Subject: [PATCH] Implement 'import' action --- app/controllers/default/units_controller.rb | 17 ++++++++++++++++- app/models/unit.rb | 10 +++++----- app/views/default/units/index.turbo_stream.erb | 3 +++ 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 app/views/default/units/index.turbo_stream.erb diff --git a/app/controllers/default/units_controller.rb b/app/controllers/default/units_controller.rb index ea47bbb..e65b8fa 100644 --- a/app/controllers/default/units_controller.rb +++ b/app/controllers/default/units_controller.rb @@ -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 diff --git a/app/models/unit.rb b/app/models/unit.rb index 53af7df..ece321b 100644 --- a/app/models/unit.rb +++ b/app/models/unit.rb @@ -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 ) diff --git a/app/views/default/units/index.turbo_stream.erb b/app/views/default/units/index.turbo_stream.erb new file mode 100644 index 0000000..2628fa7 --- /dev/null +++ b/app/views/default/units/index.turbo_stream.erb @@ -0,0 +1,3 @@ +<%= turbo_stream.update :units do %> + <%= render(@units) || render_no_items %> +<% end %>