This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
orbit-4-1/app/controllers/orbit_backend_controller.rb

189 lines
6.5 KiB
Ruby
Raw Normal View History

class OrbitBackendController< ApplicationController
before_filter :force_order,:except => [:public]
before_filter :setup_vars
before_filter :set_current_user
# before_filter {|c| c.front_end_available(@app_title)}
# before_filter :check_user_can_use
include OrbitCoreLib::PermissionUnility
2012-03-16 03:09:44 +00:00
include AdminHelper
layout 'new_admin'
def setup_vars
@app_title = request.fullpath.split('/')[2]
@app_title = request.fullpath.split('/')[1] if(@app_title == "back_end")
@app_title.gsub!(/[?].*/,'')
@module_app = ModuleApp.first(conditions: {:key => @app_title} )
end
2012-04-29 15:39:28 +00:00
private
def force_order
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)
case object_class.fields[option].type.to_s
when 'BigDecimal', 'Boolean', 'Date', 'DateTime', 'Float', 'Integer', 'String', 'Symbol', 'Time'
(objects = objects.order_by(option, params[:direction])) rescue nil
when 'Object'
objects = get_objects_from_referenced_objects(object_class.fields[option].options[:class_name].constantize, objects, option)
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
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)
s = object[I18n.locale] rescue nil
s = object.i18n_variable unless s rescue nil
s = object.name unless s rescue nil
s = object.title unless s rescue nil
if s
case s.class.to_s
when "String"
2012-05-22 12:00:07 +00:00
s.downcase rescue ''
2012-04-29 15:39:28 +00:00
when "I18nVariable"
2012-05-22 12:00:07 +00:00
s[I18n.locale].downcase rescue ''
2012-04-29 15:39:28 +00:00
else
nil
end
end
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.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
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
def get_viewable(object_class)
objects = get_objects(object_class).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
if query
2012-05-22 09:55:45 +00:00
objects = object_class.all.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