bisscomm/refinerycms-products

View on GitHub
app/helpers/refinery/products/admin/categories_helper.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Refinery
  module Products
    module Admin
      module CategoriesHelper
        def parent_id_nested_set_options(current_category)
          categories = []
          nested_set_options(::Refinery::Products::Category, current_category) {|category| categories << category}
          # category.title needs the :translations association, doing something like
          # nested_set_options(::Refinery::Products::Category.includes(:translations), category) doesn't work, yet.
          # See https://github.com/collectiveidea/awesome_nested_set/pull/123
          ActiveRecord::Associations::Preloader.new.preload(categories, :translations)
          categories.map {|category| ["#{'-' * category.level} #{category.title}", category.id]}
        end

        def category_title_with_translations(category)
          category.title.presence || category.translations.detect { |t| t.title.present?}.title
        end
      end
    end
  end
end