joenoon-strip_attributes 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.rdoc +56 -0
  2. data/Rakefile +30 -0
  3. data/lib/strip_attributes.rb +47 -0
  4. metadata +73 -0
@@ -0,0 +1,56 @@
1
+ == StripAttributes
2
+
3
+ StripAttributes is a Rails plugin that automatically strips all ActiveRecord
4
+ model attributes of leading and trailing whitespace before validation. If the
5
+ attribute is blank, it strips the value to +nil+.
6
+
7
+ It works by adding a before_validation hook to the record. By default, all
8
+ attributes are stripped of whitespace, but <tt>:only</tt> and <tt>:except</tt>
9
+ options can be used to limit which attributes are stripped. Both options accept
10
+ a single attribute (<tt>:only => :field</tt>) or arrays of attributes (<tt>:except =>
11
+ [:field1, :field2, :field3]</tt>).
12
+
13
+ === Changes from the original StripAttributes
14
+
15
+ Defaults to all models get stripping, and options per-model can be overrriden. Inspired by the xss_terminate style.
16
+
17
+ === Examples
18
+
19
+ # all attributes will be stripped (default)
20
+ class DrunkPokerPlayer < ActiveRecord::Base
21
+ end
22
+
23
+ # all attributes will be stripped except :boxers
24
+ class SoberPokerPlayer < ActiveRecord::Base
25
+ strip_attributes! :except => :boxers
26
+ end
27
+
28
+ # only :shoe, :sock, and :glove will be stripped
29
+ class ConservativePokerPlayer < ActiveRecord::Base
30
+ strip_attributes! :only => [:shoe, :sock, :glove]
31
+ end
32
+
33
+ === Installation
34
+
35
+ gem 'strip_attributes', :git => 'http://github.com/joenoon/strip_attributes.git'
36
+
37
+ === Credits
38
+
39
+ StripAttributes source is hosted on GitHub[http://github.com/]:
40
+ http://github.com/joenoon/strip_attributes
41
+
42
+ Original Credits:
43
+
44
+ The idea was triggered by the information at
45
+ http://wiki.rubyonrails.org/rails/pages/HowToStripWhitespaceFromModelFields
46
+ but was modified from the original to include more idiomatic ruby and rails
47
+ support.
48
+
49
+ Original StripAttributes source is hosted on GitHub[http://github.com/]:
50
+ http://github.com/rmm5t/strip_attributes
51
+
52
+ === License
53
+
54
+ Copyright (c) 2009-2010 Joe Noon released under the MIT license
55
+ Copyright (c) 2007-2008 Ryan McGeary released under the MIT license
56
+ http://en.wikipedia.org/wiki/MIT_License
@@ -0,0 +1,30 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the stripattributes plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the stripattributes plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'Stripattributes'
19
+ rdoc.options << '--line-numbers'
20
+ rdoc.rdoc_files.include('README.rdoc')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ desc 'Publishes rdoc to rubyforge server'
25
+ task :publish_rdoc => :rdoc do
26
+ cmd = "scp -r rdoc/* rmm5t@rubyforge.org:/var/www/gforge-projects/stripattributes"
27
+ puts "\nPublishing rdoc: #{cmd}\n\n"
28
+ system(cmd)
29
+ end
30
+
@@ -0,0 +1,47 @@
1
+ module StripAttributes
2
+
3
+ def self.included(klass)
4
+ klass.send :extend, ClassMethods
5
+ # sets up default of stripping for all fields
6
+ klass.send :strip_attributes!
7
+ klass.class_inheritable_reader :strip_attributes_options
8
+ klass.send :include, InstanceMethods
9
+ end
10
+
11
+ module ClassMethods
12
+ def strip_attributes!(options = {})
13
+ write_inheritable_attribute(:strip_attributes_options, {
14
+ :except => Array(options[:except] || []),
15
+ :only => Array(options[:only] || [])
16
+ })
17
+ end
18
+ end
19
+
20
+ module InstanceMethods
21
+
22
+ def strip_attributes!
23
+ return if strip_attributes_options.nil?
24
+
25
+ self.class.columns.each do |column|
26
+ next unless (column.type == :string || column.type == :text)
27
+
28
+ field = column.name.to_sym
29
+ value = self[field]
30
+
31
+ if !value.respond_to?(:strip)
32
+ next
33
+ elsif strip_attributes_options[:except].include?(field)
34
+ next
35
+ elsif strip_attributes_options[:only].any? && !strip_attributes_options[:only].include?(field)
36
+ next
37
+ else
38
+ self[field] = (value.blank?) ? nil : value.strip
39
+ end
40
+ end
41
+
42
+ end
43
+ end
44
+ end
45
+
46
+ ActiveRecord::Base.send :include, StripAttributes
47
+ ActiveRecord::Base.before_validation :strip_attributes!
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: joenoon-strip_attributes
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 1
9
+ - 3
10
+ version: 1.1.3
11
+ platform: ruby
12
+ authors:
13
+ - Joe Noon
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-23 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: StripAttributes is a Rails plugin that automatically strips all ActiveRecord model attributes of leading and trailing whitespace before validation. If the attribute is blank, it strips the value to nil.
23
+ email: joenoon@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - Rakefile
32
+ - README.rdoc
33
+ - lib/strip_attributes.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/joenoon
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ hash: 57
49
+ segments:
50
+ - 1
51
+ - 8
52
+ - 7
53
+ version: 1.8.7
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 23
60
+ segments:
61
+ - 1
62
+ - 3
63
+ - 6
64
+ version: 1.3.6
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.3.7
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: strip_attributes
72
+ test_files: []
73
+