lib/hobo_fields/model.rb in hobo_fields-2.1.2 vs lib/hobo_fields/model.rb in hobo_fields-2.2.0

- old
+ new

@@ -96,11 +96,11 @@ column_options[:comment] = options.delete(:comment) if options.has_key?(:comment) index_options = {} index_options[:name] = options.delete(:index) if options.has_key?(:index) bt = belongs_to_without_field_declarations(name, *args, &block) - refl = reflections[name.to_sym] + refl = reflections[name.to_s] fkey = refl.foreign_key declare_field(fkey.to_sym, :integer, column_options) if refl.options[:polymorphic] declare_polymorphic_type_field(name, column_options) index(["#{name}_type", fkey], index_options) if index_options[:name]!=false @@ -155,17 +155,19 @@ # field declaration def self.add_validations_for_field(name, type, args) validates_presence_of name if :required.in?(args) validates_uniqueness_of name, :allow_nil => !:required.in?(args) if :unique.in?(args) + # Support for custom validations in Hobo Fields type_class = HoboFields.to_class(type) if type_class && type_class.public_method_defined?("validate") self.validate do |record| v = record.send(name)._?.validate record.errors.add(name, v) if v.is_a?(String) end end + end def self.add_formatting_for_field(name, type, args) type_class = HoboFields.to_class(type) if type_class && "format".in?(type_class.instance_methods) @@ -207,10 +209,10 @@ raise RuntimeError, "attr_types called on a stale class object (#{self.name}). Avoid storing persistent references to classes" end attr_types[name] or - if (refl = reflections[name.to_sym]) + if (refl = reflections[name.to_s]) if refl.macro.in?([:has_one, :belongs_to]) && !refl.options[:polymorphic] refl.klass else refl end