activerecord-confirmable 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-confirmable.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Yoshimoto
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.en.md ADDED
@@ -0,0 +1,79 @@
1
+ # Activerecord::Confirmable
2
+
3
+ Confirmation view helper of model on creating or updating.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'activerecord-confirmable'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install activerecord-confirmable
18
+
19
+ ## Usage
20
+
21
+ This module is adding by virtual confimed attribute to your model. #confirmation? method returns submittion status.
22
+
23
+
24
+ validation fail
25
+ +---------------+-----------------------------+
26
+ | | |
27
+ v | |
28
+ +--------------+ submit +--------------+ submit
29
+ | new / edit | ----------> | confirmation | ----------> done
30
+ +--------------+ +--------------+
31
+ confirmed != '1' confirmed == '1'
32
+
33
+ ^ |
34
+ | back (set confirmed = false) |
35
+ +--------------------------------+
36
+
37
+ exapmle:
38
+
39
+ class Article
40
+ include ActiveRecord::Confirmable
41
+ end
42
+
43
+ class ArticlesController
44
+ def create
45
+ @article = Article.new(params[:article])
46
+ @article.confirmed = false if params[:commit] == "back"
47
+ if @article.save
48
+ redirect_to @article
49
+ else
50
+ render action: "new"
51
+ end
52
+ end
53
+
54
+ def update
55
+ @article = Article.find(params[:id])
56
+ params[:article][:confirmed] = false if params[:commit] == "back"
57
+ if @article.update_attributes(params[:article])
58
+ redirect_to @article
59
+ else
60
+ render action: "edit"
61
+ end
62
+ end
63
+ end
64
+
65
+ articles/_form.html.erb
66
+ <%= form_for(@article) do |f| %>
67
+ <%= f.text_field :title %>
68
+ <%= f.submit f.object.confirmation? ? "submit" : "confirm" %>
69
+ <%= f.submit "back" if f.object.confirmation? %>
70
+ <%= f.hidden_field :confirmed %>
71
+ <% end %>
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create new Pull Request
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # Activerecord::Confirmable
2
+
3
+ Railsで、controllerにconfirmアクションを作らずに、確認画面を実装するためのモジュールです。
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'activerecord-confirmable'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install activerecord-confirmable
18
+
19
+ ## Usage
20
+
21
+ このモジュールをモデルにincludeすると、confirmedアトリビュートが追加されます。フォームに、hidden_field :confirmed を追加すると確認画面に遷移するようになります。#confirmation? がtrueの場合はモデルが確認画面の状態なので、適切にviewを切り替えてください。
22
+
23
+ validation fail
24
+ +---------------+-----------------------------+
25
+ | | |
26
+ v | |
27
+ +--------------+ submit +--------------+ submit
28
+ | new / edit | ----------> | confirmation | ----------> done
29
+ +--------------+ +--------------+
30
+ confirmed != '1' confirmed == '1'
31
+
32
+ ^ |
33
+ | back (set confirmed = false) |
34
+ +--------------------------------+
35
+
36
+ exapmle:
37
+
38
+ class Article
39
+ include ActiveRecord::Confirmable
40
+ end
41
+
42
+ class ArticlesController
43
+ def create
44
+ @article = Article.new(params[:article])
45
+ @article.confirmed = false if params[:commit] == "back"
46
+ if @article.save
47
+ redirect_to @article
48
+ else
49
+ render action: "new"
50
+ end
51
+ end
52
+
53
+ def update
54
+ @article = Article.find(params[:id])
55
+ params[:article][:confirmed] = false if params[:commit] == "back"
56
+ if @article.update_attributes(params[:article])
57
+ redirect_to @article
58
+ else
59
+ render action: "edit"
60
+ end
61
+ end
62
+ end
63
+
64
+ articles/_form.html.erb
65
+ <%= form_for(@article) do |f| %>
66
+ <%= f.text_field :title %>
67
+ <%= f.submit f.object.confirmation? ? "submit" : "confirm" %>
68
+ <%= f.submit "back" if f.object.confirmation? %>
69
+ <%= f.hidden_field :confirmed %>
70
+ <% end %>
71
+
72
+ ## Contributing
73
+
74
+ 1. Fork it
75
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
76
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
77
+ 4. Push to the branch (`git push origin my-new-feature`)
78
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ task :default => [:spec]
5
+ begin
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = 'spec/**/*_spec.rb'
9
+ spec.rspec_opts = ['-cfs']
10
+ end
11
+ rescue LoadError => e
12
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/activerecord-confirmable/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Beyond"]
6
+ gem.email = ["beyond@be.to"]
7
+ gem.description = %q{ confirmatin view on model creation / changes }
8
+ gem.summary = %q{ confirmatin view without confirm action adding to controller. }
9
+ gem.homepage = "https://github.com/beyond/activerecord-confirmable"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "activerecord-confirmable"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Activerecord::Confirmable::VERSION
17
+
18
+ gem.add_runtime_dependency "activerecord"
19
+ gem.add_runtime_dependency "activesupport"
20
+
21
+ gem.add_development_dependency "rspec"
22
+ gem.add_development_dependency 'rake', '~> 0.9.2.2'
23
+ gem.add_development_dependency 'rdoc', '~> 3.11'
24
+ gem.add_development_dependency 'sqlite3'
25
+ end
@@ -0,0 +1,19 @@
1
+ module ActiveRecord
2
+ module Confirmable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ validates_acceptance_of :confirmed
7
+ after_rollback :check_confirming
8
+ end
9
+
10
+ def check_confirming
11
+ errors.messages.delete( :confirmed )
12
+ self.confirmed = errors.empty? ? '1' : '' if self.confirmed
13
+ end
14
+
15
+ def confirmation?
16
+ !self.confirmed.blank?
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ module Activerecord
2
+ module Confirmable
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,2 @@
1
+ require "activerecord-confirmable/version"
2
+ require "activerecord-confirmable/active_record/confirmable"
@@ -0,0 +1,135 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecord::Confirmable do
4
+ context "confirmed attribute is not set" do
5
+ subject { Article.new }
6
+
7
+ context "validation is fail" do
8
+ it "should not be valid" do
9
+ subject.should_not be_valid
10
+ subject.save.should be_false
11
+ subject.errors[ :confirmed ].should be_empty
12
+ end
13
+ end
14
+
15
+ context "validation is success" do
16
+ it "should be saved" do
17
+ subject.title = "title"
18
+ subject.should be_valid
19
+ subject.save.should be_true
20
+ end
21
+ end
22
+ end
23
+
24
+ context "confirmed is blank string (new state)" do
25
+ subject { Article.new confirmed: "" }
26
+
27
+ context "validation is fail" do
28
+ it "should not be in confirmation status yet" do
29
+ subject.should_not be_valid
30
+ subject.save.should be_false
31
+ subject.errors[ :confirmed ].should be_empty
32
+ subject.confirmation?.should be_false
33
+ end
34
+ end
35
+
36
+ context "validation is success" do
37
+ before do
38
+ subject.title = "title"
39
+ end
40
+
41
+ it "should be in confirmation" do
42
+ subject.should_not be_valid
43
+ subject.save.should be_false
44
+ subject.errors[ :confirmed ].should be_empty
45
+ subject.confirmation?.should be_true
46
+ end
47
+
48
+ it "should be accepted" do
49
+ subject.save
50
+ subject.confirmed.should == "1"
51
+ end
52
+ end
53
+ end
54
+
55
+ context "confirmed is '1' string (prepare to submit)" do
56
+ subject { Article.new confirmed: "1" }
57
+
58
+ context "validation is fail" do
59
+ it "should be required title" do
60
+ subject.should_not be_valid
61
+ subject.save.should be_false
62
+ subject.errors[ :confirmed ].should be_empty
63
+ subject.confirmation?.should be_false
64
+ end
65
+ end
66
+
67
+ context "validation is success" do
68
+ before do
69
+ subject.title = "title"
70
+ end
71
+
72
+ it "shoud be submittable" do
73
+ subject.should be_valid
74
+ subject.save.should be_true
75
+ end
76
+ end
77
+ end
78
+
79
+ context "confirmed is false (back button from confimation)" do
80
+ subject { Article.new confirmed: false }
81
+
82
+ context "validation is fail" do
83
+ it "should be new state" do
84
+ subject.should_not be_valid
85
+ subject.save.should be_false
86
+ subject.errors[ :confirmed ].should be_empty
87
+ subject.confirmation?.should be_false
88
+ end
89
+ end
90
+
91
+ context "validation is success" do
92
+ before do
93
+ subject.title = "title"
94
+ end
95
+
96
+ it "should be new state" do
97
+ subject.title = "title"
98
+ subject.should_not be_valid
99
+ subject.save.should be_false
100
+ subject.confirmation?.should be_false
101
+ end
102
+ end
103
+ end
104
+
105
+ context "confirmed is 'false' string (back and submit)" do
106
+ subject { Article.new confirmed: "false" }
107
+
108
+ context "validation is fail" do
109
+ it "should be new state" do
110
+ subject.should_not be_valid
111
+ subject.save.should be_false
112
+ subject.errors[ :confirmed ].should be_empty
113
+ subject.confirmation?.should be_false
114
+ end
115
+ end
116
+
117
+ context "validation is success" do
118
+ before do
119
+ subject.title = "title"
120
+ end
121
+
122
+ it "should be submittable" do
123
+ subject.should_not be_valid
124
+ subject.save.should be_false
125
+ subject.confirmation?.should be_true
126
+ end
127
+
128
+ it "should be accepted" do
129
+ subject.save
130
+ subject.confirmed.should == "1"
131
+ end
132
+ end
133
+ end
134
+ end
135
+
@@ -0,0 +1,36 @@
1
+ require 'active_record'
2
+ require File.expand_path("../lib/activerecord-confirmable", File.dirname(__FILE__))
3
+
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # Require this file using `require "spec_helper"` to ensure that it is only
7
+ # loaded once.
8
+ #
9
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
10
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
11
+
12
+ ActiveRecord::Schema.define do
13
+ create_table "articles", :force => true do |t|
14
+ t.string "title"
15
+ t.string "body"
16
+ end
17
+ end
18
+
19
+ class Article < ActiveRecord::Base
20
+ include ActiveRecord::Confirmable
21
+
22
+ attr_accessible :title, :confirmed
23
+ validates :title, presence: true
24
+ end
25
+
26
+ RSpec.configure do |config|
27
+ config.treat_symbols_as_metadata_keys_with_true_values = true
28
+ config.run_all_when_everything_filtered = true
29
+ config.filter_run :focus
30
+
31
+ # Run specs in random order to surface order dependencies. If you find an
32
+ # order dependency and want to debug it, you can fix the order by providing
33
+ # the seed, which is printed after each run.
34
+ # --seed 1234
35
+ config.order = 'random'
36
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-confirmable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Beyond
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &21337600 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *21337600
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &21336940 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *21336940
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &21336200 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *21336200
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &21335600 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.2.2
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *21335600
58
+ - !ruby/object:Gem::Dependency
59
+ name: rdoc
60
+ requirement: &21335080 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '3.11'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *21335080
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: &21334680 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *21334680
80
+ description: ! ' confirmatin view on model creation / changes '
81
+ email:
82
+ - beyond@be.to
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - .gitignore
88
+ - .rspec
89
+ - Gemfile
90
+ - LICENSE
91
+ - README.en.md
92
+ - README.md
93
+ - Rakefile
94
+ - activerecord-confirmable.gemspec
95
+ - lib/activerecord-confirmable.rb
96
+ - lib/activerecord-confirmable/active_record/confirmable.rb
97
+ - lib/activerecord-confirmable/version.rb
98
+ - spec/confirmable_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: https://github.com/beyond/activerecord-confirmable
101
+ licenses: []
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 1.8.10
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: confirmatin view without confirm action adding to controller.
124
+ test_files:
125
+ - spec/confirmable_spec.rb
126
+ - spec/spec_helper.rb