Sha256: 5c7cb08f14c1bec28a61d5aa2a501c5b9db392a6a0e3bdec18926fbe63be97be
Contents?: true
Size: 1.58 KB
Versions: 10
Compression:
Stored size: 1.58 KB
Contents
require 'active_support' class LHS::Item < LHS::Proxy module Validation extend ActiveSupport::Concern def valid?(options = {}) options ||= {} self.errors = nil raise 'No validation endpoint found!' unless validation_endpoint record = LHS::Record.for_url(validation_endpoint.url) validation_params = validation_endpoint.options[:validates] == true ? { persist: false } : { validation_endpoint.options[:validates] => false } params = validation_endpoint.options.fetch(:params, {}) .merge(params_from_embeded_href) .merge(validation_params) begin record.request( options.merge( url: validation_endpoint.url, method: :post, params: params, body: _data.to_json, headers: { 'Content-Type' => 'application/json' } ) ) true rescue LHC::Error => e self.errors = LHS::Errors.new(e.response) false end end alias validate valid? private def validation_endpoint endpoint = embeded_endpoint if _data.href # take embeded first endpoint ||= _data._record.find_endpoint(_data._raw) validates = endpoint.options && endpoint.options.fetch(:validates, false) raise 'Endpoint does not support validations!' unless validates endpoint end def embeded_endpoint LHS::Endpoint.for_url(_data.href) end def params_from_embeded_href return {} if !_data.href || !embeded_endpoint LHC::Endpoint.values_as_params(embeded_endpoint.url, _data.href) end end end
Version data entries
10 entries across 10 versions & 1 rubygems