Skip to content

Commit

Permalink
Fixing gem loading issues on Ruby 1.9.x - favoring Rails project gem …
Browse files Browse the repository at this point in the history
…version first, otherwise try load the gem from the plugin.
  • Loading branch information
grimen committed Oct 2, 2009
1 parent 8292606 commit 0d54901
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
8 changes: 7 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding: utf-8
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
Expand All @@ -22,7 +23,12 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
end

begin
require 'jeweler'
begin
require 'jeweler'
rescue LoadError
gem 'technicalpickles-jeweler', '>= 1.0.0'
require 'jeweler'
end
Jeweler::Tasks.new do |gemspec|
gemspec.name = "validation_reflection"
gemspec.summary = "Adds reflective access to validations"
Expand Down
13 changes: 8 additions & 5 deletions lib/validation_reflection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding: utf-8
#--
# Copyright (c) 2006-2008, Michael Schuerig, michael@schuerig.de
#
Expand Down Expand Up @@ -58,7 +59,7 @@ def included(base)
def load_config
if ::File.file?(CONFIG_PATH)
config = ::OpenStruct.new
config.reflected_validations = reflected_validations
config.reflected_validations = @@reflected_validations
silence_warnings do
eval(::IO.read(CONFIG_PATH), binding, CONFIG_PATH)
end
Expand Down Expand Up @@ -109,25 +110,27 @@ def reflect_on_validations_for(attr_name)
end

private


# Store validation info for easy and fast access.
#
def remember_validation_metadata(validation_type, *attr_names)
configuration = attr_names.last.is_a?(::Hash) ? attr_names.pop : {}
attr_names.each do |attr_name|
self.write_inheritable_array :validations,
[::ActiveRecord::Reflection::MacroReflection.new(validation_type, attr_name.to_sym, configuration, self)]
end
end

def ignoring_subvalidations(ignore)
save_ignore = self.in_ignored_subvalidation
unless in_ignored_subvalidation
unless self.in_ignored_subvalidation
self.in_ignored_subvalidation = ignore
yield
end
ensure
self.in_ignored_subvalidation = save_ignore
end

end
end
end
Expand Down
1 change: 1 addition & 0 deletions rails/init.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# encoding: utf-8
require 'validation_reflection'
19 changes: 16 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
ENV["RAILS_ENV"] = "test"
# encoding: utf-8
ENV['RAILS_ENV'] = 'test'
RAILS_ROOT = File.join(File.dirname(__FILE__))

require 'rubygems'
require 'test/unit'
require 'active_record'

begin
require 'active_record'
rescue LoadError
gem 'activerecord', '>= 1.2.3'
require 'active_record'
end

begin
require 'test/unit'
rescue LoadError
gem 'test-unit', '>= 1.2.3'
require 'test/unit'
end
1 change: 1 addition & 0 deletions test/validation_reflection_test.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# encoding: utf-8
require File.join(File.dirname(__FILE__), 'test_helper')

ActiveRecord::Base.class_eval do
Expand Down

0 comments on commit 0d54901

Please sign in to comment.