mm-devise 1.1.10 → 1.2

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.
@@ -1,91 +0,0 @@
1
- require 'validatable'
2
-
3
- module Validatable
4
- # Monkey-patch Validatable::Errors to support generation of error message from
5
- # a Symbol.
6
- class Errors
7
- @@default_error_messages = {}
8
-
9
- # Holds a hash with all the default error messages that can be replaced by your own copy or localizations.
10
- def self.default_error_messages=(default_error_messages)
11
- @@default_error_messages = default_error_messages
12
- end
13
-
14
- def self.default_error_message(key, field, *values)
15
- field = field.to_s.humanize
16
- @@default_error_messages[key] % [field, *values].flatten
17
- end
18
-
19
- # original add before monkey-patch
20
- # def add(attribute, message) #:nodoc:
21
- # errors[attribute.to_sym] = [] if errors[attribute.to_sym].nil?
22
- # errors[attribute.to_sym] << message
23
- # end
24
-
25
- alias_method :original_add, :add
26
-
27
- # If the message is a Symbol, allow +default_error_message+ to generate
28
- # the message, including translation.
29
- def add(field_name, message)
30
- if message.kind_of?(Symbol)
31
- message = self.class.default_error_message(message, field_name)
32
- end
33
- fld_name = field_name.to_sym
34
- errors[fld_name] = [] if errors[fld_name].nil?
35
- original_add(fld_name, message) unless errors[fld_name].include?(message)
36
- end
37
- end
38
- end
39
-
40
- module Validatable
41
- class ValidatesPresenceOf < ValidationBase #:nodoc:
42
- def message(instance)
43
- super || "can't be blank"
44
- end
45
- end
46
- end
47
-
48
- module Validatable
49
- class ValidatesLengthOf < ValidationBase #:nodoc:
50
- def message(instance)
51
- min = !within.nil? ? within.first : minimum
52
- max = !within.nil? ? within.last : maximum
53
- super || "must be between #{min} and #{max} characters long"
54
- end
55
- end
56
- end
57
-
58
-
59
- # Default error messages consistent with ActiveModel messages and devise
60
- # expectations.
61
- Validatable::Errors.default_error_messages = {
62
- :absent => 'must be absent',
63
- :inclusion => 'is not included in the list',
64
- :exclusion => 'is reserved',
65
- :invalid => 'is invalid',
66
- :confirmation => " doesn't match confirmation",
67
- :accepted => 'must be accepted',
68
- :nil => 'must not be nil',
69
- :empty => "can't be empty",
70
- :blank => "can't be blank",
71
- :length_between => 'must be between %s and %s characters long',
72
- :too_long => 'is too long (maximum is %s characters)',
73
- :too_short => 'is too short (minimum is %s characters)',
74
- :wrong_length => 'is the wrong length (should be %s characters)',
75
- :taken => 'has already been taken',
76
- :not_a_number => 'is not a number',
77
- :not_an_integer => 'must be an integer',
78
- :greater_than => 'must be greater than %s',
79
- :greater_than_or_equal_to => 'must be greater than or equal to ',
80
- :equal_to => 'must be equal to %s',
81
- :not_equal_to => 'must not be equal to %s',
82
- :less_than => 'must be less than %s',
83
- :less_than_or_equal_to => 'must be less than or equal to %s',
84
- :value_between => 'must be between %s and %s',
85
- :odd => 'must be odd',
86
- :even => 'must be even',
87
- :primitive => 'must be of type %s',
88
- :not_found => 'not found',
89
- :already_confirmed => 'was already confirmed',
90
- :not_locked => 'was not locked'
91
- }
@@ -1,86 +0,0 @@
1
- module MongoMapper
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
-
14
- module Devise
15
- module Orm
16
- module MongoMapper
17
- module Schema
18
- include Devise::Schema
19
-
20
- SCHEMA_OPTIONS = {
21
- :null => :required,
22
- :limit => :length
23
- }
24
-
25
- # Tell how to apply schema methods. This automatically maps :limit to
26
- # :length and :null to :required.
27
- def apply_devise_schema(name, type, options={})
28
- SCHEMA_OPTIONS.each do |old_key, new_key|
29
- next unless options.key?(old_key)
30
- if :null == old_key
31
- # :required is opposite of :null
32
- options[new_key] = !options.delete(old_key)
33
- else
34
- options[new_key] = options.delete(old_key)
35
- end
36
- end
37
-
38
- options.delete(:default) if options[:default].nil?
39
- required_option = options.delete(:required)
40
- length_option = options.delete(:length)
41
-
42
- type = Time if type == DateTime
43
-
44
- key name, type, options
45
-
46
- counter = ::MongoMapper::ValidationUtil.inc_counter
47
-
48
- handle_email(name, counter) if name == :email
49
- handle_length(name, counter, length_option) if length_option
50
- handle_required(name, counter) if required_option
51
- end
52
-
53
- protected
54
-
55
- def make_key prefix, name, counter
56
- "#{prefix}_#{name}_#{counter}"
57
- end
58
-
59
- def handle_required name, counter
60
- key = make_key :presence, name, counter
61
- validates_presence_of name.to_sym, :key => key
62
- end
63
-
64
- def handle_email name, counter
65
- key = make_key :unique, name, counter
66
- validates_uniqueness_of name.to_sym, :key => key
67
- end
68
-
69
- def handle_length name, counter, length_option
70
- key = make_key :length, name, counter
71
- options = case length_option
72
- when Fixnum
73
- {:maximum => length_option}
74
- when Hash
75
- length_option
76
- else
77
- raise ArgumentError, "length validation option must be either a Fixnum or Hash"
78
- end
79
- options[:key] = key
80
-
81
- validates_length_of name.to_sym, options
82
- end
83
- end
84
- end
85
- end
86
- end
@@ -1,68 +0,0 @@
1
- require 'mongo_mapper'
2
- require 'devise/orm/mongo_mapper/compatibility'
3
- require 'devise/orm/mongo_mapper/schema'
4
- require 'devise/orm/mongo_mapper/date_time'
5
-
6
- module Devise
7
- module Orm
8
- module MongoMapper
9
- module Hook
10
- def devise_modules_hook!
11
- extend Schema
12
- include ActiveModel::Validations
13
- include ActiveModelCompatibility
14
- include Compatibility
15
- class << self; attr_reader :descendants; end;
16
-
17
- def self.validates_uniqueness_of(*fields)
18
- validates_with UniquenessValidator, _merge_attributes(fields)
19
- end
20
-
21
- yield
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/mongomapper/blob/rails3/lib/mongo_mapper/plugins/validations.rb
28
- # ==============================================================
29
- # validates_each in mongo_mapper 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
- MongoMapper::Document.append_extensions(Devise::Models)
60
- MongoMapper::Document.append_extensions(Devise::Orm::MongoMapper::Hook)
61
-
62
-
63
- # module MongoMapper
64
- # module Document
65
- # extend ::Devise::Models
66
- # extend ::Devise::Orm::MongoMapper::Hook
67
- # end
68
- # end
@@ -1,8 +0,0 @@
1
- require 'rails/test_help'
2
-
3
- class ActiveSupport::TestCase
4
- setup do
5
- User.destroy_all
6
- Admin.destroy_all
7
- end
8
- end
@@ -1,46 +0,0 @@
1
- require 'test_helper'
2
-
3
- # See data_mapper_test.rb in this folder for what this file is doing.
4
- if DEVISE_ORM == :mongo_mapper
5
-
6
- class ValidatableTest < ActiveSupport::TestCase
7
- undef test_should_require_a_password_with_minimum_of_6_characters
8
-
9
- # DataMapper uses a :value_between error message when given a minimum and
10
- # maximum; ActiveModel shows either the :too_long or :too_short message.
11
- test 'should require a password with minimum of 6 characters' do
12
- user = new_user(:password => '12345', :password_confirmation => '12345')
13
- assert user.invalid?
14
- # assert_equal 'is too short (minimum is 6 characters)', user.errors[:password].join
15
- assert_equal 'must be between 6 and 20 characters long', user.errors[:password].join
16
- end
17
-
18
- undef test_should_require_a_password_with_maximum_of_20_characters_long
19
-
20
- # Same issue as previous test
21
- test 'should require a password with maximum of 20 characters long' do
22
- user = new_user(:password => 'x'*21, :password_confirmation => 'x'*21)
23
- assert user.invalid?
24
- # assert_equal 'is too long (maximum is 20 characters)', user.errors[:password].join
25
- assert_equal 'must be between 6 and 20 characters long', user.errors[:password].join
26
- end
27
-
28
- end
29
-
30
- class AuthenticationOthersTest < ActionController::IntegrationTest
31
-
32
- undef test_registration_in_xml_format_works_when_recognizing_path
33
-
34
- # DM's validates_confirmation_of requires the confirmation field to be present,
35
- # while ActiveModel by default skips the confirmation test if the confirmation
36
- # value is nil. This test takes advantage of AM's behavior, so just add the
37
- # :password_confirmation value.
38
- test 'registration in xml format works when recognizing path' do
39
- assert_nothing_raised do
40
- # post user_registration_path(:format => 'xml', :user => {:email => "test@example.com", :password => "invalid"} )
41
- post user_registration_path(:format => 'xml', :user => {:email => "test@example.com", :password => "invalid", :password_confirmation => "invalid"} )
42
- end
43
- end
44
- end
45
-
46
- end
@@ -1,48 +0,0 @@
1
- require 'test_helper'
2
-
3
- # This file contains test cases that override devise tests, in the cases that
4
- # the difference is values from DM versus those expected by devise is not
5
- # particularly important and getting DM to pass the original devise tests would
6
- # be difficult.
7
- #
8
- # This file contains tests shared by both data_mapper and
9
- # data_mapper_active_model ORM setups.
10
- # Tests specific to the data_mapper orm which uses dm-validations are in dm_validations_test.rb
11
- # Tests specific to the data_mapper_active_model orm which uses ActiveModel
12
- # validations would be in active_model_test.rb, but there aren't any (I would
13
- # be rather surprised if there ever were any).
14
- #
15
- # For each test, an explanation is given as to why I chose to override the test,
16
- # and the original assertion is commented above the DM-specific assertion.
17
-
18
- class TrackableHooksTest < ActionController::IntegrationTest
19
-
20
- undef test_current_and_last_sign_in_timestamps_are_updated_on_each_sign_in
21
-
22
- # DataMapper uses a DateTime type where ActiveRecord uses Time. The test is
23
- # that the tested properties are being set, so just check for kind_of?(DateTime)
24
- # instead of kind_of?(Time)
25
- test "current and last sign in timestamps are updated on each sign in" do
26
- user = create_user
27
- assert_nil user.current_sign_in_at
28
- assert_nil user.last_sign_in_at
29
-
30
- sign_in_as_user
31
- user.reload
32
-
33
- assert_kind_of Time, user.current_sign_in_at
34
- assert_kind_of Time, user.last_sign_in_at
35
-
36
- assert_equal user.current_sign_in_at, user.last_sign_in_at
37
- assert user.current_sign_in_at >= user.created_at
38
-
39
- visit destroy_user_session_path
40
- new_time = 2.seconds.from_now
41
- Time.stubs(:now).returns(new_time)
42
-
43
- sign_in_as_user
44
- user.reload
45
- assert user.current_sign_in_at > user.last_sign_in_at
46
- end
47
- end
48
-
@@ -1,13 +0,0 @@
1
- require 'shared_admin'
2
- require File.join(File.dirname(__FILE__), '../mongo_mapper/shim.rb')
3
-
4
- class Admin
5
- include MongoMapper::Document
6
-
7
- key :username, String
8
- timestamps!
9
-
10
- include SharedAdmin
11
- include Shim
12
-
13
- end
@@ -1,13 +0,0 @@
1
- require 'shared_user'
2
- require File.join(File.dirname(__FILE__), '../mongo_mapper/shim.rb')
3
-
4
- class User
5
- include MongoMapper::Document
6
-
7
- key :username, String
8
- key :facebook_token, String
9
- timestamps!
10
-
11
- include SharedUser
12
- include Shim
13
- end