module Dhatu class CategoriesController < ResourceController before_action :require_update_permission, only: [:update_status, :make_parent, :mark_as_featured, :remove_from_featured] before_action :get_features def update_status @category = @r_object = Dhatu::Category.find(params[:id]) case params[:status] when "unpublished" @category.unpublish! when "removed" @category.remove! when "published" @category.publish! end set_notification(true, I18n.t('status.success'), I18n.t('state.changed', item: default_item_name.titleize, new_state: @r_object.status)) render_row end def make_parent @category = @r_object = Dhatu::Category.find(params[:id]) @category.update_attribute(:parent_id, nil) get_collections render_accordingly end def mark_as_featured @category = @r_object = Dhatu::Category.find(params[:id]) @category.update_attribute(:featured, true) if @category.published? render_row end def remove_from_featured @category = @r_object = Dhatu::Category.find(params[:id]) @category.update_attribute(:featured, false) if @category.featured? render_row end private def save_resource if @r_object.valid? previous_parent_id = @r_object.parent_id_was @r_object.save if @r_object.parent_id != previous_parent_id @r_object.parent.set_end_node! previous_category = Dhatu::Category.find_by_id(previous_parent_id) previous_category.set_end_node! if previous_category end get_collections if @resource_options[:layout] = :table set_flash_message(I18n.translate("forms.save", item: default_item_name.titleize), :success) end set_resource_notification(@r_object) action_name = params[:action].to_s == "create" ? "new" : "edit" url = @r_object.persisted? ? resource_url(@r_object) : nil render_or_redirect(@r_object.errors.any?, url, action_name) end def get_features @features = Feature.categorisable.published.order("name ASC").all end def permitted_params params[:category].permit(:name, :permalink, :one_liner, :description, :parent_id, :priority, :category_type) end def set_navs set_nav("admin/categories") end def get_collections @current_category = Dhatu::Category.find_by_id(params[:parent_id]) filter_param_mapping parse_filters if @query @relation = Dhatu::Category.includes(:parent) else if @current_category @relation = @current_category.sub_categories.includes(:parent) else @relation = Dhatu::Category.where("parent_id IS NULL") end end @relation = @relation.order("priority ASC, name ASC") apply_filters @categories = @r_objects = @relation.page(@current_page).per(@per_page) return true end def apply_filters @category_type = params[:category_type] || "All" case @category_type when "none" @relation = @relation.joins("LEFT JOIN features f on f.name = categories.category_type").where("f.name is NULL") when "all_including_children" @relation = Dhatu::Category.where("") when "All" @relation = @relation.where("") else @relation = @relation.where("category_type = ?", params[:category_type]) end @relation = @relation.search(@query) if @query @relation = @relation.status(@status) if @status @relation = @relation.featured(@featured) unless @featured.nil? end def filter_config @filter_config = { boolean_filters: [ { filter_name: :featured } ], string_filters: [ { filter_name: :query }, { filter_name: :status } ], reference_filters: [], variable_filters: [], } end def resource_controller_configuration { page_title: "Categories", js_view_path: "/kuppayam/workflows/peacock", view_path: "/dhatu/categories", collection_name: :categories, item_name: :category, class: Dhatu::Category } end def breadcrumbs_configuration { heading: "Manage Categories", icon: "fa-sitemap", description: "Listing all Categories", links: [{name: "Home", link: breadcrumb_home_path, icon: 'fa-home'}, {name: "Manage Categories", link: dhatu.categories_path, icon: 'fa-sitemap', active: true}] } end def permitted_params params.require("dhatu/category").permit(:name, :one_liner, :description, :priority, :parent_id, :category_type) end def set_navs set_nav("dhatu/categories") end end end