Improved code style, fixed bug [SCI-5828]

This commit is contained in:
Martin Artnik 2021-06-29 15:57:23 +02:00
parent d67d53b926
commit e1b0b65465
5 changed files with 25 additions and 37 deletions

View file

@ -43,8 +43,8 @@ module SearchableModel
(attrs.map.with_index do |a, i|
if %w(repository_rows.id repository_number_values.data).include?(a)
"((#{a})::text) #{like} :t#{i} OR "
elsif a == "('EX' || id)"
"('EX' || id) #{like} :t#{i} OR "
elsif a == Experiment::EXPERIMENT_CODE_SQL
"#{Experiment::EXPERIMENT_CODE_SQL} #{like} :t#{i} OR "
else
col = options[:at_search].to_s == 'true' ? "lower(#{a})": a
"(trim_html_tags(#{col})) #{like} :t#{i} OR "
@ -69,8 +69,8 @@ module SearchableModel
(attrs.map.with_index do |a, i|
if %w(repository_rows.id repository_number_values.data).include?(a)
"((#{a})::text) #{like} ANY (array[:t#{i}]) OR "
elsif a == "('EX' || id)"
"('EX' || id) #{like} :t#{i} OR "
elsif a == Experiment::EXPERIMENT_CODE_SQL
"#{Experiment::EXPERIMENT_CODE_SQL} #{like} ANY (array[:t#{i}]) OR "
else
"(trim_html_tags(#{a})) #{like} ANY (array[:t#{i}]) OR "
end
@ -90,8 +90,8 @@ module SearchableModel
(attrs.map.with_index do |a, i|
if %w(repository_rows.id repository_number_values.data).include?(a)
"((#{a})::text) #{like} :t#{i} OR "
elsif a == "('EX' || id)"
"('EX' || id) #{like} :t#{i} OR "
elsif a == Experiment::EXPERIMENT_CODE_SQL
"#{Experiment::EXPERIMENT_CODE_SQL} #{like} :t#{i} OR "
else
"(trim_html_tags(#{a})) #{like} :t#{i} OR "
end

View file

@ -3,6 +3,8 @@ class Experiment < ApplicationRecord
include SearchableModel
include SearchableByNameModel
EXPERIMENT_CODE_SQL = "('EX' || id)".freeze
before_save -> { report_elements.destroy_all }, if: -> { !new_record? && project_id_changed? }
belongs_to :project, inverse_of: :experiments, touch: true
@ -70,19 +72,25 @@ class Experiment < ApplicationRecord
new_query =
Experiment
.where('experiments.project_id IN (?)', projects_ids)
.where_attributes_like([:name, :description, "('EX' || id)"], query, options)
.where_attributes_like(
[:name, :description, EXPERIMENT_CODE_SQL], query, options
)
return include_archived ? new_query : new_query.active
elsif include_archived
new_query =
Experiment
.where(project: project_ids)
.where_attributes_like([:name, :description, "('EX' || id)"], query, options)
.where_attributes_like(
[:name, :description, EXPERIMENT_CODE_SQL], query, options
)
else
new_query =
Experiment
.active
.where(project: project_ids)
.where_attributes_like([:name, :description, "('EX' || id)"], query, options)
.where_attributes_like(
[:name, :description, EXPERIMENT_CODE_SQL], query, options
)
end
# Show all results if needed

View file

@ -1,15 +0,0 @@
# frozen_string_literal: true
class AddCodeIndexToExperiments < ActiveRecord::Migration[6.1]
def up
ActiveRecord::Base.connection.execute(
"CREATE INDEX index_experiments_on_experiment_code ON experiments using gin (('EX'::text || id) gin_trgm_ops);"
)
end
def down
ActiveRecord::Base.connection.execute(
'DROP INDEX index_experiments_on_experiment_code;'
)
end
end

View file

@ -1,27 +1,23 @@
# frozen_string_literal: true
require File.expand_path('app/helpers/database_helper')
include DatabaseHelper
class FixExperimentIndices < ActiveRecord::Migration[6.1]
def up
remove_index :experiments, name: 'index_experiments_on_name', column: 'name'
ActiveRecord::Base.connection.execute(
'CREATE INDEX index_experiments_on_name ON experiments using gin (trim_html_tags(name) gin_trgm_ops);'
)
add_gin_index_without_tags(:experiments, :name)
add_gin_index_without_tags(:experiments, :description)
ActiveRecord::Base.connection.execute(
'CREATE INDEX index_experiments_on_description ON experiments ' \
'using gin (trim_html_tags(description) gin_trgm_ops);'
"CREATE INDEX index_experiments_on_experiment_code ON experiments using gin (('EX'::text || id) gin_trgm_ops);"
)
end
def down
ActiveRecord::Base.connection.execute(
'DROP INDEX index_experiments_on_name;'
)
ActiveRecord::Base.connection.execute(
'DROP INDEX index_experiments_on_description;'
)
remove_index :experiments, name: 'index_experiments_on_code'
remove_index :experiments, name: 'index_experiments_on_description', column: 'description'
remove_index :experiments, name: 'index_experiments_on_name', column: 'name'
add_index :experiments, :name
end

View file

@ -7250,7 +7250,6 @@ INSERT INTO "schema_migrations" (version) VALUES
('20210407143303'),
('20210410100006'),
('20210506125657'),
('20210618104853'),
('20210622101238');