orbit-basic/app/controllers/default_widget_controller.rb

71 lines
2.5 KiB
Ruby
Raw Normal View History

2012-12-03 10:52:36 +00:00
class DefaultWidgetController< OrbitWidgetController
def front_end_available(var)
2013-01-21 08:56:37 +00:00
@page_part = case params[:item_type]
when "page"
Page.find params[:page_id]
when "page_part"
PagePart.find params[:part_id]
end
2012-12-03 10:52:36 +00:00
@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?
@tags = Tag.find(params["tag_id"]) rescue nil
result_ids = nil
if params["category_id"].blank?
result_ids = @tags.collect{|tag| tag.send(ori_class_name.downcase.pluralize).entries.collect{|t| t.id}}.flatten
else
result_ids = @tags.collect{|tag| tag.send(ori_class_name.downcase.pluralize).where(:category_id=>params["category_id"]).available_for_lang(I18n.locale).can_display.collect{|t| t.id}}.flatten
end
result = eval("#{ori_class_name}.where(:id=>result_ids)")
elsif params["category_id"].blank?
result = eval(@default_widget["query"])
else
result = eval("#{ori_class_name}.where(:category_id=>params['category_id'])")
end
result.available_for_lang(I18n.locale).can_display
end
2012-12-03 10:52:36 +00:00
def default_widget
@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
@fields = @page_part.frontend_field
@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
@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)
else
@data = query_for_default_widget.limit(@data_limit).includes(@widget_image_field)
end
case params[:type]
when "typeA"
@tag_class = 'default_widget_typeA'
@partial = "typeA"
render "typeA"
when /typeB_/
@tag_class = "default_widget_#{params[:type]}"
@partial = "typeB"
render "typeB"
when "typeC"
@tag_class = 'default_widget_typeC'
@partial = "typeC"
render "typeC"
end
2013-01-03 11:38:14 +00:00
2012-12-03 10:52:36 +00:00
end
end