Empact-sexy_pg_constraints 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -4,10 +4,11 @@ If you're on PostgreSQL and see the importance of data-layer constraints - this
4
4
 
5
5
  == Install
6
6
  As a gem
7
- gem install maxim-sexy_pg_constraints --source http://gems.github.com
7
+ gem install Empact-sexy_pg_constraints
8
8
  or as a plugin
9
- script/plugin install git://github.com/maxim/sexy_pg_constraints.git
10
-
9
+ script/plugin install git://github.com/Empact/sexy_pg_constraints.git
10
+ or in bundler
11
+ gem 'Empact-sexy_pg_constraints', :require => 'sexy_pg_constraints'
11
12
 
12
13
  One more thing. Make sure that in your environment.rb file you have the following line uncommented.
13
14
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -38,11 +38,24 @@ module SexyPgConstraints
38
38
  # Example:
39
39
  # constrain :books, :year, :within => 1980..2008
40
40
  # constrain :books, :year, :within => 1980...2009
41
- # (the two lines above do the same thing)
41
+ # constrain :books, :year, :within => {:range => 1979..2008, :exclude_beginning => true}
42
+ # constrain :books, :year, :within => {:range => 1979..2009, :exclude_beginning => true, :exclude_end => true}
43
+ # (the four lines above do the same thing)
42
44
  #
43
45
  def within(table, column, options)
44
46
  column_ref = column.to_s.include?('.') ? column : "#{table}.#{column}"
45
- "check (#{column_ref} >= #{options.begin} and #{column_ref} #{options.exclude_end? ? ' < ' : ' <= '} #{options.end})"
47
+ if options.respond_to?(:to_hash)
48
+ options = options.to_hash
49
+ options.assert_valid_keys(:range, :exclude_end, :exclude_beginning)
50
+ range = options.fetch(:range)
51
+ exclude_end = options.has_key?(:exclude_end) ? options.fetch(:exclude_end) : range.exclude_end?
52
+ exclude_beginning = options.has_key?(:exclude_beginning) ? options.fetch(:exclude_beginning) : false
53
+ else
54
+ range = options
55
+ exclude_end = range.exclude_end?
56
+ exclude_beginning = false
57
+ end
58
+ "check (#{column_ref} >#{'=' unless exclude_beginning} #{range.begin} and #{column_ref} <#{'=' unless exclude_end} #{range.end})"
46
59
  end
47
60
 
48
61
  ##
@@ -212,6 +212,50 @@ class SexyPgConstraintsTest < Test::Unit::TestCase
212
212
  end
213
213
  end
214
214
 
215
+ def test_within_exclude_beginning
216
+ ActiveRecord::Migration.constrain :books, :from, :within => {:range => 5...11, :exclude_beginning => true}
217
+
218
+ assert_prohibits Book, :from, :within do |book|
219
+ book.from = 11
220
+ end
221
+
222
+ assert_prohibits Book, :from, :within do |book|
223
+ book.from = 5
224
+ end
225
+
226
+ assert_allows Book do |book|
227
+ book.from = 10
228
+ end
229
+
230
+ ActiveRecord::Migration.deconstrain :books, :from, :within
231
+
232
+ assert_allows Book do |book|
233
+ book.from = 5
234
+ end
235
+ end
236
+
237
+ def test_within_exclude_end_overrides_range
238
+ ActiveRecord::Migration.constrain :books, :from, :within => {:range => 5...11, :exclude_end => false}
239
+
240
+ assert_prohibits Book, :from, :within do |book|
241
+ book.from = 12
242
+ end
243
+
244
+ assert_prohibits Book, :from, :within do |book|
245
+ book.from = 4
246
+ end
247
+
248
+ assert_allows Book do |book|
249
+ book.from = 11
250
+ end
251
+
252
+ ActiveRecord::Migration.deconstrain :books, :from, :within
253
+
254
+ assert_allows Book do |book|
255
+ book.from = 12
256
+ end
257
+ end
258
+
215
259
  def test_length_within_inclusive
216
260
  ActiveRecord::Migration.constrain :books, :title, :length_within => 5..11
217
261
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: Empact-sexy_pg_constraints
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.1
5
+ version: 0.2.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Maxim Chernyak
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-04-21 00:00:00 Z
14
+ date: 2011-05-26 00:00:00 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activerecord
@@ -123,7 +123,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
123
  requirements:
124
124
  - - ">="
125
125
  - !ruby/object:Gem::Version
126
- hash: 1726302836925487223
126
+ hash: -864157024733078018
127
127
  segments:
128
128
  - 0
129
129
  version: "0"