Sha256: 5a439cacf66e05bdd3bf924dd6b25f3d8d9f7e5c1b730e5c3bd3f93f18a58402
Contents?: true
Size: 1.87 KB
Versions: 10
Compression:
Stored size: 1.87 KB
Contents
module Lolita module Menu module NestedTree module ClassMethods # Allows to use tree structure relationship between model records # # === # # Usage: # # lolita_nested_tree :scope => "User" # # === # # Configuration options: # * <tt>:scope</tt> - model name (as String) # def lolita_nested_tree(options = {}) @lolita_nested_tree ||= Lolita::Menu::NestedTree::Configuration.new(self, options) end end class Configuration attr_reader :klass, :options, :scope, :build_method def initialize(base_class, options = {}) @klass = base_class @options = options || {} initialize_options normalize_attributes validate extend_scope_classes end def scope_keys @scope.map do |scope| @klass.reflect_on_association(scope.to_sym).foreign_key.to_sym end end def scope_key_for(klass) scope_reflections.detect do |reflection| reflection.klass == klass end.foreign_key.to_sym end def scope_classes @scope.map do |scope| @klass.reflect_on_association(scope.to_sym).klass end end def scope_reflections @scope.map do |scope| @klass.reflect_on_association(scope.to_sym) end end private def initialize_options @options.each do |attr, value| self.respond_to?(attr.to_sym) && instance_variable_set(:"@#{attr}", value) end end def normalize_attributes @scope = (@scope.is_a?(Array) && @scope || [@scope]).compact end def validate @scope.each do |scope| raise ArgumentError, "#{@klass} should reflect on association #{scope}" unless @klass.reflect_on_association(scope.to_sym) end end def extend_scope_classes scope_classes.each do |klass| klass.send(:include, Lolita::Menu::NestedTree::Scope) end end end end end end
Version data entries
10 entries across 10 versions & 1 rubygems