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, forest_user) ⇒ ResourceUpdater

Returns a new instance of ResourceUpdater.


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

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.


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


38
39
40
# File 'app/services/forest_liana/resource_updater.rb', line 38

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

#performObject


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

def perform
  begin
    collection_name = ForestLiana.name_for(@resource)
    scoped_records = ForestLiana::ScopeManager.apply_scopes_on_records(@resource, @user, collection_name, @params[:timezone])
    @record = scoped_records.find(@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


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

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