Permalink
Browse files

Implemented counters

  • Loading branch information...
Antono Vasiljev
Antono Vasiljev committed Apr 3, 2012
1 parent f06a0a8 commit 91ae1436c30dc236856b9051b0a4742adbb68785
View
@@ -5,16 +5,16 @@
** DONE select better cloud hosting
** DONE [#A] Migrate to ttyrec
** DONE [#A] Search with solr
-** TODO [#B] Comments!!!
-** TODO [#C] Blog
-** TODO [#B] Better copywrigthing
+** DONE [#B] Better copywrigthing
** TODO [#C] Packages :community:
*** DONE Arch
*** DONE Ubuntu
*** DONE Debian
*** DONE Gentoo
*** TODO AltLinux
*** TODO Older Ubuntu
+** TODO [#B] Comments!!!
+** TODO [#C] Blog
** TODO [#A] Setup new instance :admin:
*** TODO ec2 micro (default)
*** TODO ec2 small (slashdotting)
@@ -15,6 +15,7 @@ def index
def show
@record = Record.find(params[:id])
+ @record.hit!(current_user) if request.format.json?
respond_with @record
end
View
@@ -14,6 +14,7 @@ class Record
field :rows, type: Integer
field :typescript, type: String
field :timing, type: String
+ field :hits, type: Integer, index: true, default: 0
field :tags, type: Array, index: true
field :license, type: String, index: true
field :created_at, type: DateTime, index: true
@@ -22,6 +23,8 @@ class Record
belongs_to :user, index: true
has_many :comments, :as => :commentable
+ has_and_belongs_to_many :viewers, :class_name => 'User', :inverse_of => nil
+
attr_accessible :title, :description, :typescript,
:timing, :tags, :columns, :rows
@@ -100,6 +103,22 @@ def tags
read_attribute(:tags).try(:join, ", ")
end
+ def hit!(user = nil)
+ if user
+ self.viewers << user unless self.viewers.include?(user)
+ else
+ self.inc(:hits, 1)
+ end
+ end
+
+ def views(type = :all)
+ case type
+ when :all then hits + viewers.count
+ when :registered then viewers.count
+ when :anonymous then hits
+ end
+ end
+
def editable_by?(usr)
return false if not usr.is_a?(User)
return true if usr.god?
@@ -21,6 +21,7 @@
%br
%input{ :type => :text, :value => "shelr play #{record_path(@record, :only_path => false, :format => :json)}", :class => 'cmdline hidden' }
+
.social= render partial: 'shared/addthis'
:javascript
@@ -1,7 +1,7 @@
.user.big
= link_to(image_tag(user.avatar_url(100)), user, :class => 'avatar')
-
+
.desc
%strong Nick
= link_to sanitize(user.nickname), user
@@ -10,7 +10,10 @@
.desc
%strong Description
.long=sanitize record.description_html
-
+ .desc
+ %strong Statistics
+ .long== #{@record.views(:anonymous)} view by Anonymous and #{@record.views(:registered)} views by registered users
+
- unless record.tags.blank?
.desc.tags
%strong Tags
@@ -25,7 +28,7 @@
= link_to('destroy', record_path(@record), method: :delete, class: 'btn btn-danger') if @record.editable_by?(current_user)
- else
-
+
- unless user.website.blank?
.desc
%strong Website
@@ -36,7 +39,7 @@
%strong Twitter
- twitter_url = sanitize "http://twitter.com/#{user.twitter_name}"
= link_to twitter_url, twitter_url
-
+
- unless user.github_name.blank?
.desc
%strong Github
@@ -47,7 +50,7 @@
.desc
%strong Bitcoin
=sanitize user.bitcoin
-
+
.desc
%strong Member Since
= user.created_at.strftime("%Y-%m-%d")
@@ -36,6 +36,19 @@
get :show, id: 'kinda id'
assigns(:record).should == 'record'
end
+
+ context "when format is json" do
+ let(:user) { Factory(:user) }
+ let(:record) { Factory(:record) }
+
+ it "hits the record with current_user" do
+ controller.stub(:current_user).and_return(user)
+ Record.stub(:find).and_return(record)
+
+ record.should_receive(:hit!).with(user)
+ get :show, id: 'id', format: 'json'
+ end
+ end
end
describe "POST create" do
View
@@ -8,8 +8,10 @@
it_behaves_like "editable by God"
it_behaves_like "editable by Owner"
- its(:owner) { should_not be_blank }
- its(:comments) { should == [] }
+ its(:owner) { should_not be_blank }
+ its(:comments) { should == [] }
+ its(:viewers) { should be_empty }
+ its(:hits) { should == 0 }
describe "on create" do
before(:each) { subject.should be_new_record }
@@ -84,4 +86,66 @@
subject.description_html.should == "<h1>Hello World</h1>\n"
end
end
+
+ describe "#hit!(user)" do
+ let(:subject) { Factory(:record) }
+
+ context "when user is not nil" do
+ let(:user) { Factory(:user) }
+
+ it "adds user to viewers" do
+ subject.viewers.should_not include(user)
+ subject.hit!(user)
+ subject.viewers.should include(user)
+ end
+
+ it "saves the record" do
+ subject.viewers.should be_empty
+ subject.hit!(user)
+ subject.reload.viewers.should_not be_empty
+ end
+
+ it "does not add user twice" do
+ subject.viewers.should be_empty
+ subject.hit!(user)
+ subject.hit!(user)
+ subject.reload.viewers.count.should == 1
+ end
+ end
+
+ context "when user is nil" do
+ it "ircreases hits count" do
+ expect { subject.hit! }.to change(subject, :hits).by(1)
+ end
+ end
+ end
+
+ describe "#views(type)" do
+ subject { Factory(:record) }
+
+ before :each do
+ subject.viewers << Factory(:user)
+ subject.viewers << Factory(:user)
+ subject.hits = 10
+ subject.save
+ end
+
+ context "when type is :anonymous" do
+ it "returns hit count" do
+ subject.views(:anonymous).should == 10
+ end
+ end
+
+ context "when type is :registered" do
+ it "returns viewers count" do
+ subject.views(:registered).should == 2
+ end
+ end
+
+ context "when type is :all" do
+ it "returns summary count" do
+ subject.views(:all).should == 12
+ end
+ end
+ end
end

0 comments on commit 91ae143

Please sign in to comment.