Sha256: c7ec22401e9986aa97d8c4cd331d8d52d3dadbba312c26e98803ecfb5ef69e58

Contents?: true

Size: 1.27 KB

Versions: 115

Compression:

Stored size: 1.27 KB

Contents

module ForestLiana
  class ResourceUpdater
    attr_accessor :record
    attr_accessor :errors

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

    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

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

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

Version data entries

115 entries across 115 versions & 1 rubygems

Version Path
forest_liana-9.7.0 app/services/forest_liana/resource_updater.rb
forest_liana-9.6.4 app/services/forest_liana/resource_updater.rb
forest_liana-9.6.3 app/services/forest_liana/resource_updater.rb
forest_liana-9.6.0 app/services/forest_liana/resource_updater.rb
forest_liana-9.5.7 app/services/forest_liana/resource_updater.rb
forest_liana-9.5.6 app/services/forest_liana/resource_updater.rb
forest_liana-9.5.5 app/services/forest_liana/resource_updater.rb
forest_liana-9.5.4 app/services/forest_liana/resource_updater.rb
forest_liana-9.5.3 app/services/forest_liana/resource_updater.rb
forest_liana-9.5.2 app/services/forest_liana/resource_updater.rb
forest_liana-9.5.1 app/services/forest_liana/resource_updater.rb
forest_liana-9.5.0 app/services/forest_liana/resource_updater.rb
forest_liana-9.4.3 app/services/forest_liana/resource_updater.rb
forest_liana-9.4.2 app/services/forest_liana/resource_updater.rb
forest_liana-9.4.1 app/services/forest_liana/resource_updater.rb
forest_liana-9.4.0 app/services/forest_liana/resource_updater.rb
forest_liana-8.3.3 app/services/forest_liana/resource_updater.rb
forest_liana-9.3.16 app/services/forest_liana/resource_updater.rb
forest_liana-9.3.15 app/services/forest_liana/resource_updater.rb
forest_liana-9.3.14 app/services/forest_liana/resource_updater.rb