Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activemodel 3.1, Validation fixes and Callback fixes #45

Merged
merged 13 commits into from
Nov 8, 2011
Merged
48 changes: 25 additions & 23 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
PATH
remote: .
specs:
couch_potato (0.5.7)
couch_potato (0.6.0)
activemodel
couchrest (>= 1.0.1)
json
json (~> 1.6.0)

GEM
remote: http://rubygems.org/
specs:
activemodel (3.0.6)
activesupport (= 3.0.6)
builder (~> 2.1.2)
i18n (~> 0.5.0)
activesupport (3.0.6)
builder (2.1.2)
couchrest (1.0.2)
json (~> 1.5.1)
activemodel (3.1.1)
activesupport (= 3.1.1)
builder (~> 3.0.0)
i18n (~> 0.6)
activesupport (3.1.1)
multi_json (~> 1.0)
builder (3.0.0)
couchrest (1.1.2)
mime-types (~> 1.15)
multi_json (~> 1.0.0)
rest-client (~> 1.6.1)
diff-lcs (1.1.2)
i18n (0.5.0)
json (1.5.1)
diff-lcs (1.1.3)
i18n (0.6.0)
json (1.6.1)
mime-types (1.16)
rake (0.8.7)
rest-client (1.6.1)
multi_json (1.0.3)
rake (0.9.2)
rest-client (1.6.7)
mime-types (>= 1.16)
rspec (2.5.0)
rspec-core (~> 2.5.0)
rspec-expectations (~> 2.5.0)
rspec-mocks (~> 2.5.0)
rspec-core (2.5.1)
rspec-expectations (2.5.0)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
rspec-mocks (~> 2.6.0)
rspec-core (2.6.4)
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)
rspec-mocks (2.6.0)
timecop (0.3.5)
tzinfo (0.3.26)
tzinfo (0.3.30)

PLATFORMS
ruby
Expand Down
62 changes: 6 additions & 56 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,78 +5,28 @@ require 'rake'
require "rspec/core/rake_task"
require 'rake/rdoctask'

def with_validatable(&block)
begin
require 'validatable'

ENV['VALIDATION_FRAMEWORK'] = 'validatable'
puts "Running task with Validatable validation framework."
yield block
rescue LoadError
STDERR.puts "WARNING: Validatable not available, skipping task."
end
end

def with_active_model(&block)
begin
require 'active_model'

ENV['VALIDATION_FRAMEWORK'] = 'active_model'
puts "Running task with ActiveModel validation framework."
yield block
rescue LoadError
STDERR.puts "WARNING: ActiveModel not available, skipping task."
end
end

task :default => :spec

task :spec_functional_validatable do
with_validatable { Rake::Task['spec_functional_default'].execute }
end

task :spec_functional_active_model do
with_active_model { Rake::Task['spec_functional_default'].execute }
end

task :spec_unit_validatable do
with_validatable { Rake::Task['spec_unit_default'].execute }
end

task :spec_unit_active_model do
with_active_model { Rake::Task['spec_unit_default'].execute }
end

desc "Run functional specs with default validation framework, override with VALIDATION_FRAMEWORK"
RSpec::Core::RakeTask.new(:spec_functional_default) do |spec|
desc "Run functional specs"
RSpec::Core::RakeTask.new(:spec_functional) do |spec|
spec.pattern = 'spec/*_spec.rb'
spec.rspec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""]
end

desc "Run unit specs with default validation framework, override with VALIDATION_FRAMEWORK"
RSpec::Core::RakeTask.new(:spec_unit_default) do |spec|
desc "Run unit specs"
RSpec::Core::RakeTask.new(:spec_unit) do |spec|
spec.pattern = 'spec/unit/*_spec.rb'
spec.rspec_opts = ['--options', "\"#{File.dirname(__FILE__)}/spec/spec.opts\""]
end

desc "Run functional specs with all validation frameworks"
task :spec_functional => [:spec_functional_validatable, :spec_functional_active_model] do
end

desc "Run unit specs with all validation frameworks"
task :spec_unit => [:spec_unit_validatable, :spec_unit_active_model] do
end

desc "Run all specs"
task :spec do
['3_0', '3_1'].each do |version|
Bundler.with_clean_env do
ENV['BUNDLE_GEMFILE'] = "active_support_#{version}"
sh "bundle install"
Rake::Task[:spec_unit_active_model].execute
Rake::Task[:spec_functional_active_model].execute
Rake::Task[:spec_unit_validatable].execute
Rake::Task[:spec_functional_validatable].execute
Rake::Task[:spec_unit].execute
Rake::Task[:spec_functional].execute
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion active_support_3_1.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ DEPENDENCIES
rake
rspec (>= 2.0)
timecop
tzinfo
tzinfo
4 changes: 1 addition & 3 deletions lib/couch_potato.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
JSON.create_id = 'ruby_class'

module CouchPotato
Config = Struct.new(:database_name, :validation_framework,
:split_design_documents_per_view, :default_language).new
Config.validation_framework = :active_model
Config = Struct.new(:database_name, :split_design_documents_per_view, :default_language).new
Config.split_design_documents_per_view = false
Config.default_language = :javascript

Expand Down
22 changes: 13 additions & 9 deletions lib/couch_potato/database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ def create_document(document, validate)

if validate
document.errors.clear
document.run_callbacks :validation_on_save do
document.run_callbacks :validation_on_create do
return false if false == document.run_callbacks(:validation_on_save) do
return false if false == document.run_callbacks(:validation_on_create) do
return false unless valid_document?(document)
end
end
end

document.run_callbacks :save do
document.run_callbacks :create do
return false if false == document.run_callbacks(:save) do
return false if false == document.run_callbacks(:create) do
res = couchrest_database.save_doc document.to_hash
document._rev = res['rev']
document._id = res['id']
Expand All @@ -152,15 +152,15 @@ def create_document(document, validate)
def update_document(document, validate)
if validate
document.errors.clear
document.run_callbacks :validation_on_save do
document.run_callbacks :validation_on_update do
return false if false == document.run_callbacks(:validation_on_save) do
return false if false == document.run_callbacks(:validation_on_update) do
return false unless valid_document?(document)
end
end
end

document.run_callbacks :save do
document.run_callbacks :update do
return false if false == document.run_callbacks(:save) do
return false if false == document.run_callbacks(:update) do
res = couchrest_database.save_doc document.to_hash
document._rev = res['rev']
end
Expand All @@ -173,7 +173,11 @@ def valid_document?(document)
errors.instance_variable_set("@messages", errors.messages.dup) if errors.respond_to?(:messages)
document.valid?
errors.each do |k, v|
document.errors.add(k, v)
if v.respond_to?(:each)
v.each {|message| document.errors.add(k, message)}
else
document.errors.add(k, v)
end
end
document.errors.empty?
end
Expand Down
1 change: 0 additions & 1 deletion lib/couch_potato/persistence/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def self.included(base) #:nodoc:

define_model_callbacks :create, :save, :update, :destroy
define_model_callbacks *[:save, :create, :update].map {|c| :"validation_on_#{c}"}
define_model_callbacks :validation unless Config.validation_framework == :active_model
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/couch_potato/persistence/json.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'active_support/core_ext/hash'

module CouchPotato
module Persistence
module Json
Expand Down Expand Up @@ -42,7 +44,7 @@ def json_create(json)
instance = self.new
instance._id = json[:_id] || json['_id']
instance._rev = json[:_rev] || json['_rev']
instance._document = json
instance._document = HashWithIndifferentAccess.new(json)
instance
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/couch_potato/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def self.rails_init
CouchPotato::Config.database_name = config
else
CouchPotato::Config.database_name = config['database']
CouchPotato::Config.validation_framework = config['validation_framework'] if config['validation_framework']
CouchPotato::Config.split_design_documents_per_view = config['split_design_documents_per_view'] if config['split_design_documents_per_view']
CouchPotato::Config.default_language = config['default_language'] if config['default_language']
end
Expand Down
12 changes: 2 additions & 10 deletions lib/couch_potato/validation.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
module CouchPotato
module Validation
def self.included(base) #:nodoc:
case CouchPotato::Config.validation_framework
when :validatable
require 'couch_potato/validation/with_validatable'
base.send :include, CouchPotato::Validation::WithValidatable
when :active_model
require 'couch_potato/validation/with_active_model'
base.send :include, CouchPotato::Validation::WithActiveModel
else
raise "Unknown CouchPotato::Config.validation_framework #{CouchPotato::Config.validation_framework.inspect}, options are :validatable or :active_model"
end
require 'couch_potato/validation/with_active_model'
base.send :include, CouchPotato::Validation::WithActiveModel
end
end
end
8 changes: 1 addition & 7 deletions lib/couch_potato/validation/with_active_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ def self.included(base)
require 'active_model'
require 'active_model/translation'
base.send :include, ::ActiveModel::Validations
base.instance_eval do
def before_validation(*names)
names.each do |name|
validate name
end
end
end
base.send :include, ::ActiveModel::Validations::Callbacks
end
end
end
Expand Down
41 changes: 0 additions & 41 deletions lib/couch_potato/validation/with_validatable.rb

This file was deleted.

Loading