Sha256: 62fd97c3448c3de26289e3910fd4c9c71b341300836d6329dc6e24e30d09e01f

Contents?: true

Size: 1.12 KB

Versions: 146

Compression:

Stored size: 1.12 KB

Contents

module PgEngine
  module ErrorHelper
    extend ActiveSupport::Concern

    # pg_engine/config/initializers/simple_form_monkey_patch.rb

    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

146 entries across 146 versions & 1 rubygems

Version Path
pg_rails-7.6.27 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.26 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.25 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.24 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.24.pre.5 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.24.pre.4 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.24.pre.3 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.23 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.22 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.22.pre.3 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.22.pre.2 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.22.pre.1 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.21 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.21.pre.11 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.21.pre.10 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.21.pre.9 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.21.pre.8 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.21.pre.7 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.21.pre.6 pg_engine/app/lib/pg_engine/error_helper.rb
pg_rails-7.6.21.pre.5 pg_engine/app/lib/pg_engine/error_helper.rb