Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/active_flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,30 @@ def flag(column, keys)

# Scopes
define_singleton_method "where_#{column}" do |*args|
integer, column_name = send("_where_#{column}", *args)
where("#{column_name} & #{integer} > 0")
integer, attribute = send("_where_#{column}", *args)
where((attribute & integer).gt(0))
end

define_singleton_method "where_all_#{column}" do |*args|
integer, column_name = send("_where_#{column}", *args)
where("#{column_name} & #{integer} = #{integer}")
integer, attribute = send("_where_#{column}", *args)
where((attribute & integer).eq(integer))
end

define_singleton_method "where_not_#{column}" do |*args|
integer, column_name = send("_where_#{column}", *args)
where("#{column_name} & #{integer} = 0")
integer, attribute = send("_where_#{column}", *args)
where((attribute & integer).eq(0))
end

define_singleton_method "where_not_all_#{column}" do |*args|
integer, column_name = send("_where_#{column}", *args)
where("#{column_name} & #{integer} < #{integer}")
integer, attribute = send("_where_#{column}", *args)
where((attribute & integer).lt(integer))
end

# utility method to extract parameters
define_singleton_method "_where_#{column}" do |*args|
return [
active_flags[column].to_i(args),
"#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column)}"
arel_table[column]
]
end
end
Expand Down
16 changes: 16 additions & 0 deletions test/active_flag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,20 @@ def test_joining_tables
assert_equal User.joins(:profile).where(profile: Profile.where_languages(:japanese)).count, 1
assert_equal User.joins(:profile).where(profile: Profile.where_languages(:chinese)).count, 0
end

def test_scope_without_permanent_connection_checkout
skip unless ActiveRecord.respond_to?(:permanent_connection_checkout)

previous = ActiveRecord.permanent_connection_checkout
ActiveRecord::Base.release_connection
ActiveRecord.permanent_connection_checkout = :disallowed

assert_equal 2, Profile.where_languages(:english).count
assert_equal 1, Profile.where_all_languages(:english, :japanese).count
assert_equal 1, Profile.where_not_languages(:english).count
assert_equal 2, Profile.where_not_all_languages(:english, :japanese).count
refute ActiveRecord::Base.connection_pool.active_connection?
ensure
ActiveRecord.permanent_connection_checkout = previous if previous
end
end
Loading