class Admin::WebResourcesController < OrbitAdminController before_action ->(module_app = @app_title) { set_variables module_app } before_action :set_link, only: [:edit, :update, :destroy] def index @tags = @module_app.tags @categories = @module_app.categories.enabled @filter_fields = filter_fields(@categories, @tags) @table_fields = [:status, :category, :title] 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")) @links = search_data(@links,[:title]).page(params[:page]).per(10) 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 @statuses = [] @link = WebLink.new end def create 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 def edit if can_edit_or_delete?(@link) @tags =@module_app.tags @categories = @module_app.categories.enabled @statuses = [] else render_401 end end def update @link.update_attributes(link_params) @link.save redirect_to admin_web_resources_path end def destroy @link.destroy redirect_to admin_web_resources_path end private def set_link @link = WebLink.find(params[:id]) end def link_params params.require(:web_link).permit! end end