Class: ForestLiana::ResourceUpdater

Inherits:
Object
  • Object
show all
Defined in:
app/services/forest_liana/resource_updater.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, params) ⇒ ResourceUpdater

Returns a new instance of ResourceUpdater.



6
7
8
9
10
# File 'app/services/forest_liana/resource_updater.rb', line 6

def initialize(resource, params)
  @resource = resource
  @params = params
  @errors = nil
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



4
5
6
# File 'app/services/forest_liana/resource_updater.rb', line 4

def errors
  @errors
end

#recordObject

Returns the value of attribute record.



3
4
5
# File 'app/services/forest_liana/resource_updater.rb', line 3

def record
  @record
end

Instance Method Details

#has_strong_parameterObject



35
36
37
# File 'app/services/forest_liana/resource_updater.rb', line 35

def has_strong_parameter
  Rails::VERSION::MAJOR > 5 || @resource.instance_method(:update_attributes!).arity == 1
end

#performObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/forest_liana/resource_updater.rb', line 12

def perform
  begin
    @record = @resource.find(@params[:id])

    if has_strong_parameter
      @record.update_attributes(resource_params)
    else
      @record.update_attributes(resource_params, without_protection: true)
    end
  rescue ActiveRecord::StatementInvalid => exception
    # NOTICE: SQL request cannot be executed properly
    @errors = [{ detail: exception.cause.error }]
  rescue ForestLiana::Errors::SerializeAttributeBadFormat => exception
    @errors = [{ detail: exception.message }]
  rescue => exception
    @errors = [{ detail: exception.message }]
  end
end

#resource_paramsObject



31
32
33
# File 'app/services/forest_liana/resource_updater.rb', line 31

def resource_params
  ResourceDeserializer.new(@resource, @params, false).perform
end