mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-02 19:30:48 +08:00
init project
This commit is contained in:
27
app/services/concerns/accepts_nested_attributes_helper.rb
Normal file
27
app/services/concerns/accepts_nested_attributes_helper.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
module AcceptsNestedAttributesHelper
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def build_accepts_nested_attributes(obj, relations, data)
|
||||
# 新记录,全部为创建
|
||||
return data if obj.new_record?
|
||||
|
||||
# 更新时,需要处理删除数据
|
||||
old_ids = relations.loaded? ? relations.map(&:id) : relations.pluck(:id)
|
||||
new_ids =
|
||||
data.map do |item|
|
||||
yield(item) if block_given?
|
||||
|
||||
# 处理参数中错误的ID
|
||||
item[:id] = item[:id].to_i
|
||||
item[:id] = nil if item[:id].zero? || !old_ids.include?(item[:id])
|
||||
item[:id]
|
||||
end
|
||||
new_ids.compact!
|
||||
|
||||
# 被删除的子项ID数组
|
||||
destroy_ids = old_ids - new_ids
|
||||
destroy_attributes = destroy_ids.map { |id| { id: id, _destroy: true } }
|
||||
|
||||
data.concat(destroy_attributes)
|
||||
end
|
||||
end
|
||||
9
app/services/concerns/callable.rb
Normal file
9
app/services/concerns/callable.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
module Callable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
module ClassMethods
|
||||
def call(*parameters)
|
||||
new(*parameters).call
|
||||
end
|
||||
end
|
||||
end
|
||||
46
app/services/concerns/elasticsearch_able.rb
Normal file
46
app/services/concerns/elasticsearch_able.rb
Normal file
@@ -0,0 +1,46 @@
|
||||
module ElasticsearchAble
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
private
|
||||
|
||||
def default_options
|
||||
{
|
||||
debug: Rails.env.development?,
|
||||
highlight: highlight_options,
|
||||
body_options: body_options,
|
||||
page: page,
|
||||
per_page: 20
|
||||
}
|
||||
end
|
||||
|
||||
def keyword
|
||||
params[:keyword].to_s.strip.presence || '*'
|
||||
end
|
||||
|
||||
def highlight_options
|
||||
{
|
||||
fragment_size: EduSetting.get('es_highlight_fragment_size') || 30,
|
||||
tag: '<span class="highlight">',
|
||||
fields: {
|
||||
'*' => { type: 'plain', number_of_fragments: 3 }
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def body_options
|
||||
hash = {}
|
||||
|
||||
hash[:min_score] = (EduSetting.get('es_min_score') || 10) if keyword != '*'
|
||||
|
||||
hash
|
||||
end
|
||||
|
||||
def per_page
|
||||
per_page = params[:per_page].to_s.strip.presence || params[:limit].to_s.strip.presence
|
||||
per_page.to_i <= 0 || per_page.to_i > 20 ? 20 : per_page.to_i
|
||||
end
|
||||
|
||||
def page
|
||||
params[:page].to_i <= 0 ? 1 : params[:page].to_i
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user