From 352fdfa76c696db00a9c543280c0e1b06eea104e Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Thu, 8 May 2014 15:33:39 +0800 Subject: [PATCH] completed the plugin --- app/controllers/admin/faqs_controller.rb | 90 +++++++- app/controllers/faqs_controller.rb | 36 +++ app/models/qa.rb | 65 +++++- app/models/qa_file.rb | 14 ++ app/models/qa_link.rb | 24 ++ app/views/admin/faqs/_form.html.erb | 240 ++++++++++++++++++++ app/views/admin/faqs/_form_qa_file.html.erb | 55 +++++ app/views/admin/faqs/_form_qa_link.html.erb | 26 +++ app/views/admin/faqs/_index.html.erb | 35 +++ app/views/admin/faqs/edit.html.erb | 3 + app/views/admin/faqs/index.html.erb | 75 +++++- app/views/admin/faqs/new.html.erb | 8 + app/views/faqs/index.html.erb | 1 + app/views/faqs/show.html.erb | 1 + config/locales/en.yml | 25 ++ config/locales/zh_tw.yml | 24 ++ lib/faq/engine.rb | 12 +- 17 files changed, 728 insertions(+), 6 deletions(-) create mode 100644 app/controllers/faqs_controller.rb create mode 100644 app/models/qa_file.rb create mode 100644 app/models/qa_link.rb create mode 100644 app/views/admin/faqs/_form.html.erb create mode 100644 app/views/admin/faqs/_form_qa_file.html.erb create mode 100644 app/views/admin/faqs/_form_qa_link.html.erb create mode 100644 app/views/admin/faqs/_index.html.erb create mode 100644 app/views/admin/faqs/edit.html.erb create mode 100644 app/views/admin/faqs/new.html.erb create mode 100644 app/views/faqs/index.html.erb create mode 100644 app/views/faqs/show.html.erb create mode 100644 config/locales/en.yml create mode 100644 config/locales/zh_tw.yml diff --git a/app/controllers/admin/faqs_controller.rb b/app/controllers/admin/faqs_controller.rb index 4bf2f63..bcf6744 100644 --- a/app/controllers/admin/faqs_controller.rb +++ b/app/controllers/admin/faqs_controller.rb @@ -1,4 +1,92 @@ -class Admin::FaqsController < ApplicationController +class Admin::FaqsController < OrbitAdminController + + before_filter :setup_vars + def index + @table_fields = ["Status","Category","Title","Tags"] + @categories = @module_app.categories + @tags = @module_app.tags + @filter_fields = { + :status=>[{:title=>"is_top",:id=>"is_top"},{:title=>"is_hot",:id=>"is_hot"},{:title=>"is_hidden",:id=>"is_hidden"}], + :category=>@categories.map{|c| {:title=>c.title, :id=>c.id}}, + :tags=>@tags.map{|tag| {:title=>tag.name, :id=>tag.id}} + } + status = params[:filters][:status].blank? ? [] : params[:filters][:status] rescue [] + categories = params[:filters][:category].blank? ? [] : params[:filters][:category] rescue [] + tags = params[:filters][:tags].blank? ? [] : params[:filters][:tags] rescue [] + + @qas = Kaminari.paginate_array(Qa.order_by(sort).with_categories(categories).with_tags(tags).with_status(status)).page(params[:page]).per(10) + + if request.xhr? + render :partial => "index" + end + end + + def sort + unless params[:sort].blank? + case params[:sort] + when "status" + @sort = [[:is_top, params[:order]], + [:is_hot, params[:order]], + [:is_hidden,params[:order]]] + when "category" + @sort = {:category_id=>params[:order]} + when "title" + @sort = {:title=>params[:order]} + when "last_modified" + @sort = {:update_user_id=>params[:order]} + end + else + @sort = {:created_at=>'desc'} + end + @sort + end + + + def new + @qa = Qa.new + @tags = @module_app.tags + @categories = @module_app.categories + respond_to do |format| + format.html # new.html.erb + format.xml { render :xml => @qa } + end + end + + def create + @qa = Qa.new(create_params) + @qa.create_user_id = current_user.id + @qa.update_user_id = current_user.id + @qa.save + redirect_to admin_faqs_path + end + + def edit + @qa = Qa.find(params[:id]) + @tags = @module_app.tags + @categories = @module_app.categories + end + + def update + @qa = Qa.find(params[:id]) + @qa.update_attributes(create_params) + @qa.update_user_id = current_user.id + @qa.save + redirect_to admin_faqs_path + end + + def destroy + @qa = Qa.find(params[:id]) + @qa.destroy + redirect_to admin_faqs_path + end + + private + def setup_vars + @module_app = ModuleApp.where(:key => "faq").first + end + + def create_params + params.require(:qa).permit! end end diff --git a/app/controllers/faqs_controller.rb b/app/controllers/faqs_controller.rb new file mode 100644 index 0000000..5acb529 --- /dev/null +++ b/app/controllers/faqs_controller.rb @@ -0,0 +1,36 @@ +class FaqsController < ApplicationController + def index + faqs = Qa.filter_by_categories.collect do |qa| + { + "link_to_show" => OrbitHelper.url_to_show(qa.to_param), + "question" => qa.title + } + end + { + "data" => faqs, + "extras" => {"widget-title"=>"Faqs"} + } + end + + def show + params = OrbitHelper.params + faq = Qa.find_by_param(params[:uid]) + faqs_files = faq.qa_files.collect do |f| + { + "file_url" => f.file.url, + "file_title" => f.title + } + end + faqs_links = faq.qa_links.collect do |f| + { + "link_url" => f.url, + "link_title" => f.title + } + end + { + "extras" => {"question" => faq.title,"answer" => faq.answer}, + "faqs_links" => faqs_links, + "faqs_files" => faqs_files + } + end +end \ No newline at end of file diff --git a/app/models/qa.rb b/app/models/qa.rb index 409e92c..3bd7267 100644 --- a/app/models/qa.rb +++ b/app/models/qa.rb @@ -1,4 +1,65 @@ +# encoding: utf-8 + class Qa include Mongoid::Document - field :title, type: String -end + include Mongoid::Timestamps + + # include OrbitModel::LanguageRestrict + include OrbitModel::Status + include OrbitTag::Taggable + include OrbitCategory::Categorizable + include Slug + + field :title,as: :slug_title, localize: true + field :answer, localize: true + + field :create_user_id + field :update_user_id + field :uid, type: String + + has_many :qa_links, :autosave => true, :dependent => :destroy + has_many :qa_files, :autosave => true, :dependent => :destroy + + accepts_nested_attributes_for :qa_files, :allow_destroy => true + accepts_nested_attributes_for :qa_links, :allow_destroy => true + + # belongs_to :qa_category + + before_save :clean_values + + # validates :title, :at_least_one => true + + + def self.search( category_id = nil ) + + if category_id.to_s.size > 0 + find(:all, :conditions => {qa_category_id: category_id}).desc( :is_top, :title ) + else + find(:all).desc( :is_top, :title) + end + + end + + def self.find_by_param(input) + self.find_by(uid: input) + end + + def self.widget_datas + where( :is_hidden => false ).desc(:is_top, :created_at) + end + + protected + + def qa_category_with_title + self.category.title + end + + def clean_values + self.qa_links.each do |link| + link.delete if link.url.blank? && link.title.blank? + end + self.tags.delete('') + end + + paginates_per 10 +end \ No newline at end of file diff --git a/app/models/qa_file.rb b/app/models/qa_file.rb new file mode 100644 index 0000000..30b694e --- /dev/null +++ b/app/models/qa_file.rb @@ -0,0 +1,14 @@ +class QaFile + + include Mongoid::Document + include Mongoid::Timestamps + + mount_uploader :file, AssetUploader + + field :description, localize: true + field :should_destroy, :type => Boolean + field :title, localize: true + + belongs_to :qa + +end diff --git a/app/models/qa_link.rb b/app/models/qa_link.rb new file mode 100644 index 0000000..77a242f --- /dev/null +++ b/app/models/qa_link.rb @@ -0,0 +1,24 @@ +class QaLink + + include Mongoid::Document + include Mongoid::Timestamps + + field :url + field :title, localize: true + + field :should_destroy, :type => Boolean + + belongs_to :qa + + before_validation :add_http + # validates :url, :presence => true, :format => /^(http|https):\/\/(([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(:[0-9]{1,5})?(\/.*)?/i + + protected + + def add_http + unless self.url[/^http:\/\//] || self.url[/^https:\/\//] + self.url = 'http://' + self.url + end + end + +end diff --git a/app/views/admin/faqs/_form.html.erb b/app/views/admin/faqs/_form.html.erb new file mode 100644 index 0000000..e1d1ddb --- /dev/null +++ b/app/views/admin/faqs/_form.html.erb @@ -0,0 +1,240 @@ +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag "lib/main-forms" %> + <%= stylesheet_link_tag "lib/fileupload" %> + <%= stylesheet_link_tag "lib/main-list" %> +<% end %> +<% content_for :page_specific_javascript do %> + <%= javascript_include_tag "lib/bootstrap-fileupload" %> + <%= javascript_include_tag "lib/bootstrap-datetimepicker" %> + <%= javascript_include_tag "lib/datetimepicker/datetimepicker.js" %> + <%= javascript_include_tag "lib/modal-preview" %> + <%= javascript_include_tag "lib/file-type" %> +<% end %> +<%#= f.error_messages %> +
+ +
+ + + + + + +
+ + +
+ + +
+ <%= f.label :category ,t(:category), :class=>"control-label muted" %> +
+ <%= f.select :category_id, @categories.collect{|t| [ t.title, t.id ]} %> +
+
+ +
+ + +
+ + +
+ +
+ + + +
+
+ +
+ + +
+ + +
+ +
+ <% @tags.each do |tag| %> + + <% end %> +
+
+ +
+ +
+ + + + + + +
+<% I18n.available_locales.each_with_index do |locale, i| %> +
"> +
+ <%= f.label :title , t('faq.question'), :class=>"control-label muted" %> +
+ <%= f.fields_for :title_translations do |f| %> + <%= f.text_field locale, :class=>'post-title', :value => (@qa.title_translations[locale] rescue nil) %> + <% end %> +
+
+
+ <%= f.label :answer ,t('faq.answer'), :class => "control-label muted" %> + <%= f.fields_for :answer_translations do |f| %> +
+
+ <%= f.text_area locale, :style=>"width:100%", :class => 'ckeditor input-block-level', :value => (@qa.answer_translations[locale] rescue nil) %> +
+
+ <% end %> +
+
+<% end %> + +
+ +
+ + + <% if @qa && !@qa.qa_links.blank? %> +
+ <% @qa.qa_links.each_with_index do |qa_link, i| %> + <%= f.fields_for :qa_links, qa_link do |f| %> + <%= render :partial => 'form_qa_link', :object => qa_link, :locals => {:f => f, :i => i} %> + <% end %> + <% end %> +
+
+ <% end %> + + +
+

+ <%= hidden_field_tag 'qa_link_field_count', @qa.qa_links.count %> + <%= t(:add) %> +

+ +
+
+ + +
+ +
+ + + <% if @qa && !@qa.qa_files.blank? %> +
+ <% @qa.qa_files.each_with_index do |qa_file, i| %> + <%= f.fields_for :qa_files, qa_file do |f| %> + <%= render :partial => 'form_qa_file', :object => qa_file, :locals => {:f => f, :i => i} %> + <% end %> + <% end %> +
+
+ <% end %> + + +
+
+

+ <%= hidden_field_tag 'qa_file_field_count', @qa.qa_files.count %> + <%= t(:add) %> +

+ +
+
+
+ +
+ +
+ <%= f.submit t('submit'), class: 'btn btn-primary' %> + <%#= button_tag t("preview"), id: "button_for_preview", name: "commit", class: 'btn post_preview', type: :button, url: preview_panel_announcement_back_end_bulletins_path %> + <%#= link_to t('cancel'), get_go_back, :class=>"btn" %> +
+ +
+<% content_for :page_specific_javascript do %> + +<% end %> + diff --git a/app/views/admin/faqs/_form_qa_file.html.erb b/app/views/admin/faqs/_form_qa_file.html.erb new file mode 100644 index 0000000..3262693 --- /dev/null +++ b/app/views/admin/faqs/_form_qa_file.html.erb @@ -0,0 +1,55 @@ +<% if form_qa_file.new_record? %> +
+<% else %> +
+ <% if form_qa_file.file.blank? %> + <%= t(:no_file) %> + <% else %> + <%= link_to content_tag(:i) + form_qa_file.file_identifier, form_qa_file.file.url, {:class => 'file-link file-type', :target => '_blank', :title => form_qa_file.file_identifier} %> + <% end %> +<% end %> +
+ + + + <% I18n.available_locales.each_with_index do |locale, i| %> + <%= locale.to_s %>"> + <%= f.fields_for :title_translations do |f| %> + <%= f.text_field locale, :class => "input-medium", placeholder: t(:alternative), :value => (form_qa_file.title_translations[locale] rescue nil) %> + <% end %> + + <% end %> + + + + <% I18n.available_locales.each_with_index do |locale, i| %> + <%= locale.to_s %>"> + <%= f.fields_for :description_translations do |f| %> + <%= f.text_field locale, :class => "input-medium", placeholder: t(:description), :value => (form_qa_file.description_translations[locale] rescue nil) %> + <% end %> + + <% end %> + + + <% if form_qa_file.new_record? %> + + + + <% else %> + + <%= f.hidden_field :id %> + + <%= f.hidden_field :_destroy, :value => nil, :class => 'should_destroy' %> + + <% end %> +
+
\ No newline at end of file diff --git a/app/views/admin/faqs/_form_qa_link.html.erb b/app/views/admin/faqs/_form_qa_link.html.erb new file mode 100644 index 0000000..5911052 --- /dev/null +++ b/app/views/admin/faqs/_form_qa_link.html.erb @@ -0,0 +1,26 @@ +
+ + <%= f.text_field :url, class: "input-large", placeholder: t(:url) %> + + + <% I18n.available_locales.each_with_index do |locale, i| %> + <%= locale.to_s %>"> + <%= f.fields_for :title_translations do |f| %> + <%= f.text_field locale, :class => "input-large", placeholder: t(:url_alt), :value => (form_link.title_translations[locale] rescue nil) %> + <% end %> + + <% end %> + + + <% if form_qa_link.new_record? %> + + + + <% else %> + + <%= f.hidden_field :id %> + + <%= f.hidden_field :_destroy, :value => nil, :class => 'should_destroy' %> + + <% end %> +
diff --git a/app/views/admin/faqs/_index.html.erb b/app/views/admin/faqs/_index.html.erb new file mode 100644 index 0000000..c48ddf7 --- /dev/null +++ b/app/views/admin/faqs/_index.html.erb @@ -0,0 +1,35 @@ + + + + <% @table_fields.each do |f| %> + <%= thead(f) %> + <% end %> + + + + <% @qas.each do |qa| %> + + + + + + + <% end %> + +
+ <%= qa.status_for_table %> + + <%= qa.category.title %> + + <%= qa.title %> + + + <% qa.tags.each do |tag| %> + <%= tag.name %> + <% end %> +
\ No newline at end of file diff --git a/app/views/admin/faqs/edit.html.erb b/app/views/admin/faqs/edit.html.erb new file mode 100644 index 0000000..3dde8d7 --- /dev/null +++ b/app/views/admin/faqs/edit.html.erb @@ -0,0 +1,3 @@ +<%= form_for @qa, :url => {:action => "update"}, :html => {:class => 'form-horizontal main-forms'} do |f| %> + <%= render :partial => 'form', :locals => {:f => f} %> +<% end %> diff --git a/app/views/admin/faqs/index.html.erb b/app/views/admin/faqs/index.html.erb index 853c242..5fe2b02 100644 --- a/app/views/admin/faqs/index.html.erb +++ b/app/views/admin/faqs/index.html.erb @@ -1,2 +1,73 @@ -

Admin::Faqs#index

-

Find me in app/views/admin/faqs/index.html.erb

+<% content_for :right_nav do %> + +
+ <% @filter_fields.keys.each do |field| %> +
+
+ <% @filter_fields[field].each do |val| %> + <%= link_to t(val[:title]), "#", :onclick => "filter.addFilter('filters[#{field}][]=#{val[:id]}')", :class => "btn btn-small #{is_filter_active?(field, val[:id])}" %> + <% end %> +
+ +
+ <% end %> +
+<% end %> +
+ <%= render 'index'%> +
+ + + diff --git a/app/views/admin/faqs/new.html.erb b/app/views/admin/faqs/new.html.erb new file mode 100644 index 0000000..a68841b --- /dev/null +++ b/app/views/admin/faqs/new.html.erb @@ -0,0 +1,8 @@ + +
+<%= form_for @qa, :url => {:action=>"create"}, :html => {:class => 'form-horizontal main-forms'} do |f| %> + <%= render :partial => 'form', :locals => {:f => f} %> +<% end %> +
+<%#= link_back %> + diff --git a/app/views/faqs/index.html.erb b/app/views/faqs/index.html.erb new file mode 100644 index 0000000..648b75c --- /dev/null +++ b/app/views/faqs/index.html.erb @@ -0,0 +1 @@ +<%= render_view %> \ No newline at end of file diff --git a/app/views/faqs/show.html.erb b/app/views/faqs/show.html.erb new file mode 100644 index 0000000..648b75c --- /dev/null +++ b/app/views/faqs/show.html.erb @@ -0,0 +1 @@ +<%= render_view %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml new file mode 100644 index 0000000..e9cdb00 --- /dev/null +++ b/config/locales/en.yml @@ -0,0 +1,25 @@ +en: + + faq: + all: All + new: New + default_widget: + question: Question + title: Question + answer: Answer + to_more: Read More + qa_category_with_title: Category + faq: FAQ + widget: + index: FAQ Widget + frontend: + faq: FAQ Frontend + read_more: Read More + question: Question + answer: Answer + qa: + create_qa_category_success: FAQ category was successfully created + editing_web_resource: Editing FAQ + list_of_web_resource: FAQ list + new_web_resource: New FAQ + update_qa_category_success: FAQ was successfully updated diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml new file mode 100644 index 0000000..aa932e8 --- /dev/null +++ b/config/locales/zh_tw.yml @@ -0,0 +1,24 @@ +zh_tw: + + faq: + default_widget: + question: 問題 + title: 問題 + answer: 回答 + to_more: 閱讀更多 + qa_category_with_title: 類別 + faq: 問與答 + widget: + index: 問與答Widget + frontend: + faq: 問與答前台 + read_more: 閱讀更多 + question: 問題 + answer: 回答 + + qa: + create_qa_category_success: 問與答類別已成功建立 + editing_web_resource: 編輯問與答 + list_of_web_resource: 問與答列表 + new_web_resource: 新增問與答 + update_qa_category_success: 問與答已成功更新 diff --git a/lib/faq/engine.rb b/lib/faq/engine.rb index fb5d32c..8becbc4 100644 --- a/lib/faq/engine.rb +++ b/lib/faq/engine.rb @@ -12,8 +12,18 @@ module Faq side_bar do head_label_i18n 'faq.faq', icon_class: "icons-help" available_for [:admin,:manager,:sub_manager] - active_for_controllers ({:private=>['faq']}) + active_for_controllers (['admin/faqs']) head_link_path "admin_faqs_path" + + context_link 'faq.all', + :link_path=>"admin_faqs_path" , + :priority=>1, + :active_for_action=>{'admin/faqs'=>"index"} + + context_link 'faq.new', + :link_path=>"new_admin_faq_path" , + :priority=>2, + :active_for_action=>{'admin/faqs'=>"new"} end end end