socialforge/app/controllers/no_uses_controller.rb

124 lines
3.4 KiB
Ruby

class NoUsesController < ApplicationController
# GET /no_uses
# GET /no_uses.json
def index
@no_uses = NoUse.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @no_uses }
end
end
# GET /no_uses/1
# GET /no_uses/1.json
def show
@no_use = NoUse.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @no_use }
end
end
# GET /no_uses/new
# GET /no_uses/new.json
def new
@no_use = NoUse.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @no_use }
end
end
# GET /no_uses/1/edit
def edit
@no_use = NoUse.find(params[:id])
end
# POST /no_uses
# POST /no_uses.json
def create
@no_use = NoUse.new(params[:no_use])
respond_to do |format|
if @no_use.save
format.html { redirect_to @no_use, notice: 'No use was successfully created.' }
format.json { render json: @no_use, status: :created, location: @no_use }
else
format.html { render action: "new" }
format.json { render json: @no_use.errors, status: :unprocessable_entity }
end
end
end
# PUT /no_uses/1
# PUT /no_uses/1.json
def update
@no_use = NoUse.find(params[:id])
respond_to do |format|
if @no_use.update_attributes(params[:no_use])
format.html { redirect_to @no_use, notice: 'No use was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @no_use.errors, status: :unprocessable_entity }
end
end
end
# DELETE /no_uses/1
# DELETE /no_uses/1.json
def destroy
@no_use = NoUse.find(params[:id])
@no_use.destroy
respond_to do |format|
format.html { redirect_to no_uses_url }
format.json { head :no_content }
end
end
private
def find_no_use
klass = Object.const_get(params[:object_type].camelcase) rescue nil
if klass && klass.respond_to?('watched_by')
@no_use = klass.find_all_by_id(Array.wrap(params[:object_id]))
end
render_404 unless @no_use.present?
end
def set_watcher(watchables, user, watching)
watchables.each do |watchable|
watchable.set_watcher(user, watching)
# @user = watchable # added by william
if watching
# 修改 user和project的状态
if watchable.instance_of?(User)
#写user_statuses表
UserStatus.find_by_user_id(watchable.id).update_watchers_count(1)
elsif watchable.instance_of?(Project)
#写project_statuese表
ProjectStatus.find_by_project_id(watchable.id).update_watchers_count(1)
end
else
# 修改 user和project的状态
if watchable.instance_of?(User)
#写user_statuses表
UserStatus.find_by_user_id(watchable.id).update_watchers_count(-1)
elsif watchable.instance_of?(Project)
#写project_statuese表 :project_status
ProjectStatus.find_by_project_id(watchable.id).update_watchers_count(-1)
end
end
end
respond_to do |format|
format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} }
end
end
end