lib/granite/form/model/attributes/reflections/represents.rb in granite-form-0.3.0 vs lib/granite/form/model/attributes/reflections/represents.rb in granite-form-0.4.0

- old
+ new

@@ -2,17 +2,25 @@ module Form module Model module Attributes module Reflections class Represents < Attribute + def self.attribute_class + Attributes::Represents + end + def self.build(target, generated_methods, name, *args, &block) options = args.extract_options! reference = target.reflect_on_association(options[:of]) if target.respond_to?(:reflect_on_association) reference ||= target.reflect_on_attribute(options[:of]) if target.respond_to?(:reflect_on_attribute) options[:of] = reference.name if reference - validates_nested = target.respond_to?(:validates_nested) && !target.validates_nested?(options[:of]) - target.validates_nested(options[:of]) if validates_nested + + if options.fetch(:validate_reference, true) + validates_nested = target.respond_to?(:validates_nested) && !target.validates_nested?(options[:of]) + target.validates_nested(options[:of]) if validates_nested + target.validates_presence_of(options[:of]) unless target.validates_presence?(options[:of]) + end super(target, generated_methods, name, *args, options, &block) end def initialize(name, options)