From 3165d97417609f111dead654b601640cce04f157 Mon Sep 17 00:00:00 2001 From: nwb Date: Wed, 11 Jun 2014 16:46:13 +0800 Subject: [PATCH] =?UTF-8?q?1.=E8=BF=81=E7=A7=BB=E8=AF=BE=E7=A8=8B=E6=96=B0?= =?UTF-8?q?=E9=97=BB=E6=95=B0=E6=8D=AE=202.=E6=B7=BB=E5=8A=A0=E8=AF=BE?= =?UTF-8?q?=E7=A8=8B=E6=96=B0=E9=97=BBindex=E8=A7=86=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/news_controller.rb | 69 +++++++---- app/models/course.rb | 1 + app/models/news.rb | 1 + app/views/news/_course_news_html.erb | 94 +++++++++++++++ app/views/news/_project_news_html.erb | 108 +++++++++++++++++ app/views/news/index.html.erb | 111 +----------------- .../20140611161801_add_courseid_to_news.rb | 10 ++ 7 files changed, 263 insertions(+), 131 deletions(-) create mode 100644 app/views/news/_course_news_html.erb create mode 100644 app/views/news/_project_news_html.erb create mode 100644 db/migrate/20140611161801_add_courseid_to_news.rb diff --git a/app/controllers/news_controller.rb b/app/controllers/news_controller.rb index ee3db741d..d340e3bb2 100644 --- a/app/controllers/news_controller.rb +++ b/app/controllers/news_controller.rb @@ -38,31 +38,52 @@ class NewsController < ApplicationController @limit = 10 end - scope = @project ? @project.news.visible : News.visible - - @news_count = scope.count - @news_pages = Paginator.new @news_count, @limit, params['page'] - @offset ||= @news_pages.offset - @newss = scope.all(:include => [:author, :project], - :order => "#{News.table_name}.created_on DESC", - :offset => @offset, - :limit => @limit) - - respond_to do |format| - format.html { - @news = News.new # for adding news inline - # huang - - if @project.project_type == 1 - render :layout => 'base_courses' - else - render :layout => false if request.xhr? - end - } - format.api - format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") } + # modify by nwb + if params[:course_id] && @course==nil + @course = Course.find(params[:course_id]) end - end + if @project + scope = @project ? @project.news.visible : News.visible + + @news_count = scope.count + @news_pages = Paginator.new @news_count, @limit, params['page'] + @offset ||= @news_pages.offset + @newss = scope.all(:include => [:author, :project], + :order => "#{News.table_name}.created_on DESC", + :offset => @offset, + :limit => @limit) + + respond_to do |format| + format.html { + @news = News.new # for adding news inline + # huang + + render :layout => false if request.xhr? + } + format.api + format.atom { render_feed(@newss, :title => (@project ? @project.name : Setting.app_title) + ": #{l(:label_news_plural)}") } + end + elsif @course + scope = @course ? @course.news.visible : News.visible + + @news_count = scope.count + @news_pages = Paginator.new @news_count, @limit, params['page'] + @offset ||= @news_pages.offset + @newss = scope.all(:include => [:author, :course], + :order => "#{News.table_name}.created_on DESC", + :offset => @offset, + :limit => @limit) + + respond_to do |format| + format.html { + @news = News.new + render :layout => 'base_courses' + } + format.api + format.atom { render_feed(@newss, :title => (@course ? @course.name : Setting.app_title) + ": #{l(:label_news_plural)}") } + end + end + end def show @comments = @news.comments diff --git a/app/models/course.rb b/app/models/course.rb index eedc0dff2..545330705 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -26,6 +26,7 @@ class Course < ActiveRecord::Base has_many :enabled_modules, :dependent => :delete_all has_many :boards, :dependent => :destroy, :order => "position ASC" has_many :course_journals_for_messages, :class_name => 'CourseJournalsForMessage', :as => :jour, :dependent => :destroy + has_many :news, :dependent => :destroy, :include => :author acts_as_taggable acts_as_nested_set :order => 'name', :dependent => :destroy diff --git a/app/models/news.rb b/app/models/news.rb index f2dba2f8e..51bd7d81d 100644 --- a/app/models/news.rb +++ b/app/models/news.rb @@ -35,6 +35,7 @@ class News < ActiveRecord::Base :author_key => :author_id acts_as_watchable + after_create :add_author_as_watcher # fq after_create :act_as_activity diff --git a/app/views/news/_course_news_html.erb b/app/views/news/_course_news_html.erb new file mode 100644 index 000000000..d980cbacf --- /dev/null +++ b/app/views/news/_course_news_html.erb @@ -0,0 +1,94 @@ +<% + btn_tips = l(:label_news_notice) + label_tips = l(:label_course_news) +%> + + + <%= label_tips %> + +<%= link_to(btn_tips, + new_course_news_path(@course), + :class => 'icon icon-add', + :onclick => 'showAndScrollTo("add-news", "news_title"); return false;') %> + +<% if @course && User.current.allowed_to?(:manage_news, @course) %> + +<% end %> +
+ <% if @newss.empty? %> +

+ <%= l(:label_no_data) %> +

+ <% else %> + <% @newss.each do |news| %> + + + + + + +
<%= link_to image_tag(url_to_avatar(news.author), :class => "avatar"), user_path(news.author) %> + + + + + + + + + + + +
+ <%= link_to_user(news.author) if news.respond_to?(:author) %><%= l(:label_project_notice) %><%= link_to h(news.title), news_path(news) %> + <%= delete_link news_path(news) if User.current.allowed_to?(:manage_news, @couese) %> +
+ <%= textilizable(news, :description) %>
<%= l :label_update_time %> + : <%= format_time(news.created_on) %><%= link_to l(:label_project_newother), news_path(news) %><%= "(#{l(:label_x_comments, :count => news.comments_count)})" if news.comments_count >= 0 %>
+
+ <% end %> + <% end %> +
+ +
+ + + <% other_formats_links do |f| %> + <%= f.link_to 'Atom', :url => {:course_id => @course, :key => User.current.rss_key} %> + <% end %> + + <% content_for :header_tags do %> + <%= auto_discovery_link_tag(:atom, params.merge({:format => 'atom', :page => nil, :key => User.current.rss_key})) %> + <%= stylesheet_link_tag 'scm' %> + <% end %> + + <% html_title(l(:label_news_plural)) -%> +
+ + diff --git a/app/views/news/_project_news_html.erb b/app/views/news/_project_news_html.erb new file mode 100644 index 000000000..416b648a9 --- /dev/null +++ b/app/views/news/_project_news_html.erb @@ -0,0 +1,108 @@ +<% + if @project.project_type == Project::ProjectType_course + btn_tips = l(:label_news_notice) + label_tips = l(:label_course_news) + else + btn_tips = l(:label_news_new) + label_tips = l(:label_news) + end +%> + + + <%= label_tips %> + +<%= link_to(btn_tips, + new_project_news_path(@project), + :class => 'icon icon-add', + :onclick => 'showAndScrollTo("add-news", "news_title"); return false;') %> + +<% if @project && User.current.allowed_to?(:manage_news, @project) %> + +<% end %> +
+ <% if @newss.empty? %> +

+ <%= l(:label_no_data) %> +

+ <% else %> + <% @newss.each do |news| %> + + + + + + +
<%= link_to image_tag(url_to_avatar(news.author), :class => "avatar"), user_path(news.author) %> + + <% if @project.project_type == 1 %> + + + + <% else %> + + + + <% end %> + + + + + + + +
+ <%= link_to_user(news.author) if news.respond_to?(:author) %><%= l(:label_project_notice) %><%= link_to h(news.title), news_path(news) %> + <%= delete_link news_path(news) if User.current.allowed_to?(:manage_news, @project) %> +
+ <%= link_to_user(news.author) if news.respond_to?(:author) %><%= l(:label_project_newshare) %> <%= link_to h(news.title), news_path(news) %> + <%= delete_link news_path(news) if User.current.allowed_to?(:manage_news, @project) %> +
+ <%= textilizable(news, :description) %>
<%= l :label_update_time %> + : <%= format_time(news.created_on) %><%= link_to l(:label_project_newother), news_path(news) %><%= "(#{l(:label_x_comments, :count => news.comments_count)})" if news.comments_count >= 0 %>
+
+ <% end %> + <% end %> +
+ +
+ + + <% other_formats_links do |f| %> + <%= f.link_to 'Atom', :url => {:project_id => @project, :key => User.current.rss_key} %> + <% end %> + + <% content_for :header_tags do %> + <%= auto_discovery_link_tag(:atom, params.merge({:format => 'atom', :page => nil, :key => User.current.rss_key})) %> + <%= stylesheet_link_tag 'scm' %> + <% end %> + + <% html_title(l(:label_news_plural)) -%> +
+ + diff --git a/app/views/news/index.html.erb b/app/views/news/index.html.erb index 4dfa21d52..2635e52a6 100644 --- a/app/views/news/index.html.erb +++ b/app/views/news/index.html.erb @@ -1,108 +1,5 @@ -<% - if @project.project_type == Project::ProjectType_course - btn_tips = l(:label_news_notice) - label_tips = l(:label_course_news) - else - btn_tips = l(:label_news_new) - label_tips = l(:label_news) - end -%> - - - <%= label_tips %> - -<%= link_to(btn_tips, - new_project_news_path(@project), - :class => 'icon icon-add', - :onclick => 'showAndScrollTo("add-news", "news_title"); return false;') %> - -<% if @project && User.current.allowed_to?(:manage_news, @project) %> - +<% if @project %> + <%= render :partial => 'project_news', locals: {project: @project} %> +<% elsif @course %> + <%= render :partial => 'course_news', locals: {course: @course} %> <% end %> -
- <% if @newss.empty? %> -

- <%= l(:label_no_data) %> -

- <% else %> - <% @newss.each do |news| %> - - - - - - -
<%= link_to image_tag(url_to_avatar(news.author), :class => "avatar"), user_path(news.author) %> - - <% if @project.project_type == 1 %> - - - - <% else %> - - - - <% end %> - - - - - - - -
- <%= link_to_user(news.author) if news.respond_to?(:author) %><%= l(:label_project_notice) %><%= link_to h(news.title), news_path(news) %> - <%= delete_link news_path(news) if User.current.allowed_to?(:manage_news, @project) %> -
- <%= link_to_user(news.author) if news.respond_to?(:author) %><%= l(:label_project_newshare) %> <%= link_to h(news.title), news_path(news) %> - <%= delete_link news_path(news) if User.current.allowed_to?(:manage_news, @project) %> -
- <%= textilizable(news, :description) %>
<%= l :label_update_time %> - : <%= format_time(news.created_on) %><%= link_to l(:label_project_newother), news_path(news) %><%= "(#{l(:label_x_comments, :count => news.comments_count)})" if news.comments_count >= 0 %>
-
- <% end %> - <% end %> -
- -
- - - <% other_formats_links do |f| %> - <%= f.link_to 'Atom', :url => {:project_id => @project, :key => User.current.rss_key} %> - <% end %> - - <% content_for :header_tags do %> - <%= auto_discovery_link_tag(:atom, params.merge({:format => 'atom', :page => nil, :key => User.current.rss_key})) %> - <%= stylesheet_link_tag 'scm' %> - <% end %> - - <% html_title(l(:label_news_plural)) -%> -
- - diff --git a/db/migrate/20140611161801_add_courseid_to_news.rb b/db/migrate/20140611161801_add_courseid_to_news.rb new file mode 100644 index 000000000..9e08a446c --- /dev/null +++ b/db/migrate/20140611161801_add_courseid_to_news.rb @@ -0,0 +1,10 @@ +class AddCourseidToNews < ActiveRecord::Migration + #迁移原课程新闻数据 + def change + add_column :news, :course_id, :int + + News.where('project_id IN (SELECT id FROM projects WHERE project_type=1)').update_all("course_id = project_id") + News.where('project_id IN (SELECT id FROM projects WHERE project_type=1)').update_all(project_id: -1) + + end +end