Skip to content

Commit

Permalink
Make is_public scopes use new OR logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Cantido committed Sep 22, 2016
1 parent 80822de commit aa46434
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/models/character.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Character < ActiveRecord::Base
# Items
relates :favorite_items, with: :ownerships, where: { favorite: true }

scope :is_public, -> { where(privacy: 'public').joins(:universe).where(universes: { privacy: "public" }) }
scope :is_public, -> { joins(:universe).where('universes.privacy = "public" OR characters.privacy = "public"') }

def description
role
Expand Down
2 changes: 1 addition & 1 deletion app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Item < ActiveRecord::Base
relates :current_owners, with: :current_ownerships
relates :makers, with: :maker_relationships

scope :is_public, -> { joins(:universe).where(universes: { privacy: "public" }) }
scope :is_public, -> { joins(:universe).where('universes.privacy = "public" OR items.privacy = "public"') }

def self.color
'amber'
Expand Down
2 changes: 1 addition & 1 deletion app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Location < ActiveRecord::Base
relates :largest_cities, with: :largest_cities_relationships
relates :notable_cities, with: :notable_cities_relationships

scope :is_public, -> { joins(:universe).where(universes: { privacy: "public" }) }
scope :is_public, -> { joins(:universe).where('universes.privacy = "public" OR locations.privacy = "public"') }

def self.icon
'terrain'
Expand Down
18 changes: 18 additions & 0 deletions test/models/character_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,22 @@ class CharacterTest < ActiveSupport::TestCase

refute @character.valid?, 'Character name not being validated for presence'
end

test 'character is_public scope' do
public_universe = create(:universe, privacy: 'public')
private_universe = create(:universe, privacy: 'private')

pub_character_pub_universe = create(:character, privacy: 'public', universe: public_universe)
pub_character_priv_universe = create(:character, privacy: 'public', universe: private_universe)
priv_character_pub_universe = create(:character, privacy: 'private', universe: public_universe)
priv_character_priv_universe = create(:character, privacy: 'private', universe: private_universe)

public_scope = Character.is_public

assert_includes public_scope, pub_character_pub_universe, "didn't contain a public character in a public universe"
assert_includes public_scope, pub_character_priv_universe, "didn't contain a public character in a private universe"
assert_includes public_scope, priv_character_pub_universe, "didn't contain a private character in a public universe"

refute_includes public_scope, priv_character_priv_universe, "contained a private character in a private universe"
end
end
18 changes: 18 additions & 0 deletions test/models/item_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,22 @@ class ItemTest < ActiveSupport::TestCase

refute @item.valid?, 'Item name is not being validated for presence'
end

test 'item is_public scope' do
public_universe = create(:universe, privacy: 'public')
private_universe = create(:universe, privacy: 'private')

pub_item_pub_universe = create(:item, privacy: 'public', universe: public_universe)
pub_item_priv_universe = create(:item, privacy: 'public', universe: private_universe)
priv_item_pub_universe = create(:item, privacy: 'private', universe: public_universe)
priv_item_priv_universe = create(:item, privacy: 'private', universe: private_universe)

public_scope = Item.is_public

assert_includes public_scope, pub_item_pub_universe, "didn't contain a public item in a public universe"
assert_includes public_scope, pub_item_priv_universe, "didn't contain a public item in a private universe"
assert_includes public_scope, priv_item_pub_universe, "didn't contain a private item in a public universe"

refute_includes public_scope, priv_item_priv_universe, "contained a private item in a private universe"
end
end
18 changes: 18 additions & 0 deletions test/models/location_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,22 @@ class LocationTest < ActiveSupport::TestCase

refute @location.valid?, 'Location name is not being validated for presence'
end

test 'location is_public scope' do
public_universe = create(:universe, privacy: 'public')
private_universe = create(:universe, privacy: 'private')

pub_location_pub_universe = create(:location, privacy: 'public', universe: public_universe)
pub_location_priv_universe = create(:location, privacy: 'public', universe: private_universe)
priv_location_pub_universe = create(:location, privacy: 'private', universe: public_universe)
priv_location_priv_universe = create(:location, privacy: 'private', universe: private_universe)

public_scope = Location.is_public

assert_includes public_scope, pub_location_pub_universe, "didn't contain a public location in a public universe"
assert_includes public_scope, pub_location_priv_universe, "didn't contain a public location in a private universe"
assert_includes public_scope, priv_location_pub_universe, "didn't contain a private location in a public universe"

refute_includes public_scope, priv_location_priv_universe, "contained a private location in a private universe"
end
end

0 comments on commit aa46434

Please sign in to comment.