validate_as_url 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/.DS_Store ADDED
Binary file
data/.gitignore CHANGED
@@ -1,17 +1,20 @@
1
- *.gem
2
1
  *.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
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
@@ -0,0 +1,17 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ validate_as_url (0.0.5)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.0.4)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.3)
16
+ rake
17
+ validate_as_url!
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.
@@ -1,82 +1,65 @@
1
- module ValidateAsUrl
2
-
3
- #require 'resolv'
4
-
5
- MessageScope = defined?(ActiveModel) ? :activemodel : :activerecord
6
-
7
- #LocalPartSpecialChars = /[\!\#\$\%\&\'\*\-\/\=\?\+\-\^\_\`\{\|\}\~]/
8
-
9
- def self.validate_as_url(url, options={})
10
- options ||={}
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
- return nil # represents no validation errors
40
- end
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
- module Validations
44
- def validate_as_url(*attr_names)
45
- options = { :on => :save,
46
- :allow_nil => false,
47
- :allow_blank => false }
48
- options.update(attr_names.pop) if attr_names.last.is_a?(Hash)
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
- validates_each(attr_names, options) do |record, attr_name, value|
51
- errors = ::ValidateAsUrl::validate_as_url(value.to_s, options)
52
- errors.each do |error|
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
- if defined?(ActiveModel)
61
- class AsUrlValidator < ActiveModel::EachValidator
62
- def validate_each(record, attribute, value)
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
@@ -4,7 +4,7 @@
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "validate_as_url"
7
- spec.version = '0.0.5'
7
+ spec.version = '0.0.6'
8
8
  spec.author = ["Mitrofanov Dmitry"]
9
9
  spec.email = ["mdima@it-guru.biz"]
10
10
  spec.description = %q{This Gem is Used for Validations URLs}
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.5
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-10 00:00:00.000000000 Z
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.md
58
+ - README.rdoc
63
59
  - Rakefile
64
60
  - lib/validate_as_url.rb
65
61
  - validate_as_url.gemspec
data/.idea/encodings.xml DELETED
@@ -1,5 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
4
- </project>
5
-
data/.idea/misc.xml DELETED
@@ -1,5 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectRootManager" version="2" project-jdk-name="RVM: ruby-1.9.3-p327" project-jdk-type="RUBY_SDK" />
4
- </project>
5
-
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
-
@@ -1,5 +0,0 @@
1
- <component name="DependencyValidationManager">
2
- <state>
3
- <option name="SKIP_IMPORT_STATEMENTS" value="false" />
4
- </state>
5
- </component>
@@ -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
@@ -1,7 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
- </component>
6
- </project>
7
-
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