summaryrefslogtreecommitdiff
path: root/lib/bulk_imports/common/transformers/prohibited_attributes_transformer.rb
blob: 858c4c8976b20638eadb7838277bc12fccf33819 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

module BulkImports
  module Common
    module Transformers
      class ProhibitedAttributesTransformer
        PROHIBITED_REFERENCES = Regexp.union(
          /\Acached_markdown_version\Z/,
          /\Aid\Z/,
          /_id\Z/,
          /_ids\Z/,
          /_html\Z/,
          /attributes/,
          /\Aremote_\w+_(url|urls|request_header)\Z/ # carrierwave automatically creates these attribute methods for uploads
        ).freeze

        def initialize(options = {})
          @options = options
        end

        def transform(context, data)
          data.each_with_object({}) do |(key, value), result|
            prohibited = prohibited_key?(key)

            unless prohibited
              result[key] = value.is_a?(Hash) ? transform(context, value) : value
            end
          end
        end

        private

        def prohibited_key?(key)
          key.to_s =~ PROHIBITED_REFERENCES
        end
      end
    end
  end
end