flag_shih_tzu 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 39037feb3a3b52c19a177cfdd173334d4f94d841
4
- data.tar.gz: f5f735bb7b3cea9470b224fbc4dc43661b883bf4
3
+ metadata.gz: 33880b74805cf7ad653f77e4d0d4768c2c2c6e4c
4
+ data.tar.gz: 0dcaec06a2d68885d2225bf66e43cadff4c2bd9d
5
5
  SHA512:
6
- metadata.gz: d50b56dc9b3217ca2766f0795f92846ad0a88356ff38a3f3153283ac5f65c220d0be7f1a7a4c22a46d2538e45d2106b9b1d8df15e28a73ada846cc7d199753ed
7
- data.tar.gz: 198b808f2f9004c159620075678939ff19efdb383c30a349d5940e68c39e250f159e2f05c73bb347e68e12ad5eb7c69b3b0d9ca525f6c9fed252f501727a0cd3
6
+ metadata.gz: 87e72860a03ca9de17d788bf274f7caf75cfe8c895447f22b576d7ccf03b201e7cea41c8846a46c401521764c3320f4493cbf32046ed3a0b91a05c65dd92032e
7
+ data.tar.gz: cbf558efa240f5ad6e1e9a0ee9af6aefc1b1ebb5f04c44f06aa892cff13c059e6bac424e8ba63a506f1831e0e616495770c984de1dc7d9df4c6cad287fe71e75
data/.travis.yml CHANGED
@@ -2,8 +2,18 @@ rvm:
2
2
  - 1.8.7
3
3
  - 1.9.2
4
4
  - 1.9.3
5
+ - 2.0.0
5
6
  gemfile:
6
7
  - gemfiles/Gemfile.activerecord-2.3.x
7
8
  - gemfiles/Gemfile.activerecord-3.0.x
8
9
  - gemfiles/Gemfile.activerecord-3.1.x
9
10
  - gemfiles/Gemfile.activerecord-3.2.x
11
+ - gemfiles/Gemfile.activerecord-4.0.x
12
+ matrix:
13
+ exclude:
14
+ - rvm: 2.0.0
15
+ gemfile: gemfiles/Gemfile.activerecord-2.3.x
16
+ - rvm: 1.8.7
17
+ gemfile: gemfiles/Gemfile.activerecord-4.0.x
18
+ - rvm: 1.9.2
19
+ gemfile: gemfiles/Gemfile.activerecord-4.0.x
data/CHANGELOG CHANGED
@@ -1,4 +1,12 @@
1
- Version 0.3.3 - JUN.20.2013
1
+ Version 0.3.5 - AUG.06.2013
2
+ * Fix Travis Build & Add Rails 4 by Peter M. Goldstein
3
+ * Implemented update_flag! by Peter Boling (see https://github.com/pboling/flag_shih_tzu/issues/27)
4
+ - sets a flag on a record without triggering callbacks or validations
5
+ - optionally syncs the instance with new flag value
6
+ * Update gemspec by Peter Boling
7
+
8
+ Version 0.3.4 - JUN.20.2013
9
+
2
10
  * Allow non sequential flag numbers by Thomas Jachmann
3
11
  * Report correct source location for class_evaled methods. by Sebastian Korfmann
4
12
  * Implemented chained_flags_with, which allows optimizing the bit search by Tatsuhiko Miyagawa
@@ -13,6 +21,9 @@ Version 0.3.3 - JUN.20.2013
13
21
  * convenience methods now have default parameter so `all_flags` works with arity 0. by Peter Boling
14
22
  * Many more tests, including arity tests by Peter Boling
15
23
 
24
+ Version 0.3.3 - JUN.20.2013
25
+ - Does not exist.
26
+
16
27
  Version 0.3.2 - NOV.06.2012
17
28
 
18
29
  * Adds skip column check option :check_for_column - from arturaz
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
8
8
  s.licenses = ['MIT']
9
9
  s.email = 'peter.boling@gmail.com'
10
10
  s.platform = Gem::Platform::RUBY
11
- s.authors = ["Patryk Peszko", "Sebastian Roebke", "David Anderson", "Tim Payton"]
12
- s.homepage = "https://github.com/xing/flag_shih_tzu"
11
+ s.authors = ["Peter Boling", "Patryk Peszko", "Sebastian Roebke", "David Anderson", "Tim Payton"]
12
+ s.homepage = "https://github.com/pboling/flag_shih_tzu"
13
13
  s.summary = %q{Bit fields for ActiveRecord}
14
14
  s.description = <<-EODOC
15
15
  Bit fields for ActiveRecord:
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec :path => '..'
4
+
5
+ gem "activerecord", "~>4.0.0"
data/lib/flag_shih_tzu.rb CHANGED
@@ -38,7 +38,7 @@ module FlagShihTzu
38
38
  end
39
39
  colmn = opts[:column]
40
40
  if opts[:check_for_column] && !check_flag_column(colmn)
41
- puts "FlagShihTzu says: Flag column #{colmn} appears to be missing!\nTo turn off this warning set check_for_colum: false in has_flags definition here: #{caller.first}"
41
+ puts "FlagShihTzu says: Flag column #{colmn} appears to be missing!\nTo turn off this warning set check_for_column: false in has_flags definition here: #{caller.first}"
42
42
  return
43
43
  end
44
44
 
@@ -192,7 +192,11 @@ module FlagShihTzu
192
192
  end
193
193
 
194
194
  def chained_flags_with(*args)
195
- where(chained_flags_condition(*args))
195
+ if (ActiveRecord::VERSION::MAJOR >= 3)
196
+ where(chained_flags_condition(*args))
197
+ else
198
+ all(:conditions => chained_flags_condition(*args))
199
+ end
196
200
  end
197
201
 
198
202
  def chained_flags_condition(colmn, *args)
@@ -213,7 +217,7 @@ module FlagShihTzu
213
217
  val = (1..(2 * max_flag_value_for_column(colmn))).to_a
214
218
  args.each do |flag|
215
219
  neg = false
216
- if flag.match /^not_/
220
+ if flag.to_s.match /^not_/
217
221
  neg = true
218
222
  flag = flag.to_s.sub(/^not_/, '').to_sym
219
223
  end
@@ -384,6 +388,25 @@ module FlagShihTzu
384
388
  not selected_flags(colmn).empty?
385
389
  end
386
390
 
391
+ # returns true if successful
392
+ # third parameter allows you to specify that `self` should also have its in-memory flag attribute updated.
393
+ def update_flag!(flag, value, update_instance = false)
394
+ truthy = FlagShihTzu::TRUE_VALUES.include?(value)
395
+ sql = self.class.set_flag_sql(flag.to_sym, truthy)
396
+ if update_instance
397
+ if truthy
398
+ self.enable_flag(flag)
399
+ else
400
+ self.disable_flag(flag)
401
+ end
402
+ end
403
+ if (ActiveRecord::VERSION::MAJOR <= 3)
404
+ self.class.update_all(sql, self.class.primary_key => id) == 1
405
+ else
406
+ self.class.where("#{self.class.primary_key} = ?", id).update_all(sql) == 1
407
+ end
408
+ end
409
+
387
410
  private
388
411
 
389
412
  def get_bit_for(flag, colmn)
@@ -1,54 +1,57 @@
1
- module ActiveModel
2
- module Validations
1
+ if ActiveRecord::VERSION::MAJOR >= 3
3
2
 
4
- class PresenceOfFlagsValidator < EachValidator
5
- def validate_each(record, attribute, value)
6
- value = record.send(:read_attribute_for_validation, attribute)
7
- check_flag(record, attribute)
8
- record.errors.add(attribute, :blank, options) if value.blank? or value == 0
9
- end
3
+ module ActiveModel
4
+ module Validations
5
+
6
+ class PresenceOfFlagsValidator < EachValidator
7
+ def validate_each(record, attribute, value)
8
+ value = record.send(:read_attribute_for_validation, attribute)
9
+ check_flag(record, attribute)
10
+ record.errors.add(attribute, :blank, options) if value.blank? or value == 0
11
+ end
10
12
 
11
- private
13
+ private
12
14
 
13
- def check_flag(record, attribute)
14
- unless record.class.flag_columns.include? attribute.to_s
15
- raise ArgumentError.new("#{attribute} is not one of the flags columns (#{record.class.flag_columns.join(', ')})")
15
+ def check_flag(record, attribute)
16
+ unless record.class.flag_columns.include? attribute.to_s
17
+ raise ArgumentError.new("#{attribute} is not one of the flags columns (#{record.class.flag_columns.join(', ')})")
18
+ end
16
19
  end
17
20
  end
18
- end
19
21
 
20
- module HelperMethods
21
- # Validates that the specified attributes are flags and are not blank.
22
- # Happens by default on save. Example:
23
- #
24
- # class Spaceship < ActiveRecord::Base
25
- # include FlagShihTzu
26
- #
27
- # has_flags({ 1 => :warpdrive, 2 => :hyperspace }, :column => 'engines')
28
- # validates_presence_of_flags :engines
29
- # end
30
- #
31
- # The engines attribute must be a flag in the object and it cannot be blank.
32
- #
33
- # Configuration options:
34
- # * <tt>:message</tt> - A custom error message (default is: "can't be blank").
35
- # * <tt>:on</tt> - Specifies when this validation is active. Runs in all
36
- # validation contexts by default (+nil+), other options are <tt>:create</tt>
37
- # and <tt>:update</tt>.
38
- # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if
39
- # the validation should occur (e.g. <tt>:if => :allow_validation</tt>, or
40
- # <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc
41
- # or string should return or evaluate to a true or false value.
42
- # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine
43
- # if the validation should not occur (e.g. <tt>:unless => :skip_validation</tt>,
44
- # or <tt>:unless => Proc.new { |spaceship| spaceship.warp_step <= 2 }</tt>). The method,
45
- # proc or string should return or evaluate to a true or false value.
46
- # * <tt>:strict</tt> - Specifies whether validation should be strict.
47
- # See <tt>ActiveModel::Validation#validates!</tt> for more information.
48
- def validates_presence_of_flags(*attr_names)
49
- validates_with PresenceOfFlagsValidator, _merge_attributes(attr_names)
22
+ module HelperMethods
23
+ # Validates that the specified attributes are flags and are not blank.
24
+ # Happens by default on save. Example:
25
+ #
26
+ # class Spaceship < ActiveRecord::Base
27
+ # include FlagShihTzu
28
+ #
29
+ # has_flags({ 1 => :warpdrive, 2 => :hyperspace }, :column => 'engines')
30
+ # validates_presence_of_flags :engines
31
+ # end
32
+ #
33
+ # The engines attribute must be a flag in the object and it cannot be blank.
34
+ #
35
+ # Configuration options:
36
+ # * <tt>:message</tt> - A custom error message (default is: "can't be blank").
37
+ # * <tt>:on</tt> - Specifies when this validation is active. Runs in all
38
+ # validation contexts by default (+nil+), other options are <tt>:create</tt>
39
+ # and <tt>:update</tt>.
40
+ # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if
41
+ # the validation should occur (e.g. <tt>:if => :allow_validation</tt>, or
42
+ # <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The method, proc
43
+ # or string should return or evaluate to a true or false value.
44
+ # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine
45
+ # if the validation should not occur (e.g. <tt>:unless => :skip_validation</tt>,
46
+ # or <tt>:unless => Proc.new { |spaceship| spaceship.warp_step <= 2 }</tt>). The method,
47
+ # proc or string should return or evaluate to a true or false value.
48
+ # * <tt>:strict</tt> - Specifies whether validation should be strict.
49
+ # See <tt>ActiveModel::Validation#validates!</tt> for more information.
50
+ def validates_presence_of_flags(*attr_names)
51
+ validates_with PresenceOfFlagsValidator, _merge_attributes(attr_names)
52
+ end
50
53
  end
51
- end
52
54
 
55
+ end
53
56
  end
54
57
  end
@@ -1,3 +1,3 @@
1
1
  module FlagShihTzu
2
- VERSION = "0.3.4"
2
+ VERSION = "0.3.5"
3
3
  end
@@ -54,16 +54,6 @@ class SpaceshipWith3CustomFlagsColumn < ActiveRecord::Base
54
54
  has_flags({ 1 => :power, 2 => :anti_ax_routine }, :column => 'hal3000')
55
55
  end
56
56
 
57
- class SpaceshipWithSymbolAndStringFlagColumns < ActiveRecord::Base
58
- self.table_name = 'spaceships_with_symbol_and_string_flag_columns'
59
- include FlagShihTzu
60
-
61
- has_flags({ 1 => :warpdrive, 2 => :hyperspace }, :column => :peace, :check_for_column => true)
62
- has_flags({ 1 => :photon, 2 => :laser, 3 => :ion_cannon, 4 => :particle_beam }, :column => :love, :check_for_column => true)
63
- has_flags({ 1 => :power, 2 => :anti_ax_routine }, :column => 'happiness', :check_for_column => true)
64
- validates_presence_of_flags :peace, :love
65
- end
66
-
67
57
  class SpaceshipWithBitOperatorQueryMode < ActiveRecord::Base
68
58
  self.table_name = 'spaceships'
69
59
  include FlagShihTzu
@@ -89,31 +79,43 @@ end
89
79
  class SpaceCarrier < Spaceship
90
80
  end
91
81
 
92
- class SpaceshipWithValidationsAndCustomFlagsColumn < ActiveRecord::Base
93
- self.table_name = 'spaceships_with_custom_flags_column'
94
- include FlagShihTzu
82
+ if (ActiveRecord::VERSION::MAJOR >= 3)
83
+ class SpaceshipWithSymbolAndStringFlagColumns < ActiveRecord::Base
84
+ self.table_name = 'spaceships_with_symbol_and_string_flag_columns'
85
+ include FlagShihTzu
95
86
 
96
- has_flags(1 => :warpdrive, 2 => :hyperspace, :column => 'bits')
97
- validates_presence_of_flags :bits
98
- end
87
+ has_flags({ 1 => :warpdrive, 2 => :hyperspace }, :column => :peace, :check_for_column => true)
88
+ has_flags({ 1 => :photon, 2 => :laser, 3 => :ion_cannon, 4 => :particle_beam }, :column => :love, :check_for_column => true)
89
+ has_flags({ 1 => :power, 2 => :anti_ax_routine }, :column => 'happiness', :check_for_column => true)
90
+ validates_presence_of_flags :peace, :love
91
+ end
99
92
 
100
- class SpaceshipWithValidationsAnd3CustomFlagsColumn < ActiveRecord::Base
101
- self.table_name = 'spaceships_with_3_custom_flags_column'
102
- include FlagShihTzu
93
+ class SpaceshipWithValidationsAndCustomFlagsColumn < ActiveRecord::Base
94
+ self.table_name = 'spaceships_with_custom_flags_column'
95
+ include FlagShihTzu
103
96
 
104
- has_flags({ 1 => :warpdrive, 2 => :hyperspace }, :column => 'engines')
105
- has_flags({ 1 => :photon, 2 => :laser, 3 => :ion_cannon, 4 => :particle_beam }, :column => 'weapons')
106
- has_flags({ 1 => :power, 2 => :anti_ax_routine }, :column => 'hal3000')
97
+ has_flags(1 => :warpdrive, 2 => :hyperspace, :column => 'bits')
98
+ validates_presence_of_flags :bits
99
+ end
107
100
 
108
- validates_presence_of_flags :engines, :weapons
109
- end
101
+ class SpaceshipWithValidationsAnd3CustomFlagsColumn < ActiveRecord::Base
102
+ self.table_name = 'spaceships_with_3_custom_flags_column'
103
+ include FlagShihTzu
110
104
 
111
- class SpaceshipWithValidationsOnNonFlagsColumn < ActiveRecord::Base
112
- self.table_name = 'spaceships_with_custom_flags_column'
113
- include FlagShihTzu
105
+ has_flags({ 1 => :warpdrive, 2 => :hyperspace }, :column => 'engines')
106
+ has_flags({ 1 => :photon, 2 => :laser, 3 => :ion_cannon, 4 => :particle_beam }, :column => 'weapons')
107
+ has_flags({ 1 => :power, 2 => :anti_ax_routine }, :column => 'hal3000')
114
108
 
115
- has_flags(1 => :warpdrive, 2 => :hyperspace, :column => 'bits')
116
- validates_presence_of_flags :id
109
+ validates_presence_of_flags :engines, :weapons
110
+ end
111
+
112
+ class SpaceshipWithValidationsOnNonFlagsColumn < ActiveRecord::Base
113
+ self.table_name = 'spaceships_with_custom_flags_column'
114
+ include FlagShihTzu
115
+
116
+ has_flags(1 => :warpdrive, 2 => :hyperspace, :column => 'bits')
117
+ validates_presence_of_flags :id
118
+ end
117
119
  end
118
120
 
119
121
  # table planets is missing intentionally to see if flagshihtzu handles missing tables gracefully
@@ -311,8 +313,17 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
311
313
  spaceship.enable_flag(:electrolytes)
312
314
  spaceship.save!
313
315
 
314
- Spaceship.update_all Spaceship.set_flag_sql(:warpdrive, true),
316
+ assert_equal false, spaceship.warpdrive
317
+ assert_equal true, spaceship.shields
318
+ assert_equal true, spaceship.electrolytes
319
+
320
+ if (ActiveRecord::VERSION::MAJOR <= 3)
321
+ Spaceship.update_all Spaceship.set_flag_sql(:warpdrive, true),
315
322
  ["id=?", spaceship.id]
323
+ else
324
+ Spaceship.where("id=?", spaceship.id).update_all Spaceship.set_flag_sql(:warpdrive, true)
325
+ end
326
+
316
327
  spaceship.reload
317
328
 
318
329
  assert_equal true, spaceship.warpdrive
@@ -325,8 +336,17 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
325
336
  spaceship.enable_flag(:electrolytes)
326
337
  spaceship.save!
327
338
 
328
- Spaceship.update_all Spaceship.set_flag_sql(:shields, false),
329
- ["id=?", spaceship.id]
339
+ assert_equal true, spaceship.warpdrive
340
+ assert_equal true, spaceship.shields
341
+ assert_equal true, spaceship.electrolytes
342
+
343
+ if (ActiveRecord::VERSION::MAJOR <= 3)
344
+ Spaceship.update_all Spaceship.set_flag_sql(:shields, false),
345
+ ["id=?", spaceship.id]
346
+ else
347
+ Spaceship.where("id=?", spaceship.id).update_all Spaceship.set_flag_sql(:shields, false)
348
+ end
349
+
330
350
  spaceship.reload
331
351
 
332
352
  assert_equal true, spaceship.warpdrive
@@ -904,55 +924,134 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
904
924
  assert_equal 'bits', @big_spaceship.class.determine_flag_colmn_for(:warpdrive)
905
925
  end
906
926
 
907
- # --------------------------------------------------
927
+ def test_update_flag_without_updating_instance!
928
+ my_spaceship = SpaceshipWith2CustomFlagsColumn.new
929
+ my_spaceship.enable_flag(:jeanlucpicard)
930
+ my_spaceship.disable_flag(:warpdrive)
931
+ my_spaceship.save
908
932
 
909
- def test_validation_should_raise_if_not_a_flag_column
910
- spaceship = SpaceshipWithValidationsOnNonFlagsColumn.new
911
- assert_raises ArgumentError do
912
- spaceship.valid?
913
- end
914
- end
933
+ assert_equal true, my_spaceship.jeanlucpicard
934
+ assert_equal false, my_spaceship.warpdrive
915
935
 
916
- def test_validation_should_succeed_with_a_blank_optional_flag
917
- spaceship = Spaceship.new
918
- assert_equal true, spaceship.valid?
919
- end
936
+ assert_equal true, my_spaceship.update_flag!(:jeanlucpicard, false)
937
+ assert_equal true, my_spaceship.update_flag!(:warpdrive, true)
920
938
 
921
- def test_validation_should_fail_with_a_nil_required_flag
922
- spaceship = SpaceshipWithValidationsAndCustomFlagsColumn.new
923
- spaceship.bits = nil
924
- assert_equal false, spaceship.valid?
925
- assert_equal ["can't be blank"], spaceship.errors.messages[:bits]
926
- end
939
+ # Not updating the instance here, so it won't reflect the result of the SQL update until after reloaded
940
+ assert_equal true, my_spaceship.jeanlucpicard
941
+ assert_equal false, my_spaceship.warpdrive
927
942
 
928
- def test_validation_should_fail_with_a_blank_required_flag
929
- spaceship = SpaceshipWithValidationsAndCustomFlagsColumn.new
930
- assert_equal false, spaceship.valid?
931
- assert_equal ["can't be blank"], spaceship.errors.messages[:bits]
932
- end
943
+ my_spaceship.reload
933
944
 
934
- def test_validation_should_succeed_with_a_set_required_flag
935
- spaceship = SpaceshipWithValidationsAndCustomFlagsColumn.new
936
- spaceship.warpdrive = true
937
- assert_equal true, spaceship.valid?
945
+ assert_equal false, my_spaceship.jeanlucpicard
946
+ assert_equal true, my_spaceship.warpdrive
938
947
  end
939
948
 
940
- def test_validation_should_fail_with_a_blank_required_flag_among_2
941
- spaceship = SpaceshipWithValidationsAnd3CustomFlagsColumn.new
942
- assert_equal false, spaceship.valid?
943
- assert_equal ["can't be blank"], spaceship.errors.messages[:engines]
944
- assert_equal ["can't be blank"], spaceship.errors.messages[:weapons]
949
+ def test_update_flag_with_updating_instance!
950
+ my_spaceship = SpaceshipWith2CustomFlagsColumn.new
951
+ my_spaceship.enable_flag(:jeanlucpicard)
952
+ my_spaceship.disable_flag(:warpdrive)
953
+ my_spaceship.save
954
+
955
+ assert_equal true, my_spaceship.jeanlucpicard
956
+ assert_equal false, my_spaceship.warpdrive
957
+
958
+ assert_equal true, my_spaceship.update_flag!(:jeanlucpicard, false, true)
959
+ assert_equal true, my_spaceship.update_flag!(:warpdrive, true, true)
960
+
961
+ # Updating the instance here, so it will reflect the result of the SQL update before and after reload
962
+ assert_equal false, my_spaceship.jeanlucpicard
963
+ assert_equal true, my_spaceship.warpdrive
945
964
 
946
- spaceship.warpdrive = true
947
- assert_equal false, spaceship.valid?
948
- assert_equal ["can't be blank"], spaceship.errors.messages[:weapons]
965
+ my_spaceship.reload
966
+
967
+ assert_equal false, my_spaceship.jeanlucpicard
968
+ assert_equal true, my_spaceship.warpdrive
949
969
  end
950
970
 
951
- def test_validation_should_succeed_with_a_set_required_flag_among_2
952
- spaceship = SpaceshipWithValidationsAnd3CustomFlagsColumn.new
953
- spaceship.warpdrive = true
954
- spaceship.photon = true
955
- assert_equal true, spaceship.valid?
971
+ # --------------------------------------------------
972
+
973
+ if (ActiveRecord::VERSION::MAJOR >= 3)
974
+
975
+ def test_validation_should_raise_if_not_a_flag_column
976
+ spaceship = SpaceshipWithValidationsOnNonFlagsColumn.new
977
+ assert_raises ArgumentError do
978
+ spaceship.valid?
979
+ end
980
+ end
981
+
982
+ def test_validation_should_succeed_with_a_blank_optional_flag
983
+ spaceship = Spaceship.new
984
+ assert_equal true, spaceship.valid?
985
+ end
986
+
987
+ def test_validation_should_fail_with_a_nil_required_flag
988
+ spaceship = SpaceshipWithValidationsAndCustomFlagsColumn.new
989
+ spaceship.bits = nil
990
+ assert_equal false, spaceship.valid?
991
+ error_message =
992
+ if spaceship.errors.respond_to?(:messages)
993
+ spaceship.errors.messages[:bits]
994
+ else
995
+ spaceship.errors.get(:bits)
996
+ end
997
+ assert_equal ["can't be blank"], error_message
998
+ end
999
+
1000
+ def test_validation_should_fail_with_a_blank_required_flag
1001
+ spaceship = SpaceshipWithValidationsAndCustomFlagsColumn.new
1002
+ assert_equal false, spaceship.valid?
1003
+ error_message =
1004
+ if spaceship.errors.respond_to?(:messages)
1005
+ spaceship.errors.messages[:bits]
1006
+ else
1007
+ spaceship.errors.get(:bits)
1008
+ end
1009
+ assert_equal ["can't be blank"], error_message
1010
+ end
1011
+
1012
+ def test_validation_should_succeed_with_a_set_required_flag
1013
+ spaceship = SpaceshipWithValidationsAndCustomFlagsColumn.new
1014
+ spaceship.warpdrive = true
1015
+ assert_equal true, spaceship.valid?
1016
+ end
1017
+
1018
+ def test_validation_should_fail_with_a_blank_required_flag_among_2
1019
+ spaceship = SpaceshipWithValidationsAnd3CustomFlagsColumn.new
1020
+ assert_equal false, spaceship.valid?
1021
+ engines_error_message =
1022
+ if spaceship.errors.respond_to?(:messages)
1023
+ spaceship.errors.messages[:engines]
1024
+ else
1025
+ spaceship.errors.get(:engines)
1026
+ end
1027
+ assert_equal ["can't be blank"], engines_error_message
1028
+
1029
+ weapons_error_message =
1030
+ if spaceship.errors.respond_to?(:messages)
1031
+ spaceship.errors.messages[:weapons]
1032
+ else
1033
+ spaceship.errors.get(:weapons)
1034
+ end
1035
+ assert_equal ["can't be blank"], weapons_error_message
1036
+
1037
+ spaceship.warpdrive = true
1038
+ assert_equal false, spaceship.valid?
1039
+
1040
+ weapons_error_message =
1041
+ if spaceship.errors.respond_to?(:messages)
1042
+ spaceship.errors.messages[:weapons]
1043
+ else
1044
+ spaceship.errors.get(:weapons)
1045
+ end
1046
+ assert_equal ["can't be blank"], weapons_error_message
1047
+ end
1048
+
1049
+ def test_validation_should_succeed_with_a_set_required_flag_among_2
1050
+ spaceship = SpaceshipWithValidationsAnd3CustomFlagsColumn.new
1051
+ spaceship.warpdrive = true
1052
+ spaceship.photon = true
1053
+ assert_equal true, spaceship.valid?
1054
+ end
956
1055
  end
957
1056
 
958
1057
  end
@@ -1046,8 +1145,10 @@ class FlagShihTzuClassMethodsTest < Test::Unit::TestCase
1046
1145
  assert_equal Spaceship.flag_columns, ['flags']
1047
1146
  assert_equal SpaceshipWith2CustomFlagsColumn.flag_columns, ['bits', 'commanders']
1048
1147
  assert_equal SpaceshipWith3CustomFlagsColumn.flag_columns, ['engines', 'weapons', 'hal3000']
1049
- assert_equal SpaceshipWithValidationsAnd3CustomFlagsColumn.flag_columns, ["engines", "weapons", "hal3000"]
1050
- assert_equal SpaceshipWithSymbolAndStringFlagColumns.flag_columns, ['peace', 'love', 'happiness']
1148
+ if (ActiveRecord::VERSION::MAJOR >= 3)
1149
+ assert_equal SpaceshipWithValidationsAnd3CustomFlagsColumn.flag_columns, ["engines", "weapons", "hal3000"]
1150
+ assert_equal SpaceshipWithSymbolAndStringFlagColumns.flag_columns, ['peace', 'love', 'happiness']
1151
+ end
1051
1152
  end
1052
1153
 
1053
1154
  end
metadata CHANGED
@@ -1,9 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flag_shih_tzu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
+ - Peter Boling
7
8
  - Patryk Peszko
8
9
  - Sebastian Roebke
9
10
  - David Anderson
@@ -11,7 +12,7 @@ authors:
11
12
  autorequire:
12
13
  bindir: bin
13
14
  cert_chain: []
14
- date: 2013-06-21 00:00:00.000000000 Z
15
+ date: 2013-08-06 00:00:00.000000000 Z
15
16
  dependencies:
16
17
  - !ruby/object:Gem::Dependency
17
18
  name: activerecord
@@ -106,6 +107,7 @@ files:
106
107
  - gemfiles/Gemfile.activerecord-3.0.x
107
108
  - gemfiles/Gemfile.activerecord-3.1.x
108
109
  - gemfiles/Gemfile.activerecord-3.2.x
110
+ - gemfiles/Gemfile.activerecord-4.0.x
109
111
  - init.rb
110
112
  - lib/flag_shih_tzu.rb
111
113
  - lib/flag_shih_tzu/validators.rb
@@ -114,7 +116,7 @@ files:
114
116
  - test/flag_shih_tzu_test.rb
115
117
  - test/schema.rb
116
118
  - test/test_helper.rb
117
- homepage: https://github.com/xing/flag_shih_tzu
119
+ homepage: https://github.com/pboling/flag_shih_tzu
118
120
  licenses:
119
121
  - MIT
120
122
  metadata: {}
@@ -143,4 +145,3 @@ test_files:
143
145
  - test/flag_shih_tzu_test.rb
144
146
  - test/schema.rb
145
147
  - test/test_helper.rb
146
- has_rdoc: