diff --git a/app/controllers/libraries_controller.rb b/app/controllers/libraries_controller.rb index 16efc0c5..db5d20b0 100644 --- a/app/controllers/libraries_controller.rb +++ b/app/controllers/libraries_controller.rb @@ -8,17 +8,22 @@ class LibrariesController < ApplicationController libraries = Library.where(nil) libraries = - if params[:type] == 'mine' + if current_user.logged? && params[:type] == 'mine' libraries.where(user_id: current_user.id).order('created_at desc') else libraries.where(status: :published).order('visited_count desc') end search = params[:search].to_s.strip - libraries = libraries.where('title LIKE :search OR uuid LIKE :search', search: "%#{search}%") if search.present? + if search.present? + libraries = libraries.where('title LIKE :search OR author_name LIKE :search OR author_school_name LIKE :search', + search: "%#{search}%") + end per_page = params[:per_page].to_i <= 0 ? 20 : params[:per_page].to_i - @libraries = paginateHelper libraries.includes(user: :user_extensions), per_page + @libraries = paginateHelper libraries.includes(:library_tags, :praise_tread_cache, user: :user_extensions), per_page + + @download_count_map = Attachment.where(container_type: 'Library', container_id: @libraries.map(&:id)).group(:container_id).count end def show diff --git a/app/controllers/praise_tread_controller.rb b/app/controllers/praise_tread_controller.rb index d56ffa31..7368e0fb 100644 --- a/app/controllers/praise_tread_controller.rb +++ b/app/controllers/praise_tread_controller.rb @@ -47,6 +47,7 @@ class PraiseTreadController < ApplicationController end respond_to do |format| format.js + format.json { render_api_ok } end end end @@ -85,6 +86,7 @@ class PraiseTreadController < ApplicationController #@obj = User.find_by_id(@obj) respond_to do |format| format.js + format.json { render_api_ok } end end @@ -177,6 +179,8 @@ class PraiseTreadController < ApplicationController @obj = Challenge.find_by_id(id) when 'Discuss' @obj = Discuss.find_by_id(id) + when 'Library' + @obj = Library.find_by_id(id) else @obj = nil end diff --git a/app/helpers/libraries_helper.rb b/app/helpers/libraries_helper.rb index 60beab53..14c6ce37 100644 --- a/app/helpers/libraries_helper.rb +++ b/app/helpers/libraries_helper.rb @@ -1,8 +1,9 @@ module LibrariesHelper - def show_library_tags(library) + def show_library_tags(library, opts = {}) html = '' library.library_tags.each do |tag| - html += content_tag(:span, tag.name, class: "edu-filter-btn fl cdefault mt10 ml10 #{library_tag_class(tag)}") + klass = "edu-filter-btn fl cdefault #{library_tag_class(tag)} #{opts[:class]}" + html += content_tag(:span, tag.name, class: klass) end raw html @@ -10,7 +11,7 @@ module LibrariesHelper def library_tag_class(tag) case tag.name - when '获奖案例' then 'edu-activity-red' + when '优秀案例' then 'edu-activity-red' when '入库案例' then 'edu-activity-blue' end end diff --git a/app/models/library.rb b/app/models/library.rb index 2ff3c4c4..c0fe35ee 100644 --- a/app/models/library.rb +++ b/app/models/library.rb @@ -2,17 +2,22 @@ class Library < ActiveRecord::Base include AASM belongs_to :user + belongs_to :cover, class_name: 'Attachment', foreign_key: :cover_id has_many :library_applies, dependent: :delete_all has_many :attachments, as: :container has_many :library_library_tags, dependent: :delete_all has_many :library_tags, through: :library_library_tags - attr_accessible :title, :content + has_one :praise_tread_cache, as: :object - validates :title, presence: true + attr_accessible :title, :content, :author_name, :author_school_name, :cover_id + + validates :title, presence: true, length: { maximum: 255 } validates :content, presence: true validates :uuid, presence: true, uniqueness: true + validates :author_name, presence: true, length: { maximum: 10 } + validates :author_school_name, length: { maximum: 50 }, allow_blank: true acts_as_attachable @@ -44,7 +49,13 @@ class Library < ActiveRecord::Base self.uuid = uuid end - def increment_visited_count! - Library.connection.execute("update libraries set visited_count = COALESCE(visited_count, 0) + 1 where id = #{id}") + def increment_visited_count!(num = 1) + increment_column!(:visited_count, num) + end + + private + + def increment_column!(column, num = 1) + self.class.connection.execute("update #{self.class.table_name} set #{column} = COALESCE(#{column}, 0) + #{num} where id = #{id}") end end \ No newline at end of file diff --git a/app/services/libraries/save_service.rb b/app/services/libraries/save_service.rb index 393d3911..88a85fc8 100644 --- a/app/services/libraries/save_service.rb +++ b/app/services/libraries/save_service.rb @@ -23,20 +23,10 @@ class Libraries::SaveService library.assign_attributes(params) library.save! - new_tag_ids = LibraryTag.where(id: params[:tag_ids].presence || []).pluck(:id) - old_tag_ids = library.library_library_tags.pluck(:library_tag_id) + deal_library_tag! - # 删除标签 - destroy_ids = old_tag_ids - new_tag_ids - library.library_library_tags.where(library_tag_id: destroy_ids).delete_all - - # 创建标签 - created_ids = new_tag_ids - old_tag_ids - created_ids.each do |id| - library.library_library_tags.create!(library_tag_id: id) - end - - Attachment.where(id: attachment_ids).update_all(container_id: library.id, container_type: 'Library') + Attachment.where(id: attachment_ids, author_id: user.id) + .update_all(container_id: library.id, container_type: 'Library') end library @@ -46,5 +36,21 @@ class Libraries::SaveService def validate_params! raise Error, '附件不能为空' if params[:attachment_ids].try(:compact).blank? + raise Error , '封面不能为空' if params[:cover_id].blank? + end + + def deal_library_tag! + new_tag_ids = LibraryTag.where(id: params[:tag_ids].presence || []).pluck(:id) + old_tag_ids = library.library_library_tags.pluck(:library_tag_id) + + # 删除标签 + destroy_ids = old_tag_ids - new_tag_ids + library.library_library_tags.where(library_tag_id: destroy_ids).delete_all + + # 创建标签 + created_ids = new_tag_ids - old_tag_ids + created_ids.each do |id| + library.library_library_tags.create!(library_tag_id: id) + end end end diff --git a/app/views/libraries/_form.html.erb b/app/views/libraries/_form.html.erb index e1e6aa0b..36d09bed 100644 --- a/app/views/libraries/_form.html.erb +++ b/app/views/libraries/_form.html.erb @@ -6,6 +6,8 @@ <%= hidden_field_tag :tag_ids, tag_ids.join(',') %>
请输入姓名
+请输入作者单位名称
+请输入描述内容
请上传附件
<%= link_to library.title, library_path(library),:class => "task-hide font-22 library_l_name" %> - <%= show_library_tags(library) %> + <%= show_library_tags(library, class: 'mt10 ml10') %> + <% if params[:type] == 'mine' %> - 上传时间:<%= library.created_at.strftime('%Y-%m-%d %H:%M') %> + 上传时间:<%= library.created_at.strftime('%Y-%m-%d %H:%M') %> <% else %> - 发布时间:<%= library.published_at.try(:strftime, '%Y-%m-%d %H:%M') %> + 发布时间:<%= library.published_at.try(:strftime, '%Y-%m-%d %H:%M') %> <% end %>
作者: - <%= link_to library.user.show_real_name, user_path(library.user) %> - 学校 + <%= library.author_name %> + <%= library.author_school_name %> - <%= library.visited_count || 0 %> 浏览 - <%= library.visited_count || 0 %> 下载 + <% download_count = @download_count_map.try(:fetch, library.id, 0) || library.attachments.count %> + <% if download_count.nonzero? %> + <%= library.visited_count || 0 %> 赞 + <% end %> + <% if library.praise_tread_cache.try(:praise_num).to_i.nonzero? %> + <%= download_count %> 下载 + <% end %> <%= library.praise_tread_cache.try(:praise_num) %> 赞
0
已赞
0