lib/classy-inheritance.rb in classy-inheritance-0.6.2 vs lib/classy-inheritance.rb in classy-inheritance-0.6.4
- old
+ new
@@ -1,30 +1,12 @@
-$:.unshift(File.dirname(__FILE__)) unless
- $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
-
-module ActiveRecord::Validations::ClassMethods
- def validates_associated(*attr_names)
- configuration = { :message => ActiveRecord::Errors.default_error_messages[:invalid], :on => :save }
- configuration.update(attr_names.extract_options!)
-
- validates_each(attr_names, configuration) do |record, attr_name, value|
- associate = record.send(attr_name)
- if associate && !associate.valid?
- associate.errors.each do |key, value|
- record.errors.add(key, value)
- end
- end
- end
- end
-end
-
-
module Stonean
module ClassyInheritance
- def self.included(base)
- base.extend Stonean::ClassyInheritance::ClassMethods
+ VERSION = '0.6.4'
+
+ def self.version
+ VERSION
end
module ClassMethods
def depends_on(model_sym, options = {})
define_relationship(model_sym,options)
@@ -38,24 +20,24 @@
validates_presence_of model_sym
end
if options.has_key?(:validates_associated_if) && options[:validates_associated_if] != true
if [Symbol, String, Proc].include?(options[:validates_associated_if].class)
- validates_associated model_sym, :if => options[:validates_associated_if]
+ validates_associated_dependent model_sym, options, :if => options[:validates_associated_if]
end
else
- validates_associated model_sym
+ validates_associated_dependent model_sym, options
end
# Before save functionality to create/update the requisite object
define_save_method(model_sym, options[:as])
# Adds a find_with_<model_sym> class method
define_find_with_method(model_sym)
if options[:as]
- define_can_be_method_on_requisite_class(options[:class_name] || model_sym, options[:as])
+ define_can_be_method_on_requisite_class(options[:class_name] || model_sym.to_s.classify, options[:as])
end
options[:attrs].each{|attr| define_accessors(model_sym, attr, options)}
end
@@ -173,21 +155,49 @@
eval("self.#{model_sym}.#{attr}= val")
end
end
def define_can_be_method_on_requisite_class(model_sym, polymorphic_name)
- klass = model_sym.to_s.classify
+ klass = model_sym.to_s
requisite_klass = eval(klass)
unless requisite_klass.respond_to?(self.name.underscore.to_sym)
requisite_klass.send :can_be, self.name.underscore,
- :as => polymorphic_name
+ :as => polymorphic_name
end
end
def polymorphic_constraints(polymorphic_name)
{ :foreign_key => "#{polymorphic_name}_id",
:conditions => "#{polymorphic_name}_type = '#{self.name}'"}
end
end # ClassMethods
end # ClassyInheritance module
end # Stonean module
-ActiveRecord::Base.send :include, Stonean::ClassyInheritance
+
+if Object.const_defined?("ActiveRecord") && ActiveRecord.const_defined?("Base")
+ module ActiveRecord::Validations::ClassMethods
+
+ def validates_associated_dependent(model_sym, options, configuration = {})
+ configuration = { :message => I18n.translate('activerecord.errors.messages.invalid'), :on => :save }.update(configuration)
+
+ validates_each(model_sym, configuration) do |record, attr_name, value|
+ associate = record.send(attr_name)
+ if associate && !associate.valid?
+ associate.errors.each do |key, value|
+ if options[:prefix]
+ key = (options[:prefix] == true) ? "#{model_sym}_#{key}" : "#{options[:prefix]}_#{key}"
+ end
+ if options[:postfix]
+ key = (options[:postfix] == true) ? "#{key}_#{model_sym}" : "#{key}_#{options[:postfix]}"
+ end
+ record.errors.add(key, value) unless record.errors[key]
+ end
+ end
+ end
+ end
+ end
+
+
+ ActiveRecord::Base.class_eval do
+ extend Stonean::ClassyInheritance::ClassMethods
+ end
+end