orbit-basic/app/controllers/default_widget_controller.rb

88 lines
3.3 KiB
Ruby

class DefaultWidgetController< OrbitWidgetController
def front_end_available(var)
@page_part = case params[:item_type]
when "page"
Page.find params[:page_id]
when "page_part"
PagePart.find params[:part_id]
end
@page_part.module_app.enable_frontend?
end
def query_for_default_widget
ori_class_name = @default_widget["query"].split('.')[0]
result = nil
result_objects = nil
if !params["tag_id"].blank?
if params["category_id"].blank? #has tag no cate
result = ori_class_name.constantize.where(:tagged_ids.in => params["tag_id"])
else #has tag and cate
result = eval("#{ori_class_name}.where(:#{get_category_field_name(ori_class_name)}.in=>params['category_id'], :tagged_ids.in => params['tag_id'])")
end
elsif params["category_id"].blank? #no tag no cate
result = eval(@default_widget["query"])
else #no tag has cate
result = eval("#{ori_class_name}.where(:#{get_category_field_name(ori_class_name)}.in=>params['category_id'])")
end
result.available_for_lang(I18n.locale).can_display.desc(:is_top, :postdate)
end
def default_widget
if !params[:id].blank?
clicked_field_name = params["clicked_field_name"].to_sym
redirect_to eval("#{@page_part.module_app.widget_fields_link_method[clicked_field_name][:method]}('#{params[:id]}', {inner: #{params[:inner] || true}})")
else
@tag_class = nil
@default_widget = @page_part.module_app.get_default_widget
@widget_image_field = @default_widget["image"] || @default_widget[:image]
case @page_part
when Page
@frontend_field_names = @page_part.frontend_field_name
@frontend_classes = @page_part.frontend_class
@frontend_sat_to_links = @page_part.frontend_sat_to_link
@data_limit = @page_part.frontend_data_count.is_a?(Fixnum) ? @page_part.frontend_data_count : (@page_part.frontend_data_count.to_i rescue 3)
@paginate = true
@page_id = @page_part.id
when PagePart
@frontend_field_names = @page_part.widget_field_name
@frontend_classes = @page_part.widget_class
@frontend_sat_to_links = @page_part.widget_sat_to_link
@fields = @page_part.widget_field
@data_limit = @page_part.widget_data_count.is_a?(Fixnum) ? @page_part.widget_data_count : (@page_part.widget_data_count.to_i rescue 3)
end
if @paginate
@data = query_for_default_widget.includes(@widget_image_field).page(params[:page_main]).per(@data_limit)
# @data = query_for_default_widget.limit(@data_limit).includes(@widget_image_field)
else
@data = query_for_default_widget.includes(@widget_image_field).limit(@data_limit)
# @data = eval(@default_widget["query"]).limit(@data_limit).includes(@widget_image_field).desc(:created_at)
end
case params[:type]
when "typeA"
@tag_class = 'default_widget_typeA'
@partial = "typeA"
when /typeB_/
@tag_class = "default_widget_#{params[:type]}"
@partial = "typeB"
when "typeC"
@tag_class = 'default_widget_typeC'
@partial = "typeC"
end
end
end
def get_category_field_name(ori_class_name)
ori_class_name = ori_class_name.constantize
ori_class_name.fields.each_key do |key|
return key if key.include?('category_id')
end
nil
end
end