过滤和全球化
Filterrific and Globalize
filterrific 似乎没有考虑翻译 table 中的内容 (Globalize)。
还有搜索翻译 table 的吗?如果内容在实际模型中,我的设置工作得很好。但是,一旦字段为空并且仅在翻译 table 中输入,则不会显示任何结果(很明显)。
我的模特:
class Manual < ApplicationRecord
translates :title, :content, :teaser, :slug
extend FriendlyId
friendly_id :title, :use => :globalize
belongs_to :user
belongs_to :support_category
has_many :manual_faqs
has_many :faqs, :through => :manual_faqs
validates :title, presence: true
validates :content, presence: true
validates :user_id, presence: true
update_index('manuals#manual') { self }
filterrific(
default_filter_params: { sorted_by: 'created_at_desc' },
available_filters: [
:sorted_by,
:search_query,
:with_user_id,
:with_created_at_gte
]
)
scope :with_user_id, lambda { |user_ids|
where(user_id: [*user_ids])
}
scope :search_query, lambda { |query|
# Searches the students table on the 'first_name' and 'last_name' columns.
# Matches using LIKE, automatically appends '%' to each term.
# LIKE is case INsensitive with MySQL, however it is case
# sensitive with PostGreSQL. To make it work in both worlds,
# we downcase everything.
return nil if query.blank?
# condition query, parse into individual keywords
terms = query.downcase.split(/\s+/)
# replace "*" with "%" for wildcard searches,
# append '%', remove duplicate '%'s
terms = terms.map { |e|
('%' + e.gsub('*', '%') + '%').gsub(/%+/, '%')
}
# configure number of OR conditions for provision
# of interpolation arguments. Adjust this if you
# change the number of OR conditions.
num_or_conds = 2
where(
terms.map { |term|
"(LOWER(manuals.title) LIKE ? OR LOWER(manuals.content) LIKE ?)"
}.join(' AND '),
*terms.map { |e| [e] * num_or_conds }.flatten
)
}
scope :sorted_by, lambda { |sort_option|
# extract the sort direction from the param value.
direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
case sort_option.to_s
when /^created_at_/
# Simple sort on the created_at column.
# Make sure to include the table name to avoid ambiguous column names.
# Joining on other tables is quite common in Filterrific, and almost
# every ActiveRecord table has a 'created_at' column.
order("manuals.created_at #{ direction }")
else
raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
end
}
scope :created_at_gte, lambda { |reference_time|
where('manuals.created_at >= ?', reference_time)
}
def self.options_for_sorted_by
[
['Date received (newest first)', 'created_at_desc'],
['Date received (oldest first)', 'created_at_asc']
]
end
end
我的控制器:
def index
@filterrific = initialize_filterrific(
Manual,
params[:filterrific],
select_options: {
sorted_by: Manual.options_for_sorted_by,
with_user_id: User.options_for_select
}
) or return
@manuals = @filterrific.find.page(params[:page])
respond_to do |format|
format.html
format.js
end
rescue ActiveRecord::RecordNotFound => e
# There is an issue with the persisted param_set. Reset it.
puts "Had to reset filterrific params: #{ e.message }"
redirect_to(reset_filterrific_url(format: :html)) and return
#respond_with(@references)
end
我根本不知道 filterrific,但我知道 Globalize,并且由于 filterrific 基于 AR 范围,因此只需加入翻译 table 即可显示结果。
这是您的 search_query
范围修改后加入和搜索加入的翻译 table(为清楚起见没有评论):
scope :search_query, lambda { |query|
return nil if query.blank?
terms = query.downcase.split(/\s+/)
terms = terms.map { |e|
('%' + e.gsub('*', '%') + '%').gsub(/%+/, '%')
}
num_or_conds = 2
where(
('(LOWER(manual_translations.title) LIKE ? OR'\
' LOWER(manual_translations.content) LIKE ?)' * (terms.count)).join(' AND '),
*terms.map { |e| [e] * num_or_conds }.flatten
).with_translations
}
请注意,我只更改了两件事:(1) 我附加了 with_translations
, a method described in this SO answer,它加入了当前语言环境的翻译,(2) 我交换了 manuals
table 用于查询中的 manual_translations
table。
因此,如果您在英语语言环境中调用此查询:
Manual.search_query("foo")
你明白了 SQL:
SELECT "manuals".* FROM "manuals"
INNER JOIN "manual_translations" ON "manual_translations"."manual_id" = "manuals"."id"
WHERE (LOWER(manual_translations.title) LIKE '%foo%' OR
LOWER(manual_translations.content) LIKE '%foo%')
AND "manual_translations"."locale" = 'en'"
请注意,with_translations
会自动在 manual_translations.locale = 'en'
上添加标签,因此您只过滤掉区域设置中的结果,我认为这就是您想要的。
让我知道这是否适合你。
filterrific 似乎没有考虑翻译 table 中的内容 (Globalize)。
还有搜索翻译 table 的吗?如果内容在实际模型中,我的设置工作得很好。但是,一旦字段为空并且仅在翻译 table 中输入,则不会显示任何结果(很明显)。
我的模特:
class Manual < ApplicationRecord
translates :title, :content, :teaser, :slug
extend FriendlyId
friendly_id :title, :use => :globalize
belongs_to :user
belongs_to :support_category
has_many :manual_faqs
has_many :faqs, :through => :manual_faqs
validates :title, presence: true
validates :content, presence: true
validates :user_id, presence: true
update_index('manuals#manual') { self }
filterrific(
default_filter_params: { sorted_by: 'created_at_desc' },
available_filters: [
:sorted_by,
:search_query,
:with_user_id,
:with_created_at_gte
]
)
scope :with_user_id, lambda { |user_ids|
where(user_id: [*user_ids])
}
scope :search_query, lambda { |query|
# Searches the students table on the 'first_name' and 'last_name' columns.
# Matches using LIKE, automatically appends '%' to each term.
# LIKE is case INsensitive with MySQL, however it is case
# sensitive with PostGreSQL. To make it work in both worlds,
# we downcase everything.
return nil if query.blank?
# condition query, parse into individual keywords
terms = query.downcase.split(/\s+/)
# replace "*" with "%" for wildcard searches,
# append '%', remove duplicate '%'s
terms = terms.map { |e|
('%' + e.gsub('*', '%') + '%').gsub(/%+/, '%')
}
# configure number of OR conditions for provision
# of interpolation arguments. Adjust this if you
# change the number of OR conditions.
num_or_conds = 2
where(
terms.map { |term|
"(LOWER(manuals.title) LIKE ? OR LOWER(manuals.content) LIKE ?)"
}.join(' AND '),
*terms.map { |e| [e] * num_or_conds }.flatten
)
}
scope :sorted_by, lambda { |sort_option|
# extract the sort direction from the param value.
direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
case sort_option.to_s
when /^created_at_/
# Simple sort on the created_at column.
# Make sure to include the table name to avoid ambiguous column names.
# Joining on other tables is quite common in Filterrific, and almost
# every ActiveRecord table has a 'created_at' column.
order("manuals.created_at #{ direction }")
else
raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
end
}
scope :created_at_gte, lambda { |reference_time|
where('manuals.created_at >= ?', reference_time)
}
def self.options_for_sorted_by
[
['Date received (newest first)', 'created_at_desc'],
['Date received (oldest first)', 'created_at_asc']
]
end
end
我的控制器:
def index
@filterrific = initialize_filterrific(
Manual,
params[:filterrific],
select_options: {
sorted_by: Manual.options_for_sorted_by,
with_user_id: User.options_for_select
}
) or return
@manuals = @filterrific.find.page(params[:page])
respond_to do |format|
format.html
format.js
end
rescue ActiveRecord::RecordNotFound => e
# There is an issue with the persisted param_set. Reset it.
puts "Had to reset filterrific params: #{ e.message }"
redirect_to(reset_filterrific_url(format: :html)) and return
#respond_with(@references)
end
我根本不知道 filterrific,但我知道 Globalize,并且由于 filterrific 基于 AR 范围,因此只需加入翻译 table 即可显示结果。
这是您的 search_query
范围修改后加入和搜索加入的翻译 table(为清楚起见没有评论):
scope :search_query, lambda { |query|
return nil if query.blank?
terms = query.downcase.split(/\s+/)
terms = terms.map { |e|
('%' + e.gsub('*', '%') + '%').gsub(/%+/, '%')
}
num_or_conds = 2
where(
('(LOWER(manual_translations.title) LIKE ? OR'\
' LOWER(manual_translations.content) LIKE ?)' * (terms.count)).join(' AND '),
*terms.map { |e| [e] * num_or_conds }.flatten
).with_translations
}
请注意,我只更改了两件事:(1) 我附加了 with_translations
, a method described in this SO answer,它加入了当前语言环境的翻译,(2) 我交换了 manuals
table 用于查询中的 manual_translations
table。
因此,如果您在英语语言环境中调用此查询:
Manual.search_query("foo")
你明白了 SQL:
SELECT "manuals".* FROM "manuals"
INNER JOIN "manual_translations" ON "manual_translations"."manual_id" = "manuals"."id"
WHERE (LOWER(manual_translations.title) LIKE '%foo%' OR
LOWER(manual_translations.content) LIKE '%foo%')
AND "manual_translations"."locale" = 'en'"
请注意,with_translations
会自动在 manual_translations.locale = 'en'
上添加标签,因此您只过滤掉区域设置中的结果,我认为这就是您想要的。
让我知道这是否适合你。