FAQ admin

This commit is contained in:
Nicolas Bally 2015-10-21 16:29:25 +02:00
parent a3d3c6063d
commit 9e3dd91a06
19 changed files with 539 additions and 1 deletions

View File

@ -0,0 +1,110 @@
# -*- encoding : utf-8 -*-
class Admin::FaqsController < ApplicationController
before_filter :auth_admin
layout "admin"
before_filter :find_faqs
def index
end
def cible
render :layout => false
end
def new
@faq = Faq.new
end
def edit
@faq = Faq.find(params[:id])
end
def create
@faq = Faq.new(faq_params)
if @faq.save
flash[:notice] = "L'faq à été ajouté avec succès."
self.find_faqs
else
render :action => "new"
end
end
def update
@faq = Faq.find(params[:id])
if params[:faq]
if @faq.update_attributes(faq_params)
flash[:notice] = "L'faq à été modifié avec succès."
else
render :action => "edit"
end
elsif params[:tag_id]
@faq.tag_by_tag_ids(params[:tag_id])
end
end
def destroy
@faq = Faq.find(params[:id])
@faq.destroy
end
protected
def find_faqs
@faqs = Faq.all
per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 5000
page = (params[:page] and params[:page] != "") ? params[:page] : 1
@faqs = @faqs.page(page).per(per_page)
#@faqs = Event.order('start_at, stop_at').after(start).before(stop)
end
private
def faq_params
params.require(:faq).permit(:author_id, :category_id, :enabled, :published_at, :title, :slug, :tags_cache, :description, :image_file_id, :title_cached)
end
end

140
app/models/faq.rb Normal file
View File

@ -0,0 +1,140 @@
class Faq < ActiveRecord::Base
belongs_to :image_file
has_one :block, :as => :blockable
after_create :after_creation
before_validation do
self.slug = self.title.to_slug
self.tags_cache = self.tags_cache.gsub(/ +/, ' ').gsub(/, /,',').gsub(/ ,/,',').gsub(/,+/, ',')
self.tags_cache = self.tags_cache.to_s.gsub(/ +/, ' ').gsub(/, /,',').gsub(/ ,/,',').gsub(/,+/, ',')
end
def after_creation
@block = Block.new(:block_name => "Contenu")
@block.blockable = self
@block.save
end
def alloweds_types
self.block.allow_types :TitleContent, :TextContent, :ImageContent, :LinkContent, :GalleryContent, :HtmlContent
end
before_save do
#self.tags_cache = nil
tag_list = self.tags_cache.split(',').uniq
tag_names = []
tag_objects_list = []
tag_list.each do |tag_name|
if tag = Tag.create(:name => tag_name.capitalize) and tag.id
self.tags << tag
else
tag = Tag.find_by_slug(tag_name.to_slug)
self.tags << tag if !self.tags.include?(tag)
end
tag_objects_list << tag
tag_names << tag.name
end
self.tags_cache = tag_names.sort.join(",").to_s
tags_to_remove = tags - tag_objects_list
tags_id_to_remove = (tags - tag_objects_list).map {|t| t.id}
self.tag_taggables.where(:tag_id => tags_id_to_remove).each { |t| t.destroy }
end
has_many :tag_taggables, :as => :taggable
has_many :tags, :through => :tag_taggables
def generate_tags_cache
self.tags_cache = self.tags.map{|a| a.name}.join(",").to_s
end
def tag_by_tag_ids(tags_id)
Tag.find(tags_id).each do |tag|
self.tags << tag if !self.tags.include?(tag)
end
self.generate_tags_cache
self.save
end
before_destroy do
self.tags_cache = ""
self.save
end
end

View File

@ -0,0 +1,22 @@
%tr#faq_row.faq_row{:id => faq.id, :class => ("warning" if !faq.enabled)}
%td=faq.title
%td{:style => "width:300px;"}
-faq.tags.order(:name).each do |tag|
.tag_label=tag.name
%td{:style => "width:140px"}
= link_to i(:"trash-o"), [:admin, faq], :confirm => 'Voulez-vous vraiment supprimer cet faq ?', :method => :delete, :remote => true
= link_to i(:pencil), edit_admin_faq_path(faq), :style => "padding:0px 0px 0px 0px;", :remote => true
= link_to i(:plus), edit_admin_faq_path(faq), :style => "padding:0px 0px 0px 0px;"
=link_to i(:"level-down"), admin_faq_path(:id => faq.id, :format => "js"), :class => "set_tag"

View File

@ -0,0 +1,24 @@
= semantic_form_for [:admin,@faq], :remote => true do |form|
.content
= form.inputs do
=form.input :enabled, :label => "Publier"
=form.input :image_file_id, :as => :qi_image_select
= form.input :title, :label => "Titre :"
=# form.input :slug, :label => "Slug :"
= form.input :description, :label => "Description courte :", :as => :text
= form.input :tags_cache, :label => "Tags :"
%script
$("#faq_tags_cache").select2({
=raw'tags:'+Tag.tag_list.to_json+','
tokenSeparators: [","]});
.actions
= form.submit "Sauvegarder", :class => "btn btn-primary"

View File

@ -0,0 +1,10 @@
.search_pannel
%table#faqs.table
=render @faqs
#pagination
= paginate @faqs, :remote => true

View File

@ -0,0 +1,13 @@
#faq_show.QI_background_middle.QI_padding_small
%table
%tr
%td{:style => "width:150px;"} Nom :
%td=@faq.title
%tr
%td{:style => "vertical-align:top"} Description courte :
%td= simple_format @faq.description

View File

@ -0,0 +1,14 @@
#tags
-Tag.order(:name).all.each do |tag|
.tag_label#tag_label{:"data-tag_id" => tag.id, :id => tag.id}
=tag.name
=ic(:"level-up")
:coffeescript
if typeof(tag_id) != 'undefined'
$(".set_tag").show()
$.each tag_id, (i,a) ->
$("#tag_label_"+a).addClass("active")

View File

@ -0,0 +1,5 @@
close_pane_hover();
$('#faqs').html("<%= escape_javascript(render(@faqs))%>");

View File

@ -0,0 +1,2 @@
$('#article_row_<%=@article.id%>').remove();

View File

@ -0,0 +1,81 @@
#toolbar-text
#menu_item_block_edit{:style => "margin-right:330px;margin-top:45px;"}
=render :partial => "admin/blocks/block", :locals => {:block => @faq.block, :sortable => true}
%br
%br
%br
%br
%br
%br
%br
%br
%br
%br
%br
#menu_item_inspector_container
#menu_item_informations
%h4
Infos sur l'faq
.panel#collapseOne{:style => "display:none;"}
=render :partial => "form"
%h4 éléments
.panel#collapse2
.block_portlets_sortable#content_types
-@faq.block.alloweds_types.each do |slug, name|
.content_type{:id => slug, :"data-type" => slug}
=#i slug.to_s.constantize.picto
=image_tag("admin/content_type/type_"+slug.to_s+".png", :alt => name, :title => name, :class => "handle")
#collapse3{:style => "display:none;"}
%h4 Modifier l'élément
.panel
#element_form
%div#element_form_action
%a.move.btn.btn-default.portlet_handle{:href => "#", :data => {:portlet_id => nil}}
%span.move_message
=ic :arrows
&nbsp;
déplacer
%span.cancel_message
=ic :ban
&nbsp;
annuler
=link_to ic(:"trash-o"), "#", :method => :delete, :data => { :confirm => "Etes-vous sûr ?"}, :remote => true, :class => "btn btn-danger trash"
%button.save.btn.btn-primary
=ic(:"floppy-o")
&nbsp;
Sauvegarder

View File

@ -0,0 +1,2 @@
show_pane_hover("<%= escape_javascript(render(:partial => "form"))%>",700,900);

View File

@ -0,0 +1,54 @@
.row{:style => "max-width:inherit;"}
.col-xs-9
.right= link_to 'Ajouter un faq', new_admin_faq_path(), :class => "btn btn-primary", :remote => true
%h1 Liste des faqs
#faqs_index=render :partial => "index_block"
.col-xs-3
%h3 Tags
=render :partial => "tags"
:coffeescript
root = exports ? this
root.tag_id = []
$(document.body).on "click", "#tags .tag_label", ->
if $(this).hasClass("active")
$(this).removeClass "active"
else
$(this).addClass "active"
root.tag_id = []
$("#tags .tag_label.active").each ->
root.tag_id.push $(this).data "tag_id"
if root.tag_id.length > 0
$(".set_tag").show()
else
$(".set_tag").hide()
$(document.body).on "click", ".set_tag", ->
$.ajax({
url:$(this).attr("href"),
type: "PUT",
data: {
tag_id : tag_id
}
})
return false

View File

@ -0,0 +1,2 @@
$('#articles_index').html("<%= escape_javascript(render(:partial => "index_block"))%>");
unset_busy();

View File

@ -0,0 +1,2 @@
show_pane_hover("<%= escape_javascript(render(:partial => "form"))%>",700,900);

View File

@ -0,0 +1,9 @@
$('#faq_row_<%= @faq.id %>').replaceWith("<%= escape_javascript(render(@faq))%>");
$('#faq_show').replaceWith("<%= escape_javascript(render(:partial => "show"))%>");
$('#tags').replaceWith("<%= escape_javascript(render(:partial => 'tags'))%>");
close_pane_hover();

View File

@ -60,6 +60,7 @@
%li.divider %li.divider
%li= link_to "Actualité", admin_articles_path %li= link_to "Actualité", admin_articles_path
%li= link_to "Agenda", admin_events_path %li= link_to "Agenda", admin_events_path
%li= link_to "FAQ", admin_faqs_path
%li.divider %li.divider
%li= link_to "Images", admin_image_files_path %li= link_to "Images", admin_image_files_path
%li= link_to "Fichiers", admin_data_files_path %li= link_to "Fichiers", admin_data_files_path

View File

@ -7,6 +7,9 @@ Survey::Application.routes.draw do
get 'actualites/:id.html' => "public/articles#show", :as => "public_article" get 'actualites/:id.html' => "public/articles#show", :as => "public_article"
get 'actualites.html' => "public/articles#index", :as => "articles" get 'actualites.html' => "public/articles#index", :as => "articles"
get 'faqs/:id.html' => "public/faqs#show", :as => "public_faq"
get 'faqs.html' => "public/faqs#index", :as => "faqs"
get "blog/archives/:year/:month.html"=> "public/articles#archives", :as => :archive_public_article get "blog/archives/:year/:month.html"=> "public/articles#archives", :as => :archive_public_article
get "blog/tags/:id.html"=> "public/articles#tags", :as => :public_tag get "blog/tags/:id.html"=> "public/articles#tags", :as => :public_tag
@ -20,10 +23,14 @@ Survey::Application.routes.draw do
get 'plaquettes/gaspillage.:f' => 'plaquettes#gaspillage', :f => "html" get 'plaquettes/gaspillage.:f' => 'plaquettes#gaspillage', :f => "html"
get 'plaquettes/:slug.:f' => 'plaquettes#show', :f => "html" get 'plaquettes/:slug.:f' => 'plaquettes#show', :f => "html"
get 'mail_assets/:token.png' => "admin/mail_trackings#update", :as => :image_tracking get 'mail_assets/:token.png' => "admin/mail_trackings#update", :as => :image_tracking
namespace :public do namespace :public do
resources :faqs
resources :orders do resources :orders do
member do member do
@ -64,8 +71,18 @@ Survey::Application.routes.draw do
end end
end end
end end
#admin #admin
namespace :admin do namespace :admin do
resources :faqs
resources :orders do resources :orders do
member do member do
get :resend get :resend

View File

@ -0,0 +1,17 @@
class CreateFaqs < ActiveRecord::Migration
def change
create_table :faqs do |t|
t.string :title
t.text :description
t.boolean :enabled
t.string :slug
t.references :image_file
t.string :tags_cache
t.string :tags_cache_slug
t.integer :category_id
t.timestamps
end
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20151021132546) do ActiveRecord::Schema.define(version: 20151021135332) do
create_table "admins", force: true do |t| create_table "admins", force: true do |t|
t.string "email", default: "", null: false t.string "email", default: "", null: false
@ -225,6 +225,19 @@ ActiveRecord::Schema.define(version: 20151021132546) do
t.datetime "updated_at" t.datetime "updated_at"
end end
create_table "faqs", force: true do |t|
t.string "title"
t.text "description"
t.boolean "enabled"
t.string "slug"
t.integer "image_file_id"
t.string "tags_cache"
t.string "tags_cache_slug"
t.integer "category_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "file_folders", force: true do |t| create_table "file_folders", force: true do |t|
t.string "name" t.string "name"
t.boolean "super_admin" t.boolean "super_admin"