sample_models 0.9.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.
Files changed (43) hide show
  1. data/.gitignore +4 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.markdown +345 -0
  4. data/Rakefile +53 -0
  5. data/VERSION +1 -0
  6. data/init.rb +2 -0
  7. data/install.rb +1 -0
  8. data/lib/sample_models/creation.rb +116 -0
  9. data/lib/sample_models/finder.rb +81 -0
  10. data/lib/sample_models/model.rb +136 -0
  11. data/lib/sample_models/sampler.rb +110 -0
  12. data/lib/sample_models.rb +117 -0
  13. data/sample_models.gemspec +89 -0
  14. data/spec/sample_models_spec.rb +11 -0
  15. data/spec_or_test/database.yml +7 -0
  16. data/spec_or_test/setup.rb +249 -0
  17. data/spec_or_test/specs_or_test_cases.rb +569 -0
  18. data/spec_or_test/vendor/validates_email_format_of/CHANGELOG +11 -0
  19. data/spec_or_test/vendor/validates_email_format_of/MIT-LICENSE +20 -0
  20. data/spec_or_test/vendor/validates_email_format_of/README +28 -0
  21. data/spec_or_test/vendor/validates_email_format_of/TODO +1 -0
  22. data/spec_or_test/vendor/validates_email_format_of/init.rb +1 -0
  23. data/spec_or_test/vendor/validates_email_format_of/lib/validates_email_format_of.rb +41 -0
  24. data/spec_or_test/vendor/validates_email_format_of/rakefile +28 -0
  25. data/spec_or_test/vendor/validates_email_format_of/test/database.yml +3 -0
  26. data/spec_or_test/vendor/validates_email_format_of/test/fixtures/people.yml +3 -0
  27. data/spec_or_test/vendor/validates_email_format_of/test/fixtures/person.rb +3 -0
  28. data/spec_or_test/vendor/validates_email_format_of/test/schema.rb +5 -0
  29. data/spec_or_test/vendor/validates_email_format_of/test/test_helper.rb +35 -0
  30. data/spec_or_test/vendor/validates_email_format_of/test/validates_email_format_of_test.rb +82 -0
  31. data/tasks/sample_models_tasks.rake +4 -0
  32. data/test/test_sample_models.rb +28 -0
  33. data/uninstall.rb +1 -0
  34. data/vendor/ar_query/MIT-LICENSE +20 -0
  35. data/vendor/ar_query/README +0 -0
  36. data/vendor/ar_query/ar_query.gemspec +16 -0
  37. data/vendor/ar_query/init.rb +1 -0
  38. data/vendor/ar_query/install.rb +1 -0
  39. data/vendor/ar_query/lib/ar_query.rb +146 -0
  40. data/vendor/ar_query/spec/ar_query_spec.rb +318 -0
  41. data/vendor/ar_query/tasks/ar_query_tasks.rake +0 -0
  42. data/vendor/ar_query/uninstall.rb +1 -0
  43. metadata +117 -0
@@ -0,0 +1,28 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => [:clean_log, :test]
7
+
8
+ desc 'Remove the old log file'
9
+ task :clean_log do
10
+ "rm -f #{File.dirname(__FILE__)}/test/debug.log" if File.exists?(File.dirname(__FILE__) + '/test/debug.log')
11
+ end
12
+
13
+ desc 'Test the validates_email_format_of plugin.'
14
+ Rake::TestTask.new(:test) do |t|
15
+ t.libs << 'lib'
16
+ t.pattern = 'test/**/*_test.rb'
17
+ t.verbose = true
18
+ end
19
+
20
+ desc 'Generate documentation for the validates_email_format_of plugin.'
21
+ Rake::RDocTask.new(:rdoc) do |rdoc|
22
+ rdoc.rdoc_dir = 'rdoc'
23
+ rdoc.title = 'validates_email_format_of plugin'
24
+ rdoc.options << '--line-numbers --inline-source'
25
+ rdoc.rdoc_files.include('README')
26
+ rdoc.rdoc_files.include('TODO')
27
+ rdoc.rdoc_files.include('lib/**/*.rb')
28
+ end
@@ -0,0 +1,3 @@
1
+ plugin_test:
2
+ adapter: sqlite3
3
+ database: ":memory:"
@@ -0,0 +1,3 @@
1
+ existing:
2
+ id: 1
3
+ email: test@example.com
@@ -0,0 +1,3 @@
1
+ class Person < ActiveRecord::Base
2
+ validates_email_format_of :email, :on => :create, :message => 'fails with custom message'
3
+ end
@@ -0,0 +1,5 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :people, :force => true do |t|
3
+ t.column "email", :string
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+ RAILS_ROOT = File.dirname(__FILE__)
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'active_record'
7
+ require 'active_record/fixtures'
8
+ require 'active_support/binding_of_caller'
9
+ require 'active_support/breakpoint'
10
+ require "#{File.dirname(__FILE__)}/../init"
11
+
12
+
13
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
14
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
15
+ ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'plugin_test'])
16
+
17
+ load(File.dirname(__FILE__) + "/schema.rb") if File.exist?(File.dirname(__FILE__) + "/schema.rb")
18
+
19
+ Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
20
+ $LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
21
+
22
+ class Test::Unit::TestCase #:nodoc:
23
+ def create_fixtures(*table_names)
24
+ if block_given?
25
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
26
+ else
27
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
28
+ end
29
+ end
30
+
31
+ self.use_transactional_fixtures = false
32
+
33
+ self.use_instantiated_fixtures = false
34
+ end
35
+
@@ -0,0 +1,82 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class ValidatesEmailFormatOfTest < Test::Unit::TestCase
4
+ fixtures :people
5
+
6
+ def setup
7
+ @valid_email = 'valid@example.com'
8
+ @invalid_email = '_invalid@example.com'
9
+ end
10
+
11
+ def test_should_allow_valid_email_addresses
12
+ ['valid@example.com',
13
+ 'valid@test.example.com',
14
+ 'valid+valid123@test.example.com',
15
+ 'valid_valid123@test.example.com',
16
+ 'valid-valid+123@test.example.co.uk',
17
+ 'valid-valid+1.23@test.example.com.au',
18
+ 'valid@example.co.uk',
19
+ 'v@example.com',
20
+ 'valid@example.ca',
21
+ 'valid123.456@example.org',
22
+ 'valid123.456@example.travel',
23
+ 'valid123.456@example.museum',
24
+ 'valid@example.mobi',
25
+ 'valid@example.info'].each do |email|
26
+ p = create_person(:email => email)
27
+ save_passes(p, email)
28
+ end
29
+ end
30
+
31
+ def test_should_not_allow_invalid_email_addresses
32
+ ['_invalid@example.com',
33
+ 'invalid@example-com',
34
+ 'invalid_@example.com',
35
+ 'invalid-@example.com',
36
+ '.invalid@example.com',
37
+ 'invalid.@example.com',
38
+ 'invalid@example.com.',
39
+ 'invalid@example.com_',
40
+ 'invalid@example.com-',
41
+ 'invalid-example.com',
42
+ 'invalid@example.b#r.com',
43
+ 'invalid@example.c',
44
+ 'invali d@example.com',
45
+ 'invalidexample.com',
46
+ 'invalid@example.'].each do |email|
47
+ p = create_person(:email => email)
48
+ save_fails(p, email)
49
+ end
50
+ end
51
+
52
+ def test_should_respect_validate_on_option
53
+ p = create_person(:email => @valid_email)
54
+ save_passes(p)
55
+
56
+ assert p.update_attributes(:email => @invalid_email)
57
+ assert_equal '_invalid@example.com', p.email
58
+ end
59
+
60
+ def test_should_allow_custom_error_message
61
+ p = create_person(:email => @invalid_email)
62
+ save_fails(p)
63
+ assert_equal 'fails with custom message', p.errors.on(:email)
64
+ end
65
+
66
+ protected
67
+ def create_person(params)
68
+ Person.new(params)
69
+ end
70
+
71
+ def save_passes(p, email = '')
72
+ assert p.valid?, " validating #{email}"
73
+ assert p.save
74
+ assert_nil p.errors.on(:email)
75
+ end
76
+
77
+ def save_fails(p, email = '')
78
+ assert !p.valid?, " validating #{email}"
79
+ assert !p.save
80
+ assert p.errors.on(:email)
81
+ end
82
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :sample_models do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,28 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + "/../spec_or_test/setup"
3
+
4
+ # auto-convert specs into tests whoooooha
5
+ @@test_class_sequence = 1
6
+
7
+ def describe(desc_name, &block)
8
+ klass = Class.new Test::Unit::TestCase
9
+ class_name = "Test#{desc_name.gsub(/\W/, '_').camelize}"
10
+ Object.const_set class_name.to_sym, klass
11
+ @@test_class_sequence += 1
12
+ def klass.it(it_name, &block)
13
+ test_name = "test_" + it_name.gsub(/ /, '_')
14
+ if instance_methods.include?(test_name)
15
+ raise "redundant describe #{it_name.inspect}"
16
+ end
17
+ self.send(:define_method, test_name, &block)
18
+ end
19
+ def klass.before(before_name, &block)
20
+ self.send(:define_method, :setup, &block)
21
+ end
22
+ klass.instance_eval &block
23
+ end
24
+
25
+ initialize_db
26
+
27
+ require File.dirname(__FILE__) + "/../spec_or_test/specs_or_test_cases"
28
+
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
File without changes
@@ -0,0 +1,16 @@
1
+ require 'rake'
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'ar_query'
5
+ s.version = '0.0.1'
6
+ s.date = '2009-02-19'
7
+ s.author = 'Francis Hwang'
8
+ s.description = 'A utility class for building options for ActiveRecord.find.'
9
+ s.summary = 'A utility class for building options for ActiveRecord.find.'
10
+ s.email = 'sera@fhwang.net'
11
+ s.homepage = 'http://github.com/fhwang/ar_query'
12
+ s.files = FileList[
13
+ 'lib/*.rb', 'MIT-LICENSE', 'README', '*.rb', 'spec/*.rb',
14
+ 'tasks/*.rake'
15
+ ].to_a
16
+ end
@@ -0,0 +1 @@
1
+ # Include hook code here
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,146 @@
1
+ class ARQuery
2
+ attr_reader :joins
3
+
4
+ def initialize(simple_values={})
5
+ @simple_values = simple_values
6
+ @base_condition = Condition.new self
7
+ @joins = UniqueArray.new simple_values[:joins]
8
+ end
9
+
10
+ def method_missing(sym, *args)
11
+ if sym == :total_entries=
12
+ @simple_values[:total_entries] = args.first
13
+ elsif [:has_conditions?, :boolean_join=].include?(sym)
14
+ @base_condition.send(sym, *args)
15
+ else
16
+ super
17
+ end
18
+ end
19
+
20
+ def add_condition(&block)
21
+ if @base_condition.has_conditions?
22
+ @base_condition.add_condition do |nested|
23
+ block.call nested
24
+ end
25
+ else
26
+ block.call @base_condition
27
+ end
28
+ end
29
+
30
+ def condition_bind_vars
31
+ @base_condition.bind_vars
32
+ end
33
+
34
+ def condition_bind_vars=(arg)
35
+ @base_condition.bind_vars = arg
36
+ end
37
+
38
+ def condition_sqls
39
+ @base_condition.sqls
40
+ end
41
+
42
+ def conditions
43
+ @base_condition
44
+ end
45
+
46
+ def to_hash
47
+ hash = @simple_values.dup
48
+ hash[:conditions] = @base_condition.to_conditions if has_conditions?
49
+ joins = @joins
50
+ if includes = @simple_values[:include]
51
+ if includes.is_a?(Array)
52
+ joins = joins.reject { |join|
53
+ includes.any? { |inc| inc.to_sym == join.to_sym }
54
+ }
55
+ else
56
+ joins = joins.reject { |join| includes.to_sym == join.to_sym }
57
+ end
58
+ end
59
+ hash[:joins] = joins unless joins.empty?
60
+ hash
61
+ end
62
+
63
+ class Condition
64
+ attr_accessor :bind_vars, :boolean_join
65
+ attr_reader :ar_query, :sqls
66
+
67
+ def initialize(ar_query)
68
+ @ar_query = ar_query
69
+ @bind_vars = []
70
+ @sqls = SQLs.new
71
+ @boolean_join = :and
72
+ @children = []
73
+ end
74
+
75
+ def []=(attr, value)
76
+ @sqls << "#{attr} = ?"
77
+ @bind_vars << value
78
+ end
79
+
80
+ def add_condition(&block)
81
+ if has_conditions?
82
+ @children << Condition.new(@ar_query)
83
+ yield @children.last
84
+ else
85
+ yield self
86
+ end
87
+ end
88
+
89
+ def has_conditions?
90
+ !@sqls.empty?
91
+ end
92
+
93
+ def to_conditions
94
+ join_str = @boolean_join == :and ? ' AND ' : ' OR '
95
+ binds = @bind_vars.dup || []
96
+ condition_sql_fragments = @sqls.map { |c_sql| "(#{c_sql})" }
97
+ @children.each do |child|
98
+ sub_conditions = child.to_conditions
99
+ if sub_conditions
100
+ if sub_conditions.is_a?(Array)
101
+ sql = sub_conditions.first
102
+ sub_binds = sub_conditions[1..-1]
103
+ condition_sql_fragments << "(#{sql})"
104
+ binds.concat sub_binds
105
+ else
106
+ condition_sql_fragments << "(#{sub_conditions})"
107
+ end
108
+ end
109
+ end
110
+ condition_sql = condition_sql_fragments.join(join_str)
111
+ if binds.empty?
112
+ condition_sql unless condition_sql == ''
113
+ else
114
+ [ condition_sql, *binds ]
115
+ end
116
+ end
117
+
118
+ class SQLs < Array
119
+ def <<(elt)
120
+ if elt.is_a?(String)
121
+ super
122
+ else
123
+ raise(
124
+ ArgumentError,
125
+ "Tried appending #{elt.inspect} to ARQuery::Condition::SQLs: Only strings are allowed"
126
+ )
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ class UniqueArray < Array
133
+ def initialize(values)
134
+ super()
135
+ if values
136
+ values = [values] unless values.is_a?(Array)
137
+ values.each do |value| self << value; end
138
+ end
139
+ end
140
+
141
+ def <<(value)
142
+ super
143
+ uniq!
144
+ end
145
+ end
146
+ end