rares-acts_as_filterable 0.0.1
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.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +13 -0
- data/Rakefile +57 -0
- data/VERSION.yml +4 -0
- data/acts_as_filterable.gemspec +53 -0
- data/init.rb +1 -0
- data/lib/acts_as_filterable.rb +7 -0
- data/lib/base.rb +54 -0
- data/rails/init.rb +1 -0
- data/test/acts_as_filterable_integration_test.rb +106 -0
- data/test/test_helper.rb +28 -0
- metadata +76 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Rob Ares
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
= acts_as_filterable
|
2
|
+
|
3
|
+
Small BS plugin that allows you to add small processing filters to
|
4
|
+
ActiveRecord attributes. This was born from working with a legacy
|
5
|
+
database that had a bunch of bad apps depending on the fields
|
6
|
+
looking just the right way.
|
7
|
+
|
8
|
+
Instead of implementing :before_validate all over the place, I threw this together
|
9
|
+
so that I can just apply macros.
|
10
|
+
|
11
|
+
== Copyright
|
12
|
+
|
13
|
+
Copyright (c) 2009 Rob Ares. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "rake"
|
3
|
+
|
4
|
+
begin
|
5
|
+
require "jeweler"
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "acts_as_filterable"
|
8
|
+
gem.summary = %Q{TODO}
|
9
|
+
gem.email = "rob.ares@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/rares/acts_as_filterable"
|
11
|
+
gem.authors = ["Rob Ares"]
|
12
|
+
|
13
|
+
gem.add_dependency("activerecord", ">= 1.15.0")
|
14
|
+
end
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
17
|
+
end
|
18
|
+
|
19
|
+
require "rake/testtask"
|
20
|
+
Rake::TestTask.new(:test) do |test|
|
21
|
+
test.libs << "lib" << "test"
|
22
|
+
test.pattern = "test/**/*_test.rb"
|
23
|
+
test.verbose = false
|
24
|
+
test.options = "-v"
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require "rcov/rcovtask"
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << "test"
|
31
|
+
test.pattern = "test/**/*_test.rb"
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require "rake/rdoctask"
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
if File.exist?("VERSION.yml")
|
46
|
+
config = YAML.load(File.read("VERSION.yml"))
|
47
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
48
|
+
else
|
49
|
+
version = ""
|
50
|
+
end
|
51
|
+
|
52
|
+
rdoc.rdoc_dir = "rdoc"
|
53
|
+
rdoc.title = "acts_as_filterable #{version}"
|
54
|
+
rdoc.rdoc_files.include("README*")
|
55
|
+
rdoc.rdoc_files.include("lib/**/*.rb")
|
56
|
+
end
|
57
|
+
|
data/VERSION.yml
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{acts_as_filterable}
|
5
|
+
s.version = "0.0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Rob Ares"]
|
9
|
+
s.date = %q{2009-06-28}
|
10
|
+
s.email = %q{rob.ares@gmail.com}
|
11
|
+
s.extra_rdoc_files = [
|
12
|
+
"LICENSE",
|
13
|
+
"README.rdoc"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
".document",
|
17
|
+
".gitignore",
|
18
|
+
"LICENSE",
|
19
|
+
"README.rdoc",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION.yml",
|
22
|
+
"acts_as_filterable.gemspec",
|
23
|
+
"init.rb",
|
24
|
+
"lib/acts_as_filterable.rb",
|
25
|
+
"lib/base.rb",
|
26
|
+
"rails/init.rb",
|
27
|
+
"test/acts_as_filterable_integration_test.rb",
|
28
|
+
"test/test_helper.rb"
|
29
|
+
]
|
30
|
+
s.has_rdoc = true
|
31
|
+
s.homepage = %q{http://github.com/rares/acts_as_filterable}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.1}
|
35
|
+
s.summary = %q{TODO}
|
36
|
+
s.test_files = [
|
37
|
+
"test/acts_as_filterable_integration_test.rb",
|
38
|
+
"test/test_helper.rb"
|
39
|
+
]
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 2
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 1.15.0"])
|
47
|
+
else
|
48
|
+
s.add_dependency(%q<activerecord>, [">= 1.15.0"])
|
49
|
+
end
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<activerecord>, [">= 1.15.0"])
|
52
|
+
end
|
53
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init"
|
data/lib/base.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
module ActsAsFilterable
|
2
|
+
|
3
|
+
module ActiveRecordExt
|
4
|
+
|
5
|
+
module Base
|
6
|
+
|
7
|
+
def self.included(klazz)
|
8
|
+
klazz.extend ClassMethods
|
9
|
+
klazz.before_validation :apply_filters
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
|
16
|
+
def filter_for_numerics(*args)
|
17
|
+
to_be_filtered[:numbers] |= args unless args.empty?
|
18
|
+
end
|
19
|
+
|
20
|
+
def filters
|
21
|
+
@filters ||= begin
|
22
|
+
f = Hash.new []
|
23
|
+
f[:numbers] = /[^0-9]*/
|
24
|
+
f
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_be_filtered
|
29
|
+
@to_be_filtered ||= Hash.new []
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
def apply_filters
|
35
|
+
self.class.to_be_filtered.each do |key, value|
|
36
|
+
value.each do |attr|
|
37
|
+
apply_filter self.class.filters[key], attr
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def apply_filter(filter, attr)
|
45
|
+
if not send(attr).blank? and send(attr).is_a?(String)
|
46
|
+
send(attr).gsub!(filter, "")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "acts_as_filterable"
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ActsAsFilterableIntegrationTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "An ActiveRecord model using acts_as_filterable" do
|
6
|
+
setup do
|
7
|
+
@model = ContactDetail.new do |cd|
|
8
|
+
cd.name = "joe smith"
|
9
|
+
cd.phone_number = "2223334444"
|
10
|
+
cd.discount = 0.25
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
should "add an #apply_filters instance method" do
|
15
|
+
@model.send(:apply_filters).nil?.should_not be(true)
|
16
|
+
end
|
17
|
+
|
18
|
+
should "know about the types of filters that will be applied to the attributes" do
|
19
|
+
ContactDetail.respond_to?(:to_be_filtered).should be(true)
|
20
|
+
end
|
21
|
+
|
22
|
+
should "make it's filters available" do
|
23
|
+
ContactDetail.respond_to?(:filters).should be(true)
|
24
|
+
end
|
25
|
+
|
26
|
+
should "default filters that don't exist to an empty array" do
|
27
|
+
ContactDetail.filters[:test].empty?.should be(true)
|
28
|
+
end
|
29
|
+
|
30
|
+
should "contain some filters initially" do
|
31
|
+
ContactDetail.filters[:numeric].nil?.should_not be(true)
|
32
|
+
end
|
33
|
+
|
34
|
+
should "freeze the macro collection so it cannot be mutated" do
|
35
|
+
ContactDetail.filters.store(:test, /./).should raise_error
|
36
|
+
end
|
37
|
+
|
38
|
+
should "add a macro to filter non-numeric values from string fields" do
|
39
|
+
ContactDetail.respond_to?(:filter_for_numerics).should be(true)
|
40
|
+
end
|
41
|
+
|
42
|
+
should "be savable with valid data" do
|
43
|
+
@model.save.should be(true)
|
44
|
+
end
|
45
|
+
|
46
|
+
context "with formatted phone number data" do
|
47
|
+
setup do
|
48
|
+
@model.phone_number = "(222) 333-4444"
|
49
|
+
@model.valid?
|
50
|
+
end
|
51
|
+
|
52
|
+
should "strip all formatting" do
|
53
|
+
@model.phone_number.should be("2223334444")
|
54
|
+
end
|
55
|
+
|
56
|
+
should "return a coercable numeric value" do
|
57
|
+
@model.phone_number.to_f.should be(2223334444)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
context "with a nill attribute value" do
|
63
|
+
setup do
|
64
|
+
@model.phone_number = nil
|
65
|
+
end
|
66
|
+
|
67
|
+
should "not raise any errors due to a nil attribute value" do
|
68
|
+
lambda { @model.valid? }.should_not raise_error
|
69
|
+
end
|
70
|
+
|
71
|
+
should "not attempt to change the attribute value" do
|
72
|
+
@model.valid?
|
73
|
+
@model.phone_number.nil?.should be(true)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "with non-character attributes" do
|
78
|
+
setup do
|
79
|
+
ContactDetail.filter_for_numerics :discount
|
80
|
+
end
|
81
|
+
|
82
|
+
should "not raise any errors due to a non-character attribute value" do
|
83
|
+
lambda { @model.valid? }.should_not raise_error
|
84
|
+
end
|
85
|
+
|
86
|
+
should "not attempt to change the attribute value" do
|
87
|
+
@model.valid?
|
88
|
+
@model.discount.should be(0.25)
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
context "with an attribute value that contains no non-numeric values to be stripped" do
|
94
|
+
setup do
|
95
|
+
@model.phone_number = "2223334444"
|
96
|
+
@model.valid?
|
97
|
+
end
|
98
|
+
|
99
|
+
should "not change the attribute value" do
|
100
|
+
@model.phone_number.should be("2223334444")
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "activerecord"
|
3
|
+
require "shoulda"
|
4
|
+
require "matchy"
|
5
|
+
|
6
|
+
gem "sqlite3-ruby"
|
7
|
+
|
8
|
+
require File.dirname(__FILE__) + "/../init"
|
9
|
+
|
10
|
+
ActiveRecord::Base.logger = Logger.new("/tmp/acts_as_filterable.log")
|
11
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "/tmp/acts_as_filterable.sqlite")
|
12
|
+
ActiveRecord::Migration.verbose = false
|
13
|
+
|
14
|
+
ActiveRecord::Schema.define do
|
15
|
+
create_table :contact_details, :force => true do |t|
|
16
|
+
t.string :name
|
17
|
+
t.string :phone_number
|
18
|
+
t.string :fax_number
|
19
|
+
t.float :discount
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class ContactDetail < ActiveRecord::Base
|
24
|
+
filter_for_numerics :phone_number, :fax_number
|
25
|
+
end
|
26
|
+
|
27
|
+
class Test::Unit::TestCase
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rares-acts_as_filterable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rob Ares
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-28 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.15.0
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: rob.ares@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION.yml
|
41
|
+
- acts_as_filterable.gemspec
|
42
|
+
- init.rb
|
43
|
+
- lib/acts_as_filterable.rb
|
44
|
+
- lib/base.rb
|
45
|
+
- rails/init.rb
|
46
|
+
- test/acts_as_filterable_integration_test.rb
|
47
|
+
- test/test_helper.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/rares/acts_as_filterable
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options:
|
52
|
+
- --charset=UTF-8
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
requirements: []
|
68
|
+
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.2.0
|
71
|
+
signing_key:
|
72
|
+
specification_version: 2
|
73
|
+
summary: TODO
|
74
|
+
test_files:
|
75
|
+
- test/acts_as_filterable_integration_test.rb
|
76
|
+
- test/test_helper.rb
|