has_url 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -0
- data/LICENSE +20 -0
- data/README.rdoc +49 -0
- data/Rakefile +24 -0
- data/has_url.gemspec +31 -0
- data/lib/has_url.rb +2 -0
- data/lib/has_url/hooks.rb +10 -0
- data/lib/has_url/railtie.rb +14 -0
- data/lib/has_url/validations.rb +93 -0
- data/lib/has_url/version.rb +3 -0
- metadata +145 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Pickabee
|
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.rdoc
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
= HasUrl
|
2
|
+
|
3
|
+
Adds validations on URL attributes for ActiveRecord models.
|
4
|
+
URL scheme will be checked and added before updating the database if necessary.
|
5
|
+
A raw_#{attribute} method will also be added to display the url without HTTP(S) scheme or trailing slash.
|
6
|
+
|
7
|
+
== Usage
|
8
|
+
|
9
|
+
In your AR model:
|
10
|
+
has_url :attribute, options
|
11
|
+
|
12
|
+
Options :
|
13
|
+
* valid_schemes : Array of schemes or single scheme accepted for the attribute. Default ['http', 'https']
|
14
|
+
* default_scheme : Scheme appended to the attribute if none found (supply the scheme string without '://'). Default 'http'
|
15
|
+
* message : Provide a error string or symbol (which will be translated, see ActiveRecord::Errors#generate_message). Default :invalid
|
16
|
+
|
17
|
+
== Example
|
18
|
+
|
19
|
+
class Person < ActiveRecord::Base
|
20
|
+
has_url :blog_link
|
21
|
+
end
|
22
|
+
|
23
|
+
person.blog_link = 'example.com'
|
24
|
+
person.save
|
25
|
+
person.blog_link #=> 'http://example.com/'
|
26
|
+
person.raw_blog_link #=> 'example.com'
|
27
|
+
|
28
|
+
class Person < ActiveRecord::Base
|
29
|
+
has_url [:blog_link, :website_url], :default_scheme => 'https', :valid_schemes => ['http', 'https', 'ftp']
|
30
|
+
end
|
31
|
+
|
32
|
+
== Requirements
|
33
|
+
|
34
|
+
* Addressable for URI parsing
|
35
|
+
|
36
|
+
== TODO
|
37
|
+
|
38
|
+
* Refactor using ActiveModel::Validator
|
39
|
+
* More tests
|
40
|
+
* Improve the raw_ method stripping, and have it accept an option to specify which schemes to strip (schemes array or boolean to strip all valid schemes)
|
41
|
+
* Add a shoulda macro (should_have_url)
|
42
|
+
* Check existence of the given attributes ?
|
43
|
+
* Optionally test the url validity with an HTTP request ?
|
44
|
+
|
45
|
+
== Notes and Copyright
|
46
|
+
|
47
|
+
This plugin was inspired by the {url_validation}[link:https://github.com/RISCfuture/url_validation] and {validates_url_of}[link:https://github.com/ihower/validates_url_of] plugins.
|
48
|
+
|
49
|
+
Copyright (c) 2012 Pickabee. Released under the MIT licence, see LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
task :default => :test
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
Rake::RDocTask.new do |rdoc|
|
9
|
+
rdoc.rdoc_dir = 'rdoc'
|
10
|
+
rdoc.title = "HasUrl"
|
11
|
+
rdoc.main = 'README.rdoc'
|
12
|
+
rdoc.rdoc_files.include('README*', 'LICENSE')
|
13
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
14
|
+
rdoc.options << '--line-numbers'
|
15
|
+
rdoc.options << '--charset=UTF-8'
|
16
|
+
end
|
17
|
+
|
18
|
+
desc 'Test the paperclip plugin.'
|
19
|
+
Rake::TestTask.new(:test) do |t|
|
20
|
+
t.libs << 'lib'
|
21
|
+
t.libs << 'test'
|
22
|
+
t.pattern = 'test/**/*_test.rb'
|
23
|
+
t.verbose = true
|
24
|
+
end
|
data/has_url.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "has_url/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "has_url"
|
7
|
+
s.version = HasUrl::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Pickabee"]
|
10
|
+
s.email = [""]
|
11
|
+
s.homepage = "https://github.com/skateinmars/has_url"
|
12
|
+
s.summary = %q{Validation on URL attributes for ActiveRecord models}
|
13
|
+
s.description = <<-EOL
|
14
|
+
Adds validations on URL attributes for ActiveRecord models.
|
15
|
+
URL scheme will be checked and added before updating the database if necessary.
|
16
|
+
A raw_attribute method will also be added to display the url without HTTP(S) scheme or trailing slash.
|
17
|
+
EOL
|
18
|
+
|
19
|
+
s.add_dependency "activerecord", '>= 3.0.0'
|
20
|
+
s.add_dependency "addressable", '>= 2.2.0'
|
21
|
+
|
22
|
+
s.add_development_dependency "rake"
|
23
|
+
s.add_development_dependency "sqlite3"
|
24
|
+
s.add_development_dependency "shoulda", '>= 3.0.0'
|
25
|
+
|
26
|
+
s.files = Dir["LICENSE", "README.rdoc", "Gemfile", "Rakefile", "has_url.gemspec", "{lib}/**/*.rb"]
|
27
|
+
s.test_files = Dir["{spec}/**/*.rb"]
|
28
|
+
s.rdoc_options = ["--line-numbers", "--charset=UTF-8", "--title", "HasUrl", "--main", "README.rdoc"]
|
29
|
+
s.extra_rdoc_files = %w[LICENSE]
|
30
|
+
s.require_paths = ["lib"]
|
31
|
+
end
|
data/lib/has_url.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
module HasUrl
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
class Railtie < Rails::Railtie # :nodoc:
|
5
|
+
railtie_name :has_url
|
6
|
+
|
7
|
+
initializer 'has_url.insert_into_active_record' do |app|
|
8
|
+
ActiveSupport.on_load :active_record do
|
9
|
+
require 'has_url/hooks'
|
10
|
+
HasUrl::Hooks.init
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'addressable/uri'
|
2
|
+
|
3
|
+
module HasUrl # :nodoc:
|
4
|
+
module Validations
|
5
|
+
|
6
|
+
# This allows you to specify attributes you want to define as containing
|
7
|
+
# a url. This will validate the URI and prepend a http scheme if needed.
|
8
|
+
# Provide this with a required attributes array (or a single attribute symbol) and an options hash.
|
9
|
+
#
|
10
|
+
# ==== Examples
|
11
|
+
#
|
12
|
+
# class Person < ActiveRecord::Base
|
13
|
+
# has_url :blog_link
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# class Person < ActiveRecord::Base
|
17
|
+
# has_url [:blog_link, :website_url]
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# ===== Using options
|
21
|
+
#
|
22
|
+
# class Person < ActiveRecord::Base
|
23
|
+
# has_url :blog_link, {:valid_schemes => ['http', 'https', 'ftp'], :default_scheme => 'http', :message => 'does not look like a valid url'}
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
def has_url(attributes, options = {})
|
27
|
+
attributes = [attributes].flatten
|
28
|
+
|
29
|
+
options.reverse_merge!({:valid_schemes => ['http', 'https'], :default_scheme => 'http' })
|
30
|
+
|
31
|
+
raise ArgumentError, "default scheme does not match a valid scheme" unless options[:valid_schemes].include?(options[:default_scheme])
|
32
|
+
|
33
|
+
attributes.each do |attribute|
|
34
|
+
#Doesnt work, raises ActiveRecord::ConnectionNotEstablished
|
35
|
+
#raise ArgumentError, "attribute #{attribute} does not exist" if self.columns.select{|c| c.name == attribute }.empty?
|
36
|
+
|
37
|
+
default_scheme_accessor = "#{attribute}_default_scheme".to_sym
|
38
|
+
valid_schemes_accessor = "#{attribute}_valid_schemes".to_sym
|
39
|
+
cattr_accessor default_scheme_accessor
|
40
|
+
self.send("#{default_scheme_accessor}=", options[:default_scheme])
|
41
|
+
cattr_accessor valid_schemes_accessor
|
42
|
+
self.send("#{valid_schemes_accessor}=", options[:valid_schemes])
|
43
|
+
|
44
|
+
define_method( "fix_#{attribute}_url") do
|
45
|
+
value = read_attribute(attribute)
|
46
|
+
if value.blank?
|
47
|
+
value = nil
|
48
|
+
else
|
49
|
+
begin
|
50
|
+
default_scheme = self.class.send("#{attribute}_default_scheme".to_sym)
|
51
|
+
uri = Addressable::URI.heuristic_parse(value, {:scheme => default_scheme}).normalize
|
52
|
+
value = uri.to_s
|
53
|
+
rescue Addressable::URI::InvalidURIError
|
54
|
+
end
|
55
|
+
end
|
56
|
+
write_attribute( attribute, value )
|
57
|
+
end
|
58
|
+
|
59
|
+
define_method( "raw_#{attribute}") do
|
60
|
+
value = read_attribute(attribute)
|
61
|
+
if value.blank?
|
62
|
+
return nil
|
63
|
+
else
|
64
|
+
return value.sub('http://', '').sub('https://', '').sub(/\/$/, '')
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
before_validation "fix_#{attribute}_url".to_sym
|
69
|
+
end
|
70
|
+
|
71
|
+
validates_each attributes do |record, attr, value|
|
72
|
+
if !value.nil?
|
73
|
+
if defined?(I18n)
|
74
|
+
message = options[:message] ? options[:message] : :invalid
|
75
|
+
error_args = [attr, message, {:value => value}]
|
76
|
+
else
|
77
|
+
error_args = [attr]
|
78
|
+
error_args << options[:message] if options[:message]
|
79
|
+
end
|
80
|
+
|
81
|
+
begin
|
82
|
+
uri = Addressable::URI.heuristic_parse(value).normalize
|
83
|
+
scheme_is_valid = self.send("#{attr}_valid_schemes".to_sym).include?(uri.scheme)
|
84
|
+
record.errors.add(*error_args) unless scheme_is_valid
|
85
|
+
rescue Addressable::URI::InvalidURIError
|
86
|
+
record.errors.add(*error_args)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_url
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Pickabee
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: addressable
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.2.0
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.2.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: sqlite3
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: shoulda
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 3.0.0
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 3.0.0
|
94
|
+
description: ! " Adds validations on URL attributes for ActiveRecord models.\n URL
|
95
|
+
scheme will be checked and added before updating the database if necessary.\n A
|
96
|
+
raw_attribute method will also be added to display the url without HTTP(S) scheme
|
97
|
+
or trailing slash.\n"
|
98
|
+
email:
|
99
|
+
- ''
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files:
|
103
|
+
- LICENSE
|
104
|
+
files:
|
105
|
+
- LICENSE
|
106
|
+
- README.rdoc
|
107
|
+
- Gemfile
|
108
|
+
- Rakefile
|
109
|
+
- has_url.gemspec
|
110
|
+
- lib/has_url/validations.rb
|
111
|
+
- lib/has_url/railtie.rb
|
112
|
+
- lib/has_url/hooks.rb
|
113
|
+
- lib/has_url/version.rb
|
114
|
+
- lib/has_url.rb
|
115
|
+
homepage: https://github.com/skateinmars/has_url
|
116
|
+
licenses: []
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options:
|
119
|
+
- --line-numbers
|
120
|
+
- --charset=UTF-8
|
121
|
+
- --title
|
122
|
+
- HasUrl
|
123
|
+
- --main
|
124
|
+
- README.rdoc
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ! '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
none: false
|
135
|
+
requirements:
|
136
|
+
- - ! '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
requirements: []
|
140
|
+
rubyforge_project:
|
141
|
+
rubygems_version: 1.8.24
|
142
|
+
signing_key:
|
143
|
+
specification_version: 3
|
144
|
+
summary: Validation on URL attributes for ActiveRecord models
|
145
|
+
test_files: []
|