Sha256: dd0ef989d2ca6b7436673b49e958d9da7597f3fa66fa6a68f68ffc11d9ece708

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

module Authenticate
  module Modules
    extend ActiveSupport::Concern

    # Methods to help your user model load Authenticate modules
    module ClassMethods

      def load_modules
        constants = []
        Authenticate.configuration.modules.each do |mod|
          puts "load_modules about to load #{mod.to_s}"
          require "authenticate/model/#{mod.to_s}" if mod.is_a?(Symbol)
          mod = load_constant(mod) if mod.is_a?(Symbol)
          constants << mod
        end
        check_fields constants
        constants.each { |mod|
          include mod
        }
      end

      private

      def load_constant module_symbol
        Authenticate::Model.const_get(module_symbol.to_s.classify)
      end

      # For each module, look at the fields it requires. Ensure the User
      # model including the module has the required fields. If it does not
      # have all required fields, huck an exception.
      def check_fields modules
        failed_attributes = []
        instance = self.new
        modules.each do |mod|
          if mod.respond_to?(:required_fields)
            mod.required_fields(self).each do |field|
              failed_attributes << field unless instance.respond_to?(field)
            end
          end
        end

        if failed_attributes.any?
          fail MissingAttribute.new(failed_attributes)
        end
      end

    end

    class MissingAttribute < StandardError
      def initialize(attributes)
        @attributes = attributes
      end

      def message
        "The following attribute(s) is (are) missing on your model: #{@attributes.join(", ")}"
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
authenticate-0.1.0 lib/authenticate/modules.rb