Sha256: e2aed9d6485b6ee8007fbff2e7421a767acd8e10320c0fe7b9525c14d1160d86

Contents?: true

Size: 1.76 KB

Versions: 152

Compression:

Stored size: 1.76 KB

Contents

module ForestLiana
  class ResourceCreator
    attr_accessor :record
    attr_accessor :errors

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

    def perform
      begin
        if has_strong_parameter
          @record = @resource.create(resource_params)
        else
          @record = @resource.create(resource_params, without_protection: true)
        end
        set_has_many_relationships
      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, true).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.to_sym)
          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

    def has_strong_parameter
      @resource.instance_method(:update_attributes!).arity == 1
    end
  end
end

Version data entries

152 entries across 152 versions & 1 rubygems

Version Path
forest_liana-4.0.2 app/services/forest_liana/resource_creator.rb
forest_liana-4.0.1 app/services/forest_liana/resource_creator.rb
forest_liana-4.0.0 app/services/forest_liana/resource_creator.rb
forest_liana-4.0.0.pre.beta.1 app/services/forest_liana/resource_creator.rb
forest_liana-3.3.0 app/services/forest_liana/resource_creator.rb
forest_liana-4.0.0.pre.beta.0 app/services/forest_liana/resource_creator.rb
forest_liana-3.2.0 app/services/forest_liana/resource_creator.rb
forest_liana-3.1.1 app/services/forest_liana/resource_creator.rb
forest_liana-3.1.0 app/services/forest_liana/resource_creator.rb
forest_liana-3.0.7 app/services/forest_liana/resource_creator.rb
forest_liana-3.0.6 app/services/forest_liana/resource_creator.rb
forest_liana-3.0.5 app/services/forest_liana/resource_creator.rb
forest_liana-3.0.4 app/services/forest_liana/resource_creator.rb
forest_liana-3.0.3 app/services/forest_liana/resource_creator.rb
forest_liana-3.0.2 app/services/forest_liana/resource_creator.rb
forest_liana-3.0.1 app/services/forest_liana/resource_creator.rb
forest_liana-3.0.0 app/services/forest_liana/resource_creator.rb
forest_liana-3.0.0.pre.beta.18 app/services/forest_liana/resource_creator.rb
forest_liana-3.0.0.pre.beta.17 app/services/forest_liana/resource_creator.rb
forest_liana-3.0.0.pre.beta.16 app/services/forest_liana/resource_creator.rb