nayutaya-active-form 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007, 2008 R.W. van 't Veer
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.
data/README ADDED
@@ -0,0 +1,15 @@
1
+ = ActiveForm
2
+
3
+ This plugin provides a base class for making forms with ActiveRecord validations without having a corresponding database table. You can use ActiveForm for:
4
+
5
+ * making forms which don't needed storage, like simple email forms
6
+ * provide extra validations on existing ActiveRecord models
7
+ * make forms for composite objects
8
+
9
+
10
+ == Author
11
+ R.W. van 't Veer
12
+
13
+
14
+ == Copyright
15
+ Copyright (c) 2007, 2008 R.W. van 't Veer
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the active_form plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the active_form plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'ActiveForm'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
@@ -0,0 +1,65 @@
1
+ # = ActiveForm - non persistent ActiveRecord
2
+ #
3
+ # Simple base class to make AR objects without a corresponding database
4
+ # table. These objects can still use AR validations but can't be saved
5
+ # to the database.
6
+ #
7
+ # == Example
8
+ #
9
+ # class FeedbackForm < ActiveForm
10
+ # column :email
11
+ # column :message, :type => :text
12
+ # validates_presence_of :email, :message
13
+ # end
14
+ #
15
+ class ActiveForm < ActiveRecord::Base
16
+ def self.columns # :nodoc:
17
+ @columns ||= []
18
+ end
19
+
20
+ # Define an attribute. It takes the following options:
21
+ # [+:type+] schema type
22
+ # [+:default+] default value
23
+ # [+:null+] whether it is nullable
24
+ # [+:human_name+] human readable name
25
+ def self.column(name, options = {})
26
+ name = name.to_s
27
+ options.each { |k,v| options[k] = v.to_s if Symbol === v }
28
+
29
+ if human_name = options.delete(:human_name)
30
+ name.instance_variable_set('@human_name', human_name)
31
+ def name.humanize; @human_name; end
32
+ end
33
+
34
+ columns << ActiveRecord::ConnectionAdapters::Column.new(
35
+ name,
36
+ options.delete(:default),
37
+ options.delete(:type),
38
+ options.include?(:null) ? options.delete(:null) : true
39
+ )
40
+
41
+ raise ArgumentError.new("unknown option(s) #{options.inspect}") unless options.empty?
42
+ end
43
+
44
+ def self.abstract_class # :nodoc:
45
+ true
46
+ end
47
+
48
+ def save # :nodoc:
49
+ if result = valid?
50
+ callback(:before_save)
51
+ callback(:before_create)
52
+
53
+ # do nothing!
54
+
55
+ callback(:after_save)
56
+ callback(:after_create)
57
+ end
58
+
59
+ result
60
+ end
61
+
62
+ def save! # :nodoc:
63
+ save or raise ActiveRecord::RecordInvalid.new(self)
64
+ end
65
+ end
@@ -0,0 +1,2 @@
1
+
2
+ # nop
@@ -0,0 +1,36 @@
1
+
2
+ Gem::Specification.new do |s|
3
+ s.specification_version = 2
4
+ s.required_rubygems_version = Gem::Requirement.new(">= 0")
5
+ s.required_ruby_version = Gem::Requirement.new(">= 1.8.6")
6
+
7
+ s.name = "nayutaya-active-form"
8
+ s.version = "0.0.1"
9
+ s.date = "2009-12-02"
10
+
11
+ s.authors = ["Yuya Kato"]
12
+ s.email = "yuyakato@gmail.com"
13
+
14
+ s.summary = "Remco van 't Veer's ActiveForm plugin"
15
+ s.description = "a non-official gem package of Remco van 't Veer's ActiveForm plugin. packaged by Nayutaya."
16
+ s.homepage = "http://github.com/nayutaya/active_form"
17
+
18
+ s.rubyforge_project = nil
19
+ s.has_rdoc = false
20
+ s.require_paths = ["lib"]
21
+
22
+ s.files = [
23
+ "lib/active_form.rb",
24
+ "lib/nayutaya-active-form.rb",
25
+ "rails/init.rb",
26
+ "test/active_form_test.rb",
27
+ "MIT-LICENSE",
28
+ "README",
29
+ "Rakefile",
30
+ "nayutaya-active-form.gemspec",
31
+ ]
32
+ s.test_files = [
33
+ "test/active_form_test.rb",
34
+ ]
35
+ s.extra_rdoc_files = []
36
+ end
@@ -0,0 +1,2 @@
1
+
2
+ require File.join(File.dirname(__FILE__), "..", "lib", "active_form.rb")
@@ -0,0 +1,139 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+
4
+ require File.dirname(__FILE__) + '/../lib/active_form'
5
+ require 'test/unit'
6
+
7
+ class ActiveFormTest < Test::Unit::TestCase
8
+ def test_class_loads
9
+ assert_nothing_raised { ActiveForm }
10
+ end
11
+
12
+ def test_can_add_columns
13
+ self.class.class_eval %q{
14
+ class CanAddColumns < ActiveForm
15
+ %w(foo bar test).each { |c| column c }
16
+ end
17
+ }
18
+ assert_equal 3, CanAddColumns.columns.size
19
+ end
20
+
21
+ def test_type_properly_set
22
+ self.class.class_eval %q{
23
+ class TypeProperlySet < ActiveForm
24
+ %w(string text date datetime boolean).each do |type|
25
+ column "a_#{type}".to_sym, :type => type.to_sym
26
+ end
27
+ end
28
+ }
29
+
30
+ assert TypeProperlySet.columns.size > 0, 'no columns added'
31
+
32
+ %w(string text date datetime boolean).each do |type|
33
+ assert_equal type, TypeProperlySet.columns_hash["a_#{type}"].sql_type
34
+ end
35
+ end
36
+
37
+ def test_default_properly_set
38
+ self.class.class_eval %q{
39
+ class DefaultPropertlySet < ActiveForm
40
+ column :bicycle, :default => 'batavus'
41
+ end
42
+ }
43
+ assert_equal 'batavus', DefaultPropertlySet.new.bicycle
44
+ end
45
+
46
+ def test_columns_are_humanizable
47
+ self.class.class_eval %q{
48
+ class Humanizable < ActiveForm
49
+ column :bicycle, :human_name => 'fiets'
50
+ end
51
+ }
52
+
53
+ assert_equal 'fiets', Humanizable.columns_hash['bicycle'].human_name
54
+ end
55
+
56
+ def test_fail_on_illegal_options
57
+ assert_raises ArgumentError do
58
+ self.class.class_eval %q{
59
+ class FailOnIllegalOption < ActiveForm
60
+ column :foo, :bar => 'yelp!'
61
+ end
62
+ }
63
+ end
64
+ end
65
+
66
+ CALLBACKS_CALLED_FOR_VALID = %w(before_validation after_validation before_validation_on_create after_validation_on_create before_save after_save before_create after_create)
67
+ CALLBACKS_NOT_CALLED_FOR_VALID = %w(before_validation_on_update after_validation_on_update)
68
+ CALLBACKS_FOR_VALID = CALLBACKS_CALLED_FOR_VALID + CALLBACKS_NOT_CALLED_FOR_VALID
69
+
70
+ def test_callbacks_called_on_valid
71
+ self.class.class_eval %q{
72
+ class WithCallbackSuccess < ActiveForm
73
+ CALLBACKS_FOR_VALID.each do |callback|
74
+ attr_accessor "#{callback}_called"
75
+ send(callback){|obj| obj.send("#{callback}_called=", true)}
76
+ end
77
+ end
78
+ }
79
+
80
+ obj = WithCallbackSuccess.new
81
+ assert obj.save
82
+ CALLBACKS_CALLED_FOR_VALID.each do |callback|
83
+ assert obj.send("#{callback}_called"), "expected #{callback} to be called"
84
+ end
85
+ CALLBACKS_NOT_CALLED_FOR_VALID.each do |callback|
86
+ assert !obj.send("#{callback}_called"), "expected #{callback} not to be called"
87
+ end
88
+ end
89
+
90
+ CALLBACKS_CALLED_FOR_INVALID = %w(before_validation before_validation_on_create after_validation after_validation_on_create)
91
+ CALLBACKS_NOT_CALLED_FOR_INVALID = %w(before_validation_on_update after_validation_on_update before_save after_save before_create after_create)
92
+ CALLBACKS_FOR_INVALID = CALLBACKS_CALLED_FOR_INVALID + CALLBACKS_NOT_CALLED_FOR_INVALID
93
+
94
+ def test_callbacks_called_on_invalid
95
+ self.class.class_eval %q{
96
+ class WithCallbackFailure < ActiveForm
97
+ column :required
98
+ validates_presence_of :required
99
+
100
+ CALLBACKS_FOR_INVALID.each do |callback|
101
+ attr_accessor "#{callback}_called"
102
+ send(callback){|obj| obj.send("#{callback}_called=", true)}
103
+ end
104
+ end
105
+ }
106
+
107
+ obj = WithCallbackFailure.new
108
+ assert !obj.save
109
+ CALLBACKS_CALLED_FOR_INVALID.each do |callback|
110
+ assert obj.send("#{callback}_called"), "expected #{callback} to be called"
111
+ end
112
+ CALLBACKS_NOT_CALLED_FOR_INVALID.each do |callback|
113
+ assert !obj.send("#{callback}_called"), "expected #{callback} not to be called"
114
+ end
115
+ end
116
+
117
+ def test_create_bang_raises_no_exception_on_valid
118
+ self.class.class_eval %q{
119
+ class CreateBangSuccess < ActiveForm; end
120
+ }
121
+
122
+ assert_nothing_raised do
123
+ CreateBangSuccess.create!
124
+ end
125
+ end
126
+
127
+ def test_create_bang_raises_exception_on_invalid
128
+ self.class.class_eval %q{
129
+ class CreateBangFailure < ActiveForm
130
+ column :required_field
131
+ validates_presence_of :required_field
132
+ end
133
+ }
134
+
135
+ assert_raises ActiveRecord::RecordInvalid do
136
+ CreateBangFailure.create!
137
+ end
138
+ end
139
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nayutaya-active-form
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yuya Kato
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-02 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: a non-official gem package of Remco van 't Veer's ActiveForm plugin. packaged by Nayutaya.
17
+ email: yuyakato@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/active_form.rb
26
+ - lib/nayutaya-active-form.rb
27
+ - rails/init.rb
28
+ - test/active_form_test.rb
29
+ - MIT-LICENSE
30
+ - README
31
+ - Rakefile
32
+ - nayutaya-active-form.gemspec
33
+ has_rdoc: true
34
+ homepage: http://github.com/nayutaya/active_form
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.8.6
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.3
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: Remco van 't Veer's ActiveForm plugin
61
+ test_files:
62
+ - test/active_form_test.rb