From 083c3bef7cb9d70bb77db26c2fc2767efaad1e30 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Fri, 1 Apr 2016 13:56:20 +0800 Subject: [PATCH] field now modifiable --- app/controllers/personal_books_controller.rb | 64 ++++++++++++------- app/models/book.rb | 20 +++--- .../get_fields_for_index.html.erb | 48 ++++++++++++++ config/routes.rb | 2 + lib/personal_book/engine.rb | 2 +- 5 files changed, 103 insertions(+), 33 deletions(-) create mode 100644 app/views/personal_books/get_fields_for_index.html.erb diff --git a/app/controllers/personal_books_controller.rb b/app/controllers/personal_books_controller.rb index b40b0af..8eadc90 100644 --- a/app/controllers/personal_books_controller.rb +++ b/app/controllers/personal_books_controller.rb @@ -5,6 +5,7 @@ class PersonalBooksController < ApplicationController page = Page.where(:page_id => params[:page_id]).first rescue nil if page.custom_string_field == "table" + fields_to_show = page.custom_array_field rescue [] fields_to_show = [ "authors", "book_title", @@ -12,7 +13,7 @@ class PersonalBooksController < ApplicationController "publisher", "isbn", "publish_date" - ] + ] if fields_to_show.blank? else fields_to_show = [ "year", @@ -79,34 +80,49 @@ class PersonalBooksController < ApplicationController "number_of_authors", "isbn", "url", - "file", "publication_date", "language" ] {"plugin_datas"=>plugin.get_plugin_data(fields_to_show)} + end - # publication_date = book.publication_date.to_date.strftime("%Y/%m/%d") rescue nil - # publish_date = book.publish_date.to_date.strftime("%Y/%m/%d") rescue nil - # files = book.book_files.map{|file| { "file_url" => file.member_book_file.url, "file_title" => (file.title.blank? ? File.basename(file.file.path) : file.title) } } rescue [] - # { - # "book_files" => files, - # "data" => { - # "title" => book.book_title, - # "year" => book.year, - # "authors" => book.authors, - # "isbn" => book.isbn, - # "language" => book.language, - # "pages" => book.pages, - # "keywords" => book.keywords, - # "publication_date" => publication_date , - # "url" => book.url, - # "note" => book.note, - # "extracted_chapters" => book.extracted_chapters, - # "publish_date" => publish_date, - # "publisher" => book.publisher, - # "editor" => book.editor - # } - # } + def get_fields_for_index + @page = Page.find(params[:page_id]) rescue nil + @fields_to_show = [ + "year", + "book_title", + "authors", + "book_paper_type", + "extracted_chapters", + "publisher", + "publish_date", + "pages", + "editor", + "author_type", + "number_of_authors", + "isbn", + "url", + "file", + "publication_date", + "language" + ] + @fields_to_show = @fields_to_show.map{|fs| [t("personal_book.#{fs}"), fs]} + @default_fields_to_show = [ + "authors", + "book_title", + "extracted_chapters", + "publisher", + "isbn", + "publish_date" + ] + render :layout => false + end + + def save_index_fields + page = Page.find(params[:page_id]) rescue nil + page.custom_array_field = params[:keys] + page.save + render :json => {"success" => true}.to_json end end diff --git a/app/models/book.rb b/app/models/book.rb index 47f0045..19da010 100644 --- a/app/models/book.rb +++ b/app/models/book.rb @@ -64,14 +64,18 @@ class Book def self.get_plugin_datas_to_member(datas) page = Page.where(:module => "personal_book").first rescue nil if !page.nil? && page.custom_string_field == "table" - fields_to_show = [ - "authors", - "book_title", - "extracted_chapters", - "publisher", - "isbn", - "publish_date" - ] + if !page.custom_array_field.blank? + fields_to_show = page.custom_array_field + else + fields_to_show = [ + "authors", + "book_title", + "extracted_chapters", + "publisher", + "isbn", + "publish_date" + ] + end else fields_to_show = [ "year", diff --git a/app/views/personal_books/get_fields_for_index.html.erb b/app/views/personal_books/get_fields_for_index.html.erb new file mode 100644 index 0000000..d904591 --- /dev/null +++ b/app/views/personal_books/get_fields_for_index.html.erb @@ -0,0 +1,48 @@ +<% if !@page.nil? %> +
+
+
    + <% if @page.custom_array_field.blank? %> + <% @default_fields_to_show.each do |fs| %> +
  • <%= t("personal_book.#{fs}") %>
  • + <% end %> + <% else %> + <% @page.custom_array_field.each do |fs| %> +
  • <%= t("personal_book.#{fs}") %>
  • + <% end %> + <% end %> +
+
+ +
+ +
+ +
+ <%= select_tag "fields_to_show_for_pp", options_for_select(@fields_to_show), prompt: "---Select something---" %> +
+
+ Add Field + + +
+
+ +<% else %> +

Page not found.

+<% end %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index d69d426..a1c4668 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -28,5 +28,7 @@ Rails.application.routes.draw do resources :book_types resources :book_intros end + get "/xhr/personal_book/get_fields_for_index" => "personal_books#get_fields_for_index" + post "/xhr/personal_book/save_index_fields" => "personal_books#save_index_fields" end end diff --git a/lib/personal_book/engine.rb b/lib/personal_book/engine.rb index 81877c4..1148de1 100644 --- a/lib/personal_book/engine.rb +++ b/lib/personal_book/engine.rb @@ -4,7 +4,7 @@ module PersonalBook OrbitApp.registration "PersonalBook",:type=> 'ModuleApp' do module_label 'module_name.book' base_url File.expand_path File.dirname(__FILE__) - personal_plugin :enable => true, :sort_number => '25', :app_name=>"Book", :intro_app_name=>"BookIntro",:path=>"/plugin/personal_book/profile",:front_path=>"/profile",:admin_path=>"/admin/books",:i18n=>'module_name.book',:module_app_name=>'PersonalBook', :one_line_title => true + personal_plugin :enable => true, :sort_number => '25', :app_name=>"Book", :intro_app_name=>"BookIntro",:path=>"/plugin/personal_book/profile",:front_path=>"/profile",:admin_path=>"/admin/books",:i18n=>'module_name.book',:module_app_name=>'PersonalBook', :one_line_title => true, :field_modifiable => true version "0.1" desktop_enabled true