dev_bj
caishi 5 years ago
commit b65e973233

@ -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

@ -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

@ -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

@ -0,0 +1,4 @@
class LibraryLibraryTag < ActiveRecord::Base
belongs_to :library
belongs_to :library_tag
end

@ -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

@ -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

@ -1,7 +1,9 @@
<p class="lineh-25 font-22 mb20">上传教学案例</p>
<div class="library-form-container">
<%= form_for(@library) do |f| %>
<% tag_ids = @library.library_tags.map(&:id) %>
<%= hidden_field_tag :apply_publish, false %>
<%= hidden_field_tag :tag_ids, tag_ids.join(',') %>
<div class="edu-back-white">
<div class="padding30">
<div class="clearfix">
@ -26,14 +28,14 @@
<div class="clearfix mb20">
<span class="upload_Title">标签</span>
<ul class="fl libraries_tab">
<li>优秀案例</li>
<li>入库案例</li>
<% LibraryTag.where(nil).each do |tag| %>
<li class="<%= tag_ids.include?(tag.id) ? 'active' : '' %>" data-id="<%= tag.id %>"><%= tag.name %></li>
<% end %>
</ul>
</div>
<div style="padding-left: 62px">
<%= render partial: 'attachments/from_libraries', locals: { container: @library } %>
<p s
tyle="height:22px;line-height:22px;"><span class="color-red none" id="file_notice">请上传附件</span></p>
<p style="height:22px;line-height:22px;"><span class="color-red none" id="file_notice">请上传附件</span></p>
</div>
</div>
<div class="padding30 bor-top-greyE">
@ -133,6 +135,12 @@
}else{
$(this).addClass("active")
}
var ids = [];
$('.libraries_tab li.active').each(function(){
ids.push($(this).data('id'))
})
console.log('ids', ids)
$('#tag_ids').val(ids);
})
});
</script>

@ -6,8 +6,7 @@
<div class="flex1">
<p class="clearfix mb15 lineh-20">
<%= link_to library.title, library_path(library),:class => "task-hide font-16 library_l_name" %>
<span class="edu-filter-btn edu-activity-blue fl cdefault ml10">入库案例</span>
<span class="edu-filter-btn edu-activity-red fl cdefault ml10">优秀案例</span>
<%= show_library_tags(library) %>
</p>
<p class="clearfix lineh-20">
<span class="color-grey-3 mr20"><%= link_to library.user.show_real_name, user_path(library.user) %></span>

@ -15,15 +15,14 @@
<span class="font-22 fl mr10 task-hide" style="max-width: 800px">
<%= @library.title %>
</span>
<span class="edu-filter-btn edu-activity-blue fl cdefault mt3">入库案例</span>
<span class="edu-filter-btn edu-activity-red fl cdefault mt3">优秀案例</span>
<%= show_library_tags(@library) %>
<% if admin_or_self %>
<% if @library.pending? %>
<span class="fl edu-filter-btn edu-activity-green mt5">草稿</span>
<span class="fl edu-filter-btn edu-activity-green mt3 ml10">草稿</span>
<% elsif @library.processing? %>
<span class="fl edu-filter-btn edu-activity-green mt5">审核中</span>
<span class="fl edu-filter-btn edu-activity-green mt3 ml10">审核中</span>
<% elsif @library.refused? %>
<span class="fl edu-filter-btn edu-activity-orange mt5">未通过</span>
<span class="fl edu-filter-btn edu-activity-orange mt3 ml10">未通过</span>
<% end %>
<% end %>
<%= link_to('返回', libraries_path, class: 'fr color-grey-9 mt5') %>

@ -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

@ -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
Loading…
Cancel
Save