Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Give user opportunity to mark currency as selected

  • Loading branch information...
commit 6410f012ae3f8b80bbe50af975b0e786589713de 1 parent 9fc3d06
@ignar authored
View
1  app/controllers/countries_controller.rb
@@ -31,6 +31,7 @@ def visit
current_user.toggle_visiting(@country)
format.html { redirect_to(countries_path, :flash => { :notice => message }) }
+ # TODO it display wrong number of not visited countries
format.json { render :json => {:message => message, :status => status, :"Visited" => current_user.countries.count, :"Not Visited" => Country.count} }
end
end
View
24 app/controllers/currencies_controller.rb
@@ -1,23 +1,37 @@
class CurrenciesController < ApplicationController
# GET /currencies
- # GET /currencies.xml
def index
@currencies = Currency.all
respond_to do |format|
format.html # index.html.erb
- format.xml { render :xml => @currencies }
end
end
# GET /currencies/1
- # GET /currencies/1.xml
def show
@currency = Currency.find(params[:id])
respond_to do |format|
format.html # show.html.erb
- format.xml { render :xml => @currency }
end
end
-end
+
+ def collect
+ @currency = Currency.find(params[:id])
+
+ respond_to do |format|
+ current_user.update_collection(@currency)
+ if current_user.collect?(@currency)
+ message = "Currency was successfully as collected"
+ status = "Collected"
+ else
+ message = "Currency was successfully as not collected"
+ status = "Not Collected"
+ end
+ format.html{ redirect_to currencies_path, :flash => {:notice => message} }
+ # TODO it display wrong number of not collected currencies
+ format.json{ render :json => {:message => message, :status => status, :"Collected" => current_user.currencies.count, :"Not Collected" => Currency.count} }
+ end
+ end
+end
View
11 app/views/currencies/index.html.erb
@@ -13,8 +13,9 @@
<h1>Currencies</h1>
- <table>
+ <table id="currencies">
<tr>
+ <th></th>
<th>Name</th>
<th>Code</th>
<th>Status</th>
@@ -22,10 +23,14 @@
</tr>
<% @currencies.each do |currency| %>
- <tr>
+ <tr id="currency_record_<%= currency.code %>">
+ <td><%= check_box_tag("currency[]", currency.code, current_user.collect?(currency),
+ :id => "currency_#{currency.code}",
+ :"data-code" => currency.code,
+ :"data-url" => collect_currency_path(currency)) %></td>
<td><%= currency.name %></td>
<td><%= currency.code %></td>
- <td><%= current_user.collect?(currency) ? 'Collected' : 'Not Collected' %></td>
+ <td class="status"><%= current_user.collect?(currency) ? 'Collected' : 'Not Collected' %></td>
<td><%= link_to 'Show', currency %></td>
</tr>
<% end %>
View
6 config/routes.rb
@@ -6,7 +6,11 @@
get :visit
end
end
- resources :currencies, :only => [:index, :show]
+ resources :currencies, :only => [:index, :show] do
+ member do
+ get :collect
+ end
+ end
resources :sessions, :only => [:new, :create] do
collection do
delete :destroy
View
3  ctracker.org
@@ -92,3 +92,6 @@ Clock summary at [2012-10-23 Tue 13:03]
* 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
+* DONE Give user opportunity to mark currency as selected
+ CLOSED: [2012-10-23 Tue 14:07]
+ CLOCK: [2012-10-23 Tue 13:10]--[2012-10-23 Tue 14:07] => 0:57
View
10 features/manage_countries.feature
@@ -4,19 +4,19 @@ Feature: Manage countries
wants to manage the countries he has visited.
Scenario: List Counties
- When list of countries exists
+ Given list of countries exists
Then I should see table with countries
@javascript
Scenario: Visit Country
- When list of countries exists
- And I check country as visited
+ Given list of countries exists
+ When I check country as visited
Then status of country should change to "Visited"
@javascript
Scenario: Undo visit Country
- When list of countries exists
- And I check country as not visited
+ Given list of countries exists
+ When I check country as not visited
Then status of country should change to "Not Visited"
Scenario: Navigating to country details page
View
14 features/manage_currencies.feature
@@ -4,9 +4,21 @@ Feature: Manage currencies
watns to manage currencies he has collected.
Scenario: List Currencies
- When list of currencies exists
+ Given list of currencies exists
Then I should see table with them
Scenario: Navigating to currency detail page
When I want to look on currency closed
Then I open currency detail page
+
+ @javascript
+ Scenario: Collect Currency
+ Given list of currencies exists
+ When I mark one the currencies as collected
+ Then its status changed to "Collected"
+
+ @javascript
+ Scenario: Undo collect Currency
+ Given list of currencies exists
+ When I mark one the currencies as not collected
+ Then its status changed to "Not Collected"
View
25 features/step_definitions/manage_currencies_steps.rb
@@ -19,3 +19,28 @@
page.should have_content(@currency1.code)
page.should have_content(@currency1.name)
end
+
+When /^I mark one the currencies as collected$/ do
+ visit currencies_path
+ @code = @currency1.code
+ within("table#currencies") do
+ find(:xpath, "//input[@type='checkbox' and @id='currency_#{@code}']").set(true)
+ end
+end
+
+Then /^its status changed to "Collected"$/ do
+ find(:xpath, "//tr[@id='currency_record_#{@code}']//td[@class='status']").text.should eq("Collected")
+end
+
+When /^I mark one the currencies as not collected$/ do
+ @user.currencies << @currency1
+ visit currencies_path
+ @code = @currency1.code
+ within("table#currencies") do
+ find(:xpath, "//input[@type='checkbox' and @id='currency_#{@code}']").set(false)
+ end
+end
+
+Then /^its status changed to "Not Collected"$/ do
+ find(:xpath, "//tr[@id='currency_record_#{@code}']//td[@class='status']").text.should eq("Not Collected")
+end
View
31 public/javascripts/currencies.js
@@ -1,3 +1,32 @@
;$(function(){
-
+ var table = $("table#currencies");
+ table.find("input[type='checkbox']").on("change", function(e){
+ var checkbox = $(e.currentTarget);
+ $.ajax({
+ url: $(checkbox).data("url")
+ , dataType: "json"
+ , beforeSend: function(jq, settings){
+ checkbox.attr("disabled", true);
+ }
+ , success: function(data, status, jq){
+ tr = table.find("tr#currency_record_"+checkbox.data("code"));
+ tr.find("td.status").html(data.status);
+ }
+ , error: function(jq, status, error){
+ alert(error);
+ }
+ , complete: function(jq, status){
+ checkbox.attr("disabled", false);
+ var data = jQuery.parseJSON(jq.responseText);
+ $('.simple_pie_chart').each(function() {
+ chart = SimplePieChart.initialize(this);
+ chart.sets = {
+ visited: data["Collected"],
+ not_visited: data["Not Collected"]
+ };
+ chart.render();
+ });
+ }
+ });
+ });
});
View
60 spec/controllers/currencies_controller_spec.rb
@@ -0,0 +1,60 @@
+require "spec_helper"
+
+describe CurrenciesController do
+ include_context "logged in user"
+ let(:country){ countries(:one) }
+ let(:first_currency){ country.currencies.first }
+
+ describe "GET index" do
+ it "should be success" do
+ get :index
+ response.should be_success
+ end
+ end
+
+ describe "GET show" do
+ it "should be success" do
+ get :show, :id => country.code
+ response.should be_success
+ end
+ end
+
+ describe "GET collect" do
+ context "html" do
+ it "should set currency as collected if it wasn't" do
+ get :collect, :id => first_currency, :format => :html
+ response.should redirect_to(currencies_path)
+ flash[:notice].should include("Currency was successfully as collected")
+ end
+
+ it "should makr currency as not collected if it was" do
+ user.update_collection(first_currency)
+ get :collect, :id => first_currency, :format => :html
+ response.should redirect_to(currencies_path)
+ flash[:notice].should include("Currency was successfully as not collected")
+ end
+ end
+
+ context "json" do
+ it "should set currency as collected if it was not" do
+ get :collect, :id => first_currency, :format => :json
+ json = ActiveSupport::JSON.decode(response.body)
+ json["message"].should eq("Currency was successfully as collected")
+ json["status"].should eq("Collected")
+ json["Collected"].should be_kind_of(Numeric)
+ json["Not Collected"].should be_kind_of(Numeric)
+ end
+
+ it "should set currency as not collecte" do
+ user.update_collection(first_currency)
+ get :collect, :id => first_currency, :format => :json
+ json = ActiveSupport::JSON.decode(response.body)
+ json["message"].should eq("Currency was successfully as not collected")
+ json["status"].should eq("Not Collected")
+ json["Collected"].should be_kind_of(Numeric)
+ json["Not Collected"].should be_kind_of(Numeric)
+ end
+ end
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.