Class: ForestLiana::ResourceUpdater

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, params, forest_user) ⇒ ResourceUpdater

Returns a new instance of ResourceUpdater.



8
9
10
11
12
13
# File 'app/services/forest_liana/resource_updater.rb', line 8

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

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



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

def errors
  @errors
end

#recordObject

Returns the value of attribute record.



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

def record
  @record
end

Instance Method Details

#has_strong_parameterObject



40
41
42
# File 'app/services/forest_liana/resource_updater.rb', line 40

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

#performObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/forest_liana/resource_updater.rb', line 15

def perform
  begin
    collection_name = ForestLiana.name_for(@resource)
    scoped_records = ForestLiana::ScopeManager.apply_scopes_on_records(@resource, @user, collection_name, @params[:timezone])
    @record = find_record(scoped_records, @resource, @params[:id])

    if has_strong_parameter
      @record.update(resource_params)
    else
      @record.update(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



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

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