Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Visit country on collection update

  • Loading branch information...
commit dbcea704119cf3efc26f6c904b49ce0d16e59b47 1 parent b026790
@ignar authored
View
19 app/models/user.rb
@@ -35,8 +35,12 @@ def visited?(country)
def update_collection(currency)
if collect?(currency)
currencies.delete(currency)
+ update_country_visiting(currency.country)
else
currencies << currency
+ single_collecting do
+ self.countries << currency.country unless visited?(currency.country)
+ end
end
end
@@ -55,11 +59,24 @@ def encrypt_password
end
def add_all_currencies_of_country(country)
- self.currencies << country.currencies
+ self.currencies << country.currencies unless @single_collecting
end
def remove_all_currencies_of_country(country)
self.currencies.delete(*country.currencies)
end
+ def single_collecting &block
+ @single_collecting = true
+ block.call
+ @single_collecting = false
+ end
+
+ def update_country_visiting(country)
+ all_currencies = country.currencies
+ if (all_currencies & self.currencies).empty? && visited?(country)
+ toggle_visiting(country)
+ end
+ end
+
end
View
3  ctracker.org
@@ -87,3 +87,6 @@ Clock summary at [2012-10-23 Tue 11:54]
* DONE Currencies index and show page
CLOSED: [2012-10-23 Tue 12:08]
CLOCK: [2012-10-23 Tue 11:55]--[2012-10-23 Tue 12:08] => 0:13
+* DONE Visit country on collection update
+ CLOSED: [2012-10-23 Tue 13:02]
+ CLOCK: [2012-10-23 Tue 12:10]--[2012-10-23 Tue 13:02] => 0:52
View
2  spec/fixtures/currencies.yml
@@ -3,10 +3,12 @@
one:
name: NameOne
code: CodeOne
+ country_id: CodeOne
two:
name: NameTwo
code: CodeTwo
+ country_id: CodeOne
three:
name: NameThree
View
18 spec/models/user_spec.rb
@@ -106,12 +106,28 @@
}.to change{subject.currencies.count}.by(1)
end
- it "should remove from cullection if user has one in it" do
+ it "should remove from collection if user has one in it" do
subject.update_collection(first_currency)
expect{
subject.update_collection(first_currency)
}.to change{subject.currencies.count}.by(-1)
end
+
+ context "making assumption that we collect only when visiting" do
+ it "country should be marked as visited on collection updating" do
+ expect{
+ subject.update_collection(first_currency)
+ }.to change{subject.countries.count}.by(1)
+ end
+ end
+
+ it "should mark country as not visited if no collected currency ramains" do
+ subject.toggle_visiting(first_country)
+ subject.update_collection(first_currency)
+ expect{
+ subject.update_collection(second_currency)
+ }.to change{subject.countries.count}.by(-1)
+ end
end
describe "#collected?" do
Please sign in to comment.
Something went wrong with that request. Please try again.