From 51011951f9943d2e2325a80d0eca0d70fc9d77da Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 10 Nov 2024 21:30:19 +0100 Subject: [PATCH] Default Units index --- .../pictograms/download-multiple-outline.svg | 1 + app/models/unit.rb | 7 ++++++- app/views/default/units/_unit.html.erb | 19 +++++++++++++++++++ app/views/default/units/index.html.erb | 8 ++++++-- config/locales/en.yml | 3 +++ 5 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 app/assets/images/pictograms/download-multiple-outline.svg create mode 100644 app/views/default/units/_unit.html.erb diff --git a/app/assets/images/pictograms/download-multiple-outline.svg b/app/assets/images/pictograms/download-multiple-outline.svg new file mode 100644 index 0000000..24d0398 --- /dev/null +++ b/app/assets/images/pictograms/download-multiple-outline.svg @@ -0,0 +1 @@ + diff --git a/app/models/unit.rb b/app/models/unit.rb index 28b38ba..471bc5d 100644 --- a/app/models/unit.rb +++ b/app/models/unit.rb @@ -15,10 +15,11 @@ class Unit < ApplicationRecord scope :defaults, ->{ where(user: nil) } scope :with_defaults, ->{ self.or(Unit.where(user: nil)) } - scope :default_diff, ->{ + scope :defaults_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_bases_units[:symbol].eq(Arel::Table.new(:bases_units)[:symbol])) .and(other_units[:symbol].eq(arel_table[:symbol])) .and(other_units[:user_id].not_eq(arel_table[:user_id])) @@ -46,4 +47,8 @@ class Unit < ApplicationRecord def movable? subunits.empty? end + + def default? + user.nil? + end end diff --git a/app/views/default/units/_unit.html.erb b/app/views/default/units/_unit.html.erb new file mode 100644 index 0000000..662a7ea --- /dev/null +++ b/app/views/default/units/_unit.html.erb @@ -0,0 +1,19 @@ +<%= tag.tr do %> + + <%= unit.symbol %> + + + + <% if current_user.at_least(:active) && unit.default? %> + <%= image_button_to t(".import"), "download-outline", import_default_unit_path(unit) %> + <% end %> + <% if current_user.at_least(:admin) %> + <% if !unit.default? %> + <%= image_button_to t(".export"), "upload-outline", export_default_unit_path(unit) %> + <% else %> + <%= image_button_to t(".delete"), "delete-outline", unit_path(unit), + method: :delete %> + <% end %> + <% end %> + +<% end %> diff --git a/app/views/default/units/index.html.erb b/app/views/default/units/index.html.erb index 321f6a0..52e6466 100644 --- a/app/views/default/units/index.html.erb +++ b/app/views/default/units/index.html.erb @@ -1,5 +1,9 @@
- <%= image_link_to t('.back'), 'arrow-left-bold-outline', units_path %> + <% if current_user.at_least(:active) %> + <%= image_link_to t('.import_all'), 'download-multiple-outline', + import_all_default_units_path, data: {turbo_stream: true} %> + <% end %> + <%= image_link_to t('.back'), 'arrow-left-bold-outline', units_path, class: 'tools' %>
@@ -12,6 +16,6 @@ - <%= render(@units, partial: 'default') || render_no_items %> + <%= render(@units) || render_no_items %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index 9f7e00c..6370bba 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -69,7 +69,10 @@ en: success: Deleted unit default: units: + unit: + delete_default: Delete default index: + import_all: Import all back: Back to units... users: index: