devise_couch 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +19 -0
- data/Gemfile.lock +128 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +86 -0
- data/Rakefile +60 -0
- data/lib/devise/orm/couchrest_model/compatibility.rb +89 -0
- data/lib/devise/orm/couchrest_model/couch_validations.rb +88 -0
- data/lib/devise/orm/couchrest_model/date_time.rb +14 -0
- data/lib/devise/orm/couchrest_model/schema.rb +85 -0
- data/lib/devise/orm/couchrest_model.rb +40 -0
- data/lib/devise/orm/couchrest_model_active_model.rb +72 -0
- data/lib/devise_couch/version.rb +5 -0
- data/lib/generators/couchrest_model/devise_generator.rb +17 -0
- metadata +76 -0
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
|
3
|
+
#gem 'rails', '3.0.0.rc'
|
4
|
+
gem 'rails', :git => 'git://github.com/rails/rails.git'
|
5
|
+
|
6
|
+
gem 'warden'
|
7
|
+
gem 'devise', :git => 'git://github.com/plataformatec/devise.git'
|
8
|
+
|
9
|
+
gem 'couchrest'
|
10
|
+
gem 'couchrest_extended_document'
|
11
|
+
gem 'couchrest_model'
|
12
|
+
|
13
|
+
gem 'oauth2'
|
14
|
+
|
15
|
+
gem 'rails3-generators', :git => 'git://github.com/shenoudab/rails3-generators.git'
|
16
|
+
|
17
|
+
gem 'webrat', '0.7.0'
|
18
|
+
gem 'mocha', :require => false
|
19
|
+
gem 'bcrypt-ruby', :require => 'bcrypt'
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
GIT
|
2
|
+
remote: git://github.com/plataformatec/devise.git
|
3
|
+
revision: 701bbf2
|
4
|
+
specs:
|
5
|
+
devise (1.2.0)
|
6
|
+
bcrypt-ruby (~> 2.1.2)
|
7
|
+
warden (~> 0.10.7)
|
8
|
+
|
9
|
+
GIT
|
10
|
+
remote: git://github.com/rails/rails.git
|
11
|
+
revision: 2a98480
|
12
|
+
specs:
|
13
|
+
actionmailer (3.0.0.rc)
|
14
|
+
actionpack (= 3.0.0.rc)
|
15
|
+
mail (~> 2.2.5)
|
16
|
+
actionpack (3.0.0.rc)
|
17
|
+
activemodel (= 3.0.0.rc)
|
18
|
+
activesupport (= 3.0.0.rc)
|
19
|
+
builder (~> 2.1.2)
|
20
|
+
erubis (~> 2.6.6)
|
21
|
+
i18n (~> 0.4.1)
|
22
|
+
rack (~> 1.2.1)
|
23
|
+
rack-mount (~> 0.6.9)
|
24
|
+
rack-test (~> 0.5.4)
|
25
|
+
tzinfo (~> 0.3.22)
|
26
|
+
activemodel (3.0.0.rc)
|
27
|
+
activesupport (= 3.0.0.rc)
|
28
|
+
builder (~> 2.1.2)
|
29
|
+
i18n (~> 0.4.1)
|
30
|
+
activerecord (3.0.0.rc)
|
31
|
+
activemodel (= 3.0.0.rc)
|
32
|
+
activesupport (= 3.0.0.rc)
|
33
|
+
arel (~> 0.4.0)
|
34
|
+
tzinfo (~> 0.3.22)
|
35
|
+
activeresource (3.0.0.rc)
|
36
|
+
activemodel (= 3.0.0.rc)
|
37
|
+
activesupport (= 3.0.0.rc)
|
38
|
+
activesupport (3.0.0.rc)
|
39
|
+
rails (3.0.0.rc)
|
40
|
+
actionmailer (= 3.0.0.rc)
|
41
|
+
actionpack (= 3.0.0.rc)
|
42
|
+
activerecord (= 3.0.0.rc)
|
43
|
+
activeresource (= 3.0.0.rc)
|
44
|
+
activesupport (= 3.0.0.rc)
|
45
|
+
bundler (>= 1.0.0.rc.2)
|
46
|
+
railties (= 3.0.0.rc)
|
47
|
+
railties (3.0.0.rc)
|
48
|
+
actionpack (= 3.0.0.rc)
|
49
|
+
activesupport (= 3.0.0.rc)
|
50
|
+
rake (>= 0.8.3)
|
51
|
+
thor (~> 0.14.0)
|
52
|
+
|
53
|
+
GEM
|
54
|
+
remote: http://rubygems.org/
|
55
|
+
specs:
|
56
|
+
abstract (1.0.0)
|
57
|
+
addressable (2.2.0)
|
58
|
+
arel (0.4.0)
|
59
|
+
activesupport (>= 3.0.0.beta)
|
60
|
+
bcrypt-ruby (2.1.2)
|
61
|
+
builder (2.1.2)
|
62
|
+
couchrest (1.0.0)
|
63
|
+
json (= 1.2.4)
|
64
|
+
mime-types (>= 1.15)
|
65
|
+
rest-client (>= 0.5)
|
66
|
+
couchrest_extended_document (1.0.0)
|
67
|
+
activesupport (>= 2.3.0)
|
68
|
+
builder (>= 2.1.2)
|
69
|
+
couchrest (>= 1.0.0)
|
70
|
+
mime-types (>= 1.15)
|
71
|
+
couchrest_model (1.0.0.beta7)
|
72
|
+
activemodel (>= 3.0.0.beta4)
|
73
|
+
activesupport (>= 2.3.5)
|
74
|
+
couchrest (>= 1.0.0.beta)
|
75
|
+
mime-types (>= 1.15)
|
76
|
+
erubis (2.6.6)
|
77
|
+
abstract (>= 1.0.0)
|
78
|
+
faraday (0.4.6)
|
79
|
+
addressable (>= 2.1.1)
|
80
|
+
rack (>= 1.0.1)
|
81
|
+
i18n (0.4.1)
|
82
|
+
json (1.2.4)
|
83
|
+
mail (2.2.5)
|
84
|
+
activesupport (>= 2.3.6)
|
85
|
+
mime-types
|
86
|
+
treetop (>= 1.4.5)
|
87
|
+
mime-types (1.16)
|
88
|
+
mocha (0.9.8)
|
89
|
+
rake
|
90
|
+
multi_json (0.0.4)
|
91
|
+
nokogiri (1.4.3.1)
|
92
|
+
oauth2 (0.0.10)
|
93
|
+
faraday (~> 0.4.1)
|
94
|
+
multi_json (>= 0.0.4)
|
95
|
+
polyglot (0.3.1)
|
96
|
+
rack (1.2.1)
|
97
|
+
rack-mount (0.6.9)
|
98
|
+
rack (>= 1.0.0)
|
99
|
+
rack-test (0.5.4)
|
100
|
+
rack (>= 1.0)
|
101
|
+
rake (0.8.7)
|
102
|
+
rest-client (1.6.0)
|
103
|
+
mime-types (>= 1.16)
|
104
|
+
thor (0.14.0)
|
105
|
+
treetop (1.4.8)
|
106
|
+
polyglot (>= 0.3.1)
|
107
|
+
tzinfo (0.3.22)
|
108
|
+
warden (0.10.7)
|
109
|
+
rack (>= 1.0.0)
|
110
|
+
webrat (0.7.0)
|
111
|
+
nokogiri (>= 1.2.0)
|
112
|
+
rack (>= 1.0)
|
113
|
+
rack-test (>= 0.5.3)
|
114
|
+
|
115
|
+
PLATFORMS
|
116
|
+
ruby
|
117
|
+
|
118
|
+
DEPENDENCIES
|
119
|
+
bcrypt-ruby
|
120
|
+
couchrest
|
121
|
+
couchrest_extended_document
|
122
|
+
couchrest_model
|
123
|
+
devise!
|
124
|
+
mocha
|
125
|
+
oauth2
|
126
|
+
rails!
|
127
|
+
warden
|
128
|
+
webrat (= 0.7.0)
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2010 YOURNAME
|
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.markdown
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# DeviseCouch for Devise and CouchrestModel
|
2
|
+
|
3
|
+
*devise_couch* will let you use [devise](http://github.com/plataformatec/devise) with [CocuhrestModel](http://github.com/couchrest/couchrest_model).
|
4
|
+
|
5
|
+
*devise_couch* is intended for use with *Rails 3* and *Devise 1.1.1*. It may work with earlier versions of devise, but it's not been tested.
|
6
|
+
|
7
|
+
This README only covers *devise_couch* specifics. Make sure to read the [devise README](http://github.com/plataformatec/devise/blob/master/README.rdoc)
|
8
|
+
|
9
|
+
## Usage options
|
10
|
+
|
11
|
+
The gem gives you the options of 2 ORM setups depending on what library you wish to use for validations:
|
12
|
+
|
13
|
+
* *couchrest_model* - Uses CocuhrestModel validations
|
14
|
+
* *couchrest_model_active_model* - Uses ActiveModel::Validations
|
15
|
+
|
16
|
+
The advantage to this is ActiveModel's I18n support for error messages, and it uses the same validations lib as devise does by default.
|
17
|
+
|
18
|
+
## ActiveModel compatibility
|
19
|
+
|
20
|
+
Currently only the <code>couchrest_model</code> option has been tested.
|
21
|
+
There should be full ActiveModel support in a future version of CocuhrestModel, after Rails 3 is released.
|
22
|
+
ActiveModel support will likely be part of *CocuhrestModel 1.0* (as mentioned by jnunemaker in a post).
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
|
26
|
+
Add *devise*, *devise_couch* and *couchrest_model* gems to your Gemfile (your Rails app Gemfile). The following gems are required
|
27
|
+
|
28
|
+
<pre>
|
29
|
+
gem 'couchrest'
|
30
|
+
gem 'couchrest_extended_document'
|
31
|
+
gem 'couchrest_model'
|
32
|
+
gem 'devise'
|
33
|
+
gem 'devise_couch'
|
34
|
+
gem 'rails3-generators', :git => 'git://github.com/shenoudab/rails3-generators.git'
|
35
|
+
</pre>
|
36
|
+
|
37
|
+
<pre>gem install rails3-generators</pre>
|
38
|
+
|
39
|
+
Alternatively use bundler to install all required gems in your Rails 3 app
|
40
|
+
|
41
|
+
<pre>bundle install</pre>
|
42
|
+
|
43
|
+
Run the generator:
|
44
|
+
|
45
|
+
<pre>rails generate devise:install</pre>
|
46
|
+
|
47
|
+
The generator will install an initializer which describes ALL Devise's
|
48
|
+
configuration options and you MUST take a look at it. Make sure to specify
|
49
|
+
either <code>mongo_mapper</code> or <code>mongo_mapper_active_model</code> (ActiveModel::Validations)
|
50
|
+
as the orm in the configuration file.
|
51
|
+
|
52
|
+
To add Devise to any of your models using the generator:
|
53
|
+
|
54
|
+
<pre>rails generate couchrest_model:devise MODEL</pre>
|
55
|
+
|
56
|
+
Example: create a User model for use with Devise
|
57
|
+
|
58
|
+
<pre>rails generate couchrest_model:devise User</pre>
|
59
|
+
|
60
|
+
Read the README for devise at [devise README](http://github.com/plataformatec/devise/blob/master/README.rdoc)
|
61
|
+
|
62
|
+
## Note on Patches/Pull Requests
|
63
|
+
|
64
|
+
* Fork the project.
|
65
|
+
* Make your feature addition or bug fix.
|
66
|
+
* Add tests for it. This is important so I don't break it in a
|
67
|
+
future version unintentionally.
|
68
|
+
* Commit, do not mess with rakefile, version, or history.
|
69
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
70
|
+
* Send me a pull request. Bonus points for topic branches.
|
71
|
+
|
72
|
+
## Thanks
|
73
|
+
|
74
|
+
*Kristian Mandrup* - - Created the mm-devise gem which was used as a template for the development of this gem.
|
75
|
+
Also made suggestions for a few critical fixes and improvements in the code. Thanks :)
|
76
|
+
|
77
|
+
*Jared Morgan* - Created the dm-devise gem which was used as a template for the development of this gem.
|
78
|
+
Also made suggestions for a few critical fixes and improvements in the code. Thanks :)
|
79
|
+
|
80
|
+
## Bugs and Feedback
|
81
|
+
|
82
|
+
For *devise_couch* specific issues, please create an issue on GitHub at: [devise_couch issues](http://github.com/shenoudab/devise_couch/issues)
|
83
|
+
|
84
|
+
## Copyright
|
85
|
+
|
86
|
+
Copyright (c) 2010 Shenouda Bertel MobiThought. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
require 'rake/testtask'
|
6
|
+
require File.join(File.dirname(__FILE__), 'lib', 'devise_couch', 'version')
|
7
|
+
|
8
|
+
desc 'Default: run tests for all CouchDB ORM setups.'
|
9
|
+
task :default => :pre_commit
|
10
|
+
|
11
|
+
desc 'Run Devise tests for all CouchDB ORM setups.'
|
12
|
+
task :pre_commit do
|
13
|
+
Dir[File.join(File.dirname(__FILE__), 'test', 'orm', '*.rb')].each do |file|
|
14
|
+
orm = File.basename(file).split(".").first
|
15
|
+
ENV['DEVISE_PATH'] ||= File.expand_path('../devise')
|
16
|
+
system "rake test DEVISE_ORM=#{orm} DEVISE_PATH=#{ENV['DEVISE_PATH']}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
desc 'Run Devise tests using CouchDB. Specify path to devise with DEVISE_PATH'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
ENV['DEVISE_ORM'] ||= 'couchrest_model'
|
23
|
+
ENV['DEVISE_PATH'] ||= File.expand_path('./')
|
24
|
+
unless File.exist?(ENV['DEVISE_PATH'])
|
25
|
+
puts "Specify the path to devise (e.g. rake DEVISE_PATH=/path/to/devise). Not found at #{ENV['DEVISE_PATH']}"
|
26
|
+
exit
|
27
|
+
end
|
28
|
+
test.libs << 'lib' << 'test'
|
29
|
+
test.libs << "#{ENV['DEVISE_PATH']}/lib"
|
30
|
+
test.libs << "#{ENV['DEVISE_PATH']}/test"
|
31
|
+
test.test_files = FileList["#{ENV['DEVISE_PATH']}/test/**/*_test.rb"] + FileList['test/**/*_test.rb']
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
36
|
+
rdoc.rdoc_dir = 'rdoc'
|
37
|
+
rdoc.title = 'DeviseCouch'
|
38
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
39
|
+
rdoc.rdoc_files.include('README.rdoc')
|
40
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
41
|
+
end
|
42
|
+
|
43
|
+
spec = Gem::Specification.new do |s|
|
44
|
+
s.name = "devise_couch"
|
45
|
+
s.summary = "CouchDB integration for Devise framework."
|
46
|
+
s.description = "CouchDB integration for Devise framework."
|
47
|
+
s.files = FileList["[A-Z]*", "{app,config,lib}/**/*"]
|
48
|
+
s.version = CouchrestModel::Devise::VERSION.dup
|
49
|
+
s.email = "sbertel@mobithought.com"
|
50
|
+
s.homepage = "http://github.com/shenoudab/devise_couch"
|
51
|
+
s.author = 'Shenouda Bertel'
|
52
|
+
end
|
53
|
+
|
54
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
55
|
+
end
|
56
|
+
|
57
|
+
desc "Install the gem #{spec.name}-#{spec.version}.gem"
|
58
|
+
task :install do
|
59
|
+
system("gem install pkg/#{spec.name}-#{spec.version}.gem --no-ri --no-rdoc")
|
60
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module Devise
|
2
|
+
module Orm
|
3
|
+
module CouchrestModel
|
4
|
+
module Compatibility
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
extend ActiveModel::Naming
|
9
|
+
|
10
|
+
include ActiveModel::Serializers::Xml
|
11
|
+
include ActiveModel::Serializers::JSON
|
12
|
+
|
13
|
+
def to_xml(options = {}, &block)
|
14
|
+
options[:except] ||= []
|
15
|
+
options[:except] << :_id
|
16
|
+
|
17
|
+
super options do |b|
|
18
|
+
b.id self.id
|
19
|
+
block.call(b) if block_given?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
# Hooks for confirmable
|
26
|
+
def before_create(*args)
|
27
|
+
wrap_hook(:before, :create, *args)
|
28
|
+
end
|
29
|
+
|
30
|
+
def after_create(*args)
|
31
|
+
wrap_hook(:after, :create, *args)
|
32
|
+
end
|
33
|
+
|
34
|
+
def before_save(*args)
|
35
|
+
wrap_hook(:before, :save, *args)
|
36
|
+
end
|
37
|
+
|
38
|
+
def wrap_hook(action, method, *args)
|
39
|
+
options = args.extract_options!
|
40
|
+
|
41
|
+
args.each do |callback|
|
42
|
+
callback_method = :"#{callback}_callback_wrap"
|
43
|
+
send action, method, callback_method
|
44
|
+
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
45
|
+
def #{callback_method}
|
46
|
+
#{callback} if #{options[:if] || true}
|
47
|
+
end
|
48
|
+
METHOD
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Add ActiveRecord like finder
|
53
|
+
def find(*args)
|
54
|
+
case args.first
|
55
|
+
when :first, :all
|
56
|
+
send(args.shift, *args)
|
57
|
+
else
|
58
|
+
where(:'_id' => args.first).first
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# def changed?
|
64
|
+
# dirty?
|
65
|
+
# end
|
66
|
+
|
67
|
+
def save(options=nil)
|
68
|
+
if options.is_a?(Hash) && options[:validate] == false
|
69
|
+
save!
|
70
|
+
else
|
71
|
+
super()
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def update_attribute(name, value)
|
76
|
+
update_attributes(name => value)
|
77
|
+
end
|
78
|
+
#
|
79
|
+
# def update_attributes(*args)
|
80
|
+
# update(*args)
|
81
|
+
# end
|
82
|
+
|
83
|
+
def invalid?
|
84
|
+
!valid?
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module Validatable
|
2
|
+
# Monkey-patch Validatable::Errors to support generation of error message from a Symbol.
|
3
|
+
class Errors
|
4
|
+
@@default_error_messages = {}
|
5
|
+
|
6
|
+
# Holds a hash with all the default error messages that can be replaced by your own copy or localizations.
|
7
|
+
def self.default_error_messages=(default_error_messages)
|
8
|
+
@@default_error_messages = default_error_messages
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.default_error_message(key, field, *values)
|
12
|
+
field = field.to_s.humanize
|
13
|
+
@@default_error_messages[key] % [field, *values].flatten
|
14
|
+
end
|
15
|
+
|
16
|
+
# original add before monkey-patch
|
17
|
+
# def add(attribute, message) #:nodoc:
|
18
|
+
# errors[attribute.to_sym] = [] if errors[attribute.to_sym].nil?
|
19
|
+
# errors[attribute.to_sym] << message
|
20
|
+
# end
|
21
|
+
|
22
|
+
alias_method :original_add, :add
|
23
|
+
|
24
|
+
# If the message is a Symbol, allow +default_error_message+ to generate
|
25
|
+
# the message, including translation.
|
26
|
+
def add(field_name, message)
|
27
|
+
if message.kind_of?(Symbol)
|
28
|
+
message = self.class.default_error_message(message, field_name)
|
29
|
+
end
|
30
|
+
fld_name = field_name.to_sym
|
31
|
+
errors[fld_name] = [] if errors[fld_name].nil?
|
32
|
+
original_add(fld_name, message) unless errors[fld_name].include?(message)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module Validatable
|
38
|
+
class ValidatesPresenceOf < ValidationBase #:nodoc:
|
39
|
+
def message(instance)
|
40
|
+
super || "can't be blank"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
module Validatable
|
46
|
+
class ValidatesLengthOf < ValidationBase #:nodoc:
|
47
|
+
def message(instance)
|
48
|
+
min = !within.nil? ? within.first : minimum
|
49
|
+
max = !within.nil? ? within.last : maximum
|
50
|
+
super || "must be between #{min} and #{max} characters long"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
# Default error messages consistent with ActiveModel messages and devise
|
57
|
+
# expectations.
|
58
|
+
Validatable::Errors.default_error_messages = {
|
59
|
+
:absent => 'must be absent',
|
60
|
+
:inclusion => 'is not included in the list',
|
61
|
+
:exclusion => 'is reserved',
|
62
|
+
:invalid => 'is invalid',
|
63
|
+
:confirmation => " doesn't match confirmation",
|
64
|
+
:accepted => 'must be accepted',
|
65
|
+
:nil => 'must not be nil',
|
66
|
+
:empty => "can't be empty",
|
67
|
+
:blank => "can't be blank",
|
68
|
+
:length_between => 'must be between %s and %s characters long',
|
69
|
+
:too_long => 'is too long (maximum is %s characters)',
|
70
|
+
:too_short => 'is too short (minimum is %s characters)',
|
71
|
+
:wrong_length => 'is the wrong length (should be %s characters)',
|
72
|
+
:taken => 'has already been taken',
|
73
|
+
:not_a_number => 'is not a number',
|
74
|
+
:not_an_integer => 'must be an integer',
|
75
|
+
:greater_than => 'must be greater than %s',
|
76
|
+
:greater_than_or_equal_to => 'must be greater than or equal to ',
|
77
|
+
:equal_to => 'must be equal to %s',
|
78
|
+
:not_equal_to => 'must not be equal to %s',
|
79
|
+
:less_than => 'must be less than %s',
|
80
|
+
:less_than_or_equal_to => 'must be less than or equal to %s',
|
81
|
+
:value_between => 'must be between %s and %s',
|
82
|
+
:odd => 'must be odd',
|
83
|
+
:even => 'must be even',
|
84
|
+
:primitive => 'must be of type %s',
|
85
|
+
:not_found => 'not found',
|
86
|
+
:already_confirmed => 'was already confirmed',
|
87
|
+
:not_locked => 'was not locked'
|
88
|
+
}
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module CouchrestModel
|
2
|
+
class ValidationUtil
|
3
|
+
class << self
|
4
|
+
attr_accessor :counter
|
5
|
+
|
6
|
+
def inc_counter
|
7
|
+
@counter ||= 0
|
8
|
+
@counter += 1
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
module Devise
|
14
|
+
module Orm
|
15
|
+
module CouchrestModel
|
16
|
+
module Schema
|
17
|
+
include Devise::Schema
|
18
|
+
|
19
|
+
SCHEMA_OPTIONS = {
|
20
|
+
:null => :required,
|
21
|
+
:limit => :length
|
22
|
+
}
|
23
|
+
|
24
|
+
# Tell how to apply schema methods. This automatically maps :limit to
|
25
|
+
# :length and :null to :required.
|
26
|
+
def apply_devise_schema(name, type, options={})
|
27
|
+
SCHEMA_OPTIONS.each do |old_key, new_key|
|
28
|
+
next unless options.key?(old_key)
|
29
|
+
if :null == old_key
|
30
|
+
# :required is opposite of :null
|
31
|
+
options[new_key] = !options.delete(old_key)
|
32
|
+
else
|
33
|
+
options[new_key] = options.delete(old_key)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
options.delete(:default) if options[:default].nil?
|
38
|
+
required_option = options.delete(:required)
|
39
|
+
length_option = options.delete(:length)
|
40
|
+
|
41
|
+
type = Time if type == DateTime
|
42
|
+
|
43
|
+
key name, type, options
|
44
|
+
|
45
|
+
counter = ::CouchrestModel::ValidationUtil.inc_counter
|
46
|
+
|
47
|
+
handle_email(name, counter) if name == :email
|
48
|
+
handle_length(name, counter, length_option) if length_option
|
49
|
+
handle_required(name, counter) if required_option
|
50
|
+
end
|
51
|
+
|
52
|
+
protected
|
53
|
+
|
54
|
+
def make_key prefix, name, counter
|
55
|
+
"#{prefix}_#{name}_#{counter}"
|
56
|
+
end
|
57
|
+
|
58
|
+
def handle_required name, counter
|
59
|
+
key = make_key :presence, name, counter
|
60
|
+
validates_presence_of name.to_sym, :key => key
|
61
|
+
end
|
62
|
+
|
63
|
+
def handle_email name, counter
|
64
|
+
key = make_key :unique, name, counter
|
65
|
+
validates_uniqueness_of name.to_sym, :key => key
|
66
|
+
end
|
67
|
+
|
68
|
+
def handle_length name, counter, length_option
|
69
|
+
key = make_key :length, name, counter
|
70
|
+
options = case length_option
|
71
|
+
when Fixnum
|
72
|
+
{:maximum => length_option}
|
73
|
+
when Hash
|
74
|
+
length_option
|
75
|
+
else
|
76
|
+
raise ArgumentError, "length validation option must be either a Fixnum or Hash"
|
77
|
+
end
|
78
|
+
options[:key] = key
|
79
|
+
|
80
|
+
validates_length_of name.to_sym, options
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "couchrest/model/base"
|
2
|
+
|
3
|
+
module Devise
|
4
|
+
module Orm
|
5
|
+
module CouchrestModel
|
6
|
+
module Hook
|
7
|
+
def devise_modules_hook!
|
8
|
+
extend Schema
|
9
|
+
#Include Compatibility
|
10
|
+
yield
|
11
|
+
return unless Devise.apply_schema
|
12
|
+
devise_modules.each { |m| send(m) if respond_to?(m, true) }
|
13
|
+
end
|
14
|
+
end #Hook
|
15
|
+
module Schema
|
16
|
+
include Devise::Schema
|
17
|
+
# Tell how to apply schema methods.
|
18
|
+
def apply_schema(name, type, options={})
|
19
|
+
return unless Devise.apply_schema
|
20
|
+
property name, :type => type
|
21
|
+
end
|
22
|
+
end #Module Schema
|
23
|
+
end #CouchRest
|
24
|
+
end #Orm
|
25
|
+
end #Devise
|
26
|
+
|
27
|
+
#CouchRest::Model::Base.send(:extend, Devise::Models)
|
28
|
+
#CouchRest::Model::Base.send(:include, Devise::Models)
|
29
|
+
|
30
|
+
#CouchRest::Model::Base.append_extensions(Devise::Models)
|
31
|
+
#CouchRest::Model::Base.append_extensions(Devise::Orm::CouchrestModel::Hook)
|
32
|
+
|
33
|
+
module CouchRest
|
34
|
+
module Model
|
35
|
+
class Base
|
36
|
+
extend ::Devise::Models
|
37
|
+
extend ::Devise::Orm::CouchrestModel::Hook
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'devise/orm/couchrest_model/compatibility'
|
2
|
+
require 'devise/orm/couchrest_model/schema'
|
3
|
+
require 'devise/orm/couchrest_model/date_time'
|
4
|
+
|
5
|
+
module Devise
|
6
|
+
module Orm
|
7
|
+
module CouchrestModel
|
8
|
+
module Hook
|
9
|
+
def devise_modules_hook!
|
10
|
+
extend Schema
|
11
|
+
include ActiveModel::Validations
|
12
|
+
include ActiveModelCompatibility
|
13
|
+
include Compatibility
|
14
|
+
class << self; attr_reader :descendants; end;
|
15
|
+
|
16
|
+
def self.validates_uniqueness_of(*fields)
|
17
|
+
validates_with UniquenessValidator, _merge_attributes(fields)
|
18
|
+
end
|
19
|
+
|
20
|
+
yield
|
21
|
+
|
22
|
+
return unless Devise.apply_schema
|
23
|
+
devise_modules.each { |m| send(m) if respond_to?(m, true) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# http://github.com/jkaramon/CouchrestModel/blob/rails3/lib/couchrest_model/plugins/validations.rb
|
28
|
+
# ==============================================================
|
29
|
+
# validates_each in couchrest_model should work just fine!
|
30
|
+
# ==============================================================
|
31
|
+
|
32
|
+
# class UniquenessValidator < ActiveModel::EachValidator
|
33
|
+
# def validate_each(target, attribute, value)
|
34
|
+
# resource = ::DataMapper.repository(target.repository.name) { target.model.first(attribute => value) }
|
35
|
+
# if resource.nil? || (target.saved? && resource.key == target.key)
|
36
|
+
# return true
|
37
|
+
# else
|
38
|
+
# target.errors.add(attribute, :taken)
|
39
|
+
# return false
|
40
|
+
# end
|
41
|
+
# end
|
42
|
+
# end
|
43
|
+
|
44
|
+
module ActiveModelCompatibility
|
45
|
+
# include ActiveModel::Validations does not make save check valid?.
|
46
|
+
# This may not be the best solution, but it seems to work. Note that
|
47
|
+
# Compatibility is included after this module; its #save method handles
|
48
|
+
# the :validate => false option.
|
49
|
+
def save(*args)
|
50
|
+
retval = valid? && super(*args)
|
51
|
+
assert_save_successful(:save, retval)
|
52
|
+
retval
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
CouchRest::Model::Base.send(:extend, Devise::Models)
|
60
|
+
CouchRest::Model::Base.send(:include, Devise::Models)
|
61
|
+
|
62
|
+
#CouchRest::Model::Base.append_extensions(Devise::Models)
|
63
|
+
#CouchRest::Model::Base.append_extensions(Devise::Orm::CouchrestModel::Hook)
|
64
|
+
|
65
|
+
#module CouchRest
|
66
|
+
# module Model
|
67
|
+
# module Base
|
68
|
+
# extend ::Devise::Models
|
69
|
+
# extend ::Devise::Orm::CouchrestModel::Hook
|
70
|
+
# end
|
71
|
+
# end
|
72
|
+
#end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'generators/devise/orm_helpers'
|
2
|
+
|
3
|
+
module CouchrestModel
|
4
|
+
module Generators
|
5
|
+
class DeviseGenerator < Rails::Generators::NamedBase
|
6
|
+
include Devise::Generators::OrmHelpers
|
7
|
+
|
8
|
+
def generate_model
|
9
|
+
invoke "couchrest_model:model", [name] unless model_exists?
|
10
|
+
end
|
11
|
+
|
12
|
+
def inject_devise_content
|
13
|
+
inject_into_file model_path, model_contents, :after => "CouchRest::Model::Base\n"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: devise_couch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Shenouda Bertel
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-13 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: CouchDB integration for Devise framework.
|
22
|
+
email: sbertel@mobithought.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- MIT-LICENSE
|
31
|
+
- Gemfile
|
32
|
+
- Gemfile.lock
|
33
|
+
- Rakefile
|
34
|
+
- README.markdown
|
35
|
+
- lib/devise_couch/version.rb
|
36
|
+
- lib/devise/orm/couchrest_model/date_time.rb
|
37
|
+
- lib/devise/orm/couchrest_model/schema.rb
|
38
|
+
- lib/devise/orm/couchrest_model/couch_validations.rb
|
39
|
+
- lib/devise/orm/couchrest_model/compatibility.rb
|
40
|
+
- lib/devise/orm/couchrest_model_active_model.rb
|
41
|
+
- lib/devise/orm/couchrest_model.rb
|
42
|
+
- lib/generators/couchrest_model/devise_generator.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/shenoudab/devise_couch
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.3.7
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: CouchDB integration for Devise framework.
|
75
|
+
test_files: []
|
76
|
+
|