with_filled_field_scope 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Marcin Nowicki
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,17 @@
1
+ = with_filled_field_scope
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Marcin Nowicki. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "with_filled_field_scope"
8
+ gem.summary = %Q{Adds with_filled_field scope to active record models}
9
+ gem.description = %Q{Adds with_filled_field scope to active record models. It scope searches for record with specified field filled.}
10
+ gem.email = "pr0d1r2@doubledrones.com"
11
+ gem.homepage = "http://github.com/Pr0d1r2/with_filled_field_scope"
12
+ gem.authors = ["Marcin Nowicki"]
13
+ gem.add_dependency "activerecord", ">= 2.3"
14
+ gem.add_dependency "searchlogic", ">= 2.3"
15
+ gem.add_development_dependency "rspec", ">= 1.2.9"
16
+ gem.add_development_dependency "active_record_connectionless", ">= 1.0.2"
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'spec/rake/spectask'
25
+ Spec::Rake::SpecTask.new(:spec) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.spec_files = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :spec => :check_dependencies
37
+
38
+ task :default => :spec
39
+
40
+ require 'rake/rdoctask'
41
+ Rake::RDocTask.new do |rdoc|
42
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "with_filled_field_scope #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
@@ -0,0 +1,34 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+ require 'searchlogic'
4
+
5
+ module ActiveRecord
6
+ module WithFilledFieldScope
7
+ module Base
8
+
9
+ FIELD_TYPES_SUPPORTING_ONLY_NOT_NULL = [:date, :decimal]
10
+
11
+ def has_with_filled_field_scope(model = self)
12
+ alias_scope :with_filled_field, lambda { |field|
13
+ if field_support_not_blank?(field, model)
14
+ send("#{field.to_s}_not_blank")
15
+ else
16
+ send("#{field.to_s}_not_null")
17
+ end
18
+ }
19
+ end
20
+
21
+ def field_support_not_blank?(field, model = self)
22
+ !FIELD_TYPES_SUPPORTING_ONLY_NOT_NULL.include?(field_column_type(field))
23
+ end
24
+
25
+ def field_column_type(field, model = self)
26
+ model.columns_hash[field.to_s].type
27
+ end
28
+
29
+ end
30
+ end
31
+ end
32
+
33
+ ActiveRecord::Base.extend(ActiveRecord::WithFilledFieldScope::Base)
34
+
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'with_filled_field_scope'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
@@ -0,0 +1,62 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'active_record_connectionless'
3
+
4
+ class WithFilledFieldScopeModel < ActiveRecord::Base
5
+ has_with_filled_field_scope
6
+ end
7
+
8
+ describe 'WithFilledFieldScopeModel' do
9
+
10
+ describe 'field_column_type' do
11
+ before do
12
+ @name_column = mock(ActiveRecord::ConnectionAdapters::Column)
13
+ @columns_hash = {'name' => @name_column}
14
+ end
15
+
16
+ it 'should read column type from models column hash' do
17
+ @name_column.should_receive(:type).and_return(:name_column_type)
18
+ WithFilledFieldScopeModel.should_receive(:columns_hash).and_return(@columns_hash)
19
+ WithFilledFieldScopeModel.field_column_type(:name).should == :name_column_type
20
+ end
21
+
22
+ it 'should allow to change model' do
23
+ @name_column.should_receive(:type).and_return(:name_column_type)
24
+ changed_model = mock(ActiveRecord::Base)
25
+ changed_model.should_receive(:columns_hash).and_return(@columns_hash)
26
+ WithFilledFieldScopeModel.field_column_type(:name, changed_model).should == :name_column_type
27
+ end
28
+ end
29
+
30
+ describe 'field_support_not_blank?' do
31
+ it 'should be false for date' do
32
+ WithFilledFieldScopeModel.should_receive(:field_column_type).with(:field).and_return(:date)
33
+ WithFilledFieldScopeModel.field_support_not_blank?(:field).should be_false
34
+ end
35
+
36
+ it 'should be false for decimal' do
37
+ WithFilledFieldScopeModel.should_receive(:field_column_type).with(:field).and_return(:decimal)
38
+ WithFilledFieldScopeModel.field_support_not_blank?(:field).should be_false
39
+ end
40
+
41
+ it 'should be true for string' do
42
+ WithFilledFieldScopeModel.should_receive(:field_column_type).with(:field).and_return(:string)
43
+ WithFilledFieldScopeModel.field_support_not_blank?(:field).should be_true
44
+ end
45
+ end
46
+
47
+ describe 'with_filled_field scope' do
48
+ it 'should use not null scope when field does not support not blank' do
49
+ WithFilledFieldScopeModel.should_receive(:field_support_not_blank?).with(:field, WithFilledFieldScopeModel).and_return(false)
50
+ WithFilledFieldScopeModel.should_receive(:field_not_null)
51
+ WithFilledFieldScopeModel.with_filled_field(:field)
52
+ end
53
+
54
+ it 'should use not blank scope when field support not blank' do
55
+ WithFilledFieldScopeModel.should_receive(:field_support_not_blank?).with(:field, WithFilledFieldScopeModel).and_return(true)
56
+ WithFilledFieldScopeModel.should_receive(:field_not_blank)
57
+ WithFilledFieldScopeModel.with_filled_field(:field)
58
+ end
59
+ end
60
+
61
+ end
62
+
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: with_filled_field_scope
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Marcin Nowicki
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-08 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 3
30
+ version: "2.3"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: searchlogic
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 2
42
+ - 3
43
+ version: "2.3"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 1
55
+ - 2
56
+ - 9
57
+ version: 1.2.9
58
+ type: :development
59
+ version_requirements: *id003
60
+ - !ruby/object:Gem::Dependency
61
+ name: active_record_connectionless
62
+ prerelease: false
63
+ requirement: &id004 !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 1
69
+ - 0
70
+ - 2
71
+ version: 1.0.2
72
+ type: :development
73
+ version_requirements: *id004
74
+ description: Adds with_filled_field scope to active record models. It scope searches for record with specified field filled.
75
+ email: pr0d1r2@doubledrones.com
76
+ executables: []
77
+
78
+ extensions: []
79
+
80
+ extra_rdoc_files:
81
+ - LICENSE
82
+ - README.rdoc
83
+ files:
84
+ - .document
85
+ - .gitignore
86
+ - LICENSE
87
+ - README.rdoc
88
+ - Rakefile
89
+ - lib/with_filled_field_scope.rb
90
+ - spec/spec.opts
91
+ - spec/spec_helper.rb
92
+ - spec/with_filled_field_scope_spec.rb
93
+ has_rdoc: true
94
+ homepage: http://github.com/Pr0d1r2/with_filled_field_scope
95
+ licenses: []
96
+
97
+ post_install_message:
98
+ rdoc_options:
99
+ - --charset=UTF-8
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ requirements: []
117
+
118
+ rubyforge_project:
119
+ rubygems_version: 1.3.6
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: Adds with_filled_field scope to active record models
123
+ test_files:
124
+ - spec/spec_helper.rb
125
+ - spec/with_filled_field_scope_spec.rb