Update fields and fix bugs

This commit is contained in:
manson 2014-08-11 19:18:35 +08:00
parent 27c13b3b6b
commit a08995af6f
6 changed files with 75 additions and 144 deletions

View File

@ -7,8 +7,11 @@ class Admin::BooksController < OrbitMemberController
before_action :set_types, only: [:index,:book_setting, :new, :edit, :create, :update] before_action :set_types, only: [:index,:book_setting, :new, :edit, :create, :update]
before_action :set_plugin before_action :set_plugin
before_action :need_access_right
before_action :allow_admin_only, :only => [:index, :book_setting]
def index def index
@writing_books = Book.order_by(:year=>'desc').page(params[:page]).per(10) @writing_books = Book.order_by(:year=>'desc').page(params[:page]).per(10)
respond_to do |format| respond_to do |format|
format.html # index.html.erb format.html # index.html.erb
format.js { } format.js { }
@ -17,26 +20,20 @@ class Admin::BooksController < OrbitMemberController
end end
def show def show
respond_to do |format| respond_to do |format|
format.html # show.html.erb format.html # show.html.erb
format.xml { render :xml => @book } format.xml { render :xml => @book }
end end
end end
def edit def edit
if has_access? @member = @book.member_profile
@members_data = Book.member_data rescue nil
else
render_401
end
end end
def update def update
@book_authors = @book.book_authors
respond_to do |format| respond_to do |format|
if @book.update_attributes(book_attributes) if @book.update_attributes(book_attributes)
format.html { redirect_to admin_books_path } format.html { redirect_to params['referer_url'] }
format.xml { head :ok } format.xml { head :ok }
else else
format.html { render action: "edit" } format.html { render action: "edit" }
@ -46,24 +43,15 @@ class Admin::BooksController < OrbitMemberController
end end
def new def new
if has_access? @book = Book.new
@book = Book.new @member = MemberProfile.find_by(:uid=>params['uid']) rescue nil
@members_data = Book.member_data rescue nil
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @book }
end
else
render_401
end
end end
def create def create
@book = Book.new(book_attributes) @book = Book.new(book_attributes)
respond_to do |format| respond_to do |format|
if @book.save if @book.save
format.html { redirect_to admin_books_path } format.html { redirect_to params['referer_url'] }
format.xml { render :xml => @book, :status => :created, :location => @book } format.xml { render :xml => @book, :status => :created, :location => @book }
else else
format.html { render action: "new" } format.html { render action: "new" }
@ -85,7 +73,7 @@ class Admin::BooksController < OrbitMemberController
end end
def destroy def destroy
@book.destroy @book.destroy
respond_to do |format| respond_to do |format|
format.html { redirect_to(admin_books_url) } format.html { redirect_to(admin_books_url) }
@ -94,10 +82,10 @@ class Admin::BooksController < OrbitMemberController
end end
def add_author_type def add_author_type
@set_author_type = BookAuthorType.new(display: 'List') @set_author_type = BookAuthorType.new(display: 'List')
@author_type_url = admin_book_author_types_path @author_type_url = admin_book_author_types_path
@set_author_type.id = params[:id] @set_author_type.id = params[:id]
respond_to do |format| respond_to do |format|
format.js format.js
end end
@ -106,16 +94,16 @@ class Admin::BooksController < OrbitMemberController
def edit_author_type def edit_author_type
@set_author_type = BookAuthorType.find(params[:book_id]) @set_author_type = BookAuthorType.find(params[:book_id])
@author_type_url = admin_book_author_type_path(@set_author_type) @author_type_url = admin_book_author_type_path(@set_author_type)
respond_to do |format| respond_to do |format|
format.js format.js
end end
end end
def add_book_type def add_book_type
@set_book_type = BookType.new(display: 'List') @set_book_type = BookType.new(display: 'List')
@book_type_url = admin_book_types_path @book_type_url = admin_book_types_path
@set_book_type.id = params[:id] @set_book_type.id = params[:id]
respond_to do |format| respond_to do |format|
format.js format.js
@ -123,10 +111,10 @@ class Admin::BooksController < OrbitMemberController
end end
def edit_book_type def edit_book_type
@set_book_type = BookType.find(params[:book_id]) @set_book_type = BookType.find(params[:book_id])
@book_type_url = admin_book_type_path(@set_book_type) @book_type_url = admin_book_type_path(@set_book_type)
respond_to do |format| respond_to do |format|
format.js format.js
end end
end end
@ -161,15 +149,15 @@ class Admin::BooksController < OrbitMemberController
uid = path[-2].split("-").last uid = path[-2].split("-").last
uid = uid.split("?").first uid = uid.split("?").first
end end
@book = Book.find_by(uid: uid) @book = Book.find_by(uid: uid)
end end
def book_attributes def book_attributes
params.require(:book).permit! rescue nil params.require(:book).permit! rescue nil
end end
def get_plugins def get_plugins
@plugins = OrbitApp::Plugin::Registration.all rescue nil @plugins = OrbitApp::Plugin::Registration.all rescue nil
end end
def set_plugin def set_plugin

View File

@ -15,7 +15,7 @@ class BooksController < ApplicationController
"widget-title" => t("module_name.book"), "widget-title" => t("module_name.book"),
"th_year" => t('personal_plugins.year'), "th_year" => t('personal_plugins.year'),
"th_title" => t("personal_book.book_title"), "th_title" => t("personal_book.book_title"),
"th_author" => t('personal_plugins.author') "th_author" => t('personal_book.authors')
}, },
"total_pages" => books.total_pages "total_pages" => books.total_pages
} }
@ -27,6 +27,9 @@ class BooksController < ApplicationController
fields_to_show = [ fields_to_show = [
"book_title", "book_title",
"author_name",
"authors",
"number_of_authors",
"editor", "editor",
"publisher", "publisher",
"publish_date", "publish_date",

View File

@ -11,6 +11,8 @@ class Book
field :extracted_chapters, type: String, localize: true field :extracted_chapters, type: String, localize: true
field :publisher, type: String, localize: true field :publisher, type: String, localize: true
field :editor, type: String, localize: true field :editor, type: String, localize: true
field :authors, type: String, localize: true
field :year, type: String field :year, type: String
field :language, type: String field :language, type: String
field :publish_date, type: DateTime field :publish_date, type: DateTime
@ -21,29 +23,24 @@ class Book
field :url, type: String field :url, type: String
field :note, type: String field :note, type: String
field :rss2_id, type: String field :rss2_id, type: String
field :co_authors, type: String field :number_of_authors
paginates_per 10 paginates_per 10
belongs_to :member_profile belongs_to :member_profile
has_many :book_files, autosave: true, dependent: :destroy has_many :book_files, autosave: true, dependent: :destroy
accepts_nested_attributes_for :book_files accepts_nested_attributes_for :book_files, :allow_destroy => true
has_and_belongs_to_many :book_authors
accepts_nested_attributes_for :book_authors
has_and_belongs_to_many :book_author_types has_and_belongs_to_many :book_author_types
belongs_to :book_type belongs_to :book_type
before_validation :add_http before_validation :add_http
after_save :save_book_files, :save_book_authors
def create_link def create_link
title = [] title = []
title << self.member_profile.name if self.member_profile.present? title << self.member_profile.name if self.member_profile.present?
title << "#{self.book_authors.collect{|j| j.name}.join(', ')}" if self.book_authors.present? title << self.authors if self.authors.present?
title << self.book_title if self.book_title.present? title << self.book_title if self.book_title.present?
title << self.publisher if self.publisher.present? title << self.publisher if self.publisher.present?
title << self.isbn if self.isbn.present? title << self.isbn if self.isbn.present?
@ -56,42 +53,6 @@ class Book
title.join(', ') title.join(', ')
end end
def self.member_data
members = MemberProfile.all
member_data = []
members.each do |m|
if (m.first_name || m.last_name)
member_data << {"memberId" => m.id.to_s, "memberName" => m.name}
else
member_data << {"memberId" => m.id.to_s, "memberName" => "No Name"}
end
end
member_data.to_json
end
def save_book_files
self.book_files.each do |t|
if t.should_destroy
t.destroy
end
end
end
def save_book_authors
self.book_authors.each do |t|
if t.should_destroy
t.destroy
end
end
end
def authors
authors = []
authors << self.member_profile.name if self.member_profile_id.present?
authors << (!self.book_authors.blank? ? "#{self.book_authors.collect{|j| j.name}.join(', ')}" : nil)
authors.join(', ')
end
def get_plugin_data(fields_to_show) def get_plugin_data(fields_to_show)
plugin_datas = [] plugin_datas = []
fields_to_show.each do |field| fields_to_show.each do |field|
@ -104,6 +65,14 @@ class Book
def get_plugin_field_data(field) def get_plugin_field_data(field)
case field case field
when "author_name"
value = []
([I18n.locale]+(Site.first.in_use_locales-[I18n.locale])).each do |locale|
if self.member_profile.first_name_translations[locale] || self.member_profile.last_name_translations[locale]
value << "#{self.member_profile.first_name_translations[locale]} #{self.member_profile.last_name_translations[locale]}"
end
end
value = value.join(" / ")
when "language" when "language"
value = I18n.t(self.language) rescue "" value = I18n.t(self.language) rescue ""
when "publish_date", "publication_date" when "publish_date", "publication_date"

View File

@ -35,7 +35,15 @@
<!-- Basic Module --> <!-- Basic Module -->
<div class="tab-pane fade in active" id="basic"> <div class="tab-pane fade in active" id="basic">
<!-- Category --> <div class="control-group">
<label class="control-label muted"><%= t("personal_plugins.author") %></label>
<div class="controls">
<%= @member.name rescue ''%>
<%= f.hidden_field :member_profile_id, :value => @member.id %>
</div>
</div>
<!-- author_type -->
<div class="control-group"> <div class="control-group">
<label class="control-label muted"><%= t("personal_book.author_type") %></label> <label class="control-label muted"><%= t("personal_book.author_type") %></label>
<div class="controls"> <div class="controls">
@ -48,17 +56,15 @@
</div> </div>
</div> </div>
<!-- authors--> <!-- number_of_authors -->
<div class="control-group input-title"> <div class="control-group">
<label class="control-label muted"><%= t("personal_journal.author") %></label> <label class="control-label muted"><%= t("personal_journal.number_of_authors") %></label>
<div class="controls"> <div class="controls">
<%= f.text_field :member_profile_id, id: "member_autocomplete", autocomplete: "off", data: { provide: "typeahead"}, class: "input-block-level", value: (@book.member_profile.name rescue nil), placeholder: t("personal_journal.author")%> <%= f.text_field :number_of_authors, :class=>'span1' %>
<%= f.hidden_field :member_profile_id, value: (@book.member_profile_id ? @book.member_profile_id : ""), id: "member_value" %>
</div> </div>
</div> </div>
<!-- Category --> <!-- book_paper_type -->
<div class="control-group"> <div class="control-group">
<label class="control-label muted"><%= t("personal_book.book_paper_type") %></label> <label class="control-label muted"><%= t("personal_book.book_paper_type") %></label>
<div class="controls"> <div class="controls">
@ -70,7 +76,7 @@
<div class="control-group"> <div class="control-group">
<label class="control-label muted"><%= t("personal_book.year") %></label> <label class="control-label muted"><%= t("personal_book.year") %></label>
<div class="controls"> <div class="controls">
<%= select_year((@book.year ? @book.year.to_i : DateTime.now.year), {:start_year => DateTime.now.year, :end_year => 1930}, {:name => 'writing_book[year]',:class => 'span1'} ) %> <%= select_year((@book.year ? @book.year.to_i : DateTime.now.year), {:start_year => DateTime.now.year, :end_year => 1930}, {:name => 'book[year]',:class => 'span1'} ) %>
</div> </div>
</div> </div>
@ -78,7 +84,7 @@
<div class="control-group"> <div class="control-group">
<label class="control-label muted"><%= t("personal_book.publish_date") %></label> <label class="control-label muted"><%= t("personal_book.publish_date") %></label>
<div class="controls"> <div class="controls">
<%= f.datetime_picker :publish_date, :no_label => true %> <%= f.datetime_picker :publish_date, :no_label => true,:format=>"yyyy/MM/dd", :placeholder=>"YYYY/MM/DD" %>
</div> </div>
</div> </div>
@ -86,7 +92,7 @@
<div class="control-group"> <div class="control-group">
<label class="control-label muted"><%= t("personal_book.publication_date") %></label> <label class="control-label muted"><%= t("personal_book.publication_date") %></label>
<div class="controls"> <div class="controls">
<%= f.datetime_picker :publication_date, :no_label => true %> <%= f.datetime_picker :publication_date, :no_label => true,:format=>"yyyy/MM/dd", :placeholder=>"YYYY/MM/DD" %>
</div> </div>
</div> </div>
@ -126,8 +132,11 @@
<div class="control-group"> <div class="control-group">
<label class="control-label muted"><%= t("personal_book.language") %></label> <label class="control-label muted"><%= t("personal_book.language") %></label>
<div class="controls"> <div class="controls">
<%= f.radio_button :language, "zh_tw" %> <%= t("personal_book.Chinese") %> <select id="book_language" name="book[language]">
<%= f.radio_button :language, "en" %> <%= t("personal_book.English") %> <% @site_in_use_locales.each do |locale| %>
<option value="<%= locale %>" <%= @book.language.eql?(locale.to_s) ? "selected" : ""%>><%= t(locale) %></option>
<% end %>
</select>
</div> </div>
</div> </div>
@ -219,28 +228,13 @@
<% end %> <% end %>
<!-- Authors--> <!-- authors-->
<div class="control-group"> <div class="control-group input-title">
<label class="control-label muted"><%= t("personal_journal.co-authors") %></label> <label class="control-label muted"><%= t("personal_book.authors") %></label>
<div class="controls"> <div class="controls">
<%= f.fields_for :authors_translations do |f| %>
<% if @book && !@book.book_authors.blank? %> <%= f.text_area locale, class: "input-block-level", placeholder: t("personal_book.authors"), value: (@book.authors_translations[locale] rescue nil) %>
<div class="exist">
<% @book.book_authors.each_with_index do |author, i| %>
<%= f.fields_for :book_authors, author do |f| %>
<%= render :partial => 'form_author', :object => author, :locals => {:f => f, :i => i} %>
<% end %>
<% end %>
<hr>
</div>
<% end %> <% end %>
<!-- Add -->
<div class="add-target-box">
</div>
<p class="add-btn-box">
<a id="add_author" class="trigger btn btn-small btn-primary"><i class="icons-plus"></i> <%= t(:add) %></a>
</p>
</div> </div>
</div> </div>
@ -281,16 +275,15 @@
<!-- Form Actions --> <!-- Form Actions -->
<div class="form-actions"> <div class="form-actions">
<%= f.hidden_field :user_id, :value => params[:user_id] if !params[:user_id].blank? %> <input type="hidden" name="referer_url" value="<%= request.referer %>">
<%= f.submit t('submit'), class: 'btn btn-primary' %> <%= f.submit t('submit'), class: 'btn btn-primary' %>
<%= link_to t('cancel'), get_go_back, :class=>"btn" %> <%= link_to t('cancel'), get_go_back, :class=>"btn" %>
</div> </div>
<% content_for :page_specific_javascript do %> <% content_for :page_specific_javascript do %>
<%= render 'author_autocomplete'%>
<script> <script>
$(document).ready(function() { $(document).ready(function() {
$('.main-forms .add-on').tooltip(); $('.main-forms .add-on').tooltip();
$(document).on('click', '#add_file', function(){ $(document).on('click', '#add_file', function(){
var new_id = $(this).prev().attr('value'); var new_id = $(this).prev().attr('value');
var old_id = new RegExp("new_book_files", "g"); var old_id = new RegExp("new_book_files", "g");
@ -314,30 +307,4 @@
}); });
}); });
</script> </script>
<script>
$(document).ready(function() {
$('.main-forms .add-on').tooltip();
$(document).on('click', '#add_author', function(){
var new_id = $(this).prev().attr('value');
var old_id = new RegExp("new_authors", "g");
var on = $('.language-nav li.active').index();
var le = $(this).parent('.add-btn-box').prev('.add-target-box').children('.start-line').length;
$(this).prev().attr('value', parseInt(new_id) + 1);
$(this).parent().siblings('.add-target-box').append(("<%= escape_javascript(add_attribute 'form_author', f, :book_authors) %>").replace(old_id, new_id));
$(this).parent('.add-btn-box').prev('.add-target-box').children('.start-line').eq(le).children('.input-append').find('.tab-content').each(function() {
$(this).children('.tab-pane').eq(on).addClass('in active').siblings().removeClass('in active');
});
formTip();
});
$(document).on('click', '.delete_file', function(){
$(this).parents('.input-prepend').remove();
});
$(document).on('click', '.remove_existing_record', function(){
if(confirm("<%= I18n.t(:sure?)%>")){
$(this).children('.should_destroy').attr('value', 1);
$(this).parents('.start-line').hide();
}
});
});
</script>
<% end %> <% end %>

View File

@ -9,6 +9,8 @@ en:
publisher : "Publishers" publisher : "Publishers"
editor: "Editor" editor: "Editor"
authors : "Authors" authors : "Authors"
author_name: "Author Name"
number_of_authors: "Number Of Authors"
tags : "Tags" tags : "Tags"
year : "Year" year : "Year"
language : "Language" language : "Language"

View File

@ -7,7 +7,9 @@ zh_tw:
book_title : "書名" book_title : "書名"
extracted_chapters : "部份章節" extracted_chapters : "部份章節"
publisher : "出版社" publisher : "出版社"
authors : "作者" authors : "全部作者"
author_name: "作者姓名"
number_of_authors: "著作人數"
tags : "領域" tags : "領域"
year : "年度" year : "年度"
language : "語言" language : "語言"