sample_models 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +17 -7
- data/VERSION +1 -1
- data/lib/sample_models.rb +36 -22
- data/lib/sample_models/creation.rb +2 -2
- data/lib/sample_models/finder.rb +1 -1
- data/lib/sample_models/model.rb +5 -1
- data/lib/sample_models/sampler.rb +3 -2
- data/sample_models.gemspec +3 -3
- data/spec/sample_models_spec.rb +1 -1
- data/spec_or_test/setup.rb +9 -3
- data/spec_or_test/specs_or_test_cases.rb +4 -2
- metadata +4 -4
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
|
-
|
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
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
20
|
-
|
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.
|
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
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
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 =
|
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 <
|
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
|
data/lib/sample_models/finder.rb
CHANGED
@@ -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 =
|
6
|
+
@attrs = SampleModels.hash_with_indifferent_access_class.new attrs
|
7
7
|
@ar_query = ARQuery.new
|
8
8
|
end
|
9
9
|
|
data/lib/sample_models/model.rb
CHANGED
@@ -32,7 +32,11 @@ module SampleModels
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def construct_finder_sql(*args)
|
35
|
-
@model_class.
|
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 =
|
40
|
-
@polymorphic_default_classes =
|
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)
|
data/sample_models.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# Generated by jeweler
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in
|
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.
|
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-
|
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
|
|
data/spec/sample_models_spec.rb
CHANGED
data/spec_or_test/setup.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require 'rubygems'
|
2
|
-
|
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
|
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
|
-
|
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
|
-
|
39
|
-
|
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:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
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-
|
18
|
+
date: 2010-11-21 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|