From a9fecec98532f3775a22819ffb9bfa7acafc4b0d Mon Sep 17 00:00:00 2001 From: chris Date: Wed, 6 Feb 2013 18:20:45 +0800 Subject: [PATCH] Add pagination to default widget for front end page --- app/controllers/default_widget_controller.rb | 23 ++++++++++------ app/controllers/pages_controller.rb | 26 +++++++++++-------- .../{typeA.html.erb => _typeA.html.erb} | 0 .../{typeB.html.erb => _typeB.html.erb} | 0 .../{typeC.html.erb => _typeC.html.erb} | 0 .../default_widget/default_widget.html.erb | 4 +++ config/routes.rb | 4 ++- 7 files changed, 37 insertions(+), 20 deletions(-) rename app/views/default_widget/{typeA.html.erb => _typeA.html.erb} (100%) rename app/views/default_widget/{typeB.html.erb => _typeB.html.erb} (100%) rename app/views/default_widget/{typeC.html.erb => _typeC.html.erb} (100%) create mode 100644 app/views/default_widget/default_widget.html.erb diff --git a/app/controllers/default_widget_controller.rb b/app/controllers/default_widget_controller.rb index 3f8c8759..c274062e 100644 --- a/app/controllers/default_widget_controller.rb +++ b/app/controllers/default_widget_controller.rb @@ -12,31 +12,38 @@ class DefaultWidgetController< OrbitWidgetController def default_widget if !params[:id].blank? - redirect_to eval("#{@page_part.module_app.widget_fields_link_method['title']['method']}('#{params[:id]}', {inner: true})") + redirect_to eval("#{@page_part.module_app.widget_fields_link_method['title']['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] - data_limit = case @page_part + case @page_part when Page @fields = @page_part.frontend_field - @page_part.frontend_data_count.is_a?(Fixnum) ? @page_part.frontend_data_count : (@page_part.frontend_data_count.to_i rescue 3) + @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 - @page_part.widget_data_count.is_a?(Fixnum) ? @page_part.widget_data_count : (@page_part.widget_data_count.to_i rescue 3) + @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 - @data = eval(@default_widget["query"]).limit(data_limit).includes(@widget_image_field).desc(:created_at) + if @paginate + @data = eval(@default_widget["query"]).includes(@widget_image_field).page(params[:page_main]).per(@data_limit).desc(:created_at) + else + @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' - render "typeA" + @partial = "typeA" when /typeB_/ @tag_class = "default_widget_#{params[:type]}" - render "typeB" + @partial = "typeB" when "typeC" @tag_class = 'default_widget_typeC' - render "typeC" + @partial = "typeC" end end end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 4d84d9c9..ece047f1 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -64,18 +64,22 @@ class PagesController < ApplicationController protected def get_item - module_app = ModuleApp.first(:conditions => {:key => params[:app_name]}) - if !params[:category_id].blank? && !params[:tag_id].blank? - @item = Item.first(:conditions => {:module_app_id => module_app.id, :app_frontend_url => params[:app_action], :category => params[:category_id], :tag => params[:tag_id]}) - @item = Item.first(:conditions => {:module_app_id => module_app.id, :app_frontend_url => params[:app_action], :category => params[:category_id], :tag => ''}) unless @item - elsif !params[:category_id].blank? - @item = Item.where(module_app_id: module_app.id, app_frontend_url: params[:app_action], category: params[:category_id]).any_in(tag: [nil,'']).first - elsif !params[:tag_id].blank? - @item = Item.where(module_app_id: module_app.id, app_frontend_url: params[:app_action], tag: params[:tag_id]).any_in(category: [nil,'']).first + if params[:same_page_id] + @item = Item.find(params[:same_page_id]) + else + module_app = ModuleApp.first(:conditions => {:key => params[:app_name]}) + if !params[:category_id].blank? && !params[:tag_id].blank? + @item = Item.first(:conditions => {:module_app_id => module_app.id, :app_frontend_url => params[:app_action], :category => params[:category_id], :tag => params[:tag_id]}) + @item = Item.first(:conditions => {:module_app_id => module_app.id, :app_frontend_url => params[:app_action], :category => params[:category_id], :tag => ''}) unless @item + elsif !params[:category_id].blank? + @item = Item.where(module_app_id: module_app.id, app_frontend_url: params[:app_action], category: params[:category_id]).any_in(tag: [nil,'']).first + elsif !params[:tag_id].blank? + @item = Item.where(module_app_id: module_app.id, app_frontend_url: params[:app_action], tag: params[:tag_id]).any_in(category: [nil,'']).first + end + @item = Item.where(module_app_id: module_app.id, app_frontend_url: params[:app_action]).all_of("tag" => {"$in" => [nil,'']},"category" => { "$in" => [nil,'']}).first unless @item + @item = Item.where(module_app_id: module_app.id, app_frontend_url: params[:app_action]).first unless @item + @item = Item.where(module_app_id: module_app.id, app_frontend_url: 'default_widget').first unless @item end - @item = Item.where(module_app_id: module_app.id, app_frontend_url: params[:app_action]).all_of("tag" => {"$in" => [nil,'']},"category" => { "$in" => [nil,'']}).first unless @item - @item = Item.where(module_app_id: module_app.id, app_frontend_url: params[:app_action]).first unless @item - @item = Item.where(module_app_id: module_app.id, app_frontend_url: 'default_widget').first unless @item raise PageError,'Missing Frontend Page' if @item.nil? end diff --git a/app/views/default_widget/typeA.html.erb b/app/views/default_widget/_typeA.html.erb similarity index 100% rename from app/views/default_widget/typeA.html.erb rename to app/views/default_widget/_typeA.html.erb diff --git a/app/views/default_widget/typeB.html.erb b/app/views/default_widget/_typeB.html.erb similarity index 100% rename from app/views/default_widget/typeB.html.erb rename to app/views/default_widget/_typeB.html.erb diff --git a/app/views/default_widget/typeC.html.erb b/app/views/default_widget/_typeC.html.erb similarity index 100% rename from app/views/default_widget/typeC.html.erb rename to app/views/default_widget/_typeC.html.erb diff --git a/app/views/default_widget/default_widget.html.erb b/app/views/default_widget/default_widget.html.erb new file mode 100644 index 00000000..b134d8ad --- /dev/null +++ b/app/views/default_widget/default_widget.html.erb @@ -0,0 +1,4 @@ +<%= render @partial %> +<% if @paginate %> + <%= paginate @data, :param_name => :page_main, :params => {:same_page_id => @page_id} %> +<% end %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index ad58dd12..e65f5dea 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -274,7 +274,9 @@ Orbit::Application.routes.draw do root :to => 'mobile#index', :as => 'mobile' end - + match '/panel/orbit_app/widget/:type' => 'pages#index_from_link', :constraints => lambda { |request| + request.query_string.include?("same_page_id=") + } match '/panel/orbit_app/widget/:type' => 'default_widget#default_widget'