url_field 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ Thursday, 3rd December 2009
2
+ - - - - - - - - - - -
3
+ - added support for multiple fields
@@ -26,6 +26,16 @@ Https? That works too (but one way only)
26
26
 
27
27
  h2. Extra
28
28
 
29
+ h3. Multiple fields
30
+
31
+ If you have multiple url_fields in a single model, just pass them as arguments to the url_field method, eg:
32
+
33
+ <pre><code>class Company < ActiveRecord::Base
34
+ url_field :website, :support_website, :more_info_website
35
+ end</code></pre>
36
+
37
+ h3. Access the correctly formed URL at any time
38
+
29
39
  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
40
 
31
41
  <pre><code>class Company < ActiveRecord::Base
@@ -34,8 +44,7 @@ end
34
44
 
35
45
  @company = Company.new
36
46
  @company.website = "www.example.com"
37
- @company.cleaned_website # => "http://www.example.com"
38
- </code></pre>
47
+ @company.cleaned_website # => "http://www.example.com"</code></pre>
39
48
 
40
49
  h2. Install
41
50
 
data/Rakefile CHANGED
@@ -19,21 +19,23 @@ PKG_FILES = FileList[
19
19
  'spec/**/*'
20
20
  ]
21
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"]
22
+ begin
23
+ require 'jeweler'
24
+ Jeweler::Tasks.new do |s|
25
+ s.name = "url_field"
26
+ s.version = "0.0.2"
27
+ s.author = "Paul Campbell"
28
+ s.email = "paul@rslw.com"
29
+ s.homepage = "http://www.github.com/paulca/url_field"
30
+ s.platform = Gem::Platform::RUBY
31
+ s.summary = "A simple ActiveRecord plugin to correctly format a URL in the database whether the user enters 'http://' or not"
32
+ s.files = PKG_FILES.to_a
33
+ s.require_path = "lib"
34
+ s.has_rdoc = false
35
+ s.extra_rdoc_files = ["README.textile"]
36
+ end
37
+ rescue LoadError
38
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
34
39
  end
35
40
 
36
- desc 'Turn this plugin into a gem.'
37
- Rake::GemPackageTask.new(spec) do |pkg|
38
- pkg.gem_spec = spec
39
- end
41
+ Jeweler::GemcutterTasks.new
@@ -4,22 +4,28 @@ module UrlField
4
4
  end
5
5
 
6
6
  module UrlFieldMethod
7
- def url_field(field)
7
+ def url_field(*args)
8
8
 
9
- before_save :clean_url_field
9
+ args.each do |field_name|
10
+ before_validation :"clean_#{field_name}_url_field" # before_save :clean_url_url_field
11
+ end
10
12
 
11
13
  class_eval do
12
14
 
13
- define_method(:clean_url_field) do
14
- self.send("#{field}=", send("cleaned_#{field}"))
15
+ args.each do |field_name|
16
+ define_method(:"clean_#{field_name}_url_field") do # def clean_url_url_field
17
+ self.send("#{field_name}=", send("cleaned_#{field_name}")) # self.url = cleaned_url
18
+ end # end
15
19
  end
16
20
 
17
21
  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)
22
+
23
+ args.each do |field_name|
24
+ define_method("cleaned_#{field_name}") do
25
+ return nil if send(field_name).nil? or send(field_name).blank?
26
+ return "http://#{send(field_name)}" unless send(field_name).match(/https?:\/\/.*$/)
27
+ send(field_name)
28
+ end
23
29
  end
24
30
  end
25
31
  end
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "..", "lib", "url_field")
@@ -0,0 +1,21 @@
1
+ sqlite:
2
+ :adapter: sqlite
3
+ :database: spec/url_field.sqlite.db
4
+
5
+ sqlite3:
6
+ :adapter: sqlite3
7
+ :database: spec/url_field.sqlite3.db
8
+
9
+ postgresql:
10
+ :adapter: postgresql
11
+ :username: postgres
12
+ :password: postgres
13
+ :database: url_field_test
14
+ :min_messages: ERROR
15
+
16
+ mysql:
17
+ :adapter: mysql
18
+ :host: localhost
19
+ :username: root
20
+ :password: password
21
+ :database: url_field_test
@@ -0,0 +1,77 @@
1
+ # Logfile created on Thu Dec 03 23:32:32 +0000 2009 by /
2
+ SQL (0.2ms) select sqlite_version(*)
3
+ SQL (0.2ms)  SELECT name
4
+ FROM sqlite_master
5
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
6
+ 
7
+ SQL (79.6ms) CREATE TABLE "test_models" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "url" varchar(255), "other_url" varchar(255), "another_url" varchar(255)) 
8
+ SQL (0.6ms)  SELECT name
9
+ FROM sqlite_master
10
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
11
+ 
12
+ SQL (5.0ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
13
+ SQL (13.5ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
14
+ SQL (0.3ms)  SELECT name
15
+ FROM sqlite_master
16
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
17
+ 
18
+ SQL (0.2ms) SELECT version FROM "schema_migrations"
19
+ SQL (1.1ms) INSERT INTO "schema_migrations" (version) VALUES ('0')
20
+ SQL (0.3ms) select sqlite_version(*)
21
+ SQL (0.5ms)  SELECT name
22
+ FROM sqlite_master
23
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
24
+ 
25
+ SQL (2.4ms) DROP TABLE "test_models"
26
+ SQL (2.9ms) CREATE TABLE "test_models" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "url" varchar(255), "other_url" varchar(255), "another_url" varchar(255)) 
27
+ SQL (0.5ms)  SELECT name
28
+ FROM sqlite_master
29
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
30
+ 
31
+ SQL (0.4ms) SELECT version FROM "schema_migrations"
32
+ TestModel Create (0.5ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, NULL, NULL)
33
+ TestModel Create (0.4ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, NULL, NULL)
34
+ TestModel Create (0.3ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, 'http://www.example.com', NULL)
35
+ TestModel Create (0.3ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, 'http://www.example.com', NULL)
36
+ TestModel Create (0.4ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, 'https://www.example.com', NULL)
37
+ TestModel Create (0.5ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, 'https://www.example.com', NULL)
38
+ SQL (0.2ms) select sqlite_version(*)
39
+ SQL (0.4ms)  SELECT name
40
+ FROM sqlite_master
41
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
42
+ 
43
+ SQL (3.1ms) DROP TABLE "test_models"
44
+ SQL (2.9ms) CREATE TABLE "test_models" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "url" varchar(255), "other_url" varchar(255), "another_url" varchar(255)) 
45
+ SQL (0.3ms)  SELECT name
46
+ FROM sqlite_master
47
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
48
+ 
49
+ SQL (0.2ms) SELECT version FROM "schema_migrations"
50
+ TestModel Create (0.4ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, NULL, NULL)
51
+ TestModel Create (0.5ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, NULL, NULL)
52
+ TestModel Create (0.3ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, 'http://www.example.com', NULL)
53
+ TestModel Create (4.3ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, 'http://www.example.com', NULL)
54
+ TestModel Create (11.4ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, 'https://www.example.com', NULL)
55
+ TestModel Create (0.4ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, 'https://www.example.com', NULL)
56
+ TestModel Create (0.5ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, NULL, 'http://www.example.com')
57
+ TestModel Create (0.4ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES('http://www.example.com', NULL, NULL)
58
+ SQL (0.3ms) select sqlite_version(*)
59
+ SQL (1.6ms)  SELECT name
60
+ FROM sqlite_master
61
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
62
+ 
63
+ SQL (3.1ms) DROP TABLE "test_models"
64
+ SQL (2.6ms) CREATE TABLE "test_models" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "url" varchar(255), "other_url" varchar(255), "another_url" varchar(255)) 
65
+ SQL (0.3ms)  SELECT name
66
+ FROM sqlite_master
67
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
68
+ 
69
+ SQL (0.1ms) SELECT version FROM "schema_migrations"
70
+ TestModel Create (0.4ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, NULL, NULL)
71
+ TestModel Create (0.4ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, NULL, NULL)
72
+ TestModel Create (0.9ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, 'http://www.example.com', NULL)
73
+ TestModel Create (0.4ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, 'http://www.example.com', NULL)
74
+ TestModel Create (0.3ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, 'https://www.example.com', NULL)
75
+ TestModel Create (0.4ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, 'https://www.example.com', NULL)
76
+ TestModel Create (0.5ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES(NULL, NULL, 'http://www.example.com')
77
+ TestModel Create (0.4ms) INSERT INTO "test_models" ("another_url", "url", "other_url") VALUES('http://www.example.com', NULL, NULL)
@@ -0,0 +1,7 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :test_models, :force => true do |t|
3
+ t.string :url
4
+ t.string :other_url
5
+ t.string :another_url
6
+ end
7
+ end
@@ -4,7 +4,35 @@ rescue LoadError
4
4
  puts "You need to install rspec in your base app"
5
5
  exit
6
6
  end
7
-
7
+
8
8
  plugin_spec_dir = File.dirname(__FILE__)
9
9
  ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
10
-
10
+
11
+ def load_schema
12
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
13
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
14
+
15
+ db_adapter = ENV['DB']
16
+
17
+ # no db passed, try one of these fine config-free DBs before bombing.
18
+ db_adapter ||=
19
+ begin
20
+ require 'rubygems'
21
+ require 'sqlite'
22
+ 'sqlite'
23
+ rescue MissingSourceFile
24
+ begin
25
+ require 'sqlite3'
26
+ 'sqlite3'
27
+ rescue MissingSourceFile
28
+ end
29
+ end
30
+
31
+ if db_adapter.nil?
32
+ raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
33
+ end
34
+
35
+ ActiveRecord::Base.establish_connection(config[db_adapter])
36
+ load(File.dirname(__FILE__) + "/schema.rb")
37
+ require File.dirname(__FILE__) + '/../rails/init.rb'
38
+ end
@@ -3,55 +3,68 @@ require File.dirname(__FILE__) + '/spec_helper'
3
3
  require File.dirname(__FILE__) + '/../lib/url_field'
4
4
 
5
5
  class TestModel < ActiveRecord::Base
6
- url_field :shortcode_url
7
-
8
- def self.table_name
9
- 'boojas'
10
- end
6
+ url_field :url, :other_url, :another_url
11
7
  end
12
8
 
13
9
  describe TestModel do
10
+
11
+ load_schema
12
+
14
13
  before do
15
14
  @test_model = TestModel.new
16
15
  end
17
16
 
18
17
  it "should leave well enough alone" do
19
18
  @test_model.save
20
- @test_model.shortcode_url.should be_nil
19
+ @test_model.url.should be_nil
21
20
  end
22
21
 
23
22
  it "should REALLY leave well enough alone" do
24
- @test_model.shortcode_url = ""
23
+ @test_model.url = ""
25
24
  @test_model.save
26
- @test_model.shortcode_url.should be_nil
25
+ @test_model.url.should be_nil
27
26
  end
28
27
 
29
28
  it "should add http://" do
30
- @test_model.shortcode_url = "www.example.com"
29
+ @test_model.url = "www.example.com"
31
30
  @test_model.save
32
- @test_model.shortcode_url.should == "http://www.example.com"
31
+ @test_model.url.should == "http://www.example.com"
33
32
  end
34
33
 
35
34
  it "should ignore http://" do
36
- @test_model.shortcode_url = "http://www.example.com"
35
+ @test_model.url = "http://www.example.com"
37
36
  @test_model.save
38
- @test_model.shortcode_url.should == "http://www.example.com"
37
+ @test_model.url.should == "http://www.example.com"
39
38
  end
40
39
 
41
40
  it "should allow https://" do
42
- @test_model.shortcode_url = "https://www.example.com"
41
+ @test_model.url = "https://www.example.com"
43
42
  @test_model.save
44
- @test_model.shortcode_url.should == "https://www.example.com"
43
+ @test_model.url.should == "https://www.example.com"
45
44
  end
46
45
 
47
46
  it "should ignore https://" do
48
- @test_model.shortcode_url = "https://www.example.com"
47
+ @test_model.url = "https://www.example.com"
49
48
  @test_model.save
50
- @test_model.shortcode_url.should == "https://www.example.com"
49
+ @test_model.url.should == "https://www.example.com"
51
50
  end
52
51
 
53
52
  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"
53
+ @test_model.url = "www.example.com"
54
+ @test_model.cleaned_url.should == "http://www.example.com"
55
+ end
56
+
57
+ it "should work for other_url" do
58
+ @test_model.other_url = "www.example.com"
59
+ @test_model.cleaned_other_url.should == "http://www.example.com"
60
+ @test_model.save
61
+ @test_model.other_url.should == "http://www.example.com"
62
+ end
63
+
64
+ it "should work for another_url" do
65
+ @test_model.another_url = "www.example.com"
66
+ @test_model.cleaned_another_url.should == "http://www.example.com"
67
+ @test_model.save
68
+ @test_model.another_url.should == "http://www.example.com"
56
69
  end
57
70
  end
@@ -0,0 +1,52 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{url_field}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Paul Campbell"]
12
+ s.date = %q{2009-12-04}
13
+ s.email = %q{paul@rslw.com}
14
+ s.extra_rdoc_files = [
15
+ "README.textile"
16
+ ]
17
+ s.files = [
18
+ "CHANGELOG",
19
+ "README.textile",
20
+ "Rakefile",
21
+ "init.rb",
22
+ "lib/url_field.rb",
23
+ "rails/init.rb",
24
+ "spec/database.yml",
25
+ "spec/debug.log",
26
+ "spec/schema.rb",
27
+ "spec/spec_helper.rb",
28
+ "spec/url_field_spec.rb",
29
+ "url_field.gemspec"
30
+ ]
31
+ s.homepage = %q{http://www.github.com/paulca/url_field}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{A simple ActiveRecord plugin to correctly format a URL in the database whether the user enters 'http://' or not}
36
+ s.test_files = [
37
+ "spec/schema.rb",
38
+ "spec/spec_helper.rb",
39
+ "spec/url_field_spec.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ else
48
+ end
49
+ else
50
+ end
51
+ end
52
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: url_field
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Campbell
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-05 00:00:00 +01:00
12
+ date: 2009-12-04 00:00:00 +00:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -22,19 +22,25 @@ extensions: []
22
22
  extra_rdoc_files:
23
23
  - README.textile
24
24
  files:
25
+ - CHANGELOG
26
+ - README.textile
27
+ - Rakefile
25
28
  - init.rb
26
29
  - lib/url_field.rb
27
- - Rakefile
28
- - README.textile
30
+ - rails/init.rb
31
+ - spec/database.yml
32
+ - spec/debug.log
33
+ - spec/schema.rb
29
34
  - spec/spec_helper.rb
30
35
  - spec/url_field_spec.rb
36
+ - url_field.gemspec
31
37
  has_rdoc: true
32
38
  homepage: http://www.github.com/paulca/url_field
33
39
  licenses: []
34
40
 
35
41
  post_install_message:
36
- rdoc_options: []
37
-
42
+ rdoc_options:
43
+ - --charset=UTF-8
38
44
  require_paths:
39
45
  - lib
40
46
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -52,9 +58,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
58
  requirements: []
53
59
 
54
60
  rubyforge_project:
55
- rubygems_version: 1.3.4
61
+ rubygems_version: 1.3.5
56
62
  signing_key:
57
63
  specification_version: 3
58
64
  summary: A simple ActiveRecord plugin to correctly format a URL in the database whether the user enters 'http://' or not
59
- test_files: []
60
-
65
+ test_files:
66
+ - spec/schema.rb
67
+ - spec/spec_helper.rb
68
+ - spec/url_field_spec.rb