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 @@
- <%= t(:add) %> -