Class: ArticleUnitsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/article_units_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

current

Methods included from PathHelper

#finance_group_transactions_path

Instance Method Details

#createObject



33
34
35
# File 'app/controllers/article_units_controller.rb', line 33

def create
  @article_unit = ArticleUnit.create(unit: params[:unit])
end

#destroyObject



37
38
39
40
# File 'app/controllers/article_units_controller.rb', line 37

def destroy
  @article_unit = ArticleUnit.find(params[:id])
  @article_unit.destroy
end

#indexObject



5
# File 'app/controllers/article_units_controller.rb', line 5

def index; end

#searchObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/article_units_controller.rb', line 7

def search
  @query = article_unit_params[:q].blank? ? nil : article_unit_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 do |existing_unit|
                                             existing_unit.unit == key
                                           end })]
  end

  unless @query.nil?
    @article_units = @article_units.select do |_key, value|
      (value[:name].downcase.include?(@query) || value[:symbol]&.downcase&.include?(@query)) &&
        (params[:only_recommended] == '0' || !value[:untranslated])
    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