european-date-text-field 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
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.
@@ -0,0 +1,11 @@
1
+ MIT-LICENSE
2
+ README.markdown
3
+ Rakefile
4
+ init.rb
5
+ install.rb
6
+ lib/european_date_text_field.rb
7
+ lib/european_date_validator.rb
8
+ test/european_date_validator_test.rb
9
+ test/test_helper.rb
10
+ uninstall.rb
11
+ Manifest
@@ -0,0 +1,53 @@
1
+ EUROPEAN DATE TEXT FIELD
2
+ ========================
3
+
4
+ This is a small gem/plugin for when you want to use a textfield to enter a European formatted date like DD/MM/YYYY, and validate to this format.
5
+
6
+ With this gem/plugin you can enter a european formatted date into your text_field.
7
+
8
+ When a correctly formatted date is passed it will be converted to a Date object and stored in the database as a date-field.
9
+
10
+ Otherwise the wrong value will be shown in the field an the database will contain nil.
11
+
12
+ SUPPORTED FORMATS
13
+ -----------------
14
+
15
+ - DD/MM/Y
16
+ - DD/MM/YYYY
17
+ - D/M/Y
18
+ - DD/M/YYYY
19
+
20
+ HOWTO
21
+ -----
22
+
23
+ Install the plugin into your Rails 3 application like this:
24
+
25
+ rails plugin install http://github.com/fousa/european-date-text-field.git
26
+
27
+ Next you should define the columns that need time conversion in your model object:
28
+
29
+ class Person < ActiveRecord::Base
30
+ european_date :birth_date
31
+ end
32
+
33
+ Then you have to change the name of your attributes in the form:
34
+
35
+ <%= f.text_field :european_birth_date %>
36
+
37
+ **BE AWARE: You must add the european_ prefix at the beginning of your column names in order for the gem/plugin to work!**
38
+
39
+ VALIDATIONS
40
+ -----------
41
+
42
+ You can also use the supplied validator like this:
43
+
44
+ validates :european_birth_date, :european_date => true
45
+
46
+ Now the birth_date will be validated according to the DD/MM/Y ,DD/MM/YYYY, D/M/Y or DD/M/YYYY formats.
47
+
48
+ **Make sure you use the column name with the european_ prefix, because it's this field we want to validate and not the database column!**
49
+
50
+ QUESTIONS
51
+ ---------
52
+
53
+ Just ask here on Github!
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('european-date-text-field', '0.2') do |p|
6
+ p.description = "A gem for when you want to use a textfield to enter a European formatted date."
7
+ p.url = "http://github.com/fousa/european-date-text-field"
8
+ p.author = "Jelle Vandebeeck"
9
+ p.ignore_pattern = ["tmp/*", "script/*"]
10
+ p.development_dependencies = []
11
+ end
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{european-date-text-field}
5
+ s.version = "0.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Jelle Vandebeeck"]
9
+ s.date = %q{2010-03-30}
10
+ s.description = %q{A gem for when you want to use a textfield to enter a European formatted date.}
11
+ s.email = %q{}
12
+ s.extra_rdoc_files = ["README.markdown", "lib/european_date_text_field.rb", "lib/european_date_validator.rb"]
13
+ s.files = ["MIT-LICENSE", "README.markdown", "Rakefile", "init.rb", "install.rb", "lib/european_date_text_field.rb", "lib/european_date_validator.rb", "test/european_date_validator_test.rb", "test/test_helper.rb", "uninstall.rb", "Manifest", "european-date-text-field.gemspec"]
14
+ s.homepage = %q{http://github.com/fousa/european-date-text-field}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "European-date-text-field", "--main", "README.markdown"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{european-date-text-field}
18
+ s.rubygems_version = %q{1.3.6}
19
+ s.summary = %q{A gem for when you want to use a textfield to enter a European formatted date.}
20
+ s.test_files = ["test/european_date_validator_test.rb", "test/test_helper.rb"]
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 3
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
data/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "european_date_validator"
2
+ require "european_date_text_field"
3
+
4
+ ActiveRecord::Base.extend EuropeanDateTextField::ClassMethods
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,27 @@
1
+ module EuropeanDateTextField
2
+ module ClassMethods
3
+ def european_date(*column_names)
4
+ column_names.each do |column_name|
5
+ define_method "european_#{column_name}=" do |date_string|
6
+ instance_variable_set("@european_#{column_name}", date_string)
7
+ if EuropeanDateValidator::EUROPEAN_DATE_MATCHER.match(date_string)
8
+ write_attribute(column_name, Date.parse(date_string))
9
+ else
10
+ write_attribute(column_name, nil)
11
+ end
12
+ end
13
+
14
+ define_method "european_#{column_name}" do
15
+ if instance_variable_get("@european_#{column_name}").nil? && read_attribute(column_name).nil?
16
+ ""
17
+ elsif instance_variable_get("@european_#{column_name}").nil?
18
+ read_attribute(column_name).strftime("%d/%m/%Y")
19
+ else
20
+ instance_variable_get("@european_#{column_name}")
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,7 @@
1
+ class EuropeanDateValidator < ActiveModel::EachValidator
2
+ EUROPEAN_DATE_MATCHER = /^(((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00|[048])))$/
3
+
4
+ def validate_each(record, attribute, value)
5
+ record.errors[attribute] << "must be in this format DD/MM/YYYY" unless value.nil? || value.is_a?(Date) || value.empty? || value =~ EUROPEAN_DATE_MATCHER
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class EuropeanDateValidatorTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: european-date-text-field
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ version: "0.2"
9
+ platform: ruby
10
+ authors:
11
+ - Jelle Vandebeeck
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-03-30 00:00:00 +02:00
17
+ default_executable:
18
+ dependencies: []
19
+
20
+ description: A gem for when you want to use a textfield to enter a European formatted date.
21
+ email: ""
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files:
27
+ - README.markdown
28
+ - lib/european_date_text_field.rb
29
+ - lib/european_date_validator.rb
30
+ files:
31
+ - MIT-LICENSE
32
+ - README.markdown
33
+ - Rakefile
34
+ - init.rb
35
+ - install.rb
36
+ - lib/european_date_text_field.rb
37
+ - lib/european_date_validator.rb
38
+ - test/european_date_validator_test.rb
39
+ - test/test_helper.rb
40
+ - uninstall.rb
41
+ - Manifest
42
+ - european-date-text-field.gemspec
43
+ has_rdoc: true
44
+ homepage: http://github.com/fousa/european-date-text-field
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --line-numbers
50
+ - --inline-source
51
+ - --title
52
+ - European-date-text-field
53
+ - --main
54
+ - README.markdown
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 1
70
+ - 2
71
+ version: "1.2"
72
+ requirements: []
73
+
74
+ rubyforge_project: european-date-text-field
75
+ rubygems_version: 1.3.6
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: A gem for when you want to use a textfield to enter a European formatted date.
79
+ test_files:
80
+ - test/european_date_validator_test.rb
81
+ - test/test_helper.rb