Skip to content

Commit

Permalink
[modify] cms site_contents : support column_values
Browse files Browse the repository at this point in the history
  • Loading branch information
itowtips committed Apr 21, 2021
1 parent 294e394 commit d97a79b
Show file tree
Hide file tree
Showing 6 changed files with 677 additions and 10 deletions.
35 changes: 29 additions & 6 deletions app/controllers/cms/search_contents/html_controller.rb
Expand Up @@ -72,6 +72,7 @@ def update
def replace_html_with_string(string, replacement)
@pages = @pages.select do |item|
update_html_fields(item) { |html| html.gsub(string, replacement) }
update_column_values_fields(item) { |html| html.gsub(string, replacement) }
end

@parts = @parts.select do |item|
Expand All @@ -89,6 +90,7 @@ def replace_html_with_url(src_url, dest_url)

@pages = @pages.select do |item|
update_html_fields(item) { |html| html.gsub(src_path, dest_path) }
update_column_values_fields(item) { |html| html.gsub(src_path, dest_path) }
end

@parts = @parts.select do |item|
Expand All @@ -105,6 +107,7 @@ def replace_html_with_regexp(string, replacement)

@pages = @pages.select do |item|
update_html_fields(item) { |html| html.gsub(regexp, replacement) }
update_column_values_fields(item) { |html| html.gsub(regexp, replacement) }
end

@parts = @parts.select do |item|
Expand All @@ -123,20 +126,40 @@ def exclude_search_results(page_ids, part_ids, layout_ids)
end

def update_html_fields(item)
item = item.becomes_with_route if item.try(:route)
item = item.becomes_with_route if item.respond_to?(:becomes_with_route)

attributes = {}
HTML_FIELDS.each do |field|
next unless item.try(field)
html = yield item.send(field)
attributes[field] = html if item.send(field) != html
end
item.set(attributes) if attributes.present?
true
end

if attributes.present?
item.set(attributes)
true
else
false
def update_column_values_fields(item)
item = item.becomes_with_route if item.respond_to?(:becomes_with_route)

item.column_values.each do |column_value|
attributes = {}
COLUMN_VALUES_FIELDS.each do |field|
next if !column_value.respond_to?(field)

old_value = column_value.send(field)
next if old_value.blank?

if old_value.is_a?(String)
new_value = yield old_value
attributes[field] = new_value if new_value != old_value
elsif old_value.is_a?(Array)
old_value = old_value.map(&:to_s)
new_value = old_value.map { |v| yield v }
attributes[field] = new_value if new_value != old_value
end
end
column_value.set(attributes) if attributes.present?
end
true
end
end
34 changes: 31 additions & 3 deletions app/controllers/concerns/cms/api_filter/contents.rb
Expand Up @@ -6,22 +6,50 @@ module Cms::ApiFilter::Contents
:contact_fax, :contact_email, :contact_link_url, :contact_link_name
].freeze

COLUMN_VALUES_FIELDS = [
:value, :text, :link_url, :link_label, :lists
].freeze

private

def search_html_with_string(string)
cond = { "$or" => HTML_FIELDS.map { |field| { field => /#{::Regexp.escape(string)}/ } } }
cond = HTML_FIELDS.map { |field| { field => /#{::Regexp.escape(string)}/ } }
cond << {
column_values: {
"$elemMatch" => {
"$or"=> COLUMN_VALUES_FIELDS.map { |key| { key => { "$in" => [/#{::Regexp.escape(string)}/] } } }
}
}
}
cond = { "$or" => cond }
search_html_with_condition(cond)
end

def search_html_with_url(url)
path = "=\"#{::Regexp.escape(url)}"
cond = { "$or" => HTML_FIELDS.map { |field| { field => /#{path}/ } } }
cond = HTML_FIELDS.map { |field| { field => /#{path}/ } }
cond << {
column_values: {
"$elemMatch" => {
"$or"=> COLUMN_VALUES_FIELDS.map { |key| { key => { "$in" => [/#{path}/] } } }
}
}
}
cond = { "$or" => cond }
search_html_with_condition(cond)
end

def search_html_with_regexp(string)
regexp = ::Regexp.new(string, ::Regexp::MULTILINE)
cond = { "$or" => HTML_FIELDS.map { |field| { field => regexp } } }
cond = HTML_FIELDS.map { |field| { field => regexp } }
cond << {
column_values: {
"$elemMatch" => {
"$or"=> COLUMN_VALUES_FIELDS.map { |key| { key => { "$in" => [regexp] } } }
}
}
}
cond = { "$or" => cond }
search_html_with_condition(cond)
end

Expand Down
13 changes: 12 additions & 1 deletion app/models/concerns/cms/addon/page_search.rb
Expand Up @@ -151,6 +151,10 @@ module PageSearch
:contact_fax, :contact_email, :contact_link_url, :contact_link_name
].freeze

COLUMN_VALUES_FIELDS = [
:value, :text, :link_url, :link_label, :lists
].freeze

included do
field :search_name, type: String
field :search_filename, type: String
Expand Down Expand Up @@ -252,7 +256,14 @@ def search_nodes
def search_keyword
return if @item.search_keyword.blank?

conds = PageSearch::KEYWORD_FIELDS.map { |field| { field => /#{::Regexp.escape(@item.search_keyword)}/ } }
conds = KEYWORD_FIELDS.map { |field| { field => /#{::Regexp.escape(@item.search_keyword)}/ } }
conds << {
column_values: {
"$elemMatch" => {
"$or"=> COLUMN_VALUES_FIELDS.map { |key| { key => { "$in" => [/#{::Regexp.escape(@item.search_keyword)}/] } } }
}
}
}
@criteria = @criteria.where("$and" => [{ "$or" => conds }])
end

Expand Down
2 changes: 2 additions & 0 deletions app/views/cms/search_contents/pages/_search.html.erb
@@ -1,4 +1,5 @@
<%= form_for :item, url: { action: :index }, html: { class: "search-pages", multipart: true, method: :post } do |f| %>
<div class="search-contents-form">
<%= render file: "cms/agents/addons/page_search/_form.html.erb", locals: { f: f } %>

<footer class="send">
Expand All @@ -11,4 +12,5 @@
<%= f.button t("ss.buttons.download"), name: "download", class: "download btn-primary" %>
<% end %>
</footer>
</div>
<% end %>

0 comments on commit d97a79b

Please sign in to comment.