Sha256: a6baf8f1bd892ac03cdd7c69520d16477389c9baee0fef480bffebc31ed8e36b

Contents?: true

Size: 1.2 KB

Versions: 10

Compression:

Stored size: 1.2 KB

Contents

module Lolita
	module Menu
		module NestedTree
			module Scope
	
				def self.included(base_class)
					base_class.extend(ClassMethods)
					base_class.send(:include, InstanceMethods)
				end

				module ClassMethods
					def sees?(tree_class)
						tree_class.lolita_nested_tree.scope_classes.include?(self)
					end
				end

				module InstanceMethods
					def sees?(tree_class)
						self.class.sees?(tree_class)
					end

					def root(tree_class, scope_attributes = {})
						tree_class.find_or_create_root(merge_scope_with_self(tree_class, scope_attributes))
					end

					def children(*args)
						self.root(*args).children
					end

					def append(item, scope_attributes = {})
						scope_attributes = merge_scope_with_self(item.class, scope_attributes)
						scope_root = self.root(item.class, scope_attributes)
						item.class.with_tree_scope(scope_attributes) do
							scope_root.append(item)
							item.reload
						end
						item
					end

					private

					def merge_scope_with_self(tree_class, scope_attributes)
						scope_attributes.merge(tree_key(tree_class) => self.id)
					end

					def tree_key(klass)
						klass.lolita_nested_tree.scope_key_for(self.class)
					end

				end
			end
		end
	end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
lolita-menu-0.3.6 lib/lolita-menu/nested_tree/scope.rb
lolita-menu-0.3.5 lib/lolita-menu/nested_tree/scope.rb
lolita-menu-0.3.4 lib/lolita-menu/nested_tree/scope.rb
lolita-menu-0.3.2 lib/lolita-menu/nested_tree/scope.rb
lolita-menu-0.3.1 lib/lolita-menu/nested_tree/scope.rb
lolita-menu-0.3.0 lib/lolita-menu/nested_tree/scope.rb
lolita-menu-0.2.2 lib/lolita-menu/nested_tree/scope.rb
lolita-menu-0.2.1 lib/lolita-menu/nested_tree/scope.rb
lolita-menu-0.2.0 lib/lolita-menu/nested_tree/scope.rb
lolita-menu-0.1.7 lib/lolita-menu/nested_tree/scope.rb