module Dhatu class CategoriesController < ResourceController before_action :get_filters before_action :require_update_permission, only: [:update_status, :make_parent, :mark_as_featured, :remove_from_featured] def choose_category_type end 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 get_filters @features = Feature.categorisable.published.order("name ASC").all end 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! if @r_object.parent 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_collections @current_category = Dhatu::Category.find_by_id(params[:parent_id]) filter_param_mapping parse_filters if @feature.blank? && @current_category @feature = Feature.find_by_name(@current_category.category_type) end if @current_category @relation = @current_category.sub_categories.includes(:parent) else @relation = Dhatu::Category.where("parent_id IS NULL") end apply_filters @relation = @relation.order("priority ASC, name ASC") @categories = @r_objects = @relation.page(@current_page).per(@per_page) return true end def apply_filters @relation = @relation.where("category_type = ?", @feature.name) if @feature @relation = @relation.search(@query) if @query @relation = @relation.status(@status) if @status # @relation = @relation.featured(@featured) unless @featured.nil? end def configure_filter_settings @filter_settings = { boolean_filters: [ { filter_name: :featured } ], string_filters: [ { filter_name: :query }, { filter_name: :status } ], reference_filters: [ { filter_name: :feature, filter_class: Feature }, ], variable_filters: [], } end def resource_controller_configuration { page_title: "Manage Categories", current_nav: "dhatu/categories", # Resource Names class: Dhatu::Category, collection_name: :categories, item_name: :category, # Conditions get_collections_after_save_resource: false, show_modal_after_create: true, show_modal_after_update: true, # Model Size can be large or generic form_model_size: :generic, show_model_size: :generic, # Layout can be table or feed # table uses html tables and feed uses div based boxes layout: :feed, # "/kuppayam/workflows/default" # # Default partial opens show and form partial according to the model_size configuration # It also respect the configuration layout and refresh the page element on both cases - i.e layout is table or feed # "/kuppayam/workflows/peacock" # "/kuppayam/workflows/parrot" # # Peacock & Parrot are old ways of doing things and it respects certail configurations # Peacock opens show and form partials in large popup where as Parrot opens it in small # Both Peacock and Parrot expect table based layout and will reload a single row after create / update # Rendering Paths view_path: "dhatu/categories", js_view_path: "/kuppayam/workflows/default", # Additional Configurations load_wysihtml5: false, tagsinput: false } end def configure_filter_ui_settings @filter_ui_settings = { feature: { object_filter: true, select_label: 'Select Category Type', current_value: @feature, values: Feature.published.where("categorisable = true").order("name ASC").all, current_filters: @filters, url_method_name: 'categories_url', filters_to_remove: [], filters_to_add: {}, show_null_filter_on_top: false, show_all_filter_on_top: true } } 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 end end