From 973bbfa82c2074a1c06861458243845a6ae8f599 Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Sun, 19 Aug 2012 22:53:40 +1000 Subject: [PATCH] Moving back to plugin cache for ActiveRecord This simplifies the code a lot and fixes the issues with date and time colummns when using the plugin parser. Add appraisals for all rails 3 versions --- Appraisals | 11 +++++ Gemfile | 2 + Rakefile | 6 ++- gemfiles/rails_3_0.gemfile | 15 +++++++ gemfiles/rails_3_1.gemfile | 15 +++++++ gemfiles/rails_3_2.gemfile | 15 +++++++ lib/validates_timeliness/orm/active_record.rb | 45 ++----------------- 7 files changed, 66 insertions(+), 43 deletions(-) create mode 100644 Appraisals create mode 100644 gemfiles/rails_3_0.gemfile create mode 100644 gemfiles/rails_3_1.gemfile create mode 100644 gemfiles/rails_3_2.gemfile diff --git a/Appraisals b/Appraisals new file mode 100644 index 00000000..2aee08ad --- /dev/null +++ b/Appraisals @@ -0,0 +1,11 @@ +appraise "rails_3_0" do + gem "rails", "~> 3.0.0" +end + +appraise "rails_3_1" do + gem "rails", "~> 3.1.0" +end + +appraise "rails_3_2" do + gem "rails", "~> 3.2.0" +end diff --git a/Gemfile b/Gemfile index d5af1ef2..fca88226 100644 --- a/Gemfile +++ b/Gemfile @@ -9,6 +9,8 @@ gem 'timecop' gem 'rspec_tag_matchers' gem 'ruby-debug', :platforms => [:ruby_18, :jruby] gem 'debugger', :platforms => [:ruby_19] +gem 'appraisal' +gem 'sqlite3' group :mongoid do gem 'mongoid', '~> 2.3.0' diff --git a/Rakefile b/Rakefile index ee0624c0..c479a7cc 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,9 @@ require 'bundler' -Bundler::GemHelper.install_tasks +require 'bundler/setup' + +require 'appraisal' -Bundler.setup +Bundler::GemHelper.install_tasks require 'rake/rdoctask' require 'rspec/core/rake_task' diff --git a/gemfiles/rails_3_0.gemfile b/gemfiles/rails_3_0.gemfile new file mode 100644 index 00000000..aab2f5a7 --- /dev/null +++ b/gemfiles/rails_3_0.gemfile @@ -0,0 +1,15 @@ +# This file was generated by Appraisal + +source "http://rubygems.org" + +gem "rspec", "~> 2.8" +gem "rspec-rails", "~> 2.8" +gem "timecop" +gem "rspec_tag_matchers" +gem "ruby-debug", :platforms=>[:ruby_18, :jruby] +gem "debugger", :platforms=>[:ruby_19] +gem "appraisal" +gem "sqlite3" +gem "rails", "~> 3.0.0" + +gemspec :path=>"../" \ No newline at end of file diff --git a/gemfiles/rails_3_1.gemfile b/gemfiles/rails_3_1.gemfile new file mode 100644 index 00000000..244f638a --- /dev/null +++ b/gemfiles/rails_3_1.gemfile @@ -0,0 +1,15 @@ +# This file was generated by Appraisal + +source "http://rubygems.org" + +gem "rspec", "~> 2.8" +gem "rspec-rails", "~> 2.8" +gem "timecop" +gem "rspec_tag_matchers" +gem "ruby-debug", :platforms=>[:ruby_18, :jruby] +gem "debugger", :platforms=>[:ruby_19] +gem "appraisal" +gem "sqlite3" +gem "rails", "~> 3.1.0" + +gemspec :path=>"../" \ No newline at end of file diff --git a/gemfiles/rails_3_2.gemfile b/gemfiles/rails_3_2.gemfile new file mode 100644 index 00000000..26d3b4a3 --- /dev/null +++ b/gemfiles/rails_3_2.gemfile @@ -0,0 +1,15 @@ +# This file was generated by Appraisal + +source "http://rubygems.org" + +gem "rspec", "~> 2.8" +gem "rspec-rails", "~> 2.8" +gem "timecop" +gem "rspec_tag_matchers" +gem "ruby-debug", :platforms=>[:ruby_18, :jruby] +gem "debugger", :platforms=>[:ruby_19] +gem "appraisal" +gem "sqlite3" +gem "rails", "~> 3.2.0" + +gemspec :path=>"../" \ No newline at end of file diff --git a/lib/validates_timeliness/orm/active_record.rb b/lib/validates_timeliness/orm/active_record.rb index f1b0b355..8e222714 100644 --- a/lib/validates_timeliness/orm/active_record.rb +++ b/lib/validates_timeliness/orm/active_record.rb @@ -3,19 +3,6 @@ module ORM module ActiveRecord extend ActiveSupport::Concern - def self.use_plugin_cache? - ::ActiveRecord::VERSION::STRING < '3.1.0' - end - - included do - if ValidatesTimeliness::ORM::ActiveRecord.use_plugin_cache? - include Reload - else - # Just use the built-in before_type_cast retrieval - alias_method :_timeliness_raw_value_for, :read_attribute_before_type_cast - end - end - module ClassMethods public @@ -36,34 +23,12 @@ def timeliness_column_for_attribute(attr_name) def define_attribute_methods super.tap do |attribute_methods_generated| - use_before_type_cast = ValidatesTimeliness::ORM::ActiveRecord.use_plugin_cache? - define_timeliness_methods use_before_type_cast + define_timeliness_methods true end end protected - def define_attribute_timeliness_methods(attr_name, before_type_cast=false) - if before_type_cast - define_timeliness_write_method(attr_name) - define_timeliness_before_type_cast_method(attr_name) - elsif ValidatesTimeliness.use_plugin_parser - define_timeliness_write_method_without_cache(attr_name) - end - end - - def define_timeliness_write_method_without_cache(attr_name) - method_body, line = <<-EOV, __LINE__ + 1 - def #{attr_name}=(value) - original_value = value - if value.is_a?(String)\n#{timeliness_type_cast_code(attr_name, 'value')}\nend - super(value) - @attributes['#{attr_name}'] = original_value - end - EOV - generated_timeliness_methods.module_eval(method_body, __FILE__, line) - end - def timeliness_type_cast_code(attr_name, var_name) type = timeliness_attribute_type(attr_name) @@ -73,11 +38,9 @@ def timeliness_type_cast_code(attr_name, var_name) end end - module Reload - def reload(*args) - _clear_timeliness_cache - super - end + def reload(*args) + _clear_timeliness_cache + super end end