diff --git a/app/controllers/admin/web_resources_controller.rb b/app/controllers/admin/web_resources_controller.rb index 0503333..ddebf46 100644 --- a/app/controllers/admin/web_resources_controller.rb +++ b/app/controllers/admin/web_resources_controller.rb @@ -7,8 +7,12 @@ class Admin::WebResourcesController < OrbitAdminController @categories = @module_app.categories.enabled @filter_fields = filter_fields(@categories, @tags) @table_fields = [:status, :category, :title] - - @links = WebLink.order_by(sort) + if params[:sort].blank? + s = {:order_position => "asc"} + else + s = sort + end + @links = WebLink.order_by(s) .with_categories(filters("category")) .with_tags(filters("tag")) .with_status(filters("status")) @@ -18,6 +22,31 @@ class Admin::WebResourcesController < OrbitAdminController render :partial => "index" if request.xhr? end + def order + empty_position_links = WebLink.where(:order_position => nil) + if empty_position_links.count > 0 + max_position = WebLink.max(:order_position) + max_position = 0 if max_position.nil? + empty_position_links.each_with_index do |epl,i| + epl.order_position = i + max_position + epl.save + end + end + @links = WebLink.all.asc(:order_position) + end + + def updateorder + ids_with_order = params[:order] + ids_with_order.each_with_index do |id,index| + link = WebLink.find(id) rescue nil + if !link.nil? + link.order_position = index + link.save + end + end + render :json => {"success" => true}.to_json + end + def new @tags =@module_app.tags @categories = @module_app.categories.enabled @@ -29,6 +58,9 @@ class Admin::WebResourcesController < OrbitAdminController link = WebLink.new(link_params) link.create_user_id = current_user.id link.update_user_id = current_user.id + max_position = WebLink.max(:order_position) + max_position = -1 if max_position.nil? + link.order_position = max_position + 1 link.save redirect_to admin_web_resources_path end diff --git a/app/controllers/web_resources_controller.rb b/app/controllers/web_resources_controller.rb index d3ea801..4242f19 100644 --- a/app/controllers/web_resources_controller.rb +++ b/app/controllers/web_resources_controller.rb @@ -2,7 +2,6 @@ class WebResourcesController < ApplicationController def index links = WebLink.where(:title.ne => "").can_display.filter_by_categories - web_link = links.collect do |link| statuses = link.statuses_with_classname.collect do |status| { diff --git a/app/models/web_link.rb b/app/models/web_link.rb index 930aa46..383fbca 100644 --- a/app/models/web_link.rb +++ b/app/models/web_link.rb @@ -10,7 +10,7 @@ class WebLink field :title, localize: true field :context, localize: true - + field :order_position, type: Integer, default: -1 field :url, localize: true field :create_user_id field :update_user_id @@ -19,7 +19,7 @@ class WebLink field :link_open, type: String LINK_OPEN_TYPES = ["local", "new_window"] - scope :can_display, ->{where(is_hidden: false).order_by([:is_top,:desc],[:created_at,:desc])} + scope :can_display, ->{where(is_hidden: false).order_by([:is_top,:desc],[:order_position,:asc])} before_save :add_http diff --git a/app/views/admin/web_resources/order.html.erb b/app/views/admin/web_resources/order.html.erb new file mode 100644 index 0000000..84d6e45 --- /dev/null +++ b/app/views/admin/web_resources/order.html.erb @@ -0,0 +1,117 @@ +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag "admin/web_links" %> +<% end %> +
Position | +Link | + + + <% @links.each_with_index do |link,i| %> +
---|---|
+ <%= (i + 1).to_s %>
+ |
+
+ <%= link.title %>
+ |
+