2014-04-28 16:31:51 +08:00
|
|
|
module HasCustomFields
|
|
|
|
extend ActiveSupport::Concern
|
2014-08-18 19:04:08 +08:00
|
|
|
|
2014-06-17 10:42:12 +08:00
|
|
|
module Helpers
|
2014-08-18 19:04:08 +08:00
|
|
|
|
2014-06-17 10:42:12 +08:00
|
|
|
def self.append_field(target, key, value, types)
|
|
|
|
if target.has_key?(key)
|
|
|
|
target[key] = [target[key]] if !target[key].is_a? Array
|
2018-05-22 14:48:39 +08:00
|
|
|
target[key] << cast_custom_field(key, value, types, _return_array = false)
|
2014-06-17 10:42:12 +08:00
|
|
|
else
|
|
|
|
target[key] = cast_custom_field(key, value, types)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
CUSTOM_FIELD_TRUE ||= ['1', 't', 'true', 'T', 'True', 'TRUE'].freeze
|
|
|
|
|
|
|
|
def self.get_custom_field_type(types, key)
|
|
|
|
return unless types
|
|
|
|
|
|
|
|
sorted_types = types.keys.select { |k| k.end_with?("*") }
|
2017-07-28 09:20:09 +08:00
|
|
|
.sort_by(&:length)
|
|
|
|
.reverse
|
2015-04-24 01:33:29 +08:00
|
|
|
|
|
|
|
sorted_types.each do |t|
|
|
|
|
return types[t] if key =~ /^#{t}/i
|
|
|
|
end
|
|
|
|
|
|
|
|
types[key]
|
|
|
|
end
|
2014-06-17 10:42:12 +08:00
|
|
|
|
2018-05-22 14:48:39 +08:00
|
|
|
def self.cast_custom_field(key, value, types, return_array = true)
|
2015-04-24 01:33:29 +08:00
|
|
|
return value unless type = get_custom_field_type(types, key)
|
2014-06-17 10:42:12 +08:00
|
|
|
|
2018-05-22 14:48:39 +08:00
|
|
|
array = nil
|
|
|
|
|
|
|
|
if Array === type
|
|
|
|
type = type[0]
|
|
|
|
array = true if return_array
|
2014-06-17 10:42:12 +08:00
|
|
|
end
|
2018-05-22 14:48:39 +08:00
|
|
|
|
|
|
|
result =
|
|
|
|
case type
|
|
|
|
when :boolean then !!CUSTOM_FIELD_TRUE.include?(value)
|
|
|
|
when :integer then value.to_i
|
2018-09-13 15:59:17 +08:00
|
|
|
when :json then parse_json_value(value, key)
|
2018-05-22 14:48:39 +08:00
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
|
|
|
|
array ? [result] : result
|
2014-06-17 10:42:12 +08:00
|
|
|
end
|
2018-09-13 15:59:17 +08:00
|
|
|
|
|
|
|
def self.parse_json_value(value, key)
|
|
|
|
::JSON.parse(value)
|
|
|
|
rescue JSON::ParserError
|
|
|
|
Rails.logger.warn("Value '#{value}' for custom field '#{key}' is not json, it is being ignored.")
|
|
|
|
{}
|
|
|
|
end
|
2014-06-17 10:42:12 +08:00
|
|
|
end
|
2014-04-28 16:31:51 +08:00
|
|
|
|
|
|
|
included do
|
2017-07-28 09:20:09 +08:00
|
|
|
has_many :_custom_fields, dependent: :destroy, class_name: "#{name}CustomField"
|
2014-04-28 16:31:51 +08:00
|
|
|
after_save :save_custom_fields
|
2014-05-15 02:38:04 +08:00
|
|
|
|
2015-08-05 14:01:52 +08:00
|
|
|
attr_accessor :preloaded_custom_fields
|
|
|
|
|
2015-04-24 01:33:29 +08:00
|
|
|
# To avoid n+1 queries, use this function to retrieve lots of custom fields in one go
|
|
|
|
# and create a "sideloaded" version for easy querying by id.
|
2014-05-15 02:38:04 +08:00
|
|
|
def self.custom_fields_for_ids(ids, whitelisted_fields)
|
|
|
|
klass = "#{name}CustomField".constantize
|
|
|
|
foreign_key = "#{name.underscore}_id".to_sym
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
|
|
|
|
return result if whitelisted_fields.blank?
|
2015-04-24 01:33:29 +08:00
|
|
|
|
|
|
|
klass.where(foreign_key => ids, :name => whitelisted_fields)
|
2017-07-28 09:20:09 +08:00
|
|
|
.pluck(foreign_key, :name, :value).each do |cf|
|
2014-05-15 02:38:04 +08:00
|
|
|
result[cf[0]] ||= {}
|
2014-06-17 10:42:12 +08:00
|
|
|
append_custom_field(result[cf[0]], cf[1], cf[2])
|
2014-05-15 02:38:04 +08:00
|
|
|
end
|
2015-04-24 01:33:29 +08:00
|
|
|
|
2014-05-15 02:38:04 +08:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2014-06-17 10:42:12 +08:00
|
|
|
def self.append_custom_field(target, key, value)
|
2015-04-24 01:33:29 +08:00
|
|
|
HasCustomFields::Helpers.append_field(target, key, value, @custom_field_types)
|
2014-05-15 02:38:04 +08:00
|
|
|
end
|
2014-06-17 10:42:12 +08:00
|
|
|
|
|
|
|
def self.register_custom_field_type(name, type)
|
|
|
|
@custom_field_types ||= {}
|
|
|
|
@custom_field_types[name] = type
|
|
|
|
end
|
|
|
|
|
2018-09-13 15:59:17 +08:00
|
|
|
def self.get_custom_field_type(name)
|
|
|
|
@custom_field_types ||= {}
|
|
|
|
@custom_field_types[name]
|
|
|
|
end
|
|
|
|
|
2015-08-05 14:01:52 +08:00
|
|
|
def self.preload_custom_fields(objects, fields)
|
|
|
|
if objects.present?
|
|
|
|
map = {}
|
|
|
|
|
|
|
|
empty = {}
|
|
|
|
fields.each do |field|
|
|
|
|
empty[field] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
objects.each do |obj|
|
|
|
|
map[obj.id] = obj
|
|
|
|
obj.preloaded_custom_fields = empty.dup
|
|
|
|
end
|
|
|
|
|
|
|
|
fk = (name.underscore << "_id")
|
|
|
|
|
2015-08-05 14:09:10 +08:00
|
|
|
"#{name}CustomField".constantize
|
|
|
|
.where("#{fk} in (?)", map.keys)
|
|
|
|
.where("name in (?)", fields)
|
2015-08-05 14:01:52 +08:00
|
|
|
.pluck(fk, :name, :value).each do |id, name, value|
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
preloaded = map[id].preloaded_custom_fields
|
2015-08-05 14:01:52 +08:00
|
|
|
|
|
|
|
if preloaded[name].nil?
|
|
|
|
preloaded.delete(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
HasCustomFields::Helpers.append_field(preloaded, name, value, @custom_field_types)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2014-04-28 16:31:51 +08:00
|
|
|
end
|
|
|
|
|
2014-04-30 01:23:13 +08:00
|
|
|
def reload(options = nil)
|
2015-05-07 00:52:09 +08:00
|
|
|
clear_custom_fields
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2015-08-05 14:01:52 +08:00
|
|
|
def custom_field_preloaded?(name)
|
|
|
|
@preloaded_custom_fields && @preloaded_custom_fields.key?(name)
|
|
|
|
end
|
|
|
|
|
2015-05-07 00:52:09 +08:00
|
|
|
def clear_custom_fields
|
2014-04-30 01:23:13 +08:00
|
|
|
@custom_fields = nil
|
|
|
|
@custom_fields_orig = nil
|
|
|
|
end
|
|
|
|
|
2015-08-05 14:01:52 +08:00
|
|
|
class PreloadedProxy
|
|
|
|
def initialize(preloaded)
|
|
|
|
@preloaded = preloaded
|
|
|
|
end
|
|
|
|
|
|
|
|
def [](key)
|
|
|
|
if @preloaded.key?(key)
|
|
|
|
@preloaded[key]
|
|
|
|
else
|
|
|
|
# for now you can not mix preload an non preload, it better just to fail
|
|
|
|
raise StandardError, "Attempting to access a non preloaded custom field, this is disallowed to prevent N+1 queries."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-28 16:31:51 +08:00
|
|
|
def custom_fields
|
2015-08-05 14:01:52 +08:00
|
|
|
if @preloaded_custom_fields
|
|
|
|
return @preloaded_proxy ||= PreloadedProxy.new(@preloaded_custom_fields)
|
|
|
|
end
|
|
|
|
|
2014-04-28 16:31:51 +08:00
|
|
|
@custom_fields ||= refresh_custom_fields_from_db.dup
|
|
|
|
end
|
|
|
|
|
|
|
|
def custom_fields=(data)
|
|
|
|
custom_fields.replace(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def custom_fields_clean?
|
2015-04-24 01:33:29 +08:00
|
|
|
# Check whether the cached version has been changed on this model
|
2014-04-28 16:31:51 +08:00
|
|
|
!@custom_fields || @custom_fields_orig == @custom_fields
|
|
|
|
end
|
|
|
|
|
2018-03-03 01:45:34 +08:00
|
|
|
# `upsert_custom_fields` will only insert/update existing fields, and will not
|
|
|
|
# delete anything. It is safer under concurrency and is recommended when
|
|
|
|
# you just want to attach fields to things without maintaining a specific
|
|
|
|
# set of fields.
|
|
|
|
def upsert_custom_fields(fields)
|
|
|
|
fields.each do |k, v|
|
|
|
|
row_count = _custom_fields.where(name: k).update_all(value: v)
|
|
|
|
if row_count == 0
|
|
|
|
_custom_fields.create!(name: k, value: v)
|
|
|
|
end
|
|
|
|
custom_fields[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-28 09:20:09 +08:00
|
|
|
def save_custom_fields(force = false)
|
2015-04-26 06:12:19 +08:00
|
|
|
if force || !custom_fields_clean?
|
2014-04-28 16:31:51 +08:00
|
|
|
dup = @custom_fields.dup
|
|
|
|
array_fields = {}
|
|
|
|
|
2018-10-18 12:23:04 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
_custom_fields.reload.each do |f|
|
|
|
|
if dup[f.name].is_a?(Array)
|
|
|
|
# we need to collect Arrays fully before we can compare them
|
|
|
|
if !array_fields.has_key?(f.name)
|
|
|
|
array_fields[f.name] = [f]
|
|
|
|
else
|
|
|
|
array_fields[f.name] << f
|
|
|
|
end
|
|
|
|
elsif dup[f.name].is_a?(Hash)
|
|
|
|
if dup[f.name].to_json != f.value
|
|
|
|
f.destroy!
|
|
|
|
else
|
|
|
|
dup.delete(f.name)
|
|
|
|
end
|
2015-04-26 06:12:19 +08:00
|
|
|
else
|
2018-10-18 12:23:04 +08:00
|
|
|
t = {}
|
|
|
|
self.class.append_custom_field(t, f.name, f.value)
|
2017-08-17 05:04:40 +08:00
|
|
|
|
2018-10-18 12:23:04 +08:00
|
|
|
if dup[f.name] != t[f.name]
|
|
|
|
f.destroy!
|
|
|
|
else
|
|
|
|
dup.delete(f.name)
|
|
|
|
end
|
2014-04-28 16:31:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-18 12:23:04 +08:00
|
|
|
# let's iterate through our arrays and compare them
|
|
|
|
array_fields.each do |field_name, fields|
|
|
|
|
if fields.length == dup[field_name].length && fields.map(&:value) == dup[field_name]
|
|
|
|
dup.delete(field_name)
|
|
|
|
else
|
|
|
|
fields.each(&:destroy!)
|
|
|
|
end
|
2014-04-28 16:31:51 +08:00
|
|
|
end
|
|
|
|
|
2018-10-18 12:23:04 +08:00
|
|
|
dup.each do |k, v|
|
|
|
|
field_type = self.class.get_custom_field_type(k)
|
2018-09-13 15:59:17 +08:00
|
|
|
|
2018-10-18 12:23:04 +08:00
|
|
|
if v.is_a?(Array) && field_type != :json
|
|
|
|
v.each { |subv| _custom_fields.create!(name: k, value: subv) }
|
|
|
|
else
|
|
|
|
_custom_fields.create!(
|
|
|
|
name: k,
|
|
|
|
value: v.is_a?(Hash) || field_type == :json ? v.to_json : v
|
|
|
|
)
|
|
|
|
end
|
2014-04-28 16:31:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
refresh_custom_fields_from_db
|
|
|
|
end
|
|
|
|
end
|
2015-01-30 06:17:32 +08:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def refresh_custom_fields_from_db
|
|
|
|
target = Hash.new
|
2017-11-16 12:13:58 +08:00
|
|
|
_custom_fields.order('id asc').pluck(:name, :value).each do |key, value|
|
2015-01-30 06:17:32 +08:00
|
|
|
self.class.append_custom_field(target, key, value)
|
|
|
|
end
|
|
|
|
@custom_fields_orig = target
|
|
|
|
@custom_fields = @custom_fields_orig.dup
|
|
|
|
end
|
|
|
|
|
2014-05-15 02:38:04 +08:00
|
|
|
end
|