From a08995af6f8d51763712e8f00ae27e21e3d41e03 Mon Sep 17 00:00:00 2001 From: manson Date: Mon, 11 Aug 2014 19:18:35 +0800 Subject: [PATCH] Update fields and fix bugs --- app/controllers/admin/books_controller.rb | 60 ++++++--------- app/controllers/books_controller.rb | 5 +- app/models/book.rb | 57 ++++---------- app/views/admin/books/_form.html.erb | 91 ++++++++--------------- config/locales/en.yml | 2 + config/locales/zh_tw.yml | 4 +- 6 files changed, 75 insertions(+), 144 deletions(-) diff --git a/app/controllers/admin/books_controller.rb b/app/controllers/admin/books_controller.rb index 132a618..ecf8c1a 100644 --- a/app/controllers/admin/books_controller.rb +++ b/app/controllers/admin/books_controller.rb @@ -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,10 +82,10 @@ 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] - + respond_to do |format| format.js end @@ -106,16 +94,16 @@ class Admin::BooksController < OrbitMemberController def edit_author_type @set_author_type = BookAuthorType.find(params[:book_id]) @author_type_url = admin_book_author_type_path(@set_author_type) - + respond_to do |format| format.js end 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 diff --git a/app/controllers/books_controller.rb b/app/controllers/books_controller.rb index c788296..2a470a2 100644 --- a/app/controllers/books_controller.rb +++ b/app/controllers/books_controller.rb @@ -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", diff --git a/app/models/book.rb b/app/models/book.rb index 8532950..3f89735 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -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" diff --git a/app/views/admin/books/_form.html.erb b/app/views/admin/books/_form.html.erb index 20be761..ee73e4a 100644 --- a/app/views/admin/books/_form.html.erb +++ b/app/views/admin/books/_form.html.erb @@ -35,7 +35,15 @@
- +
+ +
+ <%= @member.name rescue ''%> + <%= f.hidden_field :member_profile_id, :value => @member.id %> +
+
+ +
@@ -48,17 +56,15 @@
- -
- + +
+
- <%= 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' %>
- +
@@ -70,7 +76,7 @@
- <%= 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'} ) %>
@@ -78,7 +84,7 @@
- <%= f.datetime_picker :publish_date, :no_label => true %> + <%= f.datetime_picker :publish_date, :no_label => true,:format=>"yyyy/MM/dd", :placeholder=>"YYYY/MM/DD" %>
@@ -86,7 +92,7 @@
- <%= f.datetime_picker :publication_date, :no_label => true %> + <%= f.datetime_picker :publication_date, :no_label => true,:format=>"yyyy/MM/dd", :placeholder=>"YYYY/MM/DD" %>
@@ -126,8 +132,11 @@
- <%= f.radio_button :language, "zh_tw" %> <%= t("personal_book.Chinese") %> - <%= f.radio_button :language, "en" %> <%= t("personal_book.English") %> +
@@ -219,28 +228,13 @@ <% end %> - -
- + +
+
- - <% if @book && !@book.book_authors.blank? %> -
- <% @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 %> -
-
+ <%= 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 %> - - -
-
-

- <%= t(:add) %> -

@@ -281,16 +275,15 @@
- <%= f.hidden_field :user_id, :value => params[:user_id] if !params[:user_id].blank? %> + <%= f.submit t('submit'), class: 'btn btn-primary' %> <%= link_to t('cancel'), get_go_back, :class=>"btn" %>
<% content_for :page_specific_javascript do %> - <%= render 'author_autocomplete'%> - <% end %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 4aaba8a..0d3f095 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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" diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 60e11ff..b18f7cd 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -7,7 +7,9 @@ zh_tw: book_title : "書名" extracted_chapters : "部份章節" publisher : "出版社" - authors : "作者" + authors : "全部作者" + author_name: "作者姓名" + number_of_authors: "著作人數" tags : "領域" year : "年度" language : "語言"