From 89b668be823e9634955f7b42ec5f70d166777aae Mon Sep 17 00:00:00 2001 From: fanqiang <316257774@qq.com> Date: Sun, 6 Apr 2014 16:34:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=B3=E8=AF=B7=E7=89=88?= =?UTF-8?q?=E4=B8=BB=E5=8A=9F=E8=83=BD=EF=BC=8C=E7=9B=AE=E5=89=8D=E5=B7=B2?= =?UTF-8?q?=E7=BB=8F=E5=8F=AF=E4=BB=A5=E5=86=99=E5=85=A5=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apply_project_masters_controller.rb | 53 +++++-- app/controllers/no_uses_controller.rb | 74 ++++----- app/controllers/relative_memos_controller.rb | 89 +++++++---- app/helpers/apply_project_masters_helper.rb | 14 +- app/helpers/no_uses_helper.rb | 14 +- app/models/apply_project_master.rb | 12 ++ app/models/no_use.rb | 12 ++ app/models/open_source_project.rb | 25 ++- app/models/relative_memo.rb | 21 +++ .../apply_project_masters/_set_apply.js.erb | 2 + app/views/no_uses/_set_no_use.js.erb | 2 + app/views/open_source_projects/index.html.erb | 15 +- app/views/relative_memos/show.html.erb | 142 ++++++++++++++++++ config/locales/zh.yml | 2 + config/routes.rb | 3 + ...031622_add_view_count_to_relative_memos.rb | 6 + db/schema.rb | 23 +-- 17 files changed, 404 insertions(+), 105 deletions(-) create mode 100644 app/views/apply_project_masters/_set_apply.js.erb create mode 100644 app/views/no_uses/_set_no_use.js.erb create mode 100644 db/migrate/20140404031622_add_view_count_to_relative_memos.rb diff --git a/app/controllers/apply_project_masters_controller.rb b/app/controllers/apply_project_masters_controller.rb index 7dadaf2ed..9e5c52348 100644 --- a/app/controllers/apply_project_masters_controller.rb +++ b/app/controllers/apply_project_masters_controller.rb @@ -1,4 +1,7 @@ class ApplyProjectMastersController < ApplicationController + + before_filter :require_login, :find_apply, :only => [:create, :delete] + # GET /apply_project_masters # GET /apply_project_masters.json def index @@ -40,17 +43,20 @@ class ApplyProjectMastersController < ApplicationController # POST /apply_project_masters # POST /apply_project_masters.json def create - @apply_project_master = ApplyProjectMaster.new(params[:apply_project_master]) - - respond_to do |format| - if @apply_project_master.save - format.html { redirect_to @apply_project_master, notice: 'Apply project master was successfully created.' } - format.json { render json: @apply_project_master, status: :created, location: @apply_project_master } - else - format.html { render action: "new" } - format.json { render json: @apply_project_master.errors, status: :unprocessable_entity } - end - end + # @apply_project_master = ApplyProjectMaster.new(params[:apply_project_master]) +# + # respond_to do |format| + # if @apply_project_master.save + # format.html { redirect_to @apply_project_master, notice: 'Apply project master was successfully created.' } + # format.json { render json: @apply_project_master, status: :created, location: @apply_project_master } + # else + # format.html { render action: "new" } + # format.json { render json: @apply_project_master.errors, status: :unprocessable_entity } + # end + # end + + set_apply(@apply, User.current, true) + end # PUT /apply_project_masters/1 @@ -80,4 +86,29 @@ class ApplyProjectMastersController < ApplicationController format.json { head :no_content } end end + + def delete + set_apply(@apply, User.current, false) + end + + private + + def find_apply + klass = Object.const_get(params[:object_type].camelcase) rescue nil + if klass && klass.respond_to?('applied_by') + @apply = klass.find_all_by_id(Array.wrap(params[:object_id])) + end + render_404 unless @apply.present? + end + + #flag标注功能,为1时设置‘申请版主’,为0时设置取消 + def set_apply(objects, user, flag) + objects.each do |object| + object.set_apply(user, flag) + 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_apply', :locals => {:user => user, :objects => objects} } + end + end end diff --git a/app/controllers/no_uses_controller.rb b/app/controllers/no_uses_controller.rb index c18c6c3b6..f66997107 100644 --- a/app/controllers/no_uses_controller.rb +++ b/app/controllers/no_uses_controller.rb @@ -1,9 +1,12 @@ class NoUsesController < ApplicationController + + before_filter :require_login, :find_no_use, :only => [:create, :delete] + # 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 } @@ -40,17 +43,20 @@ class NoUsesController < ApplicationController # POST /no_uses # POST /no_uses.json def create - @no_use = NoUse.new(params[:no_use]) + set_no_use(@no_use, User.current, true) - 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 + # 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 + def delete + set_no_use(@no_use, User.current, false) end # PUT /no_uses/1 @@ -72,52 +78,34 @@ class NoUsesController < ApplicationController # DELETE /no_uses/1 # DELETE /no_uses/1.json def destroy - @no_use = NoUse.find(params[:id]) - @no_use.destroy + # @no_use = NoUse.find(params[:id]) + # @no_use.destroy + + set_no_use(@no_use, User.current, false) - respond_to do |format| - format.html { redirect_to no_uses_url } - format.json { head :no_content } - end + # 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') + if klass && klass.respond_to?('no_use_for') @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 - + #flag标注功能,为1时设置‘没有帮助’,为0时设置取消 + def set_no_use(objects, user, flag) + objects.each do |object| + object.set_no_use(user, flag) 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} } + format.js { render :partial => 'set_no_use', :locals => {:user => user, :objects => objects} } end end end diff --git a/app/controllers/relative_memos_controller.rb b/app/controllers/relative_memos_controller.rb index 98ca8cd52..70d2f00c2 100644 --- a/app/controllers/relative_memos_controller.rb +++ b/app/controllers/relative_memos_controller.rb @@ -2,6 +2,15 @@ class RelativeMemosController < ApplicationController helper :sort include SortHelper + helper :apply_project_masters + include ApplyProjectMastersHelper + helper :no_uses + include NoUsesHelper + + before_filter :find_memo, :except => [:new, :create] + + layout 'base_opensource_p' + # GET /open_source_projects # GET /open_source_projects.json def index @@ -22,42 +31,41 @@ class RelativeMemosController < ApplicationController # GET /open_source_projects/1 # GET /open_source_projects/1.json + REPLIES_PER_PAGE = 20 unless const_defined?(:REPLIES_PER_PAGE) def show - # @open_source_project = OpenSourceProject.find(params[:id]) -# - # sort_init 'updated_at', 'desc' - # sort_update 'created_at' => "#{RelativeMemo.table_name}.created_at", - # 'replies' => "#{RelativeMemo.table_name}.replies_count", - # 'updated_at' => "COALESCE (last_replies_relative_memos.created_at, #{RelativeMemo.table_name}.created_at)" -# - # @memo = RelativeMemo.new(:open_source_project => @open_source_project) - # @topic_count = @open_source_project.topics.count - # @topic_pages = Paginator.new @topic_count, per_page_option, params['page'] - # @memos = @open_source_project.topics. - # reorder("#{RelativeMemo.table_name}.sticky DESC"). - # includes(:last_reply). - # limit(@topic_pages.per_page). - # offset(@topic_pages.offset). - # order(sort_clause). - # all + pre_count = REPLIES_PER_PAGE + + @memo = @memo.root # 取出楼主,防止输入帖子id让回复作为主贴显示 + @memo.update_column(:viewed_count_local, (@memo.viewed_count_local.to_i + 1)) + + page = params[:page] + if params[:r] && page.nil? + offset = @memo.children.where("#{Memo.table_name}.id < ?", params[:r].to_i).count + page = 1 + offset / pre_count + else + + end + @reply_count = @memo.children.count + @reply_pages = Paginator.new @reply_count, pre_count, page + @replies = @memo.children. + includes(:author, :attachments). + reorder("#{RelativeMemo.table_name}.created_at ASC"). + limit(@reply_pages.per_page). + offset(@reply_pages.offset). + all + + @mome_new = Memo.new - - # @offset, @limit = api_offset_and_limit({:limit => 10}) - # @forum = Forum.find(params[:id]) - # @memos_all = @forum.topics - # @topic_count = @memos_all.count - # @topic_pages = Paginator.new @topic_count, @limit, params['page'] + # @memo = Memo.find_by_id(params[:id]) + # @forum = Forum.find(params[:forum_id]) + # @replies = @memo.replies + # @mome_new = Memo.new - # @offset ||= @topic_pages.offset - # @memos = @memos_all.offset(@offset).limit(@limit).all - - # respond_to do |format| - # format.html { - # render :layout => "base_opensource_p" - # } - # format.json { render json: @open_source_project } - # end + respond_to do |format| + format.html # show.html.erb + format.json { render json: @memo } + end end @@ -120,4 +128,21 @@ class RelativeMemosController < ApplicationController format.json { head :no_content } end end + + private + + def find_memo + return unless find_osp + @memo = @open_source_project.relative_memos.find(params[:id]) + rescue ActiveRecord::RecordNotFound + render_404 + nil + end + + def find_osp + @open_source_project = OpenSourceProject.find(params[:open_source_project_id]) + rescue ActiveRecord::RecordNotFound + render_404 + nil + end end diff --git a/app/helpers/apply_project_masters_helper.rb b/app/helpers/apply_project_masters_helper.rb index 11724cde0..a2dccba35 100644 --- a/app/helpers/apply_project_masters_helper.rb +++ b/app/helpers/apply_project_masters_helper.rb @@ -9,15 +9,23 @@ module ApplyProjectMastersHelper # css = @watch_flag ? ([watcher_css(objects), watched ? 'icon ' : 'icon '].join(' ') << options[0].to_s) : # ([watcher_css(objects), watched ? 'icon icon-fav ' : 'icon icon-fav-off '].join(' ') << options[0].to_s) + css = apply_css(objects) << options[0].to_s + text = applied ? (allowed ? ("123") : ("123")) : ("231") - url = apply_project_master_path( + url = apply_project_masters_path( :object_type => objects.first.class.to_s.underscore, :object_id => (objects.size == 1 ? objects.first.id : objects.map(&:id).sort) ) method = applied ? 'delete' : 'post' - link_to text, url, :remote => true, :method => method - #, :class => css + link_to text, url, :remote => true, :method => method, :class => css end + + def apply_css(objects) + objects = Array.wrap(objects) + id = (objects.size == 1 ? objects.first.id : 'bulk') + "#{objects.first.class.to_s.underscore}-#{id}-watcher" + end + end diff --git a/app/helpers/no_uses_helper.rb b/app/helpers/no_uses_helper.rb index 2c7c268fc..a7a5ac24b 100644 --- a/app/helpers/no_uses_helper.rb +++ b/app/helpers/no_uses_helper.rb @@ -5,18 +5,24 @@ module NoUsesHelper clicked = objects.any? {|object| object.no_use_for?(user)} # @watch_flag = (objects.first.instance_of?(User) or objects.first.instance_of?(Project) or (objects.first.instance_of?(Bid))) - # css = @watch_flag ? ([watcher_css(objects), watched ? 'icon ' : 'icon '].join(' ') << options[0].to_s) : + css = no_use_css(objects) << options[0].to_s # ([watcher_css(objects), watched ? 'icon icon-fav ' : 'icon icon-fav-off '].join(' ') << options[0].to_s) - text = clicked ? ("123") : ("231") + text = clicked ? l(:no_use) : l(:cancel_no_use) - url = apply_project_master_path( + url = no_uses_path( :object_type => objects.first.class.to_s.underscore, :object_id => (objects.size == 1 ? objects.first.id : objects.map(&:id).sort) ) method = clicked ? 'delete' : 'post' - link_to text, url, :remote => true, :method => method + link_to text, url, :remote => true, :method => method, :class => css #, :class => css end + + def no_use_css(objects) + objects = Array.wrap(objects) + id = (objects.size == 1 ? objects.first.id : 'bulk') + "#{objects.first.class.to_s.underscore}-#{id}-watcher" + end end diff --git a/app/models/apply_project_master.rb b/app/models/apply_project_master.rb index 336c102df..f4d21b2bb 100644 --- a/app/models/apply_project_master.rb +++ b/app/models/apply_project_master.rb @@ -1,3 +1,15 @@ class ApplyProjectMaster < ActiveRecord::Base # attr_accessible :title, :body + belongs_to :apply, :polymorphic => true + belongs_to :user + + validates_presence_of :user + validates_uniqueness_of :user_id, :scope => [:apply_type, :apply_id] + validate :validate_user + + protected + + def validate_user + errors.add :user_id, :invalid unless user.nil? || user.active? + end end diff --git a/app/models/no_use.rb b/app/models/no_use.rb index 333174a8d..a46e519e8 100644 --- a/app/models/no_use.rb +++ b/app/models/no_use.rb @@ -1,3 +1,15 @@ class NoUse < ActiveRecord::Base # attr_accessible :title, :body + belongs_to :no_use, :polymorphic => true + belongs_to :user + + validates_presence_of :user + validates_uniqueness_of :user_id, :scope => [:no_use_type, :no_use_id] + validate :validate_user + + protected + + def validate_user + errors.add :user_id, :invalid unless user.nil? || user.active? + end end diff --git a/app/models/open_source_project.rb b/app/models/open_source_project.rb index 57bcfeabc..320b79f7e 100644 --- a/app/models/open_source_project.rb +++ b/app/models/open_source_project.rb @@ -2,11 +2,21 @@ class OpenSourceProject < ActiveRecord::Base attr_accessible :String include Redmine::SafeAttributes + has_many :applies, :class_name => "ApplyProjectMaster", :as => :apply, :dependent => :delete_all has_many :topics, :class_name => 'RelativeMemo', :foreign_key => 'osp_id', :conditions => "#{RelativeMemo.table_name}.parent_id IS NULL", :order => "#{RelativeMemo.table_name}.created_at DESC", :dependent => :destroy - has_many :relative_memos, :dependent => :destroy + has_many :relative_memos, :class_name => 'RelativeMemo', :foreign_key => 'osp_id', :dependent => :destroy has_many :tags, :through => :project_tags, :class_name => 'Tag' has_many :project_tags, :class_name => 'ProjectTags' + + acts_as_taggable + + scope :applied_by, lambda { |user_id| + { :include => :apply_project_master, + :conditions => ["#{ApplyProjectMaster.table_name}.user_id = ?", user_id] } + } + + def short_description(length = 255) description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description end @@ -19,6 +29,19 @@ class OpenSourceProject < ActiveRecord::Base false end + def set_apply(user, flag=true) + flag ? set_filter(user) : remove_filter(user) + end + + def set_filter(user) + self.applies << ApplyProjectMaster.new(:user => user, :status => 1) + end + + def remove_filter(user) + return nil unless user && user.is_a?(User) + ApplyProjectMaster.delete_all "apply_type = '#{self.class}' AND apply_id = #{self.id} AND user_id = #{user.id}" + end + def reset_counters! self.class.reset_counters!(id) end diff --git a/app/models/relative_memo.rb b/app/models/relative_memo.rb index cd2d0e8a6..a1532dcb2 100644 --- a/app/models/relative_memo.rb +++ b/app/models/relative_memo.rb @@ -6,6 +6,9 @@ class RelativeMemo < ActiveRecord::Base has_many :tags, :through => :project_tags, :class_name => 'Tag' has_many :project_tags, :class_name => 'ProjectTags' + + has_many :no_uses, :as => :no_use, :dependent => :delete_all + acts_as_taggable validates_presence_of :osp_id, :subject @@ -77,8 +80,26 @@ class RelativeMemo < ActiveRecord::Base } def no_use_for?(user) + self.no_uses.each do |no_use| + if no_use.user_id == user.id + return true + end + end false end + + def set_no_use(user, flag=true) + flag ? set_filter(user) : remove_filter(user) + end + + def set_filter(user) + self.no_uses << NoUse.new(:user => user) + end + + def remove_filter(user) + return nil unless user && user.is_a?(User) + NoUse.delete_all "no_use_type = '#{self.class}' AND no_use_id = #{self.id} AND user_id = #{user.id}" + end def reset_counters! if parent && parent.id diff --git a/app/views/apply_project_masters/_set_apply.js.erb b/app/views/apply_project_masters/_set_apply.js.erb new file mode 100644 index 000000000..2c2075743 --- /dev/null +++ b/app/views/apply_project_masters/_set_apply.js.erb @@ -0,0 +1,2 @@ +<% selector = ".#{apply_css(objects)}" %> +$("<%= selector %>").each(function(){$(this).replaceWith("<%= escape_javascript apply_super_user(objects, user) %>")}); diff --git a/app/views/no_uses/_set_no_use.js.erb b/app/views/no_uses/_set_no_use.js.erb new file mode 100644 index 000000000..fa0c5a8e8 --- /dev/null +++ b/app/views/no_uses/_set_no_use.js.erb @@ -0,0 +1,2 @@ +<% selector = ".#{no_use_css(objects)}" %> +$("<%= selector %>").each(function(){$(this).replaceWith("<%= escape_javascript no_use_link(objects, user) %>")}); diff --git a/app/views/open_source_projects/index.html.erb b/app/views/open_source_projects/index.html.erb index 50d2fbb5e..032e0a82b 100644 --- a/app/views/open_source_projects/index.html.erb +++ b/app/views/open_source_projects/index.html.erb @@ -149,7 +149,7 @@ li { -
<%=link_to @memo.author.name, user_path(@memo.author) if @memo.author%>
++ <% if @memo.attachments.any?%> + <% options = {:author => true, :deletable => @memo.deleted_attach_able_by?(User.current) } %> + <%= render :partial => 'attachments/links', :locals => {:attachments => @memo.attachments, :options => options} %> + <% end %> +
+<%= pages_count += 1 %>楼 :
++ <%= link_to image_tag(url_to_avatar(reply.author), :class => "avatar"), user_path(reply.author) if reply.author %> + | +
+ <%=h reply.content.html_safe %>
+
+ + <% if reply.attachments.any?%> + <% options = {:author => true, :deletable => reply.deleted_attach_able_by?(User.current) } %> + <%= render :partial => 'attachments/links', :locals => {:attachments => reply.attachments, :options => options} %> + <% end %> + + |
+
<%= authoring reply.created_at, reply.author.name if reply.author %> | +