Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit a86c875af0977d2238dbf5357f59d9d53be987ff @molte committed Jul 10, 2009
Showing with 161 additions and 0 deletions.
  1. +20 −0 MIT-LICENSE
  2. +47 −0 README.rdoc
  3. +23 −0 Rakefile
  4. +2 −0 init.rb
  5. +61 −0 lib/acts_as_url.rb
  6. +5 −0 test/acts_as_url_test.rb
  7. +3 −0 test/test_helper.rb
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Molte Emil Strange Andersen
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,47 @@
+= acts_as_url
+
+This acts_as extension adds the protocol (eg 'http://') to a url database column if missing.
+It also validates the url against a regular expression.
+
+== Usage/examples
+
+A common usecase would be in blog comments where you want the user to be able to specify his/her website:
+
+ class Comment < ActiveRecord::Base
+ acts_as_url :website
+ end
+
+ >> comment = Comment.new
+ => #<Comment:0x245fed8 ... >
+
+ # The protocol is added before the comment is saved to the database.
+ >> comment.website = 'example.com/directory/file.html'
+ => "example.com/directory/file.html"
+
+ >> comment.website
+ => "http://example.com/directory/file.html"
+
+ # The protocol is already given and is therefore not added, however a trailing slash is appended.
+ >> comment.website = 'https://example.org'
+ => "https://example.org"
+
+ >> comment.website
+ => "https://example.org/"
+
+== Options
+
+By default allowed protocols are http and https, but you can set it specificly for an attribute as a hash value (with the attribute as the key):
+ acts_as_url :website, :repository => 'git'
+
+If you want to allow several protocols, you can use an array:
+ acts_as_url :website => ['https', 'http']
+
+Note that the first value in the array will always be the one prepended to the url if a protocol is omitted by the user.
+ >> comment.website = 'www.example.com'
+ => "www.example.com"
+
+ >> comment.website
+ => "https://www.example.com/"
+
+
+Copyright (c) 2009 Molte Emil Strange Andersen, released under the MIT license
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the acts_as_url plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the acts_as_url plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'ActsAsUrl'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1,2 @@
+require 'acts_as_url'
+ActiveRecord::Base.send(:include, ActsAsUrl)
@@ -0,0 +1,61 @@
+module ActsAsUrl
+
+ def self.included(base)
+ base.send(:extend, ClassMethods)
+ end
+
+ module ClassMethods
+ DEFAULT_ALLOWED_PROTOCOLS = %w( http https )
+
+ URL_SUBDOMAINS_PATTERN = '([-\w]+\.)+'
+ URL_TLD_PATTERN = '[a-z]{2,6}'
+ URL_PORT_PATTERN = '(:\d{1,5})?'
+
+ def acts_as_url(*attributes)
+ protocols = attributes.last.is_a?(Hash) ? attributes.pop : {}
+ attributes += protocols.keys
+
+ attributes.each do |attribute|
+ # Set default protocols
+ protocols[attribute] ||= DEFAULT_ALLOWED_PROTOCOLS
+ # Append "://" to each protocol
+ protocols[attribute] = protocols[attribute].to_a.collect { |p| p + '://' }
+
+ add_url_validation(attribute, protocols[attribute])
+
+ # Define writer method
+ define_method((attribute.to_s + '=').to_sym) do |url|
+ # Get provided protocol if any
+ provided_protocol = protocols[attribute].reject { |p| !url.starts_with?(p) }.first
+ protocol_included = !provided_protocol.nil?
+
+ # Make sure the host name is appended by a slash
+ url += '/' unless (protocol_included ? url[provided_protocol.length, url.length - provided_protocol.length] : url).include?('/')
+ # Add protocol to url if missing
+ url = protocols[attribute].first + url unless protocol_included
+
+ write_attribute(attribute, url)
+ end
+
+ # Define reader method
+ define_method(attribute) do
+ url = read_attribute(attribute)
+
+ # Shorthand for URI.parse(url)
+ def url.to_uri
+ URI.parse(self)
+ end
+
+ url
+ end
+ end
+ end
+
+ private
+ def add_url_validation(attribute, protocols)
+ validates_format_of(attribute, :with => /\A(#{protocols.join('|')})#{URL_SUBDOMAINS_PATTERN + URL_TLD_PATTERN + URL_PORT_PATTERN}\/\S*\z/, :allow_blank => true)
+ end
+
+ end
+
+end
@@ -0,0 +1,5 @@
+require 'test_helper'
+
+class ActsAsUrlTest < ActiveSupport::TestCase
+
+end
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'

0 comments on commit a86c875

Please sign in to comment.