Sha256: d7c0e1a133079d424a2f1f993e86c79ec8f6ab4763f7df3117942126172b040e

Contents?: true

Size: 1.31 KB

Versions: 16

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require 'swagger/blocks'

module Apicasso
  # This class is injected into `ActiveRecord` to enable Swagger docs to be
  # generated automatically based on schema and I18n definitions in your
  # own application.
  module ActiveRecordExtension
    extend ActiveSupport::Concern
    # Module with class methods of Apicasso
    module ClassMethods
      # Method that map validations for consumption on the Swagger JSON
      # @param validation [Array] a validator to be checked
      # @returns [Array] All validated attributes
      def validated_attrs_for(validation)
        if validation.is_a?(String) || validation.is_a?(Symbol)
          klass = 'ActiveRecord::Validations::' \
                  "#{validation.to_s.camelize}Validator"
          validation = klass.constantize
        end
        validators.select { |v| v.is_a?(validation) }
                  .map(&:attributes)
                  .flatten
                  .map(&:to_sym)
      end

      def presence_validators?
        presence_validators.present?
      end

      # Method that lists all presence validators
      def presence_validators
        validated_attrs_for(:presence)
      end
    end
  end
end

# Include the extension to avoid including on all files mannually
ActiveRecord::Base.send(:include, Apicasso::ActiveRecordExtension)

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
apicasso-0.7.3 lib/apicasso/active_record_extension.rb
apicasso-0.7.2 lib/apicasso/active_record_extension.rb
apicasso-0.7.1 lib/apicasso/active_record_extension.rb
apicasso-0.7.0 lib/apicasso/active_record_extension.rb
apicasso-0.6.8 lib/apicasso/active_record_extension.rb
apicasso-0.6.7 lib/apicasso/active_record_extension.rb
apicasso-0.6.6 lib/apicasso/active_record_extension.rb
apicasso-0.6.5 lib/apicasso/active_record_extension.rb
apicasso-0.6.4 lib/apicasso/active_record_extension.rb
apicasso-0.6.3 lib/apicasso/active_record_extension.rb
apicasso-0.6.2 lib/apicasso/active_record_extension.rb
apicasso-0.6.1 lib/apicasso/active_record_extension.rb
apicasso-0.6.0 lib/apicasso/active_record_extension.rb
apicasso-0.5.2 lib/apicasso/active_record_extension.rb
apicasso-0.5.1 lib/apicasso/active_record_extension.rb
apicasso-0.5.0 lib/apicasso/active_record_extension.rb