activemodel-warnings 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ nbproject
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in activerecord-warnings.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,63 @@
1
+ This gem allows you to mark some validations as optional using very simple syntax
2
+ and let them be skipped by setting one attribute.
3
+
4
+ Works with active model and active record.
5
+
6
+ Example:
7
+
8
+ class OptionalData < Struct.new(:telephone, :address)
9
+ include ActiveModel::Validations
10
+ include ActiveModel::Warnings
11
+
12
+ validates_presence_of :telephone, :warning => true
13
+ warnings do
14
+ validates_presence_of :address
15
+ end
16
+ end
17
+
18
+ data = OptionalData.new()
19
+ data.valid? => false
20
+ data.skip_warnings = true
21
+ data.valid? => true
22
+
23
+ You have three ways of activating the gem:
24
+
25
+ 1)
26
+ require 'activemodel-warnings'
27
+ to use regular attr_writer :skip_warnings for storing the value.
28
+
29
+ With bundler:
30
+ # Gemfile
31
+ gem 'activemodel-warnings', '~> 1.0.0'
32
+
33
+
34
+ The two other ways are useful if skip_warnings attribute is set by user on form checkbox generated by rails:
35
+
36
+ 2)
37
+ require 'activemodel-warnings/boolean'
38
+ if you want the attribute of skip_warning=(attribute) method to be parsed with global Boolean() method
39
+ ex. provided by global_boolean gem after activating it with "GlobalBoolean.kernel!"
40
+
41
+ With bundler:
42
+ # Gemfile
43
+ gem 'global_boolean', '~> 0.1.1'
44
+ gem 'activemodel-warnings', '~> 1.0.0', :require => "activemodel-warnings/boolean"
45
+
46
+ # application.rb
47
+ GlobalBoolean.kernel!
48
+
49
+ 3)
50
+ require 'activemodel-warnings/global_boolean'
51
+ if you want the attribute of skip_warnings=(attribute) to be parsed with GlobalBoolean.Boolean() method
52
+ ex. provided by global_boolean gem but without messing with Kernel namespace
53
+
54
+ With bundler:
55
+ # Gemfile
56
+ gem 'global_boolean', '~> 0.1.1'
57
+ gem 'activemodel-warnings', '~> 1.0.0', :require => "activemodel-warnings/global_boolean"
58
+
59
+
60
+
61
+ Be sure to require global_boolean (if using it) before requiring this gem.
62
+ If using bundler then just put the declaration of global_boolean in Gemfile above the declaration
63
+ of activemodel-warnings.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake'
5
+ require 'rake/testtask'
6
+ require 'rake/rdoctask'
7
+
8
+ desc 'Default: run unit tests.'
9
+ task :default => :test
10
+
11
+ desc 'Test the activemodel-warnings plugin.'
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'lib'
14
+ t.libs << 'test'
15
+ t.pattern = 'test/**/*_test.rb'
16
+ t.verbose = true
17
+ end
18
+
19
+ desc 'Generate documentation for the activemodel-warnings plugin.'
20
+ Rake::RDocTask.new(:rdoc) do |rdoc|
21
+ rdoc.rdoc_dir = 'rdoc'
22
+ rdoc.title = 'Active Model Warnings'
23
+ rdoc.rdoc_files.include('README')
24
+ rdoc.rdoc_files.include('lib/**/*.rb')
25
+ end
26
+
27
+
data/TODO ADDED
@@ -0,0 +1 @@
1
+ switch to: require active_model-warnings ?
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "activemodel-warnings/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "activemodel-warnings"
7
+ s.version = ActiveModel::Warnings::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Robert Pankowecki (Gavdi Pollska)"]
10
+ s.email = ["robert.pankowecki@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Mark some validations as warnings and let them be easily skipped}
13
+ s.description = %q{Mark some validations as warnings and let them be easily skipped}
14
+
15
+ s.required_rubygems_version = ">= 1.3.6"
16
+
17
+ s.rubyforge_project = "global_boolean"
18
+
19
+ s.add_dependency "activemodel", "~> 3.0.3"
20
+
21
+ s.add_development_dependency "bundler", "~> 1.0.7"
22
+ s.add_development_dependency "mocha"
23
+
24
+ # s.add_optional_dependency "global_boolean", "~> 0.1.1" # There is no such thing as add_optional_dependency. Just to notify the users
25
+ # Read http://yehudakatz.com/2010/04/17/ruby-require-order-problems/
26
+ # Read README file to learn about how to use the gem with or without global_boolean support
27
+
28
+ s.files = `git ls-files`.split("\n")
29
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
30
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
31
+ s.require_paths = ["lib"]
32
+ end
@@ -0,0 +1,9 @@
1
+ require 'activemodel-warnings'
2
+
3
+ module ActiveModel
4
+ module Warnings
5
+ def skip_warnings=(v)
6
+ @skip_warnings = Boolean(v)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ require 'activemodel-warnings'
2
+ require 'global_boolean'
3
+
4
+ module ActiveModel
5
+ module Warnings
6
+ def skip_warnings=(v)
7
+ @skip_warnings = GlobalBoolean.Boolean(v)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveModel
2
+
3
+ module Warnings
4
+ VERSION = "1.0.1"
5
+ end
6
+
7
+ end
@@ -0,0 +1,51 @@
1
+ require 'active_model'
2
+
3
+ module ActiveModel
4
+
5
+ module Warnings
6
+
7
+ attr_accessor :skip_warnings
8
+
9
+ def self.included(klass)
10
+ klass.extend(ClassMethods)
11
+ end
12
+
13
+ module ClassMethods
14
+
15
+ def warnings()
16
+ @warnings_block = true
17
+ yield
18
+ ensure
19
+ @warnings_block = false
20
+ end
21
+
22
+ def validate(*args, &block)
23
+ options = args.extract_options!
24
+
25
+ if options[:warning] || @warnings_block
26
+ options = options.dup
27
+ options[:warning] ||= @warnings_block
28
+ options[:if] = Array.wrap(options[:if])
29
+ options[:if] << "skip_warnings != true"
30
+ end
31
+ args << options
32
+
33
+ super
34
+ end
35
+
36
+ def validates_with(*args, &block)
37
+ options = args.extract_options!
38
+
39
+ if options[:warning] || @warnings_block
40
+ options = options.dup
41
+ options[:warning] ||= @warnings_block
42
+ end
43
+ args << options
44
+ super
45
+ end
46
+
47
+ end # ClassMethods
48
+
49
+ end # Warnings
50
+
51
+ end # ActiveModel
@@ -0,0 +1,5 @@
1
+ require 'activemodel-warnings'
2
+
3
+ require 'test/unit'
4
+ require 'active_support/test_case'
5
+ require 'mocha'
@@ -0,0 +1,50 @@
1
+ load File.join(File.dirname(__FILE__), 'test_helper.rb')
2
+
3
+
4
+ class WarningsTest < ActiveSupport::TestCase
5
+
6
+ class TestMe < Struct.new(:one, :two, :three)
7
+ include ActiveModel::Validations
8
+ include ActiveModel::Warnings
9
+
10
+ validates_presence_of :one
11
+ validates_presence_of :two, :warning => true
12
+ warnings do
13
+ validates_presence_of :three
14
+ end
15
+
16
+ end
17
+
18
+ setup do
19
+ @valid = TestMe.new("one", "two", "three")
20
+ @invalid = TestMe.new()
21
+ end
22
+
23
+ test "without skipping" do
24
+ @valid.errors.expects(:add).never
25
+ @valid.valid?
26
+
27
+ @invalid.errors.expects(:add).with(:one, :blank, anything)
28
+ @invalid.errors.expects(:add).with(:two, :blank, anything)
29
+ @invalid.errors.expects(:add).with(:three, :blank, anything)
30
+ @invalid.valid?
31
+ end
32
+
33
+ test "with skipping" do
34
+ @valid.skip_warnings = true
35
+ @valid.errors.expects(:add).never
36
+ @valid.valid?
37
+
38
+ @invalid.skip_warnings = true
39
+ @invalid.errors.expects(:add).with(:one, :blank, anything)
40
+ @invalid.valid?
41
+ end
42
+
43
+ test "validator recives the warning option" do
44
+ assert ! TestMe.validators_on(:one).first.options[:warning]
45
+ assert TestMe.validators_on(:two).first.options[:warning]
46
+ assert TestMe.validators_on(:three).first.options[:warning]
47
+ end
48
+
49
+
50
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activemodel-warnings
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 1
9
+ version: 1.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Robert Pankowecki (Gavdi Pollska)
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-08 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activemodel
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 3
30
+ - 0
31
+ - 3
32
+ version: 3.0.3
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: bundler
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 1
45
+ - 0
46
+ - 7
47
+ version: 1.0.7
48
+ type: :development
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: mocha
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ description: Mark some validations as warnings and let them be easily skipped
64
+ email:
65
+ - robert.pankowecki@gmail.com
66
+ executables: []
67
+
68
+ extensions: []
69
+
70
+ extra_rdoc_files: []
71
+
72
+ files:
73
+ - .gitignore
74
+ - Gemfile
75
+ - README
76
+ - Rakefile
77
+ - TODO
78
+ - activemodel-warnings.gemspec
79
+ - lib/activemodel-warnings.rb
80
+ - lib/activemodel-warnings/boolean.rb
81
+ - lib/activemodel-warnings/global_boolean.rb
82
+ - lib/activemodel-warnings/version.rb
83
+ - test/test_helper.rb
84
+ - test/warnings_test.rb
85
+ has_rdoc: true
86
+ homepage: ""
87
+ licenses: []
88
+
89
+ post_install_message:
90
+ rdoc_options: []
91
+
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ segments:
108
+ - 1
109
+ - 3
110
+ - 6
111
+ version: 1.3.6
112
+ requirements: []
113
+
114
+ rubyforge_project: global_boolean
115
+ rubygems_version: 1.3.7
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: Mark some validations as warnings and let them be easily skipped
119
+ test_files: []
120
+