sample_models 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -3,21 +3,31 @@ require 'rake/testtask'
3
3
  require 'rake/rdoctask'
4
4
  require 'rubygems'
5
5
  gem 'rspec'
6
- require 'spec/rake/spectask'
7
6
 
8
- desc 'Default: run specs.'
7
+ ActiveRecordVersions = %w(3.0.1 2.3.10)
8
+
9
+ desc 'Default: run all tests and specs.'
9
10
  task :default => [:test, :spec]
10
11
 
11
12
  desc "Run all specs"
12
13
  task :spec do
13
- cmd = "spec spec/sample_models_spec.rb"
14
- puts cmd
15
- puts `#{cmd}`
14
+ ActiveRecordVersions.each do |ar_version|
15
+ ENV['ACTIVE_RECORD_VERSION'] = ar_version
16
+ cmd = "rspec spec/sample_models_spec.rb"
17
+ puts ar_version
18
+ puts cmd
19
+ puts `#{cmd}`
20
+ end
16
21
  end
17
22
 
18
23
  desc "Run all tests"
19
- Rake::TestTask.new do |t|
20
- t.test_files = FileList['test/*.rb']
24
+ task :test do
25
+ ActiveRecordVersions.each do |ar_version|
26
+ cmd = "ACTIVE_RECORD_VERSION=#{ar_version} ruby test/test_sample_models.rb"
27
+ puts cmd
28
+ puts `cd . && #{cmd}`
29
+ puts
30
+ end
21
31
  end
22
32
 
23
33
  desc 'Generate documentation for the sample_models plugin.'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
data/lib/sample_models.rb CHANGED
@@ -1,10 +1,4 @@
1
1
  if RAILS_ENV == 'test' # no reason to run this code outside of test mode
2
-
3
- require "#{File.dirname(__FILE__)}/sample_models/creation"
4
- require "#{File.dirname(__FILE__)}/sample_models/finder"
5
- require "#{File.dirname(__FILE__)}/sample_models/model"
6
- require "#{File.dirname(__FILE__)}/sample_models/sampler"
7
- require "#{File.dirname(__FILE__)}/../vendor/ar_query/lib/ar_query"
8
2
 
9
3
  module SampleModels
10
4
  mattr_reader :models
@@ -20,6 +14,14 @@ module SampleModels
20
14
  def self.configure(model_class, opts ={})
21
15
  yield ConfigureRecipient.new(model_class) if block_given?
22
16
  end
17
+
18
+ def self.hash_with_indifferent_access_class
19
+ if ActiveSupport.const_defined?('HashWithIndifferentAccess')
20
+ ActiveSupport::HashWithIndifferentAccess
21
+ else
22
+ HashWithIndifferentAccess
23
+ end
24
+ end
23
25
 
24
26
  protected
25
27
 
@@ -93,25 +95,37 @@ module ActiveRecord
93
95
  class Base
94
96
  include SampleModels
95
97
  end
96
-
97
- module Validations
98
- module ClassMethods
99
- [:validates_email_format_of,
100
- :validates_inclusion_of, :validates_presence_of,
101
- :validates_uniqueness_of].each do |validation|
102
- if method_defined?(validation)
103
- define_method "#{validation}_with_sample_models".to_sym do |*args|
104
- send "#{validation}_without_sample_models".to_sym, *args
105
- SampleModels.models[self].record_validation(
106
- validation, *args
107
- )
108
- end
109
- alias_method_chain validation, :sample_models
110
- end
111
- end
98
+ end
99
+
100
+ validation_recipients = [ActiveRecord::Validations::ClassMethods]
101
+ if Object.const_defined?('ActiveModel')
102
+ validation_recipients << ActiveModel::Validations::HelperMethods
103
+ end
104
+ validations_to_intercept = [
105
+ :validates_email_format_of, :validates_inclusion_of, :validates_presence_of,
106
+ :validates_uniqueness_of
107
+ ]
108
+ validations_to_intercept.each do |validation|
109
+ recipient = validation_recipients.detect { |vr|
110
+ vr.method_defined?(validation)
111
+ }
112
+ if recipient
113
+ method_name = "#{validation}_with_sample_models".to_sym
114
+ recipient.send(:define_method, method_name) do |*args|
115
+ send "#{validation}_without_sample_models".to_sym, *args
116
+ SampleModels.models[self].record_validation(validation, *args)
112
117
  end
118
+ recipient.alias_method_chain validation, :sample_models
119
+ else
120
+ raise "Can't find who defines the validation method #{validation}"
113
121
  end
114
122
  end
123
+
124
+ require "#{File.dirname(__FILE__)}/sample_models/creation"
125
+ require "#{File.dirname(__FILE__)}/sample_models/finder"
126
+ require "#{File.dirname(__FILE__)}/sample_models/model"
127
+ require "#{File.dirname(__FILE__)}/sample_models/sampler"
128
+ require "#{File.dirname(__FILE__)}/../vendor/ar_query/lib/ar_query"
115
129
 
116
130
  end # if RAILS_ENV == 'test'
117
131
 
@@ -2,7 +2,7 @@ module SampleModels
2
2
  class Creation
3
3
  def initialize(sampler, attrs)
4
4
  @sampler = sampler
5
- @orig_attrs = HashWithIndifferentAccess.new attrs
5
+ @orig_attrs = SampleModels.hash_with_indifferent_access_class.new attrs
6
6
  @attrs = Attributes.new(@sampler, attrs)
7
7
  end
8
8
 
@@ -48,7 +48,7 @@ module SampleModels
48
48
  save! if needs_another_save
49
49
  end
50
50
 
51
- class Attributes < HashWithIndifferentAccess
51
+ class Attributes < SampleModels.hash_with_indifferent_access_class
52
52
  def initialize(sampler, hash)
53
53
  @sampler = sampler
54
54
  hash = Sampler.reify_association_hashes model, hash
@@ -3,7 +3,7 @@ module SampleModels
3
3
  def initialize(model, attrs)
4
4
  @model = model
5
5
  attrs = Sampler.reify_association_hashes @model, attrs.clone
6
- @attrs = HashWithIndifferentAccess.new attrs
6
+ @attrs = SampleModels.hash_with_indifferent_access_class.new attrs
7
7
  @ar_query = ARQuery.new
8
8
  end
9
9
 
@@ -32,7 +32,11 @@ module SampleModels
32
32
  end
33
33
 
34
34
  def construct_finder_sql(*args)
35
- @model_class.send(:construct_finder_sql, *args)
35
+ if @model_class.method(:scoped).arity == -1
36
+ @model_class.scoped.apply_finder_options(*args).arel.to_sql
37
+ else
38
+ @model_class.send(:construct_finder_sql, *args)
39
+ end
36
40
  end
37
41
 
38
42
  def has_many_associations
@@ -36,8 +36,9 @@ module SampleModels
36
36
  def initialize(model_class)
37
37
  @model_class = model_class
38
38
  @configured_default_attrs = {}
39
- @named_sample_attrs = HashWithIndifferentAccess.new
40
- @polymorphic_default_classes = HashWithIndifferentAccess.new
39
+ @named_sample_attrs = SampleModels.hash_with_indifferent_access_class.new
40
+ @polymorphic_default_classes =
41
+ SampleModels.hash_with_indifferent_access_class.new
41
42
  end
42
43
 
43
44
  def create_sample(*args)
@@ -1,15 +1,15 @@
1
1
  # Generated by jeweler
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sample_models}
8
- s.version = "1.0.0"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Francis Hwang"]
12
- s.date = %q{2010-11-06}
12
+ s.date = %q{2010-11-21}
13
13
  s.description = %q{
14
14
  A library for making it extremely fast for Rails developers to set up and save ActiveRecord instances when writing test cases. It aims to:
15
15
 
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__) + "/../spec_or_test/setup"
2
2
  require 'test/unit/assertions'
3
3
 
4
- class Spec::Example::ExampleGroup
4
+ class RSpec::Core::ExampleGroup
5
5
  include Test::Unit::Assertions
6
6
  end
7
7
 
@@ -1,7 +1,9 @@
1
1
  require 'rubygems'
2
- require 'active_record'
2
+ gem 'activerecord', ENV['ACTIVE_RECORD_VERSION']
3
3
  RAILS_ENV = 'test'
4
+ require 'active_record'
4
5
  require 'active_record/base'
6
+ require 'active_support/core_ext/logger'
5
7
  require File.dirname(__FILE__) +
6
8
  '/vendor/validates_email_format_of/lib/validates_email_format_of'
7
9
  require File.dirname(__FILE__) + '/../lib/sample_models'
@@ -105,8 +107,9 @@ class Appointment < ActiveRecord::Base
105
107
 
106
108
  validates_presence_of :calendar_id, :user_id
107
109
  validates_uniqueness_of :start_time
110
+ validate :validate_calendar_has_same_user_id
108
111
 
109
- def validate
112
+ def validate_calendar_has_same_user_id
110
113
  if calendar.user_id != user_id
111
114
  errors.add "Appointment needs same user as the calendar"
112
115
  end
@@ -191,7 +194,9 @@ class Video < ActiveRecord::Base
191
194
  belongs_to :network
192
195
  belongs_to :episode
193
196
 
194
- def validate
197
+ validate :validate_episode_has_same_show_id
198
+
199
+ def validate_episode_has_same_show_id
195
200
  if episode && episode.show_id != show_id
196
201
  errors.add "Video needs same show as the episode; show_id is #{show_id.inspect} while episode.show_id is #{episode.show_id.inspect}"
197
202
  end
@@ -247,3 +252,4 @@ SampleModels.configure Video do |video|
247
252
  end
248
253
  video.view_count.default 0
249
254
  end
255
+
@@ -35,8 +35,10 @@ describe "Model.sample" do
35
35
  end
36
36
 
37
37
  it "should raise the standard validation error if you break the model's validates_email_format_of validation" do
38
- assert_raise(ActiveRecord::RecordInvalid) do
39
- User.sample(:email => 'call.me')
38
+ unless ENV['ACTIVE_RECORD_VERSION'] =~ /^3\./
39
+ assert_raise(ActiveRecord::RecordInvalid) do
40
+ User.sample(:email => 'call.me')
41
+ end
40
42
  end
41
43
  end
42
44
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sample_models
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 1.0.0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Francis Hwang
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-06 00:00:00 -04:00
18
+ date: 2010-11-21 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21