can_cleanup_param.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. module ApplicationModel::CanCleanupParam
  3. extend ActiveSupport::Concern
  4. # methods defined here are going to extend the class, not the instance of it
  5. class_methods do
  6. =begin
  7. remove all not used model attributes of params
  8. result = Model.param_cleanup(params)
  9. for object creation, ignore id's
  10. result = Model.param_cleanup(params, true)
  11. returns
  12. result = params # params with valid attributes of model
  13. =end
  14. def param_cleanup(params, new_object = false)
  15. if params.respond_to?(:permit!)
  16. params = params.permit!.to_h
  17. end
  18. if params.nil?
  19. raise ArgumentError, "No params for #{self}!"
  20. end
  21. data = {}
  22. params.each do |key, value|
  23. data[key.to_s] = value
  24. end
  25. # ignore id for new objects
  26. if new_object && params[:id]
  27. data.delete('id')
  28. end
  29. # only use object attributes
  30. clean_params = ActiveSupport::HashWithIndifferentAccess.new
  31. new.attributes.each_key do |attribute|
  32. next if !data.key?(attribute)
  33. # check reference records, referenced by _id attributes
  34. reflect_on_all_associations.map do |assoc|
  35. class_name = assoc.options[:class_name]
  36. next if !class_name
  37. name = "#{assoc.name}_id"
  38. next if !data.key?(name)
  39. next if data[name].blank?
  40. next if assoc.klass.lookup(id: data[name])
  41. raise ArgumentError, "Invalid value for param '#{name}': #{data[name].inspect}"
  42. end
  43. clean_params[attribute] = data[attribute]
  44. end
  45. # we do want to set this via database
  46. filter_unused_params(clean_params)
  47. end
  48. private
  49. =begin
  50. remove all not used params of object (per default :updated_at, :created_at, :updated_by_id and :created_by_id)
  51. if import mode is enabled, just do not used :action and :controller
  52. result = Model.filter_unused_params(params)
  53. returns
  54. result = params # params without listed attributes
  55. =end
  56. def filter_unused_params(data)
  57. params = %i[action controller updated_at created_at updated_by_id created_by_id updated_by created_by]
  58. if Setting.get('import_mode') == true
  59. params = %i[action controller]
  60. end
  61. params.each do |key|
  62. data.delete(key)
  63. end
  64. data
  65. end
  66. end
  67. =begin
  68. merge preferences param
  69. record = Model.find(123)
  70. new_preferences = record.param_preferences_merge(param_preferences)
  71. =end
  72. def param_preferences_merge(new_params)
  73. return new_params if new_params.blank?
  74. return new_params if preferences.blank?
  75. new_params[:preferences] = preferences.merge(new_params[:preferences] || {})
  76. new_params
  77. end
  78. end