orbit-basic/app/controllers/orbit_backend_controller.rb

200 lines
6.9 KiB
Ruby
Raw Normal View History

class OrbitBackendController < ApplicationController
2012-12-03 10:52:36 +00:00
include OrbitCoreLib::AppBackendUtility
include OrbitCoreLib::PermissionUtility
2013-04-26 03:18:57 +00:00
include OrbitTag::Tagging
2012-03-16 03:09:44 +00:00
include AdminHelper
2012-08-23 08:05:14 +00:00
include ApplicationHelper
layout 'new_admin'
def setup_vars
2012-11-20 07:47:07 +00:00
@app_title ||= controller_path.split('/')[1].singularize
@module_app ||= ModuleApp.first(conditions: {:key => @app_title} )
2013-01-03 02:58:52 +00:00
raise ModuleAppError, 'Can not find ModuleApp' if @module_app.nil?
end
2012-04-29 15:39:28 +00:00
private
2012-08-23 08:05:14 +00:00
def force_order_for_visitor
check_backend_openness
setup_vars
set_current_user
2012-08-23 08:05:14 +00:00
end
def force_order_for_user
setup_vars
set_current_user
authenticate_user!
check_user_can_use
end
def check_user_can_use
unless check_permission
2012-04-26 08:45:46 +00:00
#redirect_to polymorphic_path(['panel',@app_title,'back_end','public'])
redirect_to root_url
end
end
2012-04-29 15:39:28 +00:00
def get_sorted_and_filtered(object_class, query=nil)
2012-05-18 19:19:55 +00:00
objects = get_objects(object_class, query)
2012-05-22 09:55:45 +00:00
object_class = object_class.classify.constantize
2012-04-29 15:39:28 +00:00
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)
2012-07-25 18:27:43 +00:00
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
2012-04-29 15:39:28 +00:00
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
2013-04-26 03:18:57 +00:00
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)
2012-04-29 15:39:28 +00:00
end
end
end
if @filter
@filter.each do |key, value|
case key
when 'status'
a = Array.new
2012-05-02 09:55:02 +00:00
objects.each do |object|
2012-04-29 15:39:28 +00:00
value.each do |v|
2012-05-02 09:55:02 +00:00
a << object if object[v]
2012-04-29 15:39:28 +00:00
end
end
objects = a.uniq
when 'categories'
a = Array.new
2012-05-02 09:55:02 +00:00
objects.each do |object|
2012-05-22 12:00:07 +00:00
a << object if (value.include?(object.send("#{object.class.to_s.underscore}_category").id.to_s) rescue nil)
2012-04-29 15:39:28 +00:00
end
objects = a.uniq
when 'tags'
a = Array.new
2012-05-02 09:55:02 +00:00
objects.each do |object|
object.tags.each do |tag|
a << object if value.include?(tag.id.to_s)
2012-04-29 15:39:28 +00:00
end
end
objects = a.uniq
end if value.size > 0
end
end
2012-05-22 09:55:45 +00:00
Kaminari.paginate_array(filter_authorized_objects(objects)).page(params[:page]).per(10)
2012-04-29 15:39:28 +00:00
end
def get_string_value_from_object(object)
2012-07-25 18:27:43 +00:00
s = object.name_translations[I18n.locale.to_s] unless s rescue nil
s = object.title_translations[I18n.locale.to_s] unless s rescue nil
2012-04-29 15:39:28 +00:00
s = object.name unless s rescue nil
s = object.title unless s rescue nil
2012-07-25 18:27:43 +00:00
s.downcase rescue ''
2012-04-29 15:39:28 +00:00
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] }
2012-04-29 15:39:28 +00:00
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
2012-04-30 06:23:41 +00:00
objects.each { |object| tmp << [get_string_value_from_object(object), object] if (object.send(option).blank? || (object.send(option).size == 0 rescue nil)) }
2012-04-29 15:39:28 +00:00
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
2012-05-18 19:19:55 +00:00
2012-07-06 10:11:55 +00:00
def get_viewable(object_class, query=nil)
objects = get_objects(object_class,query).order_by(:created_at, :desc)
2012-05-18 19:19:55 +00:00
Kaminari.paginate_array(objects).page(params[:page]).per(10)
end
def get_objects(object_class, query=nil)
object_class = object_class.classify.constantize
2012-07-06 10:11:55 +00:00
# debugger
# a=1
2012-05-18 19:19:55 +00:00
if query
2012-07-06 10:11:55 +00:00
objects = object_class.where(query)
2012-05-18 19:19:55 +00:00
else
2012-05-22 09:55:45 +00:00
objects = object_class.all
2012-05-18 19:19:55 +00:00
end
2012-05-22 09:55:45 +00:00
objects
end
def filter_authorized_objects(objects)
2012-05-18 19:19:55 +00:00
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
2012-05-18 19:19:55 +00:00
}
end
objects
end
end