Class: ForestLiana::ResourceDeserializer

Inherits:
Object
  • Object
show all
Defined in:
app/deserializers/forest_liana/resource_deserializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(resource, params, with_relationships) ⇒ ResourceDeserializer

Returns a new instance of ResourceDeserializer.



6
7
8
9
10
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 6

def initialize(resource, params, with_relationships)
  @params = params.permit! if params.respond_to?(:permit!)
  @resource = resource
  @with_relationships = with_relationships
end

Instance Method Details

#acts_as_taggable_attribute?(attr) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 189

def acts_as_taggable_attribute?(attr)
  @resource.acts_as_taggable.to_a.include?(attr)
end

#attributes_serializedObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 170

def attributes_serialized
  if Rails::VERSION::MAJOR >= 5
    @attributes.select do |attribute|
      @resource.type_for_attribute(attribute).class ==
        ::ActiveRecord::Type::Serialized
    end
  else
    # NOTICE: Silent deprecation warnings for removed
    #         "serialized_attributes" in Rails 5
    ActiveSupport::Deprecation.silence do
      @resource.serialized_attributes
    end
  end
end

#carrierwave_attribute?(attr) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 185

def carrierwave_attribute?(attr)
  @resource.uploaders.include?(attr.try(:to_sym))
end

#column?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 166

def column?(attribute)
  @resource.columns.find { |column| column.name == attribute }.present?
end

#extract_acts_as_taggableObject



122
123
124
125
126
127
128
129
130
131
132
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 122

def extract_acts_as_taggable
  return if @params['data']['attributes'].blank?
  return unless has_acts_as_taggable?

  @params['data']['attributes'].each do |key, value|
    if acts_as_taggable_attribute?(key)
      @attributes["#{key.singularize}_list"] = value
      @attributes.delete(key)
    end
  end
end

#extract_attributesObject



25
26
27
28
29
30
31
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 25

def extract_attributes
  if @params[:data][:attributes]
    @params['data']['attributes'].select { |attribute| column?(attribute) }
  else
    ActionController::Parameters.new()
  end
end

#extract_attributes_serializeObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 33

def extract_attributes_serialize
  attributes_serialized.each do |attribute, serializer|
    value = @params[:data][:attributes][attribute]
    begin
      if @attributes.has_key?(attribute)
        @attributes[attribute] = value.nil? ? nil : JSON::parse(value)
      end
    rescue
      message = "Bad format for '#{attribute}' attribute value."
      raise ForestLiana::Errors::SerializeAttributeBadFormat.new(message)
    end
  end
end

#extract_carrierwaveObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 107

def extract_carrierwave
  return if @params['data']['attributes'].blank?
  return unless @resource.respond_to?(:uploaders)

  @params['data']['attributes'].each do |key, value|
    if value && carrierwave_attribute?(key)
      if value.match(/\Adata:\w+\/.+;base64,.+/)
        @attributes[key] = ForestLiana::Base64StringIO.new(value)
      else
        @attributes.delete(key)
      end
    end
  end
end

#extract_deviseObject



146
147
148
149
150
151
152
153
154
155
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 146

def extract_devise
  return if @params['data']['attributes'].blank?
  return unless has_devise?

  if @params['data']['attributes']['password'] == '**********'
    @params['data']['attributes'].delete('password')
  elsif @params['data']['attributes']['password'].present?
    @attributes['password'] = @params['data']['attributes']['password']
  end
end

#extract_paperclipObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 83

def extract_paperclip
  return if @params['data']['attributes'].blank?
  return unless @resource.respond_to?(:attachment_definitions)

  paperclip_attr = @params['data']['attributes']
    .select do |attr|
      !column?(attr) &&
        paperclip_handler?(@params['data']['attributes'][attr])
    end

  if paperclip_attr
    # NOTICE: Force to set the file_name attribute to support
    #         validates_attachment_file_name option
    array_keys = paperclip_attr.keys
    array_keys.each do |key|
      extension = /\Adata:image\/([a-z]+);base64/.match(paperclip_attr[key])
        .try(:[], 1)
      paperclip_attr["#{key}_file_name"] = "image.#{extension}"
    end

    @attributes.merge!(paperclip_attr)
  end
end

#extract_relationshipsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 47

def extract_relationships
  if @params['data']['relationships']
    @params['data']['relationships'].each do |name, relationship|
      data = relationship['data']
      # Rails 3 requires a :sym argument for the reflect_on_association
      # call.
      association = @resource.reflect_on_association(name.try(:to_sym))

      if [:has_one, :belongs_to].include?(association.try(:macro))
        # TODO: refactor like this?
        #if data.blank?
          #@attributes[name] = nil
        #else
          #@attributes[name] = association.klass.find(data[:id])
        #end

        # ActionController::Parameters do not inherit from Hash anymore
        # since Rails 5.
        if (data.is_a?(Hash) || data.is_a?(ActionController::Parameters)) && data[:id]
          if (SchemaUtils.polymorphic?(association))
            @attributes[association.foreign_key] = data[:id]
            @attributes[association.foreign_type] = data[:type]
          else
            @attributes[name] = association.klass.find(data[:id])
          end
        elsif data.blank?
          @attributes[name] = nil
        end
      end
    end

    # Strong parameter permit all new relationships attributes.
    @attributes = @attributes.permit! if @attributes.respond_to?(:permit!)
  end
end

#extract_smart_fields_valuesObject



134
135
136
137
138
139
140
141
142
143
144
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 134

def extract_smart_fields_values
  # NOTICE: Look for some Smart Field setters and apply them if any.
  ForestLiana.schema_for_resource(@resource).fields.each do |field|
    if field.try(:[], :set) &&
      @params['data']['attributes'].has_key?(field[:field])
      # WARNING: The Smart Fields setters may override other changes.
      @attributes = field[:set].call(@attributes,
        @params['data']['attributes'][field[:field]])
    end
  end
end

#has_acts_as_taggable?Boolean

Returns:

  • (Boolean)


193
194
195
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 193

def has_acts_as_taggable?
  @resource.try(:taggable?)
end

#has_devise?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 197

def has_devise?
  @resource.respond_to?(:devise_modules?)
end

#paperclip_handler?(attr) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
160
161
162
163
164
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 157

def paperclip_handler?(attr)
  begin
    Paperclip.io_adapters.handler_for(attr)
    true
  rescue Paperclip::AdapterRegistry::NoHandlerError
    false
  end
end

#performObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/deserializers/forest_liana/resource_deserializer.rb', line 12

def perform
  @attributes = extract_attributes
  extract_attributes_serialize
  extract_relationships if @with_relationships
  extract_paperclip
  extract_carrierwave
  extract_acts_as_taggable
  extract_smart_fields_values
  extract_devise

  @attributes
end