Sha256: 5b0c11db2be1332c47da267b9372d26dfe4c6974b6e35dfc2899c9fd3943b116

Contents?: true

Size: 1.13 KB

Versions: 21

Compression:

Stored size: 1.13 KB

Contents

module ForestLiana
  class ResourceUpdater
    attr_accessor :record

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

    def perform
      @record = @resource.find(@params[:id])
      @record.update_attributes!(resource_params)
      set_has_many_relationships
    end

    def resource_params
      ResourceDeserializer.new(@resource, @params[:resource]).perform
    end

    def set_has_many_relationships
      if @params['data']['relationships']
        @params['data']['relationships'].each do |name, relationship|
          data = relationship['data']
          association = @resource.reflect_on_association(name)
          if [:has_many, :has_and_belongs_to_many].include?(
            association.try(:macro))
            if data.is_a?(Array)
              data.each do |x|
                existing_records = @record.send(name)
                new_record = association.klass.find(x[:id])
                if !existing_records.include?(new_record)
                  existing_records << new_record
                end
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
forest_liana-1.3.26 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.25 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.24 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.23 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.22 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.21 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.20 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.19 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.18 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.17 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.16 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.15 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.14 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.13 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.12 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.11 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.10 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.9 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.8 app/services/forest_liana/resource_updater.rb
forest_liana-1.3.7 app/services/forest_liana/resource_updater.rb