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

View File

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

View File

@ -11,6 +11,8 @@ class Book
field :extracted_chapters, type: String, localize: true
field :publisher, type: String, localize: true
field :editor, type: String, localize: true
field :authors, type: String, localize: true
field :year, type: String
field :language, type: String
field :publish_date, type: DateTime
@ -21,29 +23,24 @@ class Book
field :url, type: String
field :note, type: String
field :rss2_id, type: String
field :co_authors, type: String
field :number_of_authors
paginates_per 10
belongs_to :member_profile
has_many :book_files, autosave: true, dependent: :destroy
accepts_nested_attributes_for :book_files
has_and_belongs_to_many :book_authors
accepts_nested_attributes_for :book_authors
accepts_nested_attributes_for :book_files, :allow_destroy => true
has_and_belongs_to_many :book_author_types
belongs_to :book_type
before_validation :add_http
after_save :save_book_files, :save_book_authors
def create_link
title = []
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.publisher if self.publisher.present?
title << self.isbn if self.isbn.present?
@ -56,42 +53,6 @@ class Book
title.join(', ')
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)
plugin_datas = []
fields_to_show.each do |field|
@ -104,6 +65,14 @@ class Book
def get_plugin_field_data(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"
value = I18n.t(self.language) rescue ""
when "publish_date", "publication_date"

View File

@ -35,7 +35,15 @@
<!-- Basic Module -->
<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">
<label class="control-label muted"><%= t("personal_book.author_type") %></label>
<div class="controls">
@ -48,17 +56,15 @@
</div>
</div>
<!-- authors-->
<div class="control-group input-title">
<label class="control-label muted"><%= t("personal_journal.author") %></label>
<!-- number_of_authors -->
<div class="control-group">
<label class="control-label muted"><%= t("personal_journal.number_of_authors") %></label>
<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.hidden_field :member_profile_id, value: (@book.member_profile_id ? @book.member_profile_id : ""), id: "member_value" %>
<%= f.text_field :number_of_authors, :class=>'span1' %>
</div>
</div>
<!-- Category -->
<!-- book_paper_type -->
<div class="control-group">
<label class="control-label muted"><%= t("personal_book.book_paper_type") %></label>
<div class="controls">
@ -70,7 +76,7 @@
<div class="control-group">
<label class="control-label muted"><%= t("personal_book.year") %></label>
<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>
@ -78,7 +84,7 @@
<div class="control-group">
<label class="control-label muted"><%= t("personal_book.publish_date") %></label>
<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>
@ -86,7 +92,7 @@
<div class="control-group">
<label class="control-label muted"><%= t("personal_book.publication_date") %></label>
<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>
@ -126,8 +132,11 @@
<div class="control-group">
<label class="control-label muted"><%= t("personal_book.language") %></label>
<div class="controls">
<%= f.radio_button :language, "zh_tw" %> <%= t("personal_book.Chinese") %>
<%= f.radio_button :language, "en" %> <%= t("personal_book.English") %>
<select id="book_language" name="book[language]">
<% @site_in_use_locales.each do |locale| %>
<option value="<%= locale %>" <%= @book.language.eql?(locale.to_s) ? "selected" : ""%>><%= t(locale) %></option>
<% end %>
</select>
</div>
</div>
@ -219,28 +228,13 @@
<% end %>
<!-- Authors-->
<div class="control-group">
<label class="control-label muted"><%= t("personal_journal.co-authors") %></label>
<!-- authors-->
<div class="control-group input-title">
<label class="control-label muted"><%= t("personal_book.authors") %></label>
<div class="controls">
<% if @book && !@book.book_authors.blank? %>
<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>
<%= f.fields_for :authors_translations do |f| %>
<%= f.text_area locale, class: "input-block-level", placeholder: t("personal_book.authors"), value: (@book.authors_translations[locale] rescue nil) %>
<% 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>
@ -281,16 +275,15 @@
<!-- 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' %>
<%= link_to t('cancel'), get_go_back, :class=>"btn" %>
</div>
<% content_for :page_specific_javascript do %>
<%= render 'author_autocomplete'%>
<script>
$(document).ready(function() {
$('.main-forms .add-on').tooltip();
$('.main-forms .add-on').tooltip();
$(document).on('click', '#add_file', function(){
var new_id = $(this).prev().attr('value');
var old_id = new RegExp("new_book_files", "g");
@ -314,30 +307,4 @@
});
});
</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 %>

View File

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

View File

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