Skip to content

Commit

Permalink
Merge pull request #27 from Mailbutler/master
Browse files Browse the repository at this point in the history
Include `table_name` in scopes to allow for JOIN operations
  • Loading branch information
kenn authored Sep 4, 2024
2 parents 19f8cf3 + 1a3e09f commit 3e9744a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/pkg/
/spec/reports/
/tmp/
.idea
3 changes: 2 additions & 1 deletion lib/active_flag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ def flag(column, keys)
where("#{column_name} & #{integer} < #{integer}")
end

# utility method to extract parameters
define_singleton_method "_where_#{column}" do |*args|
return [
args.extract_options!,
active_flags[column].to_i(args),
connection.quote_table_name_for_assignment(table_name, column)
"#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column)}"
]
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_flag/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ActiveFlag
VERSION = '1.6.0'
VERSION = '1.7.0'
end
10 changes: 10 additions & 0 deletions test/active_flag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,14 @@ def test_scope
assert_equal Profile.where_not_languages(:chinese).count, 3
assert_equal Profile.where_not_all_languages(:chinese).count, 3
end

def test_joining_tables
assert_equal Profile.joins(:users).where_languages(:english).count, 2
assert_equal Profile.joins(:users).where_languages(:japanese).count, 1
assert_equal Profile.joins(:users).where_languages(:chinese).count, 0

assert_equal User.joins(:profile).where(profile: Profile.where_languages(:english)).count, 2
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
end
20 changes: 18 additions & 2 deletions test/load_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,35 @@
t.integer :figures
end

ActiveRecord::Base.connection.create_table :users, force: true do |t|
t.string :name, null: false
t.string :languages # to require table-specificity in SQL queries
t.integer :profile_id, null: false
end

class Profile < ActiveRecord::Base
has_many :users

flag :languages, [:english, :spanish, :chinese, :french, :japanese]
flag :others, [:thing]
flag :figures, [:square, :circle]
end

class User < ActiveRecord::Base
belongs_to :profile
end

class SubProfile < Profile
end

class Other < ActiveRecord::Base
flag :others, [:another]
end

Profile.create(languages: [:english])
Profile.create(languages: [:japanese])
english_profile = Profile.create(languages: [:english])
japanese_profile = Profile.create(languages: [:japanese])
Profile.create(languages: [:english, :japanese])

User.create(name: 'Prince Harry', profile: english_profile)
User.create(name: 'Queen Elizabeth', profile: english_profile)
User.create(name: 'Shigeru Myamoto', profile: japanese_profile)

0 comments on commit 3e9744a

Please sign in to comment.