Sha256: 7641dd24e07be8c1b4801fc2d3bf4a3c47193a33efbf9f92150cacc0a5021791

Contents?: true

Size: 1.06 KB

Versions: 18

Compression:

Stored size: 1.06 KB

Contents

module PgEngine
  module ErrorHelper
    extend ActiveSupport::Concern

    def merge_association_errors(object, details, assoc_key)
      details = details.except(assoc_key)
      assoc_items = object.send(assoc_key).map(&:errors).map(&:details)
      merged = assoc_items.inject({}) { |acc, el| acc.merge(el) }
      merged = merged.transform_values { |errs| errs.pluck(:error) }
      details.merge(merged)
    end

    def error_types(object, associations: [])
      details = object.errors.details.transform_values do |errs|
        errs.pluck(:error)
      end
      associations.each do |assoc_key|
        next unless details.key? assoc_key

        details = merge_association_errors(object, details, assoc_key)
      end
      details.values.flatten.uniq
    end

    def error_message_for(object, associations: [])
      types = error_types(object, associations:)

      if types == [:blank]
        :only_presence_errors
      elsif types.include? :blank
        :multiple_error_types
      elsif types.present?
        :not_presence_errors
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
pg_rails-7.0.8.pre.alpha.26 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.25 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.24 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.23 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.22 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.21 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.20 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.19 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.18 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.17 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.16 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.15 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.14 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.13 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.12 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.11 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.10 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.0.8.pre.alpha.9 pg_engine/app/lib/pg_engine/error_helper.rb