Sha256: 470b3c90db58f60c1f42a7c955a7e0cdeeace157aca781c7da39f4e6d7fd1b0e

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module ActionCallback
  private

  def initialize_validation_chain(mod)
    mod.define_singleton_method(:_validation_chain) do
      @_validation_chain
    end
  end

  def define_validation
    define_method(:validate) do |*args, &block|
      @_validation_chain ||= Validation::Chain.new

      options = args.last
      if options.key?(:before)
        validator_mth_name = args.first
        options[:before].each do |mth_name|
          _validation_chain.append(:before, mth_name, validator_mth_name)
          undef_method(mth_name) if included_modules.map(&:to_s).include?('ActionWithValidations')

          class_eval <<-RUBY
            module ActionWithValidations
              define_method(:#{mth_name}) do |*args, &block|
                self.class._validation_chain.before_chain_of(:#{mth_name}).each { |v| send(v) }

                should_run = true
                should_run = !errors.present? if self.class.ancestors.include?(ActiveRecord::Base)

                super(*args, &block) if should_run
              end
            end
          RUBY

          prepend self::ActionWithValidations
        end
      else
        super(*args, &block)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
action_callback-0.3.0 lib/action_callback/define_validation.rb