2017-01-31 17:13:45 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
2017-05-02 15:21:13 +00:00
|
|
|
module ApplicationModel::CanCleanupParam
|
2017-01-31 17:13:45 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
# methods defined here are going to extend the class, not the instance of it
|
|
|
|
class_methods do
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
remove all not used model attributes of params
|
|
|
|
|
|
|
|
result = Model.param_cleanup(params)
|
|
|
|
|
|
|
|
for object creation, ignore id's
|
|
|
|
|
|
|
|
result = Model.param_cleanup(params, true)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = params # params with valid attributes of model
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def param_cleanup(params, new_object = false)
|
|
|
|
|
2017-09-08 08:28:34 +00:00
|
|
|
if params.respond_to?(:permit!)
|
|
|
|
params = params.permit!.to_h
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if params.nil?
|
|
|
|
raise ArgumentError, "No params for #{self}!"
|
|
|
|
end
|
|
|
|
|
|
|
|
data = {}
|
2017-10-01 12:25:52 +00:00
|
|
|
params.each do |key, value|
|
2018-07-26 14:24:31 +00:00
|
|
|
data[key.to_s] = value
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
|
|
|
|
# ignore id for new objects
|
|
|
|
if new_object && params[:id]
|
2018-07-26 14:24:31 +00:00
|
|
|
data.delete('id')
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# only use object attributes
|
2018-07-26 14:24:31 +00:00
|
|
|
clean_params = ActiveSupport::HashWithIndifferentAccess.new
|
2017-11-23 08:09:44 +00:00
|
|
|
new.attributes.each_key do |attribute|
|
2018-07-26 14:24:31 +00:00
|
|
|
next if !data.key?(attribute)
|
2017-01-31 17:13:45 +00:00
|
|
|
|
|
|
|
# check reference records, referenced by _id attributes
|
2017-10-01 12:25:52 +00:00
|
|
|
reflect_on_all_associations.map do |assoc|
|
2017-01-31 17:13:45 +00:00
|
|
|
class_name = assoc.options[:class_name]
|
|
|
|
next if !class_name
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-07-26 14:24:31 +00:00
|
|
|
name = "#{assoc.name}_id"
|
2017-01-31 17:13:45 +00:00
|
|
|
next if !data.key?(name)
|
|
|
|
next if data[name].blank?
|
|
|
|
next if assoc.klass.lookup(id: data[name])
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-01-31 17:13:45 +00:00
|
|
|
raise ArgumentError, "Invalid value for param '#{name}': #{data[name].inspect}"
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2018-07-26 14:24:31 +00:00
|
|
|
clean_params[attribute] = data[attribute]
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
|
|
|
|
# we do want to set this via database
|
|
|
|
filter_unused_params(clean_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
remove all not used params of object (per default :updated_at, :created_at, :updated_by_id and :created_by_id)
|
|
|
|
|
2018-01-30 00:19:23 +00:00
|
|
|
if import mode is enabled, just do not used :action and :controller
|
|
|
|
|
2017-01-31 17:13:45 +00:00
|
|
|
result = Model.filter_unused_params(params)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = params # params without listed attributes
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def filter_unused_params(data)
|
2018-01-30 00:19:23 +00:00
|
|
|
params = %i[action controller updated_at created_at updated_by_id created_by_id updated_by created_by]
|
|
|
|
if Setting.get('import_mode') == true
|
|
|
|
params = %i[action controller]
|
|
|
|
end
|
|
|
|
params.each do |key|
|
2017-01-31 17:13:45 +00:00
|
|
|
data.delete(key)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-01-31 17:13:45 +00:00
|
|
|
data
|
|
|
|
end
|
2018-07-26 14:24:31 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
merge preferences param
|
|
|
|
|
|
|
|
record = Model.find(123)
|
|
|
|
|
|
|
|
new_preferences = record.param_preferences_merge(param_preferences)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def param_preferences_merge(new_params)
|
|
|
|
return new_params if new_params.blank?
|
|
|
|
return new_params if preferences.blank?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-07-26 14:24:31 +00:00
|
|
|
new_params[:preferences] = preferences.merge(new_params[:preferences] || {})
|
|
|
|
new_params
|
2017-01-31 17:13:45 +00:00
|
|
|
end
|
|
|
|
end
|