Permalink
Browse files

Completed search and tags

  • Loading branch information...
cpursley committed Dec 5, 2012
1 parent 5bcd18c commit a9761258d72fe9dcd02c0139ff543648dcaa74fd
View
@@ -6,6 +6,7 @@ gem 'will_paginate', '3.0.3'
gem 'bootstrap-will_paginate', '0.0.6'
gem "validate_url"
gem 'simple_form'
+gem 'acts-as-taggable-on'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
View
@@ -28,6 +28,8 @@ GEM
activesupport (3.2.8)
i18n (~> 0.6)
multi_json (~> 1.0)
+ acts-as-taggable-on (2.3.3)
+ rails (~> 3.0)
arel (3.0.2)
bcrypt-ruby (3.0.1)
bootstrap-sass (2.0.4.0)
@@ -121,6 +123,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ acts-as-taggable-on
bootstrap-sass
bootstrap-will_paginate (= 0.0.6)
coffee-rails (~> 3.2.1)
@@ -353,6 +353,14 @@ border-bottom:1px solid #fafafa;
margin-bottom:10px;
}
+#tag_cloud {
+ width: 400px;
+ line-height: 1.6em;
+ .s { font-size: 0.6em; }
+ .m { font-size: 1.0em; }
+ .l { font-size: 1.6em; }
+}
+
/* Pagination */
.pagination {
@@ -5,24 +5,29 @@ class DreamsController < ApplicationController
# GET /dreams
# GET /dreams.json
def index
- @dreams = Dream.where(:state => '4').search(params[:search]).order('accepted desc').paginate(:page => params[:page], :per_page => 10)
+ @dreams = Dream.where(:state => '4').paginate(:page => params[:page], :per_page => 8)
@recent_dreams = Dream.recent
-
respond_to do |format|
format.html # index.html.erb
format.json { render json: @dreams }
end
end
def all
- @dreams = Dream.where(:state => ['3', '4']).paginate(:page => params[:page], :per_page => 10)
+ @dreams = Dream.where(:state => ['3', '4']).search(params[:search]).order('accepted desc').paginate(:page => params[:page], :per_page => 8)
@recent_dreams = Dream.recent
respond_to do |format|
format.html { render 'index' }
format.json { render :json => @dreams }
end
+
+ if params[:tag]
+ @dreams = Dream.tagged_with(params[:tag])
+ else
+ @dreams = Dream.all
+ end
end
def mydreams
View
@@ -3,8 +3,10 @@ class Dream < ActiveRecord::Base
has_many :comments, :dependent => :destroy
has_many :ratings, :dependent => :destroy
- attr_accessible :title, :teaser, :body, :version, :changelog, :user_id, :message, :freezebody, :state, :submitted, :accepted
+ attr_accessible :title, :teaser, :body, :version, :changelog, :user_id, :message, :freezebody, :state, :submitted, :accepted, :tag_list
+ acts_as_taggable
+
validates :user_id, :presence => true
validates :title, :presence => true, :length => { :maximum => 80 }
validates :teaser, :presence => true, :length => { :maximum => 500 }
@@ -12,18 +12,19 @@
<% if @dream.state < 3 %>
- <%= f.input :title %>
- <%= f.input :teaser %>
+ <%= f.input :title, :label => 'Title' %>
+ <%= f.input :teaser, :label => 'Teaser - dream summary' %>
<% else %>
- <%= f.input :title %>
- <%= f.input :teaser %>
+ <%= f.input :title, :label => 'Title' %>
+ <%= f.input :teaser, :label => 'Teaser - dream summary' %>
<% end %>
- <%= f.input :body %>
+ <%= f.input :body, :label => 'Body - dream details' %>
+ <%= f.input :tag_list, :label => 'Tags - seperate using comma' %>
<div>
<%= f.submit "Submit", class: "btn" %>
@@ -4,6 +4,7 @@
<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> tag, tag - <i class="icon-comment"></i><%= dream.comments.count %></div>
<p><%= dream.teaser %></p>
+ <p><%= raw dream.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %></p>
</br>
</div>
<% end %>
@@ -6,6 +6,7 @@
<div class="comment-author"><strong>By <%=@dream.user.fullname %></strong></div><br>
<p><%= @dream.body %></p>
+ <p><%= raw @dream.tag_list.map { |t| link_to t, tag_path(t) }.join(', ') %></p>
</div>
</div>
@@ -1,7 +1,7 @@
<div class="span4">
- <!-- Sidebar -->
+
<div class="sidebar">
- <!-- Widget -->
+
<div class="widget">
<h3><span>Search</span></h3>
<div class="form">
@@ -24,9 +24,13 @@
<div class="widget">
<h3><span>Tags</span></h3>
<ul>
- <li>still need to set tags up - railscast?</li>
+ <div id="tag_cloud"><% tag_cloud Dream.tag_counts, %w{s m l} do |tag, css_class| %>
+ <%= link_to tag.name, tag_path(tag.name), class: css_class %>
+ <% end %>
+ </div>
</ul>
</div>
</div>
</div>
+
View
@@ -26,6 +26,7 @@
resources :ratings, :only => [:create, :update, :destroy]
end
+ get 'tags/:tag', to: 'dreams#index', as: :tag
root :to => "dreams#index"
@@ -0,0 +1,30 @@
+class ActsAsTaggableOnMigration < ActiveRecord::Migration
+ def self.up
+ create_table :tags do |t|
+ t.string :name
+ end
+
+ create_table :taggings do |t|
+ t.references :tag
+
+ # You should make sure that the column created is
+ # long enough to store the required class names.
+ t.references :taggable, :polymorphic => true
+ t.references :tagger, :polymorphic => true
+
+ # Limit is created to prevent MySQL error on index
+ # length for MyISAM table type: http://bit.ly/vgW2Ql
+ t.string :context, :limit => 128
+
+ t.datetime :created_at
+ end
+
+ add_index :taggings, :tag_id
+ add_index :taggings, [:taggable_id, :taggable_type, :context]
+ end
+
+ def self.down
+ drop_table :taggings
+ drop_table :tags
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20121124151038) do
+ActiveRecord::Schema.define(:version => 20121205114123) do
create_table "comments", :force => true do |t|
t.integer "user_id"
@@ -49,6 +49,23 @@
add_index "ratings", ["dream_id"], :name => "index_ratings_on_dream_id"
+ create_table "taggings", :force => true do |t|
+ t.integer "tag_id"
+ t.integer "taggable_id"
+ t.string "taggable_type"
+ t.integer "tagger_id"
+ t.string "tagger_type"
+ t.string "context", :limit => 128
+ t.datetime "created_at"
+ end
+
+ add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
+ add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context"
+
+ create_table "tags", :force => true do |t|
+ t.string "name"
+ end
+
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false

0 comments on commit a976125

Please sign in to comment.