Permalink
Browse files

Merged origin/master

  • Loading branch information...
2 parents f8a255c + 28d6454 commit 3fc48b70569022fbbfcbf405cece0390eb362f6d @pboling committed Nov 6, 2012
Showing with 48 additions and 13 deletions.
  1. +17 −4 README.rdoc
  2. +1 −8 Rakefile
  3. +1 −1 gemfiles/Gemfile.activerecord-3.2.x
  4. +13 −0 lib/flag_shih_tzu.rb
  5. +15 −0 test/flag_shih_tzu_test.rb
  6. +1 −0 test/test_helper.rb
View
@@ -83,6 +83,12 @@ Then:
==Usage
+FlagShihTzu assumes that your ActiveRecord model already has an integer field
+to store the flags, which should be defined to not allow NULL values and
+should have a default value of 0 (which means all flags are initially set to
+false).
+
+
===Defining the flags
class Spaceship < ActiveRecord::Base
@@ -91,11 +97,10 @@ Then:
has_flags 1 => :warpdrive,
2 => :shields,
3 => :electrolytes
-
end
-+has_flags+ takes a hash. The keys must be positive integers and represent
-the position of the bit being used to enable or disable the flag.
++has_flags+ takes a hash. The keys must be positive integers and represent
+the position of the bit being used to enable or disable the flag.
<b>The keys must not be changed once in use, or you will get wrong results.</b>
That is why the plugin forces you to set them explicitly.
The values are symbols for the flags being created.
@@ -108,7 +113,7 @@ the defined flags as a bit field.
The bit position of a flag corresponds to the given key.
-This way, we can use bit operators on the stored integer value to set, unset
+This way, we can use bit operators on the stored integer value to set, unset
and check individual flags.
+---+---+---+ +---+---+---+
@@ -183,6 +188,14 @@ on Spaceship:
Spaceship.flag_columns # [:features, :crew]
+Opionally, you can set the <tt>:bang_methods</tt> option to true to enable the bang methods:
+
+ Spaceship#electrolytes!
+ Spaceship#not_electrolytes!
+
+which respectively enables or disables the electrolytes flag.
+
+
===Generated named scopes
The following named scopes become available:
View
@@ -28,15 +28,8 @@ namespace :test do
desc 'Test against all supported ActiveRecord versions'
task :all do
%w(2.3.x 3.0.x 3.1.x 3.2.x).each do |version|
- sh "BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-#{version}' bundle"
+ sh "BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-#{version}' bundle --quiet"
sh "BUNDLE_GEMFILE='gemfiles/Gemfile.activerecord-#{version}' bundle exec rake test"
end
end
-
- desc 'Measures test coverage'
- task :coverage do
- rm_f "coverage"
- system("rcov -Ilib test/*_test.rb")
- system("open coverage/index.html") if PLATFORM['darwin']
- end
end
@@ -2,4 +2,4 @@ source "http://rubygems.org"
gemspec :path => '..'
-gem "activerecord", "~>3.2.0rc1"
+gem "activerecord", "~>3.2.0"
View
@@ -94,6 +94,19 @@ def self.unset_#{flag_name}_sql
end
EVAL
+ # Define bancg methods when requested
+ if flag_options[colmn][:bang_methods]
+ class_eval <<-EVAL
+ def #{flag_name}!
+ enable_flag(:#{flag_name}, '#{colmn}')
+ end
+
+ def not_#{flag_name}!
+ disable_flag(:#{flag_name}, '#{colmn}')
+ end
+ EVAL
+ end
+
# Define the named scopes if the user wants them and AR supports it
if flag_options[colmn][:named_scopes] && respond_to?(named_scope_method)
class_eval <<-EVAL
View
@@ -61,6 +61,13 @@ class SpaceshipWithBitOperatorQueryMode < ActiveRecord::Base
has_flags(1 => :warpdrive, 2 => :shields, :flag_query_mode => :bit_operator)
end
+class SpaceshipWithBangMethods < ActiveRecord::Base
+ self.table_name = 'spaceships'
+ include FlagShihTzu
+
+ has_flags(1 => :warpdrive, 2 => :shields, :bang_methods => true)
+end
+
class SpaceCarrier < Spaceship
end
@@ -563,6 +570,14 @@ def test_should_respect_true_values_like_active_record
end
end
+ def test_should_define_bang_methods
+ spaceship = SpaceshipWithBangMethods.new
+ spaceship.warpdrive!
+ assert spaceship.warpdrive
+ spaceship.not_warpdrive!
+ assert !spaceship.warpdrive
+ end
+
def test_should_return_a_sql_set_method_for_flag
assert_equal "spaceships.flags = spaceships.flags | 1", Spaceship.send( :sql_set_for_flag, :warpdrive, 'flags', true)
assert_equal "spaceships.flags = spaceships.flags & ~1", Spaceship.send( :sql_set_for_flag, :warpdrive, 'flags', false)
View
@@ -4,6 +4,7 @@
require "flag_shih_tzu"
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
+ActiveRecord::Migration.verbose = false
configs = YAML.load_file(File.dirname(__FILE__) + "/database.yml")
ActiveRecord::Base.configurations = configs

0 comments on commit 3fc48b7

Please sign in to comment.