Sha256: 6f1b03f353b73beca3b55100388f8879649cc815a427ce9f25fe7ee9f83e6595

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

module ErpTechSvcs
	module Utils
		module DefaultNestedSetMethods
			def self.included(base)
				base.extend(ClassMethods)
			end

			def to_label
				description
			end

			def leaf
			  children.size == 0
			end

			def to_json_with_leaf(options = {})
				self.to_json_without_leaf(options.merge(:methods => :leaf))
			end
			alias_method_chain :to_json, :leaf

      def to_tree_hash(options={})
        additional_values = options[:additional_values] || {}
        options[:additional_values] = additional_values.merge({
          :text => self.to_label,
          :leaf => self.leaf,
          :children => self.children.collect{|child| child.to_tree_hash(options)}
        })
        tree_hash = self.to_hash(options)
        tree_hash[:iconCls] = options[:icon_cls] if options[:icon_cls]
        tree_hash
      end

			module ClassMethods
				def find_roots
					where("parent_id = nil")
				end

				def find_children(parent_id = nil)
					parent_id.to_i == 0 ? self.roots : find(parent_id).children
				end
			end

		end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
erp_tech_svcs-3.0.3 lib/erp_tech_svcs/utils/default_nested_set_methods.rb
erp_tech_svcs-3.0.2 lib/erp_tech_svcs/utils/default_nested_set_methods.rb
erp_tech_svcs-3.0.1 lib/erp_tech_svcs/utils/default_nested_set_methods.rb