Permalink
Browse files

add categories

  • Loading branch information...
1 parent 67bef74 commit 733720acff65ca55af359abdde4bf2d17744de8d @aquayee committed Apr 19, 2012
Showing with 561 additions and 44 deletions.
  1. +2 −0 Gemfile
  2. +4 −1 Gemfile.lock
  3. +2 −0 app/assets/javascripts/categories.js
  4. +4 −0 app/assets/stylesheets/categories.css
  5. +10 −0 app/controllers/blogs_controller.rb
  6. +83 −0 app/controllers/categories_controller.rb
  7. +6 −0 app/controllers/posts_controller.rb
  8. +11 −0 app/helpers/categories_helper.rb
  9. +3 −0 app/models/category.rb
  10. +2 −2 app/models/fts.rb
  11. +4 −0 app/models/post.rb
  12. +3 −0 app/models/technology.rb
  13. +11 −8 app/models/user.rb
  14. +21 −0 app/views/categories/_form.html.erb
  15. +6 −0 app/views/categories/edit.html.erb
  16. +23 −0 app/views/categories/index.html.erb
  17. +5 −0 app/views/categories/new.html.erb
  18. +8 −0 app/views/categories/show.haml
  19. +1 −1 app/views/layouts/application.haml
  20. +4 −0 config/routes.rb
  21. +1 −1 config/sunspot.yml
  22. +0 −13 db/migrate/20110812135348_create_tech_blogs_for6users.rb
  23. +0 −10 db/migrate/20110812141220_refresh_tag_for6users.rb
  24. +9 −0 db/migrate/20120418133339_create_categories.rb
  25. +10 −0 db/migrate/20120418133958_add_ancestry_to_categories.rb
  26. +9 −0 db/migrate/20120418134530_add_category_id_on_post.rb
  27. +25 −8 db/seeds.rb
  28. +157 −0 spec/controllers/categories_controller_spec.rb
  29. +15 −0 spec/helpers/categories_helper_spec.rb
  30. +5 −0 spec/models/category_spec.rb
  31. +11 −0 spec/requests/categories_spec.rb
  32. +35 −0 spec/routing/categories_routing_spec.rb
  33. +18 −0 spec/views/categories/edit.html.erb_spec.rb
  34. +20 −0 spec/views/categories/index.html.erb_spec.rb
  35. +18 −0 spec/views/categories/new.html.erb_spec.rb
  36. +15 −0 spec/views/categories/show.html.erb_spec.rb
View
@@ -1,9 +1,11 @@
source 'http://ruby.taobao.org'
#source 'http://rubygems.org'
+
gem 'rails', "~> 3.1.0"
# Bundle edge Rails instead:
#gem 'rake','0.8.7'
+gem 'ancestry'
gem 'unidecode'
gem 'sqlite3'
gem 'devise'
View
@@ -31,6 +31,8 @@ GEM
activesupport (3.1.0)
multi_json (~> 1.0)
addressable (2.2.6)
+ ancestry (1.2.5)
+ activerecord (>= 2.2.2)
annotate (2.4.0)
arel (2.2.1)
bcrypt-ruby (3.0.1)
@@ -175,7 +177,7 @@ GEM
sprockets (2.0.0)
hike (~> 1.2)
rack (~> 1.0)
- tilt (~> 1.1, != 1.3.0)
+ tilt (!= 1.3.0, ~> 1.1)
sqlite3 (1.3.4)
sunspot (1.2.1)
escape (= 0.0.4)
@@ -209,6 +211,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ ancestry
annotate
capybara
cucumber
@@ -0,0 +1,2 @@
+// Place all the behaviors and hooks related to the matching controller here.
+// All this logic will automatically be available in application.js.
@@ -0,0 +1,4 @@
+/*
+ Place all the styles related to the matching controller here.
+ They will automatically be included in application.css.
+*/
@@ -14,6 +14,16 @@ def tech
@posts = Post.joins("inner join blogs on blogs.id = posts.blog_id").where("blogs.kind = ?", Blog.tech ).where("posts.is_deleted is NULL and (posts.type = ? or posts.type = ?)", Technology.name, Video.name).order("created_at desc").paginate :page => params[:page], :per_page => 15
end
+ #List all the post by specified category
+ #GET /cat/:id
+ def tech_list_by_cat
+ @category = Category.find(params[:id])
+ cat_ids = @category.subtree_ids
+ @posts = Post.joins("inner join blogs on blogs.id = posts.blog_id").where("blogs.kind = ?", Blog.tech ).where("posts.is_deleted is NULL and (posts.type = ? or posts.type = ?) and posts.category_id in (?)", Technology.name, Video.name, ).order("created_at desc").paginate :page => params[:page], :per_page => 15
+ render :tech
+ end
+
+
# GET /blogs
# GET /blogs.xml
def index
@@ -0,0 +1,83 @@
+class CategoriesController < ApplicationController
+ # GET /categories
+ # GET /categories.json
+ def index
+ @categories = Category.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @categories }
+ end
+ end
+
+ # GET /categories/1
+ # GET /categories/1.json
+ def show
+ @category = Category.find_by_name(Post.name)
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @category }
+ end
+ end
+
+ # GET /categories/new
+ # GET /categories/new.json
+ def new
+ @category = Category.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @category }
+ end
+ end
+
+ # GET /categories/1/edit
+ def edit
+ @category = Category.find(params[:id])
+ end
+
+ # POST /categories
+ # POST /categories.json
+ def create
+ @category = Category.new(params[:category])
+
+ respond_to do |format|
+ if @category.save
+ format.html { redirect_to @category, notice: 'Category was successfully created.' }
+ format.json { render json: @category, status: :created, location: @category }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @category.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /categories/1
+ # PUT /categories/1.json
+ def update
+ @category = Category.find(params[:id])
+
+ respond_to do |format|
+ if @category.update_attributes(params[:category])
+ format.html { redirect_to @category, notice: 'Category was successfully updated.' }
+ format.json { head :ok }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @category.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /categories/1
+ # DELETE /categories/1.json
+ def destroy
+ @category = Category.find(params[:id])
+ @category.destroy
+
+ respond_to do |format|
+ format.html { redirect_to categories_url }
+ format.json { head :ok }
+ end
+ end
+end
@@ -163,4 +163,10 @@ def destroy
format.xml { head :ok }
end
end
+
+
+ def list_post_by_cat
+ cat_ids = Category.find(params[:id]).subtree_ids
+ @posts = Post.joins("inner join blogs on blogs.id = posts.blog_id").where("blogs.kind = ?", Blog.tech ).where("posts.is_deleted is NULL and (posts.type = ? or posts.type = ?) and posts.category_id in (?)", Technology.name, Video.name, ).order("created_at desc").paginate :page => params[:page], :per_page => 15
+ end
end
@@ -0,0 +1,11 @@
+module CategoriesHelper
+ def draw(items)
+ result = []
+ items.map do |item, sub_items|
+ p "adding item #{item.name} #{item.id}"
+ result << yield(item)
+ result += draw(sub_items){ |i| "<li><a href='#{list_post_by_cat_path(i.id)}'>#{'&nbsp;' * i.depth * 2} #{i.name}</a></li>" }
+ end
+ result
+ end
+end
@@ -0,0 +1,3 @@
+class Category < ActiveRecord::Base
+ has_ancestry
+end
View
@@ -3,8 +3,6 @@ class Fts < Post
has_attached_file :shoot, :styles => { :medium => "500>", :thumb => "250", :square => "280x280#" }
- scope :only_posts_and_fts, where("type is NULL or type = ? ", Fts.name )
- scope :only_fts, where("type = ? ", Fts.name )
validates_presence_of :shoot
@@ -19,6 +17,8 @@ class Fts < Post
string :type
end
+ #scope :only_posts_and_fts, where("type is NULL or type = ? ", Fts.name )
+ #scope :only_fts, where("type = ? ", Fts.name )
private
View
@@ -20,6 +20,7 @@ class Post < ActiveRecord::Base
belongs_to :blog
belongs_to :user
+ belongs_to :category
has_many :comments, :class_name => "Comment", :foreign_key => "parent_id"
has_many :photos
@@ -60,6 +61,9 @@ def default_nil
strip_tags self.content
end
string :type
+ #integer :category_ids, :multiple => true do
+ # self.category.path_ids
+ #end
end
def set_def_published_on
@@ -8,6 +8,9 @@ class Technology < Post
strip_tags self.content
end
string :type
+ integer :category_ids, :multiple => true do
+ self.category.path_ids if self.category
+ end
end
end
View
@@ -124,7 +124,7 @@ def add_newsfeeds(id)
# #Avoid add duplicated newsfeeds
# #return if $redis.lrange(self.redis_key(:newsfeeds), 0, -1).include?(id)
# #$redis.lpush(self.redis_key(:newsfeeds), id)
- # puts "calling my self to add news feeds"
+ puts "calling add news feeds by id"
User.add_newsfeeds( [self.id], Post.find(id) )
end
@@ -209,7 +209,7 @@ def migrate_blog_to_user
end
logger.debug "total followers #{followers.size}"
followers.uniq.each do |follower|
- user.follow!(follower)
+ follower.follow!(user)
end
end
end
@@ -235,15 +235,18 @@ def follow!(user)
#Wihtout the owner of this message
#puts "before create notification"
#notification = Notification.new(:content => {:owner => self.id, :subject_id => self.id, :verb => 'following', :type => 'followed', :object_id => blog.id, :subject => self.name, :object => blog.name}.to_json, :user => self)
+ p "Create new notification"
notification = Notification.new(:content => {:owner => self.id, :subject_id => self.id, :verb => 'following', :type => 'followed', :object_id => user.id, :subject => self.name, :object => user.name}.to_json, :user => self)
- #puts "after create notification"
+ puts "after create notification"
#puts "if save notification successfully, put to news feeds"
- #if notification.save
- # puts "save notification successfully, put to news feeds"
- user.add_newsfeeds(notification.id) if notification.save
- # puts "add news feeds after notification saved"
- #end
+ if notification.save
+ puts "save notification successfully, put to news feeds"
+ user.add_newsfeeds(notification.id)
+ puts "add news feeds after notification saved"
+ else
+ puts "save notification failed"
+ end
#$redis.lpush(blog.user.redis_key(:newsfeeds), notification.id)
#puts "add newsfeeds succesfully"
@@ -0,0 +1,21 @@
+<%= form_for(@category) do |f| %>
+ <% if @category.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@category.errors.count, "error") %> prohibited this category from being saved:</h2>
+
+ <ul>
+ <% @category.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing category</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @category %> |
+<%= link_to 'Back', categories_path %>
@@ -0,0 +1,23 @@
+<h1>Listing categories</h1>
+
+<table>
+ <tr>
+ <th>Name</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @categories.each do |category| %>
+ <tr>
+ <td><%= category.name %></td>
+ <td><%= link_to 'Show', category %></td>
+ <td><%= link_to 'Edit', edit_category_path(category) %></td>
+ <td><%= link_to 'Destroy', category, confirm: 'Are you sure?', method: :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Category', new_category_path %>
@@ -0,0 +1,5 @@
+<h1>New category</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', categories_path %>
@@ -0,0 +1,8 @@
+%p#notice= notice
+%p
+ %b Name:
+ = @category.name
+ = raw draw(@category.descendants.arrange){ |i| "<li><a href='#{list_post_by_cat_path(i.id)}'>#{i.name}</a></li>" }
+= link_to 'Edit', edit_category_path(@category)
+|
+\#{link_to 'Back', categories_path}
@@ -8,7 +8,7 @@
= yield :css
= csrf_meta_tag
%body
- = render "shared/top_bar"
+ -#= render "shared/top_bar"
= yield
= javascript_include_tag "jquery-1.7.1.min"
= javascript_include_tag "rails"
View
@@ -1,6 +1,8 @@
Aquayee::Application.routes.draw do
+ resources :categories
+
resources :delivery_comments
resources :deliveries
@@ -21,6 +23,8 @@
# devise_for :users
devise_for :users , :controllers => { :registrations => "users/registrations" }
+ match 'cat/:id' => 'blogs#list_post_by_cat', :as => 'list_post_by_cat'
+
match 'url' => 'advertisements#log_access', :via => 'get', :as => "access_advertisement"
match 'users/vote/:id' => 'users#vote', :via => ['get','post'], :as => "user_vote_answer"
View
@@ -7,7 +7,7 @@ production:
development:
solr:
hostname: localhost
- port: 8982
+ port: 8981
log_level: INFO
test:
@@ -1,13 +0,0 @@
-class CreateTechBlogsFor6users < ActiveRecord::Migration
- def self.up
- [21,17,16,14,10].each do |id|
- user = User.find(id)
- puts "creating tech blog for #{user.name}"
- blog = user.find_or_create_tech_blog
- puts "tech blog id:#{blog.id}"
- end
- end
-
- def self.down
- end
-end
@@ -1,10 +0,0 @@
-class RefreshTagFor6users < ActiveRecord::Migration
- def self.up
- Post.all.each do |post|
- post.save_tag
- end
- end
-
- def self.down
- end
-end
Oops, something went wrong.

0 comments on commit 733720a

Please sign in to comment.