Sha256: abe355208fa84b1c50bd15d8d7dcd6094a953451e19ac8b9f8390780595cf8d0

Contents?: true

Size: 1.4 KB

Versions: 2

Compression:

Stored size: 1.4 KB

Contents

module RealtimeValidations

  class ValidationsController < ApplicationController

    include Controller

    def validate
      model_name = params[:model]
      request.path =~ /^\/validations(.+)$/
      request_path = $1
      args = remove_unused_args Rails.application.routes.recognize_path(request_path)
      field = params[:field]
      field =~ /^[^\[]+\[(\w+)\]$/
      field = $1
      value = params[:value]
      validates = params[:validates]
      if args.has_key? :id
        model = Object.const_get("#{model_name}".camelize.to_sym).find args[:id]
        args.delete :id
      else
        model = Object.const_get("#{model_name}".camelize.to_sym).new
      end
      args.each do |key, value|
        full_key = key.to_s.singularize
        model.send "#{full_key}=", value if model.respond_to? full_key.to_sym
      end
      model.send "#{field}=", value
      model.send "#{field}_confirmation=", validates if validates
      before_model_validation model if respond_to? :before_model_validation
      begin
        model.valid?
      rescue
      end
      errors = model.errors[field.to_sym].uniq
      respond_to do |format|
        format.json { render :json => { :field => params[:field],
                                        :errors => errors } }
      end
    end

    private

    def remove_unused_args(args)
      [:action, :controller].each { |key| args.delete key }
      args
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
realtime-validations-0.0.2 app/controllers/realtime_validations/validations_controller.rb
realtime-validations-0.0.1 app/controllers/realtime_validations/validations_controller.rb