Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Backend functionality for collecting statistic data

  • Loading branch information...
commit c0620041f82689a82e4eb6fd16012401816a3a14 1 parent 44a5a46
@ignar authored
View
8 app/controllers/countries_controller.rb
@@ -42,7 +42,13 @@ def visit
end
def statistic
- #pluck
+ user_countries = UserCountry.user_statistic(current_user)
+ respond_to do |format|
+ format.json{ render :json => {
+ :collection => user_countries
+ }
+ }
+ end
end
end
View
10 app/controllers/currencies_controller.rb
@@ -39,4 +39,14 @@ def collect
}
end
end
+
+ def statistic
+ user_countries = UserCurrency.user_statistic(current_user)
+ respond_to do |format|
+ format.json{ render :json => {
+ :collection => user_countries
+ }
+ }
+ end
+ end
end
View
5 app/models/user_country.rb
@@ -1,7 +1,12 @@
class UserCountry < ActiveRecord::Base
+ self.include_root_in_json = false
belongs_to :user
belongs_to :country, :foreign_key => :country_code
validates :user, :presence => true, :allow_blank => false
validates :country, :presence => true, :allow_blank => false
+
+ scope :user_statistic, lambda{ |user| where(:user_id => user.id).
+ select("strftime( '%d.%m.%Y', created_at) as date, count(*) as cnt").
+ group("date") }
end
View
5 app/models/user_currency.rb
@@ -1,7 +1,12 @@
class UserCurrency < ActiveRecord::Base
+ self.include_root_in_json = false
belongs_to :user
belongs_to :currency, :foreign_key => :currency_code
validates :user, :presence => true, :allow_blank => false
validates :currency, :presence => true, :allow_blank => false
+
+ scope :user_statistic, lambda{ |user| where(:user_id => user.id).
+ select("strftime( '%d.%m.%Y', created_at) as date, count(*) as cnt").
+ group("date") }
end
View
5 config/routes.rb
@@ -4,6 +4,8 @@
resources :countries, :only => [:index, :show] do
member do
get :visit
+ end
+ collection do
get :statistic
end
end
@@ -11,6 +13,9 @@
member do
get :collect
end
+ collection do
+ get :statistic
+ end
end
resources :sessions, :only => [:new, :create] do
collection do
View
7 ctracker.org
@@ -1,10 +1,10 @@
#+STARTUP: showall
#+BEGIN: clocktable :maxlevel 2 :scope file
-Clock summary at [2012-10-25 Thu 02:21]
+Clock summary at [2012-10-25 Thu 03:07]
| Headline | Time |
|----------------------------------------------------------------------------------+---------|
-| *Total time* | *16:45* |
+| *Total time* | *17:28* |
|----------------------------------------------------------------------------------+---------|
| DONE Setup. Understand the task. Read existed code. | 0:37 |
| DONE Rake task | 0:07 |
@@ -29,7 +29,7 @@ Clock summary at [2012-10-25 Thu 02:21]
| DONE Filter on country page and "check/uncheck" controll | 1:04 |
| DONE Filter on currencies page | 0:37 |
| DONE Add apply controll | 0:14 |
-| TODO Progress chart | 0:38 |
+| TODO Progress chart | 1:21 |
| DONE Rewrite from hbtm to has_many | 1:52 |
#+END:
#+STARTUP: hidestars
@@ -116,6 +116,7 @@ Clock summary at [2012-10-25 Thu 02:21]
CLOSED: [2012-10-23 Tue 17:57]
CLOCK: [2012-10-23 Tue 16:50]--[2012-10-23 Tue 17:04] => 0:14
* TODO Progress chart
+ CLOCK: [2012-10-25 Thu 02:24]--[2012-10-25 Thu 03:07] => 0:43
CLOCK: [2012-10-24 Wed 21:59]--[2012-10-24 Wed 22:37] => 0:38
* DONE Rewrite from hbtm to has_many
CLOSED: [2012-10-25 Thu 02:21]
View
9 spec/controllers/countries_controller_spec.rb
@@ -57,7 +57,16 @@
end
context "GET statistic" do
+ before(:each) do
+ user.toggle_visiting(country)
+ end
+ it "should collect statistic of countries visiting" do
+ get :statistic, :format => :json
+ json = ActiveSupport::JSON.decode(response.body)
+ json["collection"].should be_kind_of(Array)
+ json["collection"][0]["cnt"].should eq(3)
+ end
end
end
View
14 spec/controllers/currencies_controller_spec.rb
@@ -57,4 +57,18 @@
end
end
+ context "GET statistic" do
+ before(:each) do
+ user.toggle_visiting(country)
+ end
+
+ it "should collect statistic of countries visiting" do
+ get :statistic, :format => :json
+ json = ActiveSupport::JSON.decode(response.body)
+ json["collection"].count.should eq(2)
+ json["collection"][0]["cnt"].should eq(2)
+ json["collection"][1]["cnt"].should eq(2)
+ end
+ end
+
end
View
4 spec/fixtures/user_currencies.yml
@@ -2,8 +2,10 @@ one:
id: 1
user_id: 1
currency_code: CodeOne
+ created_at: <%= Date.today - 1.day %>
two:
id: 2
user_id: 1
- currency_code: CodeTwo
+ currency_code: CodeTwo
+ created_at: <%= Date.today - 1.day %>
View
8 spec/models/user_country_spec.rb
@@ -28,4 +28,12 @@
uc.errors[:country].should include("can't be blank")
end
end
+
+ describe ".user_statistic" do
+ it "should select count of visited countries per day" do
+ uc = UserCountry.user_statistic(user)
+ uc.first.cnt.should eq(2)
+ uc.first.date.should be_present
+ end
+ end
end
View
12 spec/models/user_currencies_spec.rb
@@ -3,6 +3,7 @@
describe UserCurrency do
let(:user){ users(:one) }
let(:currency){ currencies(:one)}
+ let(:country){ countries(:three) }
subject{ user_currencies(:one) }
it { should be_valid }
@@ -28,4 +29,15 @@
uc.errors[:currency].should include("can't be blank")
end
end
+
+ describe ".user_statistic" do
+ it "should select count of collected currencies per day" do
+ user.toggle_visiting(country)
+ uc = UserCurrency.user_statistic(user)
+ uc.all.count.should eq(2)
+ uc.first.cnt.should eq(2)
+ uc.last.cnt.should eq(2)
+ uc.first.date.should be_present
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.