validations_step_fu 0.0.1
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/.gitignore +4 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README +13 -0
- data/README.md +109 -0
- data/Rakefile +2 -0
- data/lib/validations_step_fu/version.rb +3 -0
- data/lib/validations_step_fu.rb +36 -0
- data/test/test_helper.rb +3 -0
- data/test/validations_step_fu_test.rb +8 -0
- data/validations_step_fu.gemspec +27 -0
- metadata +111 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Kosuke Matsuda
|
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
data/README.md
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# ValidationsStepFu
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
5
|
+
1つのモデルの入力フォームが複数のページにまたがる場合のvalidationsをDRYに保つためのRails 3プラグイン
|
6
|
+
|
7
|
+
|
8
|
+
## Example
|
9
|
+
|
10
|
+
以下のように User モデルの入力フォームが複数のページにまたがる場合
|
11
|
+
|
12
|
+
### views
|
13
|
+
|
14
|
+
# app/views/users/step1.html.erb
|
15
|
+
<%- form_for @user, :url => step2_user_path do |f| -%>
|
16
|
+
<%= f.text_field :name %>
|
17
|
+
<%- end -%>
|
18
|
+
|
19
|
+
# app/views/users/step2.html.erb
|
20
|
+
<%- form_for @user, :url => confirm_new_user_path do |f| -%>
|
21
|
+
<%= f.text_field :age %>
|
22
|
+
<%= f.text_field :hobby %>
|
23
|
+
<%- end -%>
|
24
|
+
|
25
|
+
### controllers
|
26
|
+
|
27
|
+
# app/controllers/users_controller.rb
|
28
|
+
def step2
|
29
|
+
unless @user.valid_step_2?
|
30
|
+
render :new and return
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def confirm
|
35
|
+
unless @user.valid_step_confirm?
|
36
|
+
render :step1 and return
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
このプラグインを使わない場合のvalidationの実装
|
41
|
+
|
42
|
+
### models
|
43
|
+
|
44
|
+
# app/models/user.rb
|
45
|
+
validates :name, :presence => true, :length => { :maximum => 30 }
|
46
|
+
validates :age, :hobby, :presence => true
|
47
|
+
|
48
|
+
def valid_step_2?
|
49
|
+
self.errors.clear
|
50
|
+
if self.name.blank? # <- oops!
|
51
|
+
self.errors.add :name, :blank
|
52
|
+
elsif self.name.length > 30 # <- oops!
|
53
|
+
self.errors.add :name, :too_long, :count => 30
|
54
|
+
end
|
55
|
+
self.errors.empty?
|
56
|
+
end
|
57
|
+
|
58
|
+
def valid_step_confirm?
|
59
|
+
self.errors.clear
|
60
|
+
if self.age.blank? # <- oops!
|
61
|
+
self.errors.add :age, :blank
|
62
|
+
end
|
63
|
+
if self.hobby.blank? # <- oops!
|
64
|
+
self.errors.add :hobby, :blank
|
65
|
+
end
|
66
|
+
self.errors.empty?
|
67
|
+
end
|
68
|
+
|
69
|
+
このプラグインを使った場合のvalidationの実装
|
70
|
+
|
71
|
+
# app/models/user.rb
|
72
|
+
validates :name, :presence => true, :length => { :maximum => 30 }
|
73
|
+
validates :age, :hobby, :presence => true
|
74
|
+
|
75
|
+
def valid_step_2?
|
76
|
+
self.errors.clear
|
77
|
+
validates_step_by(:name) # <- awesome!
|
78
|
+
self.errors.empty?
|
79
|
+
end
|
80
|
+
|
81
|
+
def valid_step_confirm?
|
82
|
+
self.errors.clear
|
83
|
+
validates_step_by(:age, :hobby) # <- awesome!
|
84
|
+
self.errors.empty?
|
85
|
+
end
|
86
|
+
|
87
|
+
また、以下のようにも書くことが可能
|
88
|
+
|
89
|
+
# app/models/user.rb
|
90
|
+
validates :name, :presence => true, :length => { :maximum => 30 }
|
91
|
+
validates :age, :hobby, :presence => true
|
92
|
+
|
93
|
+
define_step_validation '1', :name # <- awesome!
|
94
|
+
define_step_validation :confirm, :age, :hobby # <- awesome!
|
95
|
+
|
96
|
+
|
97
|
+
## Features
|
98
|
+
|
99
|
+
### instance methods
|
100
|
+
|
101
|
+
* validates\_step\_by
|
102
|
+
|
103
|
+
### class methods
|
104
|
+
|
105
|
+
* define\_step\_validation
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
Copyright (c) 2011 Kosuke Matsuda, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module ValidationsStepFu
|
2
|
+
|
3
|
+
module Definition
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module InstanceMethods
|
7
|
+
def define_step_validation(name, *attrs)
|
8
|
+
define_method "valid_step_#{name}?" do
|
9
|
+
self.errors.clear
|
10
|
+
validates_step_by(*attrs)
|
11
|
+
self.errors.empty?
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module Extension
|
18
|
+
extend ActiveSupport::Concern
|
19
|
+
|
20
|
+
module InstanceMethods
|
21
|
+
private
|
22
|
+
def validates_step_by(*attrs)
|
23
|
+
attrs = attrs.map(&:to_sym)
|
24
|
+
self.class.validators.each do |validator|
|
25
|
+
if (validator.attributes & attrs).present?
|
26
|
+
validator.validate(self)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
ActiveModel::Validations.send :include, ValidationsStepFu::Extension
|
36
|
+
ActiveModel::Validations::ClassMethods.send :include, ValidationsStepFu::Definition
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "validations_step_fu/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "validations_step_fu"
|
7
|
+
s.version = ValidationsStepFu::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Kosuke Matsuda"]
|
10
|
+
s.email = ["hi@matsuda.me"]
|
11
|
+
s.homepage = "https://github.com/matsuda/validations_step_fu"
|
12
|
+
s.summary = %q{A extension activemodel's validations to validation step by step.}
|
13
|
+
s.description = %q{A extension activemodel's validations to validation step by step.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "validations_step_fu"
|
16
|
+
s.required_rubygems_version = ">= 1.3.6"
|
17
|
+
|
18
|
+
s.add_dependency "railties", ">= 3.0.0"
|
19
|
+
s.add_dependency "activemodel", ">= 3.0.0"
|
20
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
21
|
+
s.add_development_dependency "rails", ">= 3.0.0"
|
22
|
+
|
23
|
+
s.files = `git ls-files`.split("\n")
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
26
|
+
s.require_paths = ["lib"]
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validations_step_fu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kosuke Matsuda
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-04-22 00:00:00 +09:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: railties
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 3.0.0
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activemodel
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 3.0.0
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: bundler
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 1.0.0
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rails
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 3.0.0
|
58
|
+
type: :development
|
59
|
+
version_requirements: *id004
|
60
|
+
description: A extension activemodel's validations to validation step by step.
|
61
|
+
email:
|
62
|
+
- hi@matsuda.me
|
63
|
+
executables: []
|
64
|
+
|
65
|
+
extensions: []
|
66
|
+
|
67
|
+
extra_rdoc_files: []
|
68
|
+
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- Gemfile
|
72
|
+
- MIT-LICENSE
|
73
|
+
- README
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- lib/validations_step_fu.rb
|
77
|
+
- lib/validations_step_fu/version.rb
|
78
|
+
- test/test_helper.rb
|
79
|
+
- test/validations_step_fu_test.rb
|
80
|
+
- validations_step_fu.gemspec
|
81
|
+
has_rdoc: true
|
82
|
+
homepage: https://github.com/matsuda/validations_step_fu
|
83
|
+
licenses: []
|
84
|
+
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: "0"
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 1.3.6
|
102
|
+
requirements: []
|
103
|
+
|
104
|
+
rubyforge_project: validations_step_fu
|
105
|
+
rubygems_version: 1.5.0
|
106
|
+
signing_key:
|
107
|
+
specification_version: 3
|
108
|
+
summary: A extension activemodel's validations to validation step by step.
|
109
|
+
test_files:
|
110
|
+
- test/test_helper.rb
|
111
|
+
- test/validations_step_fu_test.rb
|