url_field 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile ADDED
@@ -0,0 +1,53 @@
1
+ h1. URL field
2
+
3
+ Sometimes you want to accept a URL field on one of your models. Take for example a Company model with a website field. You want the URL to always have "http://" but sometimes users enter http://, sometimes they dont.
4
+
5
+ Your app shouldn't care. Enter the URL field plugin.
6
+
7
+ This simple plugin allows you to enter either "http://www.example.com" or just "www.example.com" and in either case it will store "http://www.example.com"
8
+
9
+ h2. Usage
10
+
11
+ <pre><code>class Company < ActiveRecord::Base
12
+ url_field :website
13
+ end
14
+
15
+ @company = Company.new
16
+ @company.website = "www.example.com"
17
+ @company.save
18
+ @company.website # => "http://www.example.com"
19
+ </code></pre>
20
+
21
+ Https? That works too (but one way only)
22
+ <pre><code>@company.website = "https://www.example.com"
23
+ @company.save
24
+ @company.website # => "https://www.example.com"
25
+ </code></pre>
26
+
27
+ h2. Extra
28
+
29
+ If you want access to the correctly formatted URL at any time (for example if you're passing it to URI.parse, before you save your model), you can prefix your URL field method name with "cleaned_" eg. "cleaned_website" if your field name was website:
30
+
31
+ <pre><code>class Company < ActiveRecord::Base
32
+ url_field :website
33
+ end
34
+
35
+ @company = Company.new
36
+ @company.website = "www.example.com"
37
+ @company.cleaned_website # => "http://www.example.com"
38
+ </code></pre>
39
+
40
+ h2. Install
41
+
42
+ Easy!
43
+
44
+ <pre><code>./script plugin install git://github.com/paulca/url_field.git
45
+ </code></pre>
46
+
47
+ h2. About me
48
+
49
+ I'm Paul Campbell. I'm an avid Ruby on Rails web developer. Follow my ramblings at "http://www.pabcas.com":http://www.pabcas.com
50
+
51
+ Follow me on Twitter "http://twitter.com/paulca":http://twitter.com/paulca
52
+
53
+ Copyright (c) 2009 Paul Campbell, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,39 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+
4
+ desc 'Default: run specs.'
5
+ task :default => :spec
6
+
7
+ desc 'Run the specs'
8
+ Spec::Rake::SpecTask.new(:spec) do |t|
9
+ t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
10
+ t.spec_files = FileList['spec/**/*_spec.rb']
11
+ end
12
+
13
+ PKG_FILES = FileList[
14
+ '[a-zA-Z]*',
15
+ 'generators/**/*',
16
+ 'lib/**/*',
17
+ 'rails/**/*',
18
+ 'tasks/**/*',
19
+ 'spec/**/*'
20
+ ]
21
+
22
+ spec = Gem::Specification.new do |s|
23
+ s.name = "url_field"
24
+ s.version = "0.0.1"
25
+ s.author = "Paul Campbell"
26
+ s.email = "paul@rslw.com"
27
+ s.homepage = "http://www.github.com/paulca/url_field"
28
+ s.platform = Gem::Platform::RUBY
29
+ s.summary = "A simple ActiveRecord plugin to correctly format a URL in the database whether the user enters 'http://' or not"
30
+ s.files = PKG_FILES.to_a
31
+ s.require_path = "lib"
32
+ s.has_rdoc = false
33
+ s.extra_rdoc_files = ["README.textile"]
34
+ end
35
+
36
+ desc 'Turn this plugin into a gem.'
37
+ Rake::GemPackageTask.new(spec) do |pkg|
38
+ pkg.gem_spec = spec
39
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "lib", "url_field")
data/lib/url_field.rb ADDED
@@ -0,0 +1,31 @@
1
+ module UrlField
2
+ def self.included(base)
3
+ base.extend UrlFieldMethod
4
+ end
5
+
6
+ module UrlFieldMethod
7
+ def url_field(field)
8
+
9
+ before_save :clean_url_field
10
+
11
+ class_eval do
12
+
13
+ define_method(:clean_url_field) do
14
+ self.send("#{field}=", send("cleaned_#{field}"))
15
+ end
16
+
17
+ private
18
+
19
+ define_method("cleaned_#{field}") do
20
+ return nil if send(field).nil? or send(field).blank?
21
+ return "http://#{send(field)}" unless send(field).match(/https?:\/\/.*$/)
22
+ send(field)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ if Object.const_defined?("ActiveRecord")
30
+ ActiveRecord::Base.send(:include, UrlField)
31
+ end
@@ -0,0 +1,10 @@
1
+ begin
2
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
3
+ rescue LoadError
4
+ puts "You need to install rspec in your base app"
5
+ exit
6
+ end
7
+
8
+ plugin_spec_dir = File.dirname(__FILE__)
9
+ ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
10
+
@@ -0,0 +1,57 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ require File.dirname(__FILE__) + '/../lib/url_field'
4
+
5
+ class TestModel < ActiveRecord::Base
6
+ url_field :shortcode_url
7
+
8
+ def self.table_name
9
+ 'boojas'
10
+ end
11
+ end
12
+
13
+ describe TestModel do
14
+ before do
15
+ @test_model = TestModel.new
16
+ end
17
+
18
+ it "should leave well enough alone" do
19
+ @test_model.save
20
+ @test_model.shortcode_url.should be_nil
21
+ end
22
+
23
+ it "should REALLY leave well enough alone" do
24
+ @test_model.shortcode_url = ""
25
+ @test_model.save
26
+ @test_model.shortcode_url.should be_nil
27
+ end
28
+
29
+ it "should add http://" do
30
+ @test_model.shortcode_url = "www.example.com"
31
+ @test_model.save
32
+ @test_model.shortcode_url.should == "http://www.example.com"
33
+ end
34
+
35
+ it "should ignore http://" do
36
+ @test_model.shortcode_url = "http://www.example.com"
37
+ @test_model.save
38
+ @test_model.shortcode_url.should == "http://www.example.com"
39
+ end
40
+
41
+ it "should allow https://" do
42
+ @test_model.shortcode_url = "https://www.example.com"
43
+ @test_model.save
44
+ @test_model.shortcode_url.should == "https://www.example.com"
45
+ end
46
+
47
+ it "should ignore https://" do
48
+ @test_model.shortcode_url = "https://www.example.com"
49
+ @test_model.save
50
+ @test_model.shortcode_url.should == "https://www.example.com"
51
+ end
52
+
53
+ it "should expose the cleaned method" do
54
+ @test_model.shortcode_url = "www.example.com"
55
+ @test_model.cleaned_shortcode_url.should == "http://www.example.com"
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: url_field
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Paul Campbell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-05 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: paul@rslw.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.textile
24
+ files:
25
+ - init.rb
26
+ - lib/url_field.rb
27
+ - Rakefile
28
+ - README.textile
29
+ - spec/spec_helper.rb
30
+ - spec/url_field_spec.rb
31
+ has_rdoc: true
32
+ homepage: http://www.github.com/paulca/url_field
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.3.4
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: A simple ActiveRecord plugin to correctly format a URL in the database whether the user enters 'http://' or not
59
+ test_files: []
60
+