-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
48 changed files
with
1,954 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
class ArticleUnitsController < ApplicationController | ||
before_action :load_available_units, only: [:search, :create, :destroy] | ||
|
||
def index; end | ||
|
||
def search | ||
@query = params[:q].blank? ? nil : params[:q].downcase | ||
|
||
existing_article_units = ArticleUnit.all.to_a | ||
@article_units = @available_units | ||
.to_h do |key, value| | ||
[key, value.merge({ code: key, record: existing_article_units.find { |existing_unit| existing_unit.unit == key } })] | ||
end | ||
|
||
unless @query.nil? | ||
@article_units = @article_units.select do |_key, value| | ||
value[:name].downcase.include?(@query) || value[:symbol]&.downcase&.include?(@query) | ||
end | ||
end | ||
|
||
@article_units = @article_units | ||
.sort { |a, b| sort_by_unit_name(@query, a, b) } | ||
.map { |_key, value| value } | ||
|
||
@article_units = @article_units.take(100) unless @query.nil? | ||
@article_units = @article_units.reject { |unit| unit[:record].nil? } if @query.nil? | ||
end | ||
|
||
def create | ||
@article_unit = ArticleUnit.create(unit: params[:unit]) | ||
end | ||
|
||
def destroy | ||
@article_unit = ArticleUnit.find(params[:id]) | ||
@article_unit.destroy | ||
end | ||
|
||
private | ||
|
||
def load_available_units | ||
@available_units = ArticleUnitsLib.units | ||
end | ||
|
||
def article_unit_params | ||
params.require(:article_unit).permit(:unit) | ||
end | ||
|
||
def sort_by_unit_name(query, a_unit, b_unit) | ||
a_name = a_unit[1][:name].downcase | ||
b_name = b_unit[1][:name].downcase | ||
|
||
unless query.nil? | ||
return -1 if a_name.starts_with?(query) && !b_name.starts_with?(query) | ||
return 1 if !a_name.starts_with?(query) && b_name.starts_with?(query) | ||
end | ||
|
||
a_name <=> b_name | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class ArticleUnit < ApplicationRecord | ||
self.primary_key = :unit | ||
|
||
def self.all_cached | ||
return @all_cached unless @all_cached.nil? | ||
|
||
@all_cached = all.load | ||
end | ||
|
||
def self.clear_cache | ||
@all_cached = nil | ||
end | ||
|
||
def self.as_hash | ||
available_units = all_cached.map(&:unit) | ||
ArticleUnitsLib.units.to_h { |code, unit| [code, unit.merge({ visible: available_units.include?(code) })] } | ||
end | ||
|
||
def self.as_options(config) | ||
additional_units = config&.dig(:additional_units) || [] | ||
options = {} | ||
|
||
available_units = all_cached.map(&:unit) | ||
ArticleUnitsLib.units.each do |code, unit| | ||
next unless available_units.include?(code) || additional_units.include?(code) | ||
|
||
options[unit[:name]] = code | ||
end | ||
|
||
options | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class ArticleUnitSerializer < ActiveModel::Serializer | ||
attributes :id, :unit | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
= link_to t('.add'), article_units_path(unit: unit), class: 'btn btn-mini', remote: true, method: :post, data: {'for-unit': unit} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
=link_to t('.delete'), article_unit, method: :delete, class: 'btn btn-mini btn-danger', remote: true, data: { confirm: t('.confirm'), 'for-unit': article_unit.unit } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
- @article_units.each do |article_unit| | ||
%tr{class: ("untranslated" if article_unit[:untranslated])} | ||
%td= article_unit[:code] | ||
%td= highlight(article_unit[:symbol], @query) | ||
%td | ||
= highlight(article_unit[:name], @query) | ||
- if article_unit[:untranslated] | ||
%a{title: t('.request_translation'), href: "https://github.com/foodcoops/foodsoft/issues/new?title=Add%20translations%20for%20article%20unit%20%27#{article_unit[:code]}%27", target: '_blank'} | ||
%i.icon-mail-forward | ||
%td= article_unit[:description] | ||
%td | ||
- if article_unit[:record].nil? | ||
= render partial: 'create_link', locals: {unit: article_unit[:code]} | ||
- else | ||
= render partial: 'destroy_link', locals: {article_unit: article_unit[:record]} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
$('div.container-fluid').prepend('#{j(render(:partial => 'shared/alert_success', :locals => {:alert_message => t('.success', name: @available_units[@article_unit.unit][:name])}))}') | ||
$('a[data-for-unit="#{@article_unit.unit}"]').replaceWith('#{j(render(partial: "destroy_link", locals: {article_unit: @article_unit}))}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
$('div.container-fluid').prepend('#{j(render(:partial => 'shared/alert_success', :locals => {:alert_message => t('.success', name: @available_units[@article_unit.unit][:name])}))}') | ||
$('a[data-for-unit="#{@article_unit.unit}"]').replaceWith('#{j(render(partial: "create_link", locals: {unit: @article_unit.unit}))}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
=form_with url: search_article_units_path, method: :get, id: :article_unit_search_form, html: {'data-remote': true} do |form| | ||
= form.text_field :q, id: :article_unit_search, placeholder: t('.search') | ||
|
||
%table.table.table-striped | ||
%thead | ||
%tr | ||
%th= heading_helper ArticleUnit, :code, short: true | ||
%th= heading_helper ArticleUnit, :symbol | ||
%th= heading_helper ArticleUnit, :name | ||
%th= heading_helper ArticleUnit, :description | ||
%th | ||
%th | ||
|
||
%tbody#article_units_search_results | ||
%br | ||
|
||
|
||
- content_for :javascript do | ||
:javascript | ||
let timer; | ||
function debounce(timeout, func){ | ||
return (...args) => { | ||
if (timer !== undefined) { | ||
clearTimeout(timer); | ||
timer = undefined; | ||
} | ||
timer = setTimeout(() => { | ||
func.apply(this, args); | ||
}, timeout); | ||
}; | ||
} | ||
|
||
$(document).ready(function () { | ||
$('#article_unit_search').on('input', debounce(250, () => { | ||
$('#article_unit_search_form').submit(); | ||
})); | ||
}); | ||
|
||
let currentXhr; | ||
|
||
$(document).on('ajax:send', function(_event, xhr) { | ||
if (timer !== undefined) { | ||
clearTimeout(timer); | ||
timer = undefined; | ||
} | ||
if (currentXhr) { | ||
currentXhr.abort(); | ||
} | ||
currentXhr = xhr; | ||
return true; | ||
}); | ||
|
||
$(document).on('ajax:complete', function(_event, _xhr, _status) { | ||
currentXhr = null; | ||
}); | ||
|
||
$('#article_unit_search_form').submit(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
$('#article_units_search_results').html('#{j(render("rows"))}'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.