diff --git a/app/controllers/libraries_controller.rb b/app/controllers/libraries_controller.rb index 1d72f158..27e814d7 100644 --- a/app/controllers/libraries_controller.rb +++ b/app/controllers/libraries_controller.rb @@ -2,6 +2,7 @@ class LibrariesController < ApplicationController layout 'base_library' before_filter :require_login, :except => [:index] + after_filter :increment_visit_count, only: [:show, :create, :edit, :update] def index libraries = Library.where(nil) @@ -25,7 +26,6 @@ class LibrariesController < ApplicationController return render_403 unless admin_or_self? || @library.published? @library_applies = @library.library_applies.where(status: :refused).order('created_at desc') - @library.increment_visited_count! end def new @@ -39,10 +39,10 @@ class LibrariesController < ApplicationController Libraries::SubmitService.new(@library).call redirect_to publish_success_libraries_path else - flash[:message] = '保存成功' - redirect_to edit_library_path(id: @library.id) + redirect_to library_path(id: @library.id) end - rescue ActiveRecord::RecordInvalid => _ + rescue ActiveRecord::RecordInvalid => e + flash[:message] = e.record.errors.full_messages.join(',') render 'new' rescue Libraries::SubmitService::Error => ex flash[:message] = ex.message @@ -63,10 +63,10 @@ class LibrariesController < ApplicationController Libraries::SubmitService.new(@library).call redirect_to publish_success_libraries_path else - flash[:message] = '保存成功' - redirect_to edit_library_path(id: @library.id) + redirect_to library_path(id: @library.id) end - rescue ActiveRecord::RecordInvalid => _ + rescue ActiveRecord::RecordInvalid => e + flash[:message] = e.record.errors.full_messages.join(',') render 'edit' rescue Libraries::SubmitService::Error => ex flash[:message] = ex.message @@ -110,6 +110,7 @@ class LibrariesController < ApplicationController def form_params @_form_params ||= begin hash = params[:library].presence || {} + hash[:tag_ids] = params[:tag_ids].to_s.split(',') hash[:attachment_ids] = (params[:attachments].presence || []).values.map{|h| h[:attachment_id]} hash end @@ -122,4 +123,8 @@ class LibrariesController < ApplicationController def admin_or_self? current_library.user_id == current_user.id || admin_or_business? end + + def increment_visit_count + @library.increment_visited_count! if @library && @library.id + end end \ No newline at end of file diff --git a/app/helpers/libraries_helper.rb b/app/helpers/libraries_helper.rb new file mode 100644 index 00000000..433effd7 --- /dev/null +++ b/app/helpers/libraries_helper.rb @@ -0,0 +1,17 @@ +module LibrariesHelper + def show_library_tags(library) + html = '' + library.library_tags.each do |tag| + html += content_tag(:span, tag.name, class: "edu-filter-btn fl cdefault mt3 ml10 " + library_tag_class(tag)) + end + + raw html + end + + def library_tag_class(tag) + case tag.name + when '优秀案例' then 'edu-activity-red' + when '入库案例' then 'edu-activity-blue' + end + end +end \ No newline at end of file diff --git a/app/models/library.rb b/app/models/library.rb index 63df5c91..2ff3c4c4 100644 --- a/app/models/library.rb +++ b/app/models/library.rb @@ -5,6 +5,8 @@ class Library < ActiveRecord::Base 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 diff --git a/app/models/library_library_tag.rb b/app/models/library_library_tag.rb new file mode 100644 index 00000000..490daf08 --- /dev/null +++ b/app/models/library_library_tag.rb @@ -0,0 +1,4 @@ +class LibraryLibraryTag < ActiveRecord::Base + belongs_to :library + belongs_to :library_tag +end \ No newline at end of file diff --git a/app/models/library_tag.rb b/app/models/library_tag.rb new file mode 100644 index 00000000..2e356812 --- /dev/null +++ b/app/models/library_tag.rb @@ -0,0 +1,6 @@ +class LibraryTag < ActiveRecord::Base + has_many :library_library_tags, dependent: :delete_all + has_many :libraries, through: :library_library_tags + + validates :name, presence: true, uniqueness: true +end \ No newline at end of file diff --git a/app/services/libraries/save_service.rb b/app/services/libraries/save_service.rb index db244836..393d3911 100644 --- a/app/services/libraries/save_service.rb +++ b/app/services/libraries/save_service.rb @@ -23,6 +23,19 @@ 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) + + # 删除标签 + 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') end diff --git a/app/views/libraries/_form.html.erb b/app/views/libraries/_form.html.erb index 5326c38e..0bb1c30a 100644 --- a/app/views/libraries/_form.html.erb +++ b/app/views/libraries/_form.html.erb @@ -1,7 +1,9 @@
上传教学案例
请上传附件
+请上传附件
<%= link_to library.title, library_path(library),:class => "task-hide font-16 library_l_name" %> - 入库案例 - 优秀案例 + <%= show_library_tags(library) %>
<%= link_to library.user.show_real_name, user_path(library.user) %> diff --git a/app/views/libraries/show.html.erb b/app/views/libraries/show.html.erb index aa865413..d1086ef2 100644 --- a/app/views/libraries/show.html.erb +++ b/app/views/libraries/show.html.erb @@ -15,15 +15,14 @@ <%= @library.title %> - 入库案例 - 优秀案例 + <%= show_library_tags(@library) %> <% if admin_or_self %> <% if @library.pending? %> - 草稿 + 草稿 <% elsif @library.processing? %> - 审核中 + 审核中 <% elsif @library.refused? %> - 未通过 + 未通过 <% end %> <% end %> <%= link_to('返回', libraries_path, class: 'fr color-grey-9 mt5') %> diff --git a/db/migrate/20190705071020_create_library_tags.rb b/db/migrate/20190705071020_create_library_tags.rb new file mode 100644 index 00000000..62b94c01 --- /dev/null +++ b/db/migrate/20190705071020_create_library_tags.rb @@ -0,0 +1,9 @@ +class CreateLibraryTags < ActiveRecord::Migration + def change + create_table :library_tags do |t| + t.string :name + end + + execute "insert into library_tags(name) values('优秀案例'),('入库案例')" + end +end diff --git a/db/migrate/20190705071810_create_library_library_tags.rb b/db/migrate/20190705071810_create_library_library_tags.rb new file mode 100644 index 00000000..3839fc19 --- /dev/null +++ b/db/migrate/20190705071810_create_library_library_tags.rb @@ -0,0 +1,8 @@ +class CreateLibraryLibraryTags < ActiveRecord::Migration + def change + create_table :library_library_tags do |t| + t.references :library + t.references :library_tag + end + end +end