polly-suppress_validations 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [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.
data/README ADDED
@@ -0,0 +1,28 @@
1
+ SuppressValidations
2
+ ===================
3
+
4
+ Allows you to temporarily disable validations for any ActiveRecord object,
5
+ mainly intended for use in script/console, though it might possibly be somewhat
6
+ usefull in unit tests as well.
7
+
8
+ Example
9
+ =======
10
+
11
+ class SomeModel < ActiveRecord::Base
12
+ validates_presence_of :some_attribute
13
+ end
14
+
15
+
16
+
17
+ include SuppressValidations
18
+
19
+ some_model = SomeModel.new
20
+ some_model.save # -> false
21
+
22
+
23
+ suppress_validations do
24
+ some_model.save # -> true
25
+ end
26
+
27
+
28
+ Copyright (c) 2009 Patrik Hedman, released under the MIT license
@@ -0,0 +1,23 @@
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 suppress_validations plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the suppress_validations plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'SuppressValidations'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,42 @@
1
+ module SuppressValidations
2
+ def self.included(klass)
3
+ ActiveRecord::Base.class_eval do
4
+ extend ClassMethods
5
+ alias_method :original_valid?, :valid?
6
+
7
+ def valid?
8
+ if self.class.validations_disabled?
9
+ true
10
+ else
11
+ original_valid?
12
+ end
13
+ end
14
+ end
15
+
16
+ klass.send :include, InstanceMethods
17
+ end
18
+
19
+ module ClassMethods
20
+ def disable_validations!
21
+ @@validations_disabled = true
22
+ end
23
+
24
+ def enable_validations!
25
+ @@validations_disabled = false
26
+ end
27
+
28
+ def validations_disabled?
29
+ @@validations_disabled ||= false
30
+ end
31
+ end
32
+
33
+ module InstanceMethods
34
+ def suppress_validations
35
+ ActiveRecord::Base.disable_validations!
36
+ ret = yield
37
+ ActiveRecord::Base.enable_validations!
38
+
39
+ return ret
40
+ end
41
+ end
42
+ end
@@ -0,0 +1 @@
1
+ require 'suppress_validations'
@@ -0,0 +1,43 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{suppress_validations}
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 = ["Patrik Hedman"]
9
+ s.date = %q{2009-07-03}
10
+ s.description = %q{A Rails plugin that let's you temporarily suppress ActiveRecord validations}
11
+ s.email = %q{patrik@moresale.se}
12
+ s.files = [
13
+ "MIT-LICENSE",
14
+ "README",
15
+ "Rakefile",
16
+ "install.rb",
17
+ "uninstall.rb",
18
+ "suppress_validations.gemspec",
19
+ "lib/suppress_validations.rb",
20
+ "rails/init.rb"
21
+ ]
22
+ s.homepage = %q{http://github.com/polly/ffmpeg}
23
+ s.rdoc_options = ["--charset=UTF-8"]
24
+ s.require_paths = ["lib"]
25
+ s.rubygems_version = %q{1.3.4}
26
+ s.summary = %q{Need to write one}
27
+ s.test_files = [
28
+ "test/database.yml",
29
+ "test/schema.rb",
30
+ "test/suppress_validations_test.rb",
31
+ "test/test_helper.rb"
32
+ ]
33
+
34
+ if s.respond_to? :specification_version then
35
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
36
+ s.specification_version = 3
37
+
38
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
39
+ else
40
+ end
41
+ else
42
+ end
43
+ end
@@ -0,0 +1,22 @@
1
+ sqlite:
2
+ :adapter: sqlite
3
+ :dbfile: vendor/plugins/suppress_validations/test/suppress_validations_plugin.sqlite.db
4
+
5
+ sqlite3:
6
+ :adapter: sqlite3
7
+ :dbfile: vendor/plugins/suppress_validations/test/suppress_validations_plugin.sqlite3.db
8
+
9
+ postgresql:
10
+ :adapter: postgresql
11
+ :username: postgres
12
+ :password: postgres
13
+ :database: suppress_validations_plugin_test
14
+ :min_messages: ERROR
15
+
16
+ mysql:
17
+ :adapter: mysql
18
+ :host: localhost
19
+ :username: root
20
+ :password: password
21
+ :database: suppress_validations_plugin_test
22
+
@@ -0,0 +1,7 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :suppress_validations_models, :force => true do |t|
3
+ t.string :title
4
+ t.text :desc
5
+ end
6
+ end
7
+
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ class SuppressValidationsTest < ActiveSupport::TestCase
4
+ include SuppressValidations
5
+ load_schema
6
+
7
+ class SuppressValidationsModel < ActiveRecord::Base
8
+ validates_presence_of :title, :desc
9
+ end
10
+
11
+ setup do
12
+ SuppressValidationsModel.delete_all
13
+ end
14
+
15
+ test "load schema" do
16
+ assert_equal [], SuppressValidationsModel.all
17
+ end
18
+
19
+ test "should be able to create a new record" do
20
+ suppressed_validations_model = SuppressValidationsModel.new
21
+ suppressed_validations_model.title = "Test"
22
+ suppressed_validations_model.desc = "Desc"
23
+ suppressed_validations_model.save
24
+
25
+ assert_equal 1, SuppressValidationsModel.count
26
+ end
27
+
28
+ test "title should be required" do
29
+ suppressed_validations_model = SuppressValidationsModel.new
30
+ suppressed_validations_model.save
31
+
32
+ assert !suppressed_validations_model.errors[:title].nil?
33
+ end
34
+
35
+ test "desc should be required" do
36
+ suppressed_validations_model = SuppressValidationsModel.new
37
+ suppressed_validations_model.save
38
+
39
+ assert !suppressed_validations_model.errors[:desc].nil?
40
+ end
41
+
42
+ test "it should be able to disable validations" do
43
+ ActiveRecord::Base.disable_validations!
44
+
45
+ assert ActiveRecord::Base.validations_disabled?
46
+ end
47
+
48
+ test "it should be able to enable validations" do
49
+ ActiveRecord::Base.enable_validations!
50
+
51
+ assert !ActiveRecord::Base.validations_disabled?
52
+ end
53
+
54
+ test "suppress_validations should disable validations" do
55
+ suppress_validations do
56
+ suppressed_validations_model = SuppressValidationsModel.new
57
+ suppressed_validations_model.save
58
+ end
59
+
60
+ assert_equal 1, SuppressValidationsModel.count
61
+ end
62
+ end
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
4
+
5
+ ENV['RAILS_ENV'] = 'test'
6
+ ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
7
+
8
+ require 'test/unit'
9
+ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
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
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polly-suppress_validations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Patrik Hedman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-03 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A Rails plugin that let's you temporarily suppress ActiveRecord validations
17
+ email: patrik@moresale.se
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - MIT-LICENSE
26
+ - README
27
+ - Rakefile
28
+ - install.rb
29
+ - uninstall.rb
30
+ - suppress_validations.gemspec
31
+ - lib/suppress_validations.rb
32
+ - rails/init.rb
33
+ has_rdoc: false
34
+ homepage: http://github.com/polly/ffmpeg
35
+ post_install_message:
36
+ rdoc_options:
37
+ - --charset=UTF-8
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.2.0
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Need to write one
59
+ test_files:
60
+ - test/database.yml
61
+ - test/schema.rb
62
+ - test/suppress_validations_test.rb
63
+ - test/test_helper.rb