Class: Caboose::Page
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Caboose::Page
- Defined in:
- app/models/caboose/page.rb
Constant Summary collapse
- CONTENT_FORMAT_HTML =
1
- CONTENT_FORMAT_TEXT =
2
- CONTENT_FORMAT_RUBY =
3
Class Method Summary collapse
- .crumb_trail(page) ⇒ Object
- .crumb_trail_helper(page_id, arr) ⇒ Object
- .find_with_fields(page_id, fields) ⇒ Object
- .has_children(page_id) ⇒ Object
- .index_page ⇒ Object
- .is_allowed(user, page_id, action) ⇒ Object
- .is_child(parent_id, child_id) ⇒ Object
- .page_ids_with_permission(user, action) ⇒ Object
- .page_with_uri(uri, get_closest_parent = true) ⇒ Object
- .page_with_uri_helper(parts, level, parent_id) ⇒ Object
- .permissible_actions(user, page_id) ⇒ Object
- .roles_with_permission(page_id, action) ⇒ Object
- .slug(str) ⇒ Object
- .subnav(page, use_redirect_urls = true, user = false) ⇒ Object
- .update_authorized_for_action(page_id, action, roles) ⇒ Object
- .update_child_perms(page_id) ⇒ Object
- .update_child_perms_helper(page, viewer_ids, editor_ids, approver_ids) ⇒ Object
- .update_child_uris(page_id) ⇒ Object
- .update_child_uris_helper(page, parent_uri) ⇒ Object
- .url(page_id) ⇒ Object
- .url_helper(page_id, arr) ⇒ Object
Instance Method Summary collapse
Class Method Details
.crumb_trail(page) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'app/models/caboose/page.rb', line 213 def self.crumb_trail(page) page_id = page.is_a?(Integer) ? page : page.id arr = [] self.crumb_trail_helper(page_id, arr) arr.reverse! trail = arr.collect do |row| Caboose::StdClass.new({ 'href' => !row.uri.nil? && row.uri.length > 0 ? row.uri : '/', 'text' => !row..nil? && row..length > 0 ? row. : row.title }) end return trail end |
.crumb_trail_helper(page_id, arr) ⇒ Object
229 230 231 232 233 234 235 |
# File 'app/models/caboose/page.rb', line 229 def self.crumb_trail_helper(page_id, arr) return if page_id.nil? || page_id <= 0 p = self.find_with_fields(page_id, [:parent_id, :title, :menu_title, :uri]) return if p.nil? arr << p self.crumb_trail_helper(p.parent_id, arr) end |
.find_with_fields(page_id, fields) ⇒ Object
39 40 41 |
# File 'app/models/caboose/page.rb', line 39 def self.find_with_fields(page_id, fields) return self.where(:id => page_id).select(fields).first end |
.has_children(page_id) ⇒ Object
317 318 319 320 |
# File 'app/models/caboose/page.rb', line 317 def self.has_children(page_id) count = self.where(:parent_id => page_id).count return count > 0 end |
.index_page ⇒ Object
43 44 45 |
# File 'app/models/caboose/page.rb', line 43 def self.index_page return self.where(:parent_id => -1).first end |
.is_allowed(user, page_id, action) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'app/models/caboose/page.rb', line 157 def self.is_allowed(user, page_id, action) user = User.logged_out_user if user.nil? # Allow a user id to be sent instead of a user object user = User.find(user) if user.is_a?(Integer) user.role_ids = [Role.logged_out_role_id] if user.role_ids.nil? t = PagePermission.table reqs = nil user.role_ids.each do |role_id| if (reqs.nil?) reqs = t[:role_id].eq(role_id) else reqs.or(t[:role_id].eq(role_id)) end end var params = { :page_id => page_id, :action => action } params << reqs if !reqs.nil? count = PagePermission.where(params).count return true if count > 0 return false end |
.is_child(parent_id, child_id) ⇒ Object
322 323 324 325 326 327 |
# File 'app/models/caboose/page.rb', line 322 def self.is_child(parent_id, child_id) pid = self.where(:page_id => child_id).first.pluck(:parent_id) return false if pid <= 0 return true if pid == parent_id return self.is_child(parent_id, pid) end |
.page_ids_with_permission(user, action) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'app/models/caboose/page.rb', line 199 def self.(user, action) if (user.is_a?(Integer)) user = Caboose::User.find(user) end ids = [] user.roles.each do |role| ids + Caboose::PagePermission.where({ :role_id => role.id, :action => action }).pluck(:page_id) end return ids.uniq end |
.page_with_uri(uri, get_closest_parent = true) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/models/caboose/page.rb', line 47 def self.page_with_uri(uri, get_closest_parent = true) uri = uri.to_s.gsub(/^(.*?)\?.*?$/, '\1') uri.chop! if uri.end_with?('/') uri[0] = '' if uri.starts_with?('/') return self.index_page if uri.length == 0 page = false parts = uri.split('/') # See where to start looking page_ids = self.where(:alias => parts[0]).limit(1).pluck(:id) page_id = !page_ids.nil? && page_ids.count > 0 ? page_ids[0] : false # Search for the page if (page_id) page_id = self.page_with_uri_helper(parts, 1, page_id) else parent_id = self.index_page page_id = self.page_with_uri_helper(parts, 0, parent_id) end return false if page_id.nil? page = self.find(page_id) if (!get_closest_parent) # // Look for an exact match return false if page.uri != uri end return page end |
.page_with_uri_helper(parts, level, parent_id) ⇒ Object
80 81 82 83 84 85 86 |
# File 'app/models/caboose/page.rb', line 80 def self.page_with_uri_helper(parts, level, parent_id) return parent_id if level >= parts.count slug = parts[level] page_ids = self.where(:parent_id => parent_id, :slug => slug).limit(1).pluck(:id) return parent_id if page_ids.nil? || page_ids.count == 0 return self.page_with_uri_helper(parts, level+1, page_ids[0]) end |
.permissible_actions(user, page_id) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'app/models/caboose/page.rb', line 185 def self.permissible_actions(user, page_id) if (user.is_a?(Integer)) user = Caboose::User.find(user) end actions = [] user.roles.each do |role| actions + Caboose::PagePermission.where({ :role_id => role.id, :page_id => page_id }).pluck(:action) end return actions.uniq end |
.roles_with_permission(page_id, action) ⇒ Object
181 182 183 |
# File 'app/models/caboose/page.rb', line 181 def self.(page_id, action) return Role.(page_id, action) end |
.slug(str) ⇒ Object
313 314 315 |
# File 'app/models/caboose/page.rb', line 313 def self.slug(str) return str.downcase.gsub(' ', '-').gsub(/[^\w-]/, '') end |
.subnav(page, use_redirect_urls = true, user = false) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'app/models/caboose/page.rb', line 237 def self.(page, use_redirect_urls = true, user = false) # Be nice and allow page ids to be sent if (page.is_a?(Integer)) page = self.find_with_fields(page, [:title, :menu_title, :custom_sort_children]) end block = Caboose::MenuBlock.new block.title = !page..nil? && page..length > 0 ? page. : page.title block.title_id = page.id pages = self.select([:id, :title, :menu_title, :alias, :slug, :uri, :redirect_url, :sort_order]).where(:parent_id => page.id, :hide => 0).reorder(:sort_order).all if (page.custom_sort_children) pages.sort! {|x,y| x.sort_order <=> y.sort_order } else pages.sort! {|x,y| x.order_title <=> y.order_title } end if (pages.nil? || pages.count == 0) # No children, go up a level parent = self.find_with_fields(page.parent_id, [:title, :menu_title, :custom_sort_children]) return block if parent.nil? # If we happen to be at the top page block.title = !parent..nil? && parent..length > 0 ? parent. : parent.title block.title_id = parent.id pages = self.select([ :id, :title, :menu_title, :alias, :slug, :uri, :redirect_url, :sort_order ]).where(:parent_id => page.parent_id, :hide => 0) if (parent.custom_sort_children) pages.sort! {|x,y| x.sort_order <=> y.sort_order } else pages.sort! {|x,y| x.order_title <=> y.order_title } end end block.links = [] pages.each do |p| link = Caboose::StdClass.new({ 'href' => !p.redirect_url.nil? && p.redirect_url.length > 0 ? p.redirect_url : p.uri, 'text' => !p..nil? && p..length > 0 ? p. : p.title, 'is_current' => p.id == page.id }) if (!use_redirect_urls && self.is_allowed(user, p.id, 'edit')) link.href = row.uri end block.links << link end return block end |
.update_authorized_for_action(page_id, action, roles) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'app/models/caboose/page.rb', line 142 def self.(page_id, action, roles) Caboose::PagePermission.where(:page_id => page_id, :action => action).destroy_all if (!roles.nil?) roles.each do |role| role_id = role.is_a?(Integer) ? role : role.id Caboose::PagePermission.create({ :page_id => page_id, :role_id => role_id, :action => action }) end end return true end |
.update_child_perms(page_id) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'app/models/caboose/page.rb', line 119 def self.update_child_perms(page_id) page = self.find(page_id) viewers = Role.(page_id, 'view') editors = Role.(page_id, 'edit') approvers = Role.(page_id, 'approve') viewer_ids = viewers.collect {|r| r.id } editor_ids = editors.collect {|r| r.id } approver_id = approvers.collect {|r| r.id } self.update_child_perms_helper(page, viewer_ids, editor_ids, approver_ids) end |
.update_child_perms_helper(page, viewer_ids, editor_ids, approver_ids) ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'app/models/caboose/page.rb', line 132 def self.update_child_perms_helper(page, viewer_ids, editor_ids, approver_ids) self.(page.id, 'view' , viewer_ids) self.(page.id, 'edit' , editor_ids) self.(page.id, 'approve' , approver_ids) page.children.each do |kid| self.update_child_perms_helper(kid, viewer_ids, editor_ids, approver_ids) end end |
.update_child_uris(page_id) ⇒ Object
88 89 90 91 92 93 |
# File 'app/models/caboose/page.rb', line 88 def self.update_child_uris(page_id) page = self.find(page_id) parent = self.find(page.parent_id) parent_uri = parent.nil? ? '/' : parent.uri self.update_child_uris_helper(ppage, parent_uri) end |
.update_child_uris_helper(page, parent_uri) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'app/models/caboose/page.rb', line 95 def self.update_child_uris_helper(page, parent_uri) return if page.redirect_url.length > 0 slug = page.slug if (slug.trim.length == 0) slug = self.get_slug(page.title) self.update_detail_field(page.id, 'slug', slug) end slug = page.slug.trim.length > 0 ? page.slug : self.get_slug(page.title) uri= "#{parent_uri}/#{slug}" if (page.alias.length > 0) uri = "/#{page.alias}" elsif (self.is_top_level(page.parent_id)) uri = "/#{page.slug}" end self.update_detail_field(page.id, 'uri', uri) page.children.each do |kid| self.update_child_uris_helper(kid, uri) end end |
.url(page_id) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'app/models/caboose/page.rb', line 287 def self.url(page_id) arr = [] self.url_helper(page_id, arr) arr.reverse! path = [] arr.each do |row| if (row.alias.length > 0) path = [row.alias] elsif (row.slug.length > 0) path << row.slug end end return path.join('/') end |
.url_helper(page_id, arr) ⇒ Object
303 304 305 306 307 308 309 310 311 |
# File 'app/models/caboose/page.rb', line 303 def self.url_helper(page_id, arr) return if page_id <= 0 p = self.find_with_fields(page_id, [:id, :parent_id, :title, :menu_title, :alias, :slug]) return if p.nil? arr << p self.url_helper(p.parent_id, arr) end |
Instance Method Details
#order_title ⇒ Object
32 33 34 35 36 37 |
# File 'app/models/caboose/page.rb', line 32 def order_title return "" + + title unless .nil? || title.nil? return unless .nil? return title unless title.nil? return "" end |