lib/classy-inheritance.rb in johnsbrn-classy-inheritance-0.6.3 vs lib/classy-inheritance.rb in johnsbrn-classy-inheritance-0.6.3.1

- old
+ new

@@ -1,35 +1,11 @@ -$:.unshift(File.dirname(__FILE__)) unless - $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) - - -module ActiveRecord::Validations::ClassMethods - def validates_associated_dependent(model_sym, options, configuration = {}) - configuration = { :message => ActiveRecord::Errors.default_error_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) - end - end - end - end -end - - module Stonean module ClassyInheritance - def self.included(base) - base.extend Stonean::ClassyInheritance::ClassMethods + VERSION = '0.6.3.1' + + def self.version + VERSION end module ClassMethods def depends_on(model_sym, options = {}) define_relationship(model_sym,options) @@ -46,11 +22,11 @@ if options.has_key?(:validates_associated_if) && options[:validates_associated_if] != true if [Symbol, String, Proc].include?(options[:validates_associated_if].class) validates_associated_dependent model_sym, options, :if => options[:validates_associated_if] end else - validates_associated_dependent model_sym, options + validates_associated_dependent model_sym, options end # Before save functionality to create/update the requisite object define_save_method(model_sym, options[:as]) @@ -182,17 +158,45 @@ def define_can_be_method_on_requisite_class(model_sym, polymorphic_name) klass = model_sym.to_s.classify 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 => ActiveRecord::Errors.default_error_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