Module: Structureable::StructureableInstanceMethods
- Includes:
- StructureableMixins::Roles
- Defined in:
- app/models/structureable.rb
Instance Method Summary collapse
-
#<<(object) ⇒ Object
Adding child objects.
-
#ancestors_cache_key ⇒ Object
This somehow identifies which are the ancestors of this structureable.
- #children_cache_key ⇒ Object
-
#destroy_dag_links ⇒ Object
When a dag node is destroyed, also destroy the corresponding dag links.
- #destroy_links ⇒ Object
-
#move_to(parent_node) ⇒ Object
Move the node to another parent.
Instance Method Details
#<<(object) ⇒ Object
Adding child objects.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'app/models/structureable.rb', line 138 def <<(object) if object.kind_of? User raise 'Users can only be assigned to groups.' unless self.kind_of? Group self.assign_user(object) unless self.child_users.include? object elsif object.kind_of? Group if self.kind_of?(Group) && (existing_link = DagLink.where(ancestor_type: 'Group', descendant_type: 'Group', ancestor_id: self.id, descendant_id: object.id, direct: false).first) existing_link.make_direct else self.child_groups << object unless self.child_groups.include? object end elsif object.kind_of? Page self.child_pages << object unless self.child_pages.include? object elsif object.kind_of? Event self.child_events << object unless self.child_events.include? object elsif object.nil? raise "Something is wrong! You've tried to add nil." else raise "Case not handled yet. Please implement this. It's easy :)" end end |
#ancestors_cache_key ⇒ Object
This somehow identifies which are the ancestors of this structureable. For example, this is used in the breadcrumb helper.
115 116 117 |
# File 'app/models/structureable.rb', line 115 def ancestors_cache_key "Group#{ancestor_group_ids if respond_to?(:ancestor_group_ids)}Page#{ancestor_page_ids if respond_to?(:ancestor_page_ids)}User#{ancestor_user_ids if respond_to?(:ancestor_user_ids)}" end |
#children_cache_key ⇒ Object
118 119 120 |
# File 'app/models/structureable.rb', line 118 def children_cache_key "Group#{child_group_ids.sum if respond_to?(:child_group_ids)}Page#{child_page_ids.sum if respond_to?(:child_page_ids)}User#{child_user_ids.sum if respond_to?(:child_user_ids)}" end |
#destroy_dag_links ⇒ Object
When a dag node is destroyed, also destroy the corresponding dag links. Otherwise, there would remain ghost dag links in the database that would corrupt the integrity of the database.
If the database gets ever messed up like this, delete the concerning direct dag links by hand and then run this rake task to re-create the indirect dag links:
rake reconstruct_indirect_dag_links:all
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'app/models/structureable.rb', line 89 def destroy_dag_links # destory only child and parent links, since the indirect links # are destroyed automatically by the DagLink model then. links = self.links_as_parent + self.links_as_child for link in links do if link.destroyable? link.destroy else # In facty, all these links should be destroyable. If this error should # be raised, something really went wrong. Please send in a bug report then # at http://github.com/fiedl/your_platform. raise "Could not destroy dag links of the structureable object that should be deleted." + " Please send in a bug report at http://github.com/fiedl/your_platform." return false end end end |
#destroy_links ⇒ Object
122 123 124 |
# File 'app/models/structureable.rb', line 122 def destroy_links self.destroy_dag_links end |
#move_to(parent_node) ⇒ Object
Move the node to another parent.
128 129 130 131 132 133 134 |
# File 'app/models/structureable.rb', line 128 def move_to(parent_node) raise 'Case not handled, yet. This node has several parents. Not moving.' if self.parents.count > 1 if parent_node != self.parents.first self.links_as_child.destroy_all parent_node << self end end |