Sha256: 1732eaef7dac0db39461a2b654bdb26acc3f5e05d5aa1e1f17b956cfb9024587

Contents?: true

Size: 1.06 KB

Versions: 25

Compression:

Stored size: 1.06 KB

Contents

module ForestLiana
  class BelongsToUpdater
    attr_accessor :errors

    def initialize(resource, association, params)
      @resource = resource
      @association = association
      @params = params
      @data = params['data']
      @errors = nil
    end

    def perform
      begin
        @record = @resource.find(@params[:id])
        if (SchemaUtils.polymorphic?(@association))
          if @data.nil?
            new_value = nil
          else
            association_klass = SchemaUtils.polymorphic_models(@association).select { |a| a.name.downcase == @data[:type] }.first
            new_value = association_klass.find(@data[:id]) if @data && @data[:id]
          end
        else
          new_value = @association.klass.find(@data[:id]) if @data && @data[:id]
        end
        @record.send("#{@association.name}=", new_value)

        @record.save
      rescue ActiveRecord::SerializationTypeMismatch => exception
        @errors = [{ detail: exception.message }]
      rescue => exception
        @errors = [{ detail: exception.message }]
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
forest_liana-9.3.3 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.3.2 app/services/forest_liana/belongs_to_updater.rb
forest_liana-8.3.1 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.3.1 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.3.0 app/services/forest_liana/belongs_to_updater.rb
forest_liana-8.3.0 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.2.3 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.2.2 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.2.1 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.2.0 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.1.10 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.1.9 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.1.8 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.1.7 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.1.6 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.1.5 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.1.4 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.1.3 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.1.2 app/services/forest_liana/belongs_to_updater.rb
forest_liana-9.1.1 app/services/forest_liana/belongs_to_updater.rb