From e2fa75f424b874c6795267d27ccc950ee64b4fe4 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Wed, 30 Dec 2015 18:12:32 +0800 Subject: [PATCH] added categories --- .../admin/research_categories_controller.rb | 48 ++++++++++++++++ app/controllers/admin/researchs_controller.rb | 1 + .../personal_researches_controller.rb | 1 + app/models/research.rb | 36 ++++++++---- app/models/research_category.rb | 9 +++ .../admin/research_categories/_form.html.erb | 24 ++++++++ .../admin/research_categories/create.js.erb | 2 + .../admin/research_categories/destroy.js.erb | 2 + .../admin/research_categories/edit.js.erb | 1 + .../admin/research_categories/new.js.erb | 1 + .../admin/research_categories/update.js.erb | 2 + app/views/admin/researchs/_form.html.erb | 10 +++- app/views/admin/researchs/_research.html.erb | 1 + .../researchs/_research_category.html.erb | 8 +++ app/views/admin/researchs/index.html.erb | 2 + app/views/admin/researchs/setting.html.erb | 55 ++++++++++++++++++- .../personal_research/_profile.html.erb | 2 + config/locales/en.yml | 1 + config/locales/zh_tw.yml | 1 + config/routes.rb | 2 + 20 files changed, 197 insertions(+), 12 deletions(-) create mode 100644 app/controllers/admin/research_categories_controller.rb create mode 100644 app/models/research_category.rb create mode 100644 app/views/admin/research_categories/_form.html.erb create mode 100644 app/views/admin/research_categories/create.js.erb create mode 100644 app/views/admin/research_categories/destroy.js.erb create mode 100644 app/views/admin/research_categories/edit.js.erb create mode 100644 app/views/admin/research_categories/new.js.erb create mode 100644 app/views/admin/research_categories/update.js.erb create mode 100644 app/views/admin/researchs/_research_category.html.erb diff --git a/app/controllers/admin/research_categories_controller.rb b/app/controllers/admin/research_categories_controller.rb new file mode 100644 index 0000000..fd0590c --- /dev/null +++ b/app/controllers/admin/research_categories_controller.rb @@ -0,0 +1,48 @@ +class Admin::ResearchCategoriesController < OrbitMemberController + + def new + @research_category = ResearchCategory.new + @url = admin_research_categories_path + end + + def create + ResearchCategory.create(r_c_params) + @research_categories = ResearchCategory.all.asc(:sort_postion) + end + + def edit + @research_category = ResearchCategory.find(params[:id]) rescue nil + @url = admin_research_category_path(@research_category) + end + + def update + research_category = ResearchCategory.find(params[:id]) rescue nil + if !research_category.nil? + research_category.update_attributes(r_c_params) + research_category.save + end + @research_categories = ResearchCategory.all.asc(:sort_postion) + end + + def destroy + research_category = ResearchCategory.find(params[:id]) rescue nil + research_category.destroy if !research_category.nil? + @research_categories = ResearchCategory.all.asc(:sort_postion) + end + + def update_order + orders = params["order"] + ResearchCategory.each do |rc| + rc.sort_position = orders["#{rc.id}"] + rc.save + end + render :json => {"success" => true}.to_json + end + + private + + def r_c_params + params.require(:research_category).permit! + end + +end \ No newline at end of file diff --git a/app/controllers/admin/researchs_controller.rb b/app/controllers/admin/researchs_controller.rb index 6cc4c8b..87bd51b 100644 --- a/app/controllers/admin/researchs_controller.rb +++ b/app/controllers/admin/researchs_controller.rb @@ -155,6 +155,7 @@ class Admin::ResearchsController < OrbitMemberController end def get_settings + @research_categories = ResearchCategory.all.asc(:sort_position) end def set_plugin diff --git a/app/controllers/personal_researches_controller.rb b/app/controllers/personal_researches_controller.rb index 6dae448..55ef69e 100644 --- a/app/controllers/personal_researches_controller.rb +++ b/app/controllers/personal_researches_controller.rb @@ -29,6 +29,7 @@ class PersonalResearchesController < ApplicationController plugin = Research.where(:is_hidden=>false).find_by(uid: params[:uid]) fields_to_show =[ "year", + "research_category", "publish_date", "research_title", "authors", diff --git a/app/models/research.rb b/app/models/research.rb index 0a72f9a..84de7fe 100644 --- a/app/models/research.rb +++ b/app/models/research.rb @@ -23,6 +23,8 @@ class Research paginates_per 10 has_many :research_files, :autosave => true, :dependent => :destroy + + belongs_to :research_category accepts_nested_attributes_for :research_files, :allow_destroy => true @@ -44,34 +46,46 @@ class Research fields_to_show = [ "year", + "research_category", "research_title", "publish_date", "authors" ] - pd_title = fields_to_show.collect do |t| - { - "plugin_data_title" => I18n.t("personal_research.#{t}") - } + fields_to_remove = [] + + pd_title = [] + + fields_to_show.each do |t| + fields_to_remove << t if datas.where(t.to_sym.ne => nil).count == 0 + pd_title << { + "plugin_data_title" => I18n.t("personal_research.#{t}") + } if !fields_to_remove.include?(t) end - plugin_datas = datas.sort_for_frontend.collect do |p| + fields_to_show = fields_to_show - fields_to_remove + + plugin_datas = datas.sort_for_frontend.collect.with_index do |p, index| pd_data = [] fields_to_show.collect do |t| if t == "research_title" - pd_data << { "data_title" => "#{p.send(t)}" } + pd_data << { "data_title" => "#{p.send(t)}" } + elsif t == "research_category" + pd_data << {"data_title" => (p.research_category.title rescue "")} else pd_data << { "data_title" => p.send(t) } end end { - "pd_datas" => pd_data + "pd_datas" => pd_data, + "type-sort" => (p.research_category.sort_position.to_i rescue -1), + "sort-index" => index } end - + plugin_datas = plugin_datas.sort_by{|pd| [pd["type-sort"], pd["sort-index"]]} return [pd_title,plugin_datas] end @@ -79,15 +93,17 @@ class Research def get_plugin_field_data(field) case field when "language" - value = I18n.t(self.language) rescue "" + value = self.language.nil? ? "" : I18n.t(self.language) rescue "" when "publish_date" value = self.publish_date.strftime("%Y-%m") rescue "" + when "research_category" + value = self.research_category.title rescue "" when "file" files = [] self.research_files.each do |research_file| url = research_file.file.url title = (research_file.title.blank? ? File.basename(research_file.file.path) : research_file.title) - files << "
  • #{title}
  • " + files << "
  • #{title}
  • " end value = files.join("") else diff --git a/app/models/research_category.rb b/app/models/research_category.rb new file mode 100644 index 0000000..d01f9fa --- /dev/null +++ b/app/models/research_category.rb @@ -0,0 +1,9 @@ +class ResearchCategory + include Mongoid::Document + include Mongoid::Timestamps + + field :title, localize: true + field :sort_position, type: Integer, default: 0 + + has_many :researches +end \ No newline at end of file diff --git a/app/views/admin/research_categories/_form.html.erb b/app/views/admin/research_categories/_form.html.erb new file mode 100644 index 0000000..2f369d0 --- /dev/null +++ b/app/views/admin/research_categories/_form.html.erb @@ -0,0 +1,24 @@ +<%= form_for(@research_category, :html =>{:class=>"form-horizontal", :style=>"margin: 0;"}, :remote => true, :url => @url ) do |f| %> + + + + + +<% end %> \ No newline at end of file diff --git a/app/views/admin/research_categories/create.js.erb b/app/views/admin/research_categories/create.js.erb new file mode 100644 index 0000000..8731765 --- /dev/null +++ b/app/views/admin/research_categories/create.js.erb @@ -0,0 +1,2 @@ +$("#research_categories tbody").html("<%= j render :partial => '/admin/researchs/research_category', :collection => @research_categories %>"); +$("#research_category_modal").modal("hide"); \ No newline at end of file diff --git a/app/views/admin/research_categories/destroy.js.erb b/app/views/admin/research_categories/destroy.js.erb new file mode 100644 index 0000000..8731765 --- /dev/null +++ b/app/views/admin/research_categories/destroy.js.erb @@ -0,0 +1,2 @@ +$("#research_categories tbody").html("<%= j render :partial => '/admin/researchs/research_category', :collection => @research_categories %>"); +$("#research_category_modal").modal("hide"); \ No newline at end of file diff --git a/app/views/admin/research_categories/edit.js.erb b/app/views/admin/research_categories/edit.js.erb new file mode 100644 index 0000000..7136add --- /dev/null +++ b/app/views/admin/research_categories/edit.js.erb @@ -0,0 +1 @@ +$("#research_category_modal").html("<%= j render 'form' %>"); \ No newline at end of file diff --git a/app/views/admin/research_categories/new.js.erb b/app/views/admin/research_categories/new.js.erb new file mode 100644 index 0000000..7136add --- /dev/null +++ b/app/views/admin/research_categories/new.js.erb @@ -0,0 +1 @@ +$("#research_category_modal").html("<%= j render 'form' %>"); \ No newline at end of file diff --git a/app/views/admin/research_categories/update.js.erb b/app/views/admin/research_categories/update.js.erb new file mode 100644 index 0000000..8731765 --- /dev/null +++ b/app/views/admin/research_categories/update.js.erb @@ -0,0 +1,2 @@ +$("#research_categories tbody").html("<%= j render :partial => '/admin/researchs/research_category', :collection => @research_categories %>"); +$("#research_category_modal").modal("hide"); \ No newline at end of file diff --git a/app/views/admin/researchs/_form.html.erb b/app/views/admin/researchs/_form.html.erb index ad2febb..27b4187 100644 --- a/app/views/admin/researchs/_form.html.erb +++ b/app/views/admin/researchs/_form.html.erb @@ -160,13 +160,21 @@ -
    +
    <%= f.datetime_picker :publish_date, :no_label => true, :format=>"yyyy/MM", :value => @research.publish_date, :new_record => @research.new_record? %>
    + +
    + +
    + <%= f.select :research_category_id, ResearchCategory.all.collect {|t| [ t.title, t.id ]} %> +
    +
    +
    diff --git a/app/views/admin/researchs/_research.html.erb b/app/views/admin/researchs/_research.html.erb index 4d3af23..6dc0356 100644 --- a/app/views/admin/researchs/_research.html.erb +++ b/app/views/admin/researchs/_research.html.erb @@ -12,5 +12,6 @@
    <%= research.member_profile.name rescue "" %> + <%= research.research_category.title rescue "" %> <% end %> \ No newline at end of file diff --git a/app/views/admin/researchs/_research_category.html.erb b/app/views/admin/researchs/_research_category.html.erb new file mode 100644 index 0000000..8e74e89 --- /dev/null +++ b/app/views/admin/researchs/_research_category.html.erb @@ -0,0 +1,8 @@ + + <%= research_category.title %> + + + <%= t(:edit) %> + <%= link_to t(:delete_), admin_research_category_path(research_category), "data-confirm" => t('sure?'), :method => :delete, :remote => true,:class=>"archive_toggle action" %> + + \ No newline at end of file diff --git a/app/views/admin/researchs/index.html.erb b/app/views/admin/researchs/index.html.erb index 207d003..9fc758b 100644 --- a/app/views/admin/researchs/index.html.erb +++ b/app/views/admin/researchs/index.html.erb @@ -5,6 +5,7 @@ <%= t('personal_research.publication_date') %> <%= t('personal_research.research_title') %> <%= t('users.name') %> + <%= t('personal_research.research_category') %> @@ -29,6 +30,7 @@
    <%= link_to content_tag(:i, nil, :class => 'icon-plus icon-white') + t(:new_), new_admin_research_path, :class => 'btn btn-primary' %> + <%= link_to content_tag(:i, nil, :class => 'icon-cog icon-white') + t('setting'), admin_research_setting_path, :class => 'btn btn-primary pull-right' %> + <%= research.research_category.title rescue "" %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 7665fa2..905f206 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -9,6 +9,7 @@ en: authors : "Authors" tags : "Tags" year : "Year" + research_category: Category language : "Language" isbn : "ISSN(ISBN)" vol_no : "Vol.No" diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 31a18fa..33ed1a4 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -10,6 +10,7 @@ zh_tw: tags : "領域" year : "年度" language : "語言" + research_category: Category isbn : "ISSN(ISBN)" vol_no : "卷數" issue_no : "期數" diff --git a/config/routes.rb b/config/routes.rb index bc25b26..c6cec9f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -25,6 +25,8 @@ Rails.application.routes.draw do end end + resources :research_categories + post "research_categories/update_order" => "research_categories#update_order" end end end