url_attributes 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.rspec +2 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +37 -0
  7. data/Rakefile +2 -0
  8. data/lib/url_attributes.rb +10 -0
  9. data/lib/url_attributes/extensions/active_record.rb +32 -0
  10. data/lib/url_attributes/extensions/active_support.rb +3 -0
  11. data/lib/url_attributes/url_validator.rb +23 -0
  12. data/lib/url_attributes/version.rb +3 -0
  13. data/spec/dummy/.gitignore +16 -0
  14. data/spec/dummy/Gemfile +40 -0
  15. data/spec/dummy/Gemfile.lock +120 -0
  16. data/spec/dummy/README.rdoc +28 -0
  17. data/spec/dummy/Rakefile +6 -0
  18. data/spec/dummy/app/assets/images/.keep +0 -0
  19. data/spec/dummy/app/assets/javascripts/application.js +16 -0
  20. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  21. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  22. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  23. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  24. data/spec/dummy/app/mailers/.keep +0 -0
  25. data/spec/dummy/app/models/.keep +0 -0
  26. data/spec/dummy/app/models/concerns/.keep +0 -0
  27. data/spec/dummy/app/models/model.rb +3 -0
  28. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  29. data/spec/dummy/bin/bundle +3 -0
  30. data/spec/dummy/bin/rails +8 -0
  31. data/spec/dummy/bin/rake +8 -0
  32. data/spec/dummy/bin/spring +18 -0
  33. data/spec/dummy/config.ru +4 -0
  34. data/spec/dummy/config/application.rb +23 -0
  35. data/spec/dummy/config/boot.rb +4 -0
  36. data/spec/dummy/config/database.yml +25 -0
  37. data/spec/dummy/config/environment.rb +5 -0
  38. data/spec/dummy/config/environments/development.rb +37 -0
  39. data/spec/dummy/config/environments/production.rb +78 -0
  40. data/spec/dummy/config/environments/test.rb +39 -0
  41. data/spec/dummy/config/initializers/assets.rb +8 -0
  42. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  43. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  44. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  45. data/spec/dummy/config/initializers/inflections.rb +16 -0
  46. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  47. data/spec/dummy/config/initializers/session_store.rb +3 -0
  48. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/spec/dummy/config/locales/en.yml +23 -0
  50. data/spec/dummy/config/routes.rb +56 -0
  51. data/spec/dummy/config/secrets.yml +22 -0
  52. data/spec/dummy/db/migrate/20141124095718_create_models.rb +9 -0
  53. data/spec/dummy/db/schema.rb +22 -0
  54. data/spec/dummy/db/seeds.rb +7 -0
  55. data/spec/dummy/lib/assets/.keep +0 -0
  56. data/spec/dummy/lib/tasks/.keep +0 -0
  57. data/spec/dummy/log/.keep +0 -0
  58. data/spec/dummy/public/404.html +67 -0
  59. data/spec/dummy/public/422.html +67 -0
  60. data/spec/dummy/public/500.html +66 -0
  61. data/spec/dummy/public/favicon.ico +0 -0
  62. data/spec/dummy/public/robots.txt +5 -0
  63. data/spec/dummy/vendor/assets/javascripts/.keep +0 -0
  64. data/spec/dummy/vendor/assets/stylesheets/.keep +0 -0
  65. data/spec/rails_helper.rb +16 -0
  66. data/spec/spec_helper.rb +9 -0
  67. data/spec/url_attributes_spec.rb +68 -0
  68. data/url_attributes.gemspec +28 -0
  69. metadata +250 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 562497d9d1a6158943dc8d6fa803f18f63f209d9
4
+ data.tar.gz: 8cfd0db3b77dc3405cfd846207a1d25e44c7ff19
5
+ SHA512:
6
+ metadata.gz: 04a6289f8d61c3ac4daf8f1d4e4d8c2e03ffc208fe03c355d9a5a5351ac75e943ef60a1908dcefed1ca276a3e33ad0f19c0392fc738beb451af62b14980b1fa3
7
+ data.tar.gz: 36d6f6f97c0221c3ce0b2e6d3eb22c406f14c412b28f6f36ffa2fd7f170a848eb0034b34853b78daa8f6210db57667b55b681f63090a06f9de5daf972268ad82
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in url_attributes.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 George Millo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # URLAttributes
2
+
3
+ A small and simple gem for validating URL attributes in your ActiveRecord models.
4
+
5
+ ## Installation
6
+
7
+ Add the gem to your `Gemfile`
8
+
9
+ gem 'url_attributes'
10
+
11
+ And run `bundle install`.
12
+
13
+ ## Usage
14
+
15
+ Add a string column to your ActiveRecord model which will contain a url, e.g. `link`,
16
+ then call the class method `url_attribute` in your model like this:
17
+
18
+ class MyAwesomeModel < ActiveRecord::Base
19
+
20
+ url_attribute :link
21
+
22
+ end
23
+
24
+ This does two things:
25
+
26
+ 1. It adds a validation to `link` that
27
+ 2. Before save, it strips trailing whitespace from the link and adds `"http://"` to the beginning
28
+ if it (or `"https://"`) is not already there.
29
+
30
+ ## Contributing
31
+
32
+ Want to contribute to this gem? You're more than welcome! Just clone the repo from GitHub,
33
+ upload your changes and open a pull request.
34
+
35
+ Make sure to add tests for your changes so they don't get accidentally broken by a future update.
36
+
37
+ (C) 2014 George Millo (Headstock Software)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,10 @@
1
+ require "url_attributes/version"
2
+ require "url_attributes/url_validator"
3
+ require "url_attributes/extensions/active_support"
4
+ require "url_attributes/extensions/active_record"
5
+
6
+ module URLAttributes
7
+ # Your code goes here...
8
+ end
9
+
10
+
@@ -0,0 +1,32 @@
1
+ # Use:
2
+ #
3
+ # # Model with string attributes called 'checkout_url' and 'homepage_url'
4
+ # class Example < ActiveRecord::Base
5
+ #
6
+ # url_attribute :checkout_url, :homepage_url
7
+ #
8
+ # end
9
+ #
10
+ # The above will cause the attribute 'url' to be validated with URLValidator (NB
11
+ # the validation will pass if no URL is present), and it will have 'http://'
12
+ # added it to it if it's not provided.
13
+ module URLAttributes
14
+ module ActiveRecord
15
+
16
+ def url_attribute(attribute_name, opts={})
17
+ validates attribute_name, :url => true, :if => "#{attribute_name}.present?"
18
+
19
+ # Add "http://" to the URL if it's not already there.
20
+ before_save do |record|
21
+ url = record[attribute_name]
22
+ if url.present? && !(url =~ /\A\s*https?:\/\//)
23
+ url = "http://#{url.try(:strip)}"
24
+ end
25
+ record[attribute_name] = url
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+
32
+ ActiveRecord::Base.send(:extend, URLAttributes::ActiveRecord)
@@ -0,0 +1,3 @@
1
+ ActiveSupport::Inflector.inflections(:en) do |inflect|
2
+ inflect.acronym 'URL'
3
+ end
@@ -0,0 +1,23 @@
1
+ class URLValidator < ActiveModel::EachValidator
2
+
3
+ def validate_each(record, attribute_name, url)
4
+ @record = record
5
+ @attribute_name = attribute_name
6
+ if url.blank? || !url.respond_to?(:strip) || !(url =~ /\./)
7
+ invalid
8
+ return
9
+ end
10
+ url = "http://#{url.strip}" unless /\Ahttps?:\/\// =~ url
11
+ uri = URI.parse(url) # This can raise a URI::InvalidURIError
12
+ invalid unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)
13
+ rescue URI::InvalidURIError
14
+ invalid
15
+ end
16
+
17
+ private
18
+
19
+ def invalid
20
+ @record.errors.add @attribute_name, "is not a valid URL"
21
+ end
22
+
23
+ end
@@ -0,0 +1,3 @@
1
+ module URLAttributes
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,16 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+ /db/*.sqlite3-journal
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ /log/*.log
16
+ /tmp
@@ -0,0 +1,40 @@
1
+ source 'https://rubygems.org'
2
+
3
+
4
+ # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
5
+ gem 'rails', '4.1.8'
6
+ # Use sqlite3 as the database for Active Record
7
+ gem 'sqlite3'
8
+ # Use SCSS for stylesheets
9
+ gem 'sass-rails', '~> 4.0.3'
10
+ # Use Uglifier as compressor for JavaScript assets
11
+ gem 'uglifier', '>= 1.3.0'
12
+ # Use CoffeeScript for .js.coffee assets and views
13
+ gem 'coffee-rails', '~> 4.0.0'
14
+ # See https://github.com/sstephenson/execjs#readme for more supported runtimes
15
+ # gem 'therubyracer', platforms: :ruby
16
+
17
+ # Use jquery as the JavaScript library
18
+ gem 'jquery-rails'
19
+ # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
20
+ gem 'turbolinks'
21
+ # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
22
+ gem 'jbuilder', '~> 2.0'
23
+ # bundle exec rake doc:rails generates the API under doc/api.
24
+ gem 'sdoc', '~> 0.4.0', group: :doc
25
+
26
+ # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
27
+ gem 'spring', group: :development
28
+
29
+ # Use ActiveModel has_secure_password
30
+ # gem 'bcrypt', '~> 3.1.7'
31
+
32
+ # Use unicorn as the app server
33
+ # gem 'unicorn'
34
+
35
+ # Use Capistrano for deployment
36
+ # gem 'capistrano-rails', group: :development
37
+
38
+ # Use debugger
39
+ # gem 'debugger', group: [:development, :test]
40
+
@@ -0,0 +1,120 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ actionmailer (4.1.8)
5
+ actionpack (= 4.1.8)
6
+ actionview (= 4.1.8)
7
+ mail (~> 2.5, >= 2.5.4)
8
+ actionpack (4.1.8)
9
+ actionview (= 4.1.8)
10
+ activesupport (= 4.1.8)
11
+ rack (~> 1.5.2)
12
+ rack-test (~> 0.6.2)
13
+ actionview (4.1.8)
14
+ activesupport (= 4.1.8)
15
+ builder (~> 3.1)
16
+ erubis (~> 2.7.0)
17
+ activemodel (4.1.8)
18
+ activesupport (= 4.1.8)
19
+ builder (~> 3.1)
20
+ activerecord (4.1.8)
21
+ activemodel (= 4.1.8)
22
+ activesupport (= 4.1.8)
23
+ arel (~> 5.0.0)
24
+ activesupport (4.1.8)
25
+ i18n (~> 0.6, >= 0.6.9)
26
+ json (~> 1.7, >= 1.7.7)
27
+ minitest (~> 5.1)
28
+ thread_safe (~> 0.1)
29
+ tzinfo (~> 1.1)
30
+ arel (5.0.1.20140414130214)
31
+ builder (3.2.2)
32
+ coffee-rails (4.0.1)
33
+ coffee-script (>= 2.2.0)
34
+ railties (>= 4.0.0, < 5.0)
35
+ coffee-script (2.3.0)
36
+ coffee-script-source
37
+ execjs
38
+ coffee-script-source (1.8.0)
39
+ erubis (2.7.0)
40
+ execjs (2.2.2)
41
+ hike (1.2.3)
42
+ i18n (0.6.11)
43
+ jbuilder (2.2.5)
44
+ activesupport (>= 3.0.0, < 5)
45
+ multi_json (~> 1.2)
46
+ jquery-rails (3.1.2)
47
+ railties (>= 3.0, < 5.0)
48
+ thor (>= 0.14, < 2.0)
49
+ json (1.8.1)
50
+ mail (2.6.3)
51
+ mime-types (>= 1.16, < 3)
52
+ mime-types (2.4.3)
53
+ minitest (5.4.3)
54
+ multi_json (1.10.1)
55
+ rack (1.5.2)
56
+ rack-test (0.6.2)
57
+ rack (>= 1.0)
58
+ rails (4.1.8)
59
+ actionmailer (= 4.1.8)
60
+ actionpack (= 4.1.8)
61
+ actionview (= 4.1.8)
62
+ activemodel (= 4.1.8)
63
+ activerecord (= 4.1.8)
64
+ activesupport (= 4.1.8)
65
+ bundler (>= 1.3.0, < 2.0)
66
+ railties (= 4.1.8)
67
+ sprockets-rails (~> 2.0)
68
+ railties (4.1.8)
69
+ actionpack (= 4.1.8)
70
+ activesupport (= 4.1.8)
71
+ rake (>= 0.8.7)
72
+ thor (>= 0.18.1, < 2.0)
73
+ rake (10.3.2)
74
+ rdoc (4.1.2)
75
+ json (~> 1.4)
76
+ sass (3.2.19)
77
+ sass-rails (4.0.4)
78
+ railties (>= 4.0.0, < 5.0)
79
+ sass (~> 3.2.2)
80
+ sprockets (~> 2.8, < 2.12)
81
+ sprockets-rails (~> 2.0)
82
+ sdoc (0.4.1)
83
+ json (~> 1.7, >= 1.7.7)
84
+ rdoc (~> 4.0)
85
+ spring (1.2.0)
86
+ sprockets (2.11.3)
87
+ hike (~> 1.2)
88
+ multi_json (~> 1.0)
89
+ rack (~> 1.0)
90
+ tilt (~> 1.1, != 1.3.0)
91
+ sprockets-rails (2.2.1)
92
+ actionpack (>= 3.0)
93
+ activesupport (>= 3.0)
94
+ sprockets (>= 2.8, < 4.0)
95
+ sqlite3 (1.3.10)
96
+ thor (0.19.1)
97
+ thread_safe (0.3.4)
98
+ tilt (1.4.1)
99
+ turbolinks (2.5.2)
100
+ coffee-rails
101
+ tzinfo (1.2.2)
102
+ thread_safe (~> 0.1)
103
+ uglifier (2.5.3)
104
+ execjs (>= 0.3.0)
105
+ json (>= 1.8.0)
106
+
107
+ PLATFORMS
108
+ ruby
109
+
110
+ DEPENDENCIES
111
+ coffee-rails (~> 4.0.0)
112
+ jbuilder (~> 2.0)
113
+ jquery-rails
114
+ rails (= 4.1.8)
115
+ sass-rails (~> 4.0.3)
116
+ sdoc (~> 0.4.0)
117
+ spring
118
+ sqlite3
119
+ turbolinks
120
+ uglifier (>= 1.3.0)
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
File without changes
@@ -0,0 +1,16 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require turbolinks
16
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end