validate_as_url 0.0.5 → 0.0.6
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/.DS_Store +0 -0
- data/.gitignore +19 -16
- data/Gemfile.lock +17 -0
- data/README.rdoc +51 -0
- data/lib/validate_as_url.rb +54 -71
- data/validate_as_url.gemspec +1 -1
- metadata +5 -9
- data/.idea/encodings.xml +0 -5
- data/.idea/misc.xml +0 -5
- data/.idea/modules.xml +0 -9
- data/.idea/scopes/scope_settings.xml +0 -5
- data/.idea/validate_as_url.iml +0 -20
- data/.idea/vcs.xml +0 -7
- data/README.md +0 -29
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
CHANGED
@@ -1,17 +1,20 @@
|
|
1
|
-
*.gem
|
2
1
|
*.rbc
|
3
|
-
|
4
|
-
|
5
|
-
.
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
2
|
+
*.sassc
|
3
|
+
*.gem
|
4
|
+
.sass-cache
|
5
|
+
capybara-*.html
|
6
|
+
.rspec
|
7
|
+
/.bundle
|
8
|
+
/vendor/bundle
|
9
|
+
/log/*
|
10
|
+
/tmp/*
|
11
|
+
/db/*.sqlite3
|
12
|
+
/public/system/*
|
13
|
+
/coverage/
|
14
|
+
/spec/tmp/*
|
15
|
+
**.orig
|
16
|
+
rerun.txt
|
17
|
+
pickle-email-*.html
|
18
|
+
.idea/.name
|
19
|
+
|
20
|
+
.idea/*
|
data/Gemfile.lock
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
= validate_as_url Gem for Rails 3.x
|
2
|
+
|
3
|
+
Validate http and https urls for valid format with standard model validations.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
Installing as a gem:
|
8
|
+
|
9
|
+
gem install validate_as_url (recommended)
|
10
|
+
|
11
|
+
Or in your Rails 3 Gemfile
|
12
|
+
|
13
|
+
gem 'validate_as_url', :git => 'git://github.com/mdima-it-guru/validate_as_url.git'
|
14
|
+
|
15
|
+
For use in Rails project add to your Gemfile following line:
|
16
|
+
|
17
|
+
gem 'validate_as_url'
|
18
|
+
|
19
|
+
== Usage
|
20
|
+
|
21
|
+
# Rails 3
|
22
|
+
class Person < ActiveRecord::Base
|
23
|
+
validate_as_url :website
|
24
|
+
end
|
25
|
+
|
26
|
+
=== Options
|
27
|
+
|
28
|
+
:message_invalid:
|
29
|
+
String. This messages appears when supplied url has wrong format. Default message: 'does not appear to be valid url',
|
30
|
+
:message_nil
|
31
|
+
String. This messages appears when supplied url is nill and 'allow_nil' is false . Default message: 'cannot be null',
|
32
|
+
:message_blank
|
33
|
+
String. This messages appears when supplied url is blank string and 'allow_blank' is false . Default message: 'cannot be blank',
|
34
|
+
:on
|
35
|
+
Symbol. Specifies when this validation is active (default is :save, other options :create, :update)
|
36
|
+
:allow_nil
|
37
|
+
Boolean. Allow nil values (default is false)
|
38
|
+
:allow_blank
|
39
|
+
Boolean. Allow blank values (default is false)
|
40
|
+
|
41
|
+
== Testing
|
42
|
+
|
43
|
+
Tested in Ruby 1.9.3
|
44
|
+
|
45
|
+
== Resources
|
46
|
+
|
47
|
+
* https://github.com/mdima-it-guru/validate_as_url
|
48
|
+
|
49
|
+
== Credits
|
50
|
+
|
51
|
+
Written by Mitrofanov Dmitry (http://myitguru.info), 2013.
|
data/lib/validate_as_url.rb
CHANGED
@@ -1,82 +1,65 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
default_options = {
|
13
|
-
message_invalid: ::I18n.t(:invalid_url, :scope => [MessageScope, :errors, :messages], :default => 'does not appear to be valid url'),
|
14
|
-
message_nil: ::I18n.t(:invalid_url, :scope => [MessageScope, :errors, :messages], :default => 'cannot be null'),
|
15
|
-
message_blank: ::I18n.t(:invalid_url, :scope => [MessageScope, :errors, :messages], :default => 'cannot be blank'),
|
16
|
-
allow_nil: false,
|
17
|
-
allow_blank: false
|
18
|
-
}
|
19
|
-
opts = options.merge(default_options) # merge the default options into the specified options, retaining all specified options
|
20
|
-
|
21
|
-
if opts[:allow_nil] == false
|
22
|
-
if url.nil?
|
23
|
-
return [ opts[:message_nil] ]
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
url = url.to_s.strip
|
28
|
-
|
29
|
-
if opts[:allow_blank] == false
|
30
|
-
if url.blank?
|
31
|
-
return [ opts[:message_blank] ]
|
1
|
+
module ActiveRecord
|
2
|
+
module Validations
|
3
|
+
class ValidateAsUrl < ActiveModel::EachValidator # :nodoc:
|
4
|
+
def initialize(options)
|
5
|
+
default_options = {
|
6
|
+
message: 'does not appear to be valid url',
|
7
|
+
allow_nil: false,
|
8
|
+
allow_blank: false
|
9
|
+
}
|
10
|
+
super(default_options.merge!(options))
|
32
11
|
end
|
33
|
-
end
|
34
|
-
|
35
|
-
unless url =~ /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
|
36
|
-
return [ opts[:message_invalid] ]
|
37
|
-
end
|
38
12
|
|
39
|
-
|
40
|
-
|
13
|
+
def validate_each(record, attribute, value)
|
14
|
+
#finder_class = find_finder_class_for(record)
|
15
|
+
#table = finder_class.arel_table
|
16
|
+
#value = deserialize_attribute(record, attribute, value)
|
17
|
+
#
|
18
|
+
#relation = build_relation(finder_class, table, attribute, value)
|
19
|
+
#relation = relation.and(table[finder_class.primary_key.to_sym].not_eq(record.id)) if record.persisted?
|
20
|
+
#relation = scope_relation(record, table, relation)
|
21
|
+
#relation = finder_class.unscoped.where(relation)
|
22
|
+
#relation = relation.merge(options[:conditions]) if options[:conditions]
|
23
|
+
#
|
24
|
+
#
|
25
|
+
#
|
26
|
+
#
|
27
|
+
#
|
28
|
+
#
|
29
|
+
#if relation.exists?
|
30
|
+
# error_options = options.except(:case_sensitive, :scope, :conditions)
|
31
|
+
# error_options[:value] = value
|
32
|
+
#
|
33
|
+
# record.errors.add(attribute, :taken, error_options)
|
34
|
+
#end
|
41
35
|
|
42
36
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
37
|
+
#if opts[:allow_nil] == false
|
38
|
+
# if url.nil?
|
39
|
+
# record.errors.add(attribute, :nil)
|
40
|
+
# return
|
41
|
+
# end
|
42
|
+
#end
|
43
|
+
#
|
44
|
+
#url = url.to_s.strip
|
45
|
+
#
|
46
|
+
#if opts[:allow_blank] == false
|
47
|
+
# if url.blank?
|
48
|
+
# record.errors.add(attribute, :blank)
|
49
|
+
# return
|
50
|
+
# end
|
51
|
+
#end
|
49
52
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
record.errors.add(attr_name, error)
|
54
|
-
end unless errors.nil?
|
53
|
+
unless url =~ /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
|
54
|
+
record.errors.add(attribute, :url_wrong_format)
|
55
|
+
end
|
55
56
|
end
|
56
57
|
end
|
57
|
-
end
|
58
|
-
end
|
59
58
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
err = ::ValidateAsUrl::validate_as_url(value, options)
|
64
|
-
unless err.nil?
|
65
|
-
record.errors[attribute] << err
|
66
|
-
record.errors[attribute].flatten!
|
59
|
+
module ClassMethods
|
60
|
+
def validates_uniqueness_of(*attr_names)
|
61
|
+
validates_with ValidateAsUrl, _merge_attributes(attr_names)
|
67
62
|
end
|
68
63
|
end
|
69
64
|
end
|
70
|
-
|
71
|
-
module ActiveModel::Validations::HelperMethods
|
72
|
-
def validate_as_url(*attr_names)
|
73
|
-
validates_with ::AsUrlValidator, _merge_attributes(attr_names)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
else
|
77
|
-
if defined?(ActiveRecord)
|
78
|
-
class ActiveRecord::Base
|
79
|
-
extend ::AsUrlValidator::Validations
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
65
|
+
end
|
data/validate_as_url.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validate_as_url
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-06-
|
12
|
+
date: 2013-06-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -50,16 +50,12 @@ executables: []
|
|
50
50
|
extensions: []
|
51
51
|
extra_rdoc_files: []
|
52
52
|
files:
|
53
|
+
- .DS_Store
|
53
54
|
- .gitignore
|
54
|
-
- .idea/encodings.xml
|
55
|
-
- .idea/misc.xml
|
56
|
-
- .idea/modules.xml
|
57
|
-
- .idea/scopes/scope_settings.xml
|
58
|
-
- .idea/validate_as_url.iml
|
59
|
-
- .idea/vcs.xml
|
60
55
|
- Gemfile
|
56
|
+
- Gemfile.lock
|
61
57
|
- LICENSE.txt
|
62
|
-
- README.
|
58
|
+
- README.rdoc
|
63
59
|
- Rakefile
|
64
60
|
- lib/validate_as_url.rb
|
65
61
|
- validate_as_url.gemspec
|
data/.idea/encodings.xml
DELETED
data/.idea/misc.xml
DELETED
data/.idea/modules.xml
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
-
<project version="4">
|
3
|
-
<component name="ProjectModuleManager">
|
4
|
-
<modules>
|
5
|
-
<module fileurl="file://$PROJECT_DIR$/.idea/validate_as_url.iml" filepath="$PROJECT_DIR$/.idea/validate_as_url.iml" />
|
6
|
-
</modules>
|
7
|
-
</component>
|
8
|
-
</project>
|
9
|
-
|
data/.idea/validate_as_url.iml
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
-
<module type="RUBY_MODULE" version="4">
|
3
|
-
<component name="FacetManager">
|
4
|
-
<facet type="gem" name="Gem">
|
5
|
-
<configuration>
|
6
|
-
<option name="GEM_APP_ROOT_PATH" value="$MODULE_DIR$" />
|
7
|
-
<option name="GEM_APP_TEST_PATH" value="" />
|
8
|
-
<option name="GEM_APP_LIB_PATH" value="$MODULE_DIR$/lib" />
|
9
|
-
</configuration>
|
10
|
-
</facet>
|
11
|
-
</component>
|
12
|
-
<component name="NewModuleRootManager">
|
13
|
-
<content url="file://$MODULE_DIR$" />
|
14
|
-
<orderEntry type="inheritedJdk" />
|
15
|
-
<orderEntry type="sourceFolder" forTests="false" />
|
16
|
-
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.3.5, RVM: ruby-1.9.3-p327) [gem]" level="application" />
|
17
|
-
<orderEntry type="library" scope="PROVIDED" name="rake (v10.0.4, RVM: ruby-1.9.3-p327) [gem]" level="application" />
|
18
|
-
</component>
|
19
|
-
</module>
|
20
|
-
|
data/.idea/vcs.xml
DELETED
data/README.md
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
# ValidateAsUrl
|
2
|
-
|
3
|
-
TODO: Write a gem description
|
4
|
-
|
5
|
-
## Installation
|
6
|
-
|
7
|
-
Add this line to your application's Gemfile:
|
8
|
-
|
9
|
-
gem 'validate_as_url'
|
10
|
-
|
11
|
-
And then execute:
|
12
|
-
|
13
|
-
$ bundle
|
14
|
-
|
15
|
-
Or install it yourself as:
|
16
|
-
|
17
|
-
$ gem install validate_as_url
|
18
|
-
|
19
|
-
## Usage
|
20
|
-
|
21
|
-
TODO: Write usage instructions here
|
22
|
-
|
23
|
-
## Contributing
|
24
|
-
|
25
|
-
1. Fork it
|
26
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
-
5. Create new Pull Request
|