archive module: sort for backend/frontend. move get_sorted_and_filtered from orbit_backend_controller.rb to application_controller.rb
This commit is contained in:
1 parent
dd206437d8
commit
c63ac751ea
15 files changed
+352
-286
No files matched your search
@@ -3,8 +3,8 @@ class ApplicationController < ActionController::Base
|
||||
|
||||
include ParserFrontEnd, ParserBackEnd, ApplicationHelper
|
||||
include OrbitApp::ErrorHandlers::PageErrorHandler
|
||||
include OrbitApp::ErrorHandlers::ObjectAuthErrorHandler
|
||||
include OrbitApp::ErrorHandlers::ModuleAppErrorHandler
|
||||
include OrbitApp::ErrorHandlers::ObjectAuthErrorHandler
|
||||
include OrbitApp::ErrorHandlers::ModuleAppErrorHandler
|
||||
|
||||
rescue_from ObjectAuthError, :with => :render_object_auth_error
|
||||
rescue_from ModuleAppError, :with => :render_module_app_error
|
||||
@@ -34,11 +34,11 @@ class ApplicationController < ActionController::Base
|
||||
|
||||
def front_end_available(module_app_title='')
|
||||
app_controller = ModuleApp.first(conditions: {:key => module_app_title} )
|
||||
unless app_controller.enable_frontend?
|
||||
unless app_controller.enable_frontend?
|
||||
render :nothing => true
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def get_all_app_engines
|
||||
ary = ["vender/plugins/new_blog"]
|
||||
app_engines = ary.collect{|t|
|
||||
@@ -51,23 +51,23 @@ class ApplicationController < ActionController::Base
|
||||
|
||||
def flaten_controller
|
||||
ary=[]
|
||||
Find.find(File.join(Rails.root , 'vendor/plugins/')) { |name|
|
||||
Find.find(File.join(Rails.root , 'vendor/plugins/')) { |name|
|
||||
require_dependency(name) if /_controller\.rb$/ =~ name
|
||||
ary << name
|
||||
}
|
||||
ary
|
||||
end
|
||||
|
||||
|
||||
# Find the parent for the given item
|
||||
def find_parent_item
|
||||
@parent_item = Item.first(:conditions => { :id => BSON::ObjectId(params[:parent_id]) }) rescue nil
|
||||
end
|
||||
|
||||
|
||||
def auth_failed_in_backend
|
||||
#redirect_to admin_dashboards_url
|
||||
#redirect_to admin_dashboards_url
|
||||
redirect_to root_path
|
||||
end
|
||||
|
||||
|
||||
def for_admin_only
|
||||
if is_admin?
|
||||
true
|
||||
@@ -76,7 +76,7 @@ class ApplicationController < ActionController::Base
|
||||
auth_failed_in_backend
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def for_app_manager
|
||||
if is_manager?
|
||||
true
|
||||
@@ -85,7 +85,7 @@ class ApplicationController < ActionController::Base
|
||||
auth_failed_in_backend
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def for_app_sub_manager
|
||||
if (@module_app.sub_managing_users.include?(current_or_guest_user) || is_manager?)
|
||||
true
|
||||
@@ -108,7 +108,7 @@ class ApplicationController < ActionController::Base
|
||||
flash[:error] = t("access.denied.object")
|
||||
auth_failed_in_backend unless (obj.get_object_auth_by_title(title).auth_users.include?(current_or_guest_user) || is_manager? || is_admin? )
|
||||
end
|
||||
|
||||
|
||||
# Render the page
|
||||
def render_page(args={})
|
||||
if @item
|
||||
@@ -132,9 +132,9 @@ class ApplicationController < ActionController::Base
|
||||
@orig_url = "http://#{request.host_with_port}/#{@item.path}?id=#{@object.id}"
|
||||
render 'shared/render_share', :layout => false
|
||||
end
|
||||
|
||||
|
||||
protected
|
||||
|
||||
|
||||
# Set I18n.locale
|
||||
def set_locale
|
||||
# update session if passed
|
||||
@@ -148,11 +148,11 @@ class ApplicationController < ActionController::Base
|
||||
else
|
||||
browser_locale = request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first rescue nil
|
||||
condition = @site_in_use_locales.include?(browser_locale)
|
||||
end
|
||||
end
|
||||
session[:locale] = condition ? (browser_locale || session[:locale]) : I18n.default_locale.to_s
|
||||
I18n.locale = session[:locale].to_sym
|
||||
end
|
||||
|
||||
|
||||
# Set the site variables
|
||||
def set_site
|
||||
# set site if exist or create site
|
||||
@@ -161,11 +161,11 @@ class ApplicationController < ActionController::Base
|
||||
@site_in_use_locales = site_locales_default_head(@site.in_use_locales)
|
||||
@site_valid_locales = site_locales_default_head(@site.valid_locales)
|
||||
end
|
||||
|
||||
|
||||
def set_current_item
|
||||
session[:current_page] = params[:id] || @item.id rescue nil
|
||||
end
|
||||
|
||||
|
||||
def decrypt_data(encrypted_data, encrypted_key, encrypted_iv)
|
||||
site = Site.find(session[:site])
|
||||
if encrypted_data
|
||||
@@ -181,7 +181,7 @@ class ApplicationController < ActionController::Base
|
||||
''
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def get_homepage
|
||||
Page.root
|
||||
end
|
||||
@@ -229,6 +229,174 @@ class ApplicationController < ActionController::Base
|
||||
redirect_to ret
|
||||
end
|
||||
|
||||
def get_sorted_and_filtered(object_class, query = nil, objects = nil)
|
||||
objects ||= get_objects(object_class, query)
|
||||
object_class = object_class.classify.constantize
|
||||
if !params[:sort].blank?
|
||||
options = params[:sort_options]
|
||||
options = [options] if !options.class.eql?(Array)
|
||||
options.each do |option|
|
||||
if object_class.fields.include?(option)
|
||||
if object_class.fields[option].type.to_s.eql?('Object') && !object_class.relations[option].nil?
|
||||
objects = get_objects_from_referenced_objects(object_class.fields[option].options[:class_name].constantize, objects, option)
|
||||
else
|
||||
(objects = objects.order_by(option, params[:direction])) rescue nil
|
||||
end
|
||||
elsif object_class.relations.include?(option)
|
||||
case object_class.relations[option].macro
|
||||
when :references_one
|
||||
a = Array.new
|
||||
objects.each { |object| a << [get_string_value_from_object(object), object] }
|
||||
sorted = params[:direction].eql?('asc') ? a.sort : a.sort.reverse!
|
||||
objects = sorted.collect {|x| x[1] }
|
||||
when :references_many, :references_and_referenced_in_many
|
||||
objects = get_objects_from_self(object_class, objects, option)
|
||||
when :referenced_in
|
||||
objects = get_objects_from_referenced_objects(object_class.relations[option].class_name.constantize, objects, "#{option}_id")
|
||||
end
|
||||
elsif option.eql?('tags')
|
||||
tag_array = @module_app.tags.inject([]){ |result, value|
|
||||
result << [value.name, value]
|
||||
}
|
||||
params[:direction].eql?('asc') ? tag_array.sort : tag_array.sort.reverse!
|
||||
sorted_objects = Array.new
|
||||
tag_array.each do |x|
|
||||
taggings = x[1].taggings
|
||||
taggings.each {|tagging| sorted_objects << tagging.taggable }
|
||||
end
|
||||
# debugger
|
||||
sorted_objects.flatten!
|
||||
sorted_objects.uniq!
|
||||
objects = get_with_nil(objects, option, sorted_objects)
|
||||
end
|
||||
end
|
||||
end
|
||||
if @filter
|
||||
@filter.each do |key, value|
|
||||
case key
|
||||
when 'status'
|
||||
a = Array.new
|
||||
objects.each do |object|
|
||||
value.each do |v|
|
||||
a << object if object[v]
|
||||
end
|
||||
end
|
||||
objects = a.uniq
|
||||
when 'categories'
|
||||
a = Array.new
|
||||
objects.each do |object|
|
||||
a << object if (value.include?(object.send("#{object.class.to_s.underscore}_category").id.to_s) rescue nil)
|
||||
end
|
||||
objects = a.uniq
|
||||
when 'tags'
|
||||
a = Array.new
|
||||
objects.each do |object|
|
||||
object.tags.each do |tag|
|
||||
a << object if value.include?(tag.id.to_s)
|
||||
end
|
||||
end
|
||||
objects = a.uniq
|
||||
end if value.size > 0
|
||||
end
|
||||
end
|
||||
Kaminari.paginate_array(filter_authorized_objects(objects)).page(params[:page]).per(10)
|
||||
end
|
||||
|
||||
def get_viewable(object_class, query=nil)
|
||||
objects = get_objects(object_class,query).order_by(:created_at, :desc)
|
||||
Kaminari.paginate_array(objects).page(params[:page]).per(10)
|
||||
end
|
||||
|
||||
def get_objects(object_class, query=nil)
|
||||
object_class = object_class.classify.constantize
|
||||
# debugger
|
||||
# a=1
|
||||
if query
|
||||
if object_class.include?(Mongoid::Sortable)
|
||||
objects = object_class.default_sort(params[:sort]).where(query)
|
||||
else
|
||||
objects = object_class.where(query)
|
||||
end
|
||||
else
|
||||
if object_class.include?(Mongoid::Sortable)
|
||||
objects = object_class.default_sort(params[:sort]).all
|
||||
else
|
||||
objects = object_class.all
|
||||
end
|
||||
end
|
||||
objects
|
||||
end
|
||||
|
||||
def get_objects_from_referenced_objects(object_class, objects, option)
|
||||
referer_ids = objects.distinct(option)
|
||||
referenced_objects = object_class.find(referer_ids) rescue nil
|
||||
if referenced_objects
|
||||
a = Array.new
|
||||
referenced_objects.to_a.each { |referer| a << [get_string_value_from_object(referer), referer.id] }
|
||||
sorted = params[:direction].eql?('asc') ? a.sort : a.sort.reverse!
|
||||
sorted_objects = sorted.collect {|x| objects.where(option => x[1]).entries }
|
||||
sorted_objects.flatten!
|
||||
sorted_objects.uniq!
|
||||
get_with_nil(objects, option, sorted_objects)
|
||||
else
|
||||
objects
|
||||
end
|
||||
end
|
||||
|
||||
def get_objects_from_self(object_class, objects, option)
|
||||
referenced_class = object_class.relations[option].class_name.constantize
|
||||
referenced_objects = referenced_class.all rescue nil
|
||||
if referenced_objects
|
||||
reverse_relation = nil
|
||||
referenced_class.relations.each { |relation| reverse_relation = relation[1].name.to_s if relation[1].class_name.eql?(object_class.to_s) }
|
||||
a = Array.new
|
||||
referenced_objects.each { |referenced_object| a << [get_string_value_from_object(referenced_object), referenced_object] }
|
||||
a.compact!
|
||||
sorted = params[:direction].eql?('asc') ? a.sort : a.sort.reverse!
|
||||
sorted_objects = Array.new
|
||||
sorted.each {|x| sorted_objects << x[1].send(reverse_relation) }
|
||||
sorted_objects.flatten!
|
||||
sorted_objects.uniq!
|
||||
get_with_nil(objects, option, sorted_objects)
|
||||
else
|
||||
objects
|
||||
end
|
||||
end
|
||||
|
||||
def get_string_value_from_object(object)
|
||||
s = object.name_translations[I18n.locale.to_s] unless s rescue nil
|
||||
s = object.title_translations[I18n.locale.to_s] unless s rescue nil
|
||||
s = object.name unless s rescue nil
|
||||
s = object.title unless s rescue nil
|
||||
s.downcase rescue ''
|
||||
end
|
||||
|
||||
def get_with_nil(objects, option, sorted_objects)
|
||||
tmp = Array.new
|
||||
objects.each { |object| tmp << [get_string_value_from_object(object), object] if (object.send(option).blank? || (object.send(option).size == 0 rescue nil)) }
|
||||
sorted = params[:direction].eql?('asc') ? tmp.sort : tmp.sort.reverse!
|
||||
sorted_tmp = sorted.collect {|a| a[1] }
|
||||
a = params[:direction].eql?('asc') ? (sorted_tmp + sorted_objects) : (sorted_objects + sorted_tmp)
|
||||
a.flatten
|
||||
end
|
||||
|
||||
|
||||
def filter_authorized_objects(objects)
|
||||
if(!is_admin? || !is_manager?)
|
||||
objects.delete_if{ |object|
|
||||
if object.is_pending == true
|
||||
if check_permission(:manager)
|
||||
object.create_user_id != current_user.id
|
||||
else
|
||||
!object.send("#{object.class.to_s.underscore}_category").authed_users('fact_check').include?(current_user) rescue false
|
||||
end
|
||||
else
|
||||
false
|
||||
end
|
||||
}
|
||||
end
|
||||
objects
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
|
||||
@@ -4,9 +4,9 @@ class OrbitBackendController < ApplicationController
|
||||
include OrbitTag::Tagging
|
||||
include AdminHelper
|
||||
include ApplicationHelper
|
||||
|
||||
|
||||
layout 'new_admin'
|
||||
|
||||
|
||||
def setup_vars
|
||||
@app_title ||= controller_path.split('/')[1].singularize
|
||||
@module_app ||= ModuleApp.first(conditions: {:key => @app_title} )
|
||||
@@ -24,177 +24,15 @@ class OrbitBackendController < ApplicationController
|
||||
|
||||
def force_order_for_user
|
||||
setup_vars
|
||||
set_current_user
|
||||
set_current_user
|
||||
authenticate_user!
|
||||
check_user_can_use
|
||||
end
|
||||
|
||||
def check_user_can_use
|
||||
def check_user_can_use
|
||||
unless check_permission
|
||||
#redirect_to polymorphic_path(['panel',@app_title,'back_end','public'])
|
||||
redirect_to root_url
|
||||
end
|
||||
end
|
||||
|
||||
def get_sorted_and_filtered(object_class, query=nil)
|
||||
objects = get_objects(object_class, query)
|
||||
object_class = object_class.classify.constantize
|
||||
if !params[:sort].blank?
|
||||
options = params[:sort_options]
|
||||
options = [options] if !options.class.eql?(Array)
|
||||
options.each do |option|
|
||||
if object_class.fields.include?(option)
|
||||
if object_class.fields[option].type.to_s.eql?('Object') && !object_class.relations[option].nil?
|
||||
objects = get_objects_from_referenced_objects(object_class.fields[option].options[:class_name].constantize, objects, option)
|
||||
else
|
||||
(objects = objects.order_by(option, params[:direction])) rescue nil
|
||||
end
|
||||
elsif object_class.relations.include?(option)
|
||||
case object_class.relations[option].macro
|
||||
when :references_one
|
||||
a = Array.new
|
||||
objects.each { |object| a << [get_string_value_from_object(object), object] }
|
||||
sorted = params[:direction].eql?('asc') ? a.sort : a.sort.reverse!
|
||||
objects = sorted.collect {|x| x[1] }
|
||||
when :references_many, :references_and_referenced_in_many
|
||||
objects = get_objects_from_self(object_class, objects, option)
|
||||
when :referenced_in
|
||||
objects = get_objects_from_referenced_objects(object_class.relations[option].class_name.constantize, objects, "#{option}_id")
|
||||
end
|
||||
elsif option.eql?('tags')
|
||||
tag_array = @module_app.tags.inject([]){ |result, value|
|
||||
result << [value.name, value]
|
||||
}
|
||||
params[:direction].eql?('asc') ? tag_array.sort : tag_array.sort.reverse!
|
||||
sorted_objects = Array.new
|
||||
tag_array.each do |x|
|
||||
taggings = x[1].taggings
|
||||
taggings.each {|tagging| sorted_objects << tagging.taggable }
|
||||
end
|
||||
# debugger
|
||||
sorted_objects.flatten!
|
||||
sorted_objects.uniq!
|
||||
objects = get_with_nil(objects, option, sorted_objects)
|
||||
end
|
||||
end
|
||||
end
|
||||
if @filter
|
||||
@filter.each do |key, value|
|
||||
case key
|
||||
when 'status'
|
||||
a = Array.new
|
||||
objects.each do |object|
|
||||
value.each do |v|
|
||||
a << object if object[v]
|
||||
end
|
||||
end
|
||||
objects = a.uniq
|
||||
when 'categories'
|
||||
a = Array.new
|
||||
objects.each do |object|
|
||||
a << object if (value.include?(object.send("#{object.class.to_s.underscore}_category").id.to_s) rescue nil)
|
||||
end
|
||||
objects = a.uniq
|
||||
when 'tags'
|
||||
a = Array.new
|
||||
objects.each do |object|
|
||||
object.tags.each do |tag|
|
||||
a << object if value.include?(tag.id.to_s)
|
||||
end
|
||||
end
|
||||
objects = a.uniq
|
||||
end if value.size > 0
|
||||
end
|
||||
end
|
||||
Kaminari.paginate_array(filter_authorized_objects(objects)).page(params[:page]).per(10)
|
||||
end
|
||||
|
||||
def get_string_value_from_object(object)
|
||||
s = object.name_translations[I18n.locale.to_s] unless s rescue nil
|
||||
s = object.title_translations[I18n.locale.to_s] unless s rescue nil
|
||||
s = object.name unless s rescue nil
|
||||
s = object.title unless s rescue nil
|
||||
s.downcase rescue ''
|
||||
end
|
||||
|
||||
def get_objects_from_referenced_objects(object_class, objects, option)
|
||||
referer_ids = objects.distinct(option)
|
||||
referenced_objects = object_class.find(referer_ids) rescue nil
|
||||
if referenced_objects
|
||||
a = Array.new
|
||||
referenced_objects.to_a.each { |referer| a << [get_string_value_from_object(referer), referer.id] }
|
||||
sorted = params[:direction].eql?('asc') ? a.sort : a.sort.reverse!
|
||||
sorted_objects = sorted.collect {|x| objects.where(option => x[1]).entries }
|
||||
sorted_objects.flatten!
|
||||
sorted_objects.uniq!
|
||||
get_with_nil(objects, option, sorted_objects)
|
||||
else
|
||||
objects
|
||||
end
|
||||
end
|
||||
|
||||
def get_objects_from_self(object_class, objects, option)
|
||||
referenced_class = object_class.relations[option].class_name.constantize
|
||||
referenced_objects = referenced_class.all rescue nil
|
||||
if referenced_objects
|
||||
reverse_relation = nil
|
||||
referenced_class.relations.each { |relation| reverse_relation = relation[1].name.to_s if relation[1].class_name.eql?(object_class.to_s) }
|
||||
a = Array.new
|
||||
referenced_objects.each { |referenced_object| a << [get_string_value_from_object(referenced_object), referenced_object] }
|
||||
a.compact!
|
||||
sorted = params[:direction].eql?('asc') ? a.sort : a.sort.reverse!
|
||||
sorted_objects = Array.new
|
||||
sorted.each {|x| sorted_objects << x[1].send(reverse_relation) }
|
||||
sorted_objects.flatten!
|
||||
sorted_objects.uniq!
|
||||
get_with_nil(objects, option, sorted_objects)
|
||||
else
|
||||
objects
|
||||
end
|
||||
end
|
||||
|
||||
def get_with_nil(objects, option, sorted_objects)
|
||||
tmp = Array.new
|
||||
objects.each { |object| tmp << [get_string_value_from_object(object), object] if (object.send(option).blank? || (object.send(option).size == 0 rescue nil)) }
|
||||
sorted = params[:direction].eql?('asc') ? tmp.sort : tmp.sort.reverse!
|
||||
sorted_tmp = sorted.collect {|a| a[1] }
|
||||
a = params[:direction].eql?('asc') ? (sorted_tmp + sorted_objects) : (sorted_objects + sorted_tmp)
|
||||
a.flatten
|
||||
end
|
||||
|
||||
|
||||
def get_viewable(object_class, query=nil)
|
||||
objects = get_objects(object_class,query).order_by(:created_at, :desc)
|
||||
Kaminari.paginate_array(objects).page(params[:page]).per(10)
|
||||
end
|
||||
|
||||
def get_objects(object_class, query=nil)
|
||||
object_class = object_class.classify.constantize
|
||||
# debugger
|
||||
# a=1
|
||||
if query
|
||||
objects = object_class.where(query)
|
||||
else
|
||||
objects = object_class.all
|
||||
end
|
||||
objects
|
||||
end
|
||||
|
||||
def filter_authorized_objects(objects)
|
||||
if(!is_admin? || !is_manager?)
|
||||
objects.delete_if{ |object|
|
||||
if object.is_pending == true
|
||||
if check_permission(:manager)
|
||||
object.create_user_id != current_user.id
|
||||
else
|
||||
!object.send("#{object.class.to_s.underscore}_category").authed_users('fact_check').include?(current_user) rescue false
|
||||
end
|
||||
else
|
||||
false
|
||||
end
|
||||
}
|
||||
end
|
||||
objects
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user