Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Completed reputation system

  • Loading branch information...
commit 2a3680546ee17e2fc77a301afa4b75776ba8804d 1 parent 1c7ac96
@cpursley authored
View
1  Gemfile
@@ -7,6 +7,7 @@ gem 'bootstrap-will_paginate', '0.0.6'
gem "validate_url"
gem 'simple_form'
gem 'acts-as-taggable-on'
+gem 'activerecord-reputation-system', require: 'reputation_system'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
View
2  Gemfile.lock
@@ -22,6 +22,7 @@ GEM
activesupport (= 3.2.8)
arel (~> 3.0.2)
tzinfo (~> 0.3.29)
+ activerecord-reputation-system (2.0.2)
activeresource (3.2.8)
activemodel (= 3.2.8)
activesupport (= 3.2.8)
@@ -123,6 +124,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ activerecord-reputation-system
acts-as-taggable-on
bootstrap-sass
bootstrap-will_paginate (= 0.0.6)
View
13 app/controllers/dreams_controller.rb
@@ -7,6 +7,8 @@ class DreamsController < ApplicationController
def index
@dreams = Dream.where(:state => '4').paginate(:page => params[:page], :per_page => 8)
@recent_dreams = Dream.recent
+ @loved_dreams = Dream.loved
+
respond_to do |format|
format.html # index.html.erb
@@ -15,8 +17,10 @@ def index
end
def all
- @dreams = Dream.where(:state => ['3', '4']).search(params[:search]).order('accepted desc').paginate(:page => params[:page], :per_page => 8)
+ @dreams = Dream.where(:state => ['3', '4']).search(params[:search]).order('accepted desc').paginate(:page => params[:page])
@recent_dreams = Dream.recent
+ @loved_dreams = Dream.loved
+
respond_to do |format|
format.html { render 'index' }
@@ -146,7 +150,12 @@ def destroy
end
end
-
+def vote
+ value = params[:type] == "up" ? 1 : -1
+ @dream = Dream.find(params[:id])
+ @dream.add_or_update_evaluation(:votes, value, current_user)
+ redirect_to :back, notice: "Thank you for voting!"
+end
def about
end
View
7 app/models/dream.rb
@@ -6,6 +6,8 @@ class Dream < ActiveRecord::Base
acts_as_taggable
+ has_reputation :votes, source: :user, aggregated_by: :sum
+
validates :user_id, :presence => true
validates :title, :presence => true, :length => { :maximum => 80 }
validates :teaser, :presence => true, :length => { :maximum => 500 }
@@ -15,7 +17,6 @@ class Dream < ActiveRecord::Base
validates :message, :length => { :maximum => 5000 }
validates :state, :presence => true, :numericality => true, :inclusion => { :in => 0..4 }
-
def self.search(search)
if search
where('title LIKE ? or teaser LIKE ?', "%#{search}%", "%#{search}%")
@@ -28,6 +29,10 @@ def self.recent
where('created_at > ?', Time.now-30.days.ago).order("created_at desc").first(5)
end
+def self.loved
+ find_with_reputation(:votes, :all, {:order => 'votes DESC'}).first(5)
+end
+
protected
def record_not_found
flash[:error] = 'The dream you requested could not be found.'
View
2  app/views/dreams/index.html.erb
@@ -2,7 +2,7 @@
<div class="entry"><% @dreams.each do |dream| %>
<div class="matter">
<h2><%= link_to dream.title, dream %></h2>
- <div class="meta"><i class="icon-calendar"></i> <%= dream.created_at.strftime("%B %d, %Y") %> - <i class="icon-folder-open"></i> <%= raw dream.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %> - <i class="icon-comment"></i><%= dream.comments.count %></div>
+ <div class="meta"><i class="icon-calendar"></i> <%= dream.created_at.strftime("%B %d, %Y") %> - <i class="icon-tags"></i> <%= raw dream.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %> - <i class="icon-comment"></i> <%= dream.comments.count %> - <i class="icon-heart"></i> <%= dream.reputation_for(:votes).to_i %></div>
<p><%= dream.teaser %></p>
</br>
</div>
View
5 app/views/dreams/show.html.erb
@@ -2,10 +2,11 @@
<div class="entry">
<div class="matter">
- <div class="meta"><i class="icon-calendar"></i><%= @dream.created_at.strftime("%B %d, %Y") %> - <i class="icon-folder-open"></i> <%= raw @dream.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %> - <i class="icon-comment"></i><%= @dream.comments.count %></div>
+ <div class="meta"><i class="icon-calendar"></i><%= @dream.created_at.strftime("%B %d, %Y") %> - <i class="icon-tags"></i> <%= raw @dream.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %> - <i class="icon-comment"></i><%= @dream.comments.count %> - <i class="icon-heart"></i> <%= @dream.reputation_for(:votes).to_i %></div>
<div class="comment-author"><strong>By <%=@dream.user.fullname %></strong></div><br>
- <p><%= @dream.body %></p>
+ <p><%= @dream.body %></p></br>
+ <div class="meta"><i class="icon-heart"></i> <%= pluralize @dream.reputation_for(:votes).to_i, "vote" %> | <%= link_to "up", vote_dream_path(@dream, type: "up"), method: "post" %></div>
</div>
</div>
View
9 app/views/layouts/_sidebar.html.erb
@@ -22,6 +22,15 @@
</div>
<div class="widget">
+ <h3><span>Most Loved</span></h3>
+ <ul>
+ <% @loved_dreams.each do |dream| %>
+ <li><%= link_to dream.title, dream_path(dream) %></li>
+ <% end %>
+ </ul>
+ </div>
+
+ <div class="widget">
<h3><span>Tags</span></h3>
<ul>
<div id="tag_cloud"><% tag_cloud Dream.tag_counts, %w{s m l} do |tag, css_class| %>
View
3  config/routes.rb
@@ -21,9 +21,10 @@
member do
put 'submit'
end
+ member { post :vote }
resources :comments, :only => [:create, :destroy]
- resources :ratings, :only => [:create, :update, :destroy]
+
end
get 'tags/:tag', to: 'dreams#index', as: :tag
View
59 db/migrate/20121206132346_create_reputation_system.rb
@@ -0,0 +1,59 @@
+##
+# Copyright 2012 Twitter, Inc
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+class CreateReputationSystem < ActiveRecord::Migration
+ def self.up
+ create_table :rs_evaluations do |t|
+ t.string :reputation_name
+ t.references :source, :polymorphic => true
+ t.references :target, :polymorphic => true
+ t.float :value, :default => 0
+ t.timestamps
+ end
+
+ add_index :rs_evaluations, :reputation_name
+ add_index :rs_evaluations, [:target_id, :target_type]
+ add_index :rs_evaluations, [:source_id, :source_type]
+
+ create_table :rs_reputations do |t|
+ t.string :reputation_name
+ t.float :value, :default => 0
+ t.string :aggregated_by
+ t.references :target, :polymorphic => true
+ t.boolean :active, :default => true
+ t.timestamps
+ end
+
+ add_index :rs_reputations, :reputation_name
+ add_index :rs_reputations, [:target_id, :target_type]
+
+ create_table :rs_reputation_messages do |t|
+ t.references :sender, :polymorphic => true
+ t.integer :receiver_id
+ t.float :weight, :default => 1
+ t.timestamps
+ end
+
+ add_index :rs_reputation_messages, [:sender_id, :sender_type]
+ add_index :rs_reputation_messages, :receiver_id
+ end
+
+ def self.down
+ drop_table :rs_evaluations
+ drop_table :rs_reputations
+ drop_table :rs_reputation_messages
+ end
+end
View
25 db/migrate/20121206132347_add_reputations_index.rb
@@ -0,0 +1,25 @@
+##
+# Copyright 2012 Twitter, Inc
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+class AddReputationsIndex < ActiveRecord::Migration
+ def self.up
+ add_index :rs_reputations, [:reputation_name, :target_id, :target_type], :name => "index_rs_reputations_on_reputation_name_and_target"
+ end
+
+ def self.down
+ remove_index :rs_reputations, :name => "index_rs_reputations_on_reputation_name_and_target"
+ end
+end
View
25 db/migrate/20121206132348_add_evaluations_index.rb
@@ -0,0 +1,25 @@
+##
+# Copyright 2012 Twitter, Inc
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+class AddEvaluationsIndex < ActiveRecord::Migration
+ def self.up
+ add_index :rs_evaluations, [:reputation_name, :source_id, :source_type, :target_id, :target_type], :name => "index_rs_evaluations_on_reputation_name_and_source_and_target"
+ end
+
+ def self.down
+ remove_index :rs_evaluations, :name => "index_rs_evaluations_on_reputation_name_and_source_and_target"
+ end
+end
View
25 db/migrate/20121206132349_add_reputation_messages_index.rb
@@ -0,0 +1,25 @@
+##
+# Copyright 2012 Twitter, Inc
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+class AddReputationMessagesIndex < ActiveRecord::Migration
+ def self.up
+ add_index :rs_reputation_messages, [:receiver_id, :sender_id, :sender_type], :name => "index_rs_reputation_messages_on_receiver_id_and_sender"
+ end
+
+ def self.down
+ remove_index :rs_reputation_messages, :name => "index_rs_reputation_messages_on_receiver_id_and_sender"
+ end
+end
View
27 db/migrate/20121206132350_change_evaluations_index_to_unique.rb
@@ -0,0 +1,27 @@
+##
+# Copyright 2012 Twitter, Inc
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+class ChangeEvaluationsIndexToUnique < ActiveRecord::Migration
+ def self.up
+ remove_index :rs_evaluations, :name => "index_rs_evaluations_on_reputation_name_and_source_and_target"
+ add_index :rs_evaluations, [:reputation_name, :source_id, :source_type, :target_id, :target_type], :name => "index_rs_evaluations_on_reputation_name_and_source_and_target", :unique => true
+ end
+
+ def self.down
+ remove_index :rs_evaluations, :name => "index_rs_evaluations_on_reputation_name_and_source_and_target"
+ add_index :rs_evaluations, [:reputation_name, :source_id, :source_type, :target_id, :target_type], :name => "index_rs_evaluations_on_reputation_name_and_source_and_target"
+ end
+end
View
28 db/migrate/20121206132351_change_reputation_messages_index_to_unique.rb
@@ -0,0 +1,28 @@
+##
+# Copyright 2012 Twitter, Inc
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+class ChangeReputationMessagesIndexToUnique < ActiveRecord::Migration
+ def self.up
+ remove_index :rs_reputation_messages, :name => "index_rs_reputation_messages_on_receiver_id_and_sender"
+ add_index :rs_reputation_messages, [:receiver_id, :sender_id, :sender_type], :name => "index_rs_reputation_messages_on_receiver_id_and_sender", :unique => true
+ end
+
+ def self.down
+ remove_index :rs_reputation_messages, :name => "index_rs_reputation_messages_on_receiver_id_and_sender"
+ add_index :rs_reputation_messages, [:receiver_id, :sender_id, :sender_type], :name => "index_rs_reputation_messages_on_receiver_id_and_sender"
+ end
+end
+
View
27 db/migrate/20121206132352_change_reputations_index_to_unique.rb
@@ -0,0 +1,27 @@
+##
+# Copyright 2012 Twitter, Inc
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+class ChangeReputationsIndexToUnique < ActiveRecord::Migration
+ def self.up
+ remove_index :rs_reputations, :name => "index_rs_reputations_on_reputation_name_and_target"
+ add_index :rs_reputations, [:reputation_name, :target_id, :target_type], :name => "index_rs_reputations_on_reputation_name_and_target", :unique => true
+ end
+
+ def self.down
+ remove_index :rs_reputations, :name => "index_rs_reputations_on_reputation_name_and_target"
+ add_index :rs_reputations, [:reputation_name, :target_id, :target_type], :name => "index_rs_reputations_on_reputation_name_and_target"
+ end
+end
View
46 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121205114123) do
+ActiveRecord::Schema.define(:version => 20121206132352) do
create_table "comments", :force => true do |t|
t.integer "user_id"
@@ -49,6 +49,50 @@
add_index "ratings", ["dream_id"], :name => "index_ratings_on_dream_id"
+ create_table "rs_evaluations", :force => true do |t|
+ t.string "reputation_name"
+ t.integer "source_id"
+ t.string "source_type"
+ t.integer "target_id"
+ t.string "target_type"
+ t.float "value", :default => 0.0
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "rs_evaluations", ["reputation_name", "source_id", "source_type", "target_id", "target_type"], :name => "index_rs_evaluations_on_reputation_name_and_source_and_target", :unique => true
+ add_index "rs_evaluations", ["reputation_name"], :name => "index_rs_evaluations_on_reputation_name"
+ add_index "rs_evaluations", ["source_id", "source_type"], :name => "index_rs_evaluations_on_source_id_and_source_type"
+ add_index "rs_evaluations", ["target_id", "target_type"], :name => "index_rs_evaluations_on_target_id_and_target_type"
+
+ create_table "rs_reputation_messages", :force => true do |t|
+ t.integer "sender_id"
+ t.string "sender_type"
+ t.integer "receiver_id"
+ t.float "weight", :default => 1.0
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "rs_reputation_messages", ["receiver_id", "sender_id", "sender_type"], :name => "index_rs_reputation_messages_on_receiver_id_and_sender", :unique => true
+ add_index "rs_reputation_messages", ["receiver_id"], :name => "index_rs_reputation_messages_on_receiver_id"
+ add_index "rs_reputation_messages", ["sender_id", "sender_type"], :name => "index_rs_reputation_messages_on_sender_id_and_sender_type"
+
+ create_table "rs_reputations", :force => true do |t|
+ t.string "reputation_name"
+ t.float "value", :default => 0.0
+ t.string "aggregated_by"
+ t.integer "target_id"
+ t.string "target_type"
+ t.boolean "active", :default => true
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+ add_index "rs_reputations", ["reputation_name", "target_id", "target_type"], :name => "index_rs_reputations_on_reputation_name_and_target", :unique => true
+ add_index "rs_reputations", ["reputation_name"], :name => "index_rs_reputations_on_reputation_name"
+ add_index "rs_reputations", ["target_id", "target_type"], :name => "index_rs_reputations_on_target_id_and_target_type"
+
create_table "taggings", :force => true do |t|
t.integer "tag_id"
t.integer "taggable_id"
Please sign in to comment.
Something went wrong with that request. Please try again.