Skip to content

Commit

Permalink
Moving back to plugin cache for ActiveRecord
Browse files Browse the repository at this point in the history
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
  • Loading branch information
adzap committed Aug 19, 2012
1 parent 62557e7 commit 973bbfa
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 43 deletions.
11 changes: 11 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 4 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
15 changes: 15 additions & 0 deletions gemfiles/rails_3_0.gemfile
Original file line number Diff line number Diff line change
@@ -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=>"../"
15 changes: 15 additions & 0 deletions gemfiles/rails_3_1.gemfile
Original file line number Diff line number Diff line change
@@ -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=>"../"
15 changes: 15 additions & 0 deletions gemfiles/rails_3_2.gemfile
Original file line number Diff line number Diff line change
@@ -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=>"../"
45 changes: 4 additions & 41 deletions lib/validates_timeliness/orm/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit 973bbfa

Please sign in to comment.