Class: Caboose::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose/page.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.crumb_trail(page) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
# File 'app/models/caboose/page.rb', line 441

def self.crumb_trail(page)    
  page_id = page.nil? || !page ? 1 : (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.menu_title.nil? && row.menu_title.length > 0 ? row.menu_title : row.title
    })
  end
  return trail
end

.crumb_trail_helper(page_id, arr) ⇒ Object



457
458
459
460
461
462
463
# File 'app/models/caboose/page.rb', line 457

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



58
59
60
# File 'app/models/caboose/page.rb', line 58

def self.find_with_fields(page_id, fields)
  return self.where(:id => page_id).select(fields).first
end

.has_children(page_id) ⇒ Object



665
666
667
668
# File 'app/models/caboose/page.rb', line 665

def self.has_children(page_id)
  count = self.where(:parent_id => page_id).count
  return count > 0
end

Returns an array of block IDs for all descendant blocks of the homepage footer



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/caboose/page.rb', line 71

def self.home_page_footer_block_ids(page)
  home_page = Caboose::Page.index_page(page.site_id)
  home_footer = home_page ? home_page.block.child('footer') : nil
  if home_footer
    footer_ids = [home_footer.id]
    home_footer.children.where("name is null").each do |f1|
      footer_ids << f1.id
      f1.children.where("name is null").each do |f2|
        footer_ids << f2.id
        f2.children.where("name is null").each do |f3|
          footer_ids << f3.id
          f3.children.where("name is null").each do |f4|
            footer_ids << f4.id
            f4.children.where("name is null").each do |f5|
              footer_ids << f5.id
            end
          end
        end
      end
    end
    return footer_ids
  else
    return [0]
  end
end

.index_page(site_id) ⇒ Object



62
63
64
# File 'app/models/caboose/page.rb', line 62

def self.index_page(site_id)
  return self.where(:site_id => site_id, :parent_id => -1).first
end

.is_allowed(user, page_id, action) ⇒ Object



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'app/models/caboose/page.rb', line 382

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(user.site_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



670
671
672
673
674
675
# File 'app/models/caboose/page.rb', line 670

def self.is_child(parent_id, child_id)
  pid = self.where(:id => child_id).limit(1).pluck(:parent_id)[0]
  return false if pid.nil? || pid <= 0
  return true if pid == parent_id
  return self.is_child(parent_id, pid)
end

.page_ids_with_permission(user, action) ⇒ Object



427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'app/models/caboose/page.rb', line 427

def self.page_ids_with_permission(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.flatten
end

.page_with_uri(host_with_port, uri, get_closest_parent = true) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'app/models/caboose/page.rb', line 289

def self.page_with_uri(host_with_port, uri, get_closest_parent = true)

  d = Caboose::Domain.where(:domain => host_with_port).first
  return false if d.nil? || d.under_construction == true
  site_id = d.site_id    
  
  uri = uri.to_s.gsub(/^(.*?)\?.*?$/, '\1')
  uri.chop! if uri.end_with?('/')
  uri[0] = '' if uri.starts_with?('/')
    
  return self.index_page(site_id) if uri.length == 0

  page = false
  parts = uri.split('/')
    
  # See where to start looking
  pagealias = parts[0].blank? ? '' : parts[0].downcase
  page_ids = self.where(:site_id => site_id, :alias => pagealias).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(site_id)
    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.blank? ? '' : page.uri.downcase) != (uri.blank? ? '' : uri.downcase)
  end
  return page   
end

.page_with_uri_helper(parts, level, parent_id) ⇒ Object



327
328
329
330
331
332
333
# File 'app/models/caboose/page.rb', line 327

def self.page_with_uri_helper(parts, level, parent_id)
  return parent_id if level >= parts.count
  slug = parts[level].downcase
  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

.pages_with_tag(parent_id, tag) ⇒ Object



687
688
689
# File 'app/models/caboose/page.rb', line 687

def self.pages_with_tag(parent_id, tag)
  self.includes(:page_tags).where(:hide => false, :parent_id => 1, :page_tags => { :tag => tag }).reorder('sort_order, title').all
end

.permissible_actions(user, page_id) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'app/models/caboose/page.rb', line 410

def self.permissible_actions(user, page_id)
  if (user.is_a?(Integer))
    user = Caboose::User.find(user)
  end
  actions = []
  user.roles.each do |role|
    acts = Caboose::PagePermission.where({
        :role_id => role.id, 
        :page_id => page_id
      }).pluq(:action)
    acts.each do |ac|
      actions << ac
    end
  end
  return actions
end

.roles_with_permission(page_id, action) ⇒ Object



406
407
408
# File 'app/models/caboose/page.rb', line 406

def self.roles_with_permission(page_id, action)
  return Role.roles_with_page_permission(page_id, action)
end

.slug(str) ⇒ Object



661
662
663
# File 'app/models/caboose/page.rb', line 661

def self.slug(str)
  return str.downcase.gsub(' ', '-').gsub(/[^\w-]/, '')
end


465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'app/models/caboose/page.rb', line 465

def self.subnav(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.menu_title.nil? && page.menu_title.length > 0 ? page.menu_title : 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 => false).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.menu_title.nil? && parent.menu_title.length > 0 ? parent.menu_title : 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 => false)
    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.menu_title.nil? && p.menu_title.length > 0 ? p.menu_title : 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

.title_for_id(page_id) ⇒ Object



49
50
51
52
# File 'app/models/caboose/page.rb', line 49

def self.title_for_id(page_id)
  p = Caboose::Page.where(:id => page_id).first
  return p ? p.title : nil
end

.update_authorized_for_action(page_id, action, roles) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'app/models/caboose/page.rb', line 367

def self.update_authorized_for_action(page_id, action, roles)
  Caboose::PagePermission.where(:page_id => page_id, :action => action).destroy_all
  if roles
    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



346
347
348
349
350
351
352
353
354
# File 'app/models/caboose/page.rb', line 346

def self.update_child_perms(page_id)
  page = self.find(page_id)
    
  viewer_ids   = Caboose::PagePermission.where(:page_id => page_id, :action => 'view'   ).all.collect{ |pp| pp.role_id }
  editor_ids   = Caboose::PagePermission.where(:page_id => page_id, :action => 'edit'   ).all.collect{ |pp| pp.role_id }
  approver_ids = Caboose::PagePermission.where(:page_id => page_id, :action => 'approve').all.collect{ |pp| pp.role_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



356
357
358
359
360
361
362
363
364
365
# File 'app/models/caboose/page.rb', line 356

def self.update_child_perms_helper(page, viewer_ids, editor_ids, approver_ids)
  
  self.update_authorized_for_action(page.id, 'view'     , viewer_ids)
  self.update_authorized_for_action(page.id, 'edit'     , editor_ids)
  self.update_authorized_for_action(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_uri(page) ⇒ Object



335
336
337
338
339
340
341
342
343
344
# File 'app/models/caboose/page.rb', line 335

def self.update_uri(page)
  #return if page.redirect_url && page.redirect_url.length > 0
  
  page.slug = self.slug(page.title) if page.slug.nil? || page.slug.strip.length == 0
  page.uri = page.alias && page.alias.strip.length > 0 ? page.alias : (page.parent ? "#{page.parent.uri}/#{page.slug}" : "#{page.slug}")
  page.uri[0] = '' if page.uri.starts_with?('/')
  page.save
  
  page.children.each { |p2| self.update_uri(p2) }     
end

.url(page_id) ⇒ Object



628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'app/models/caboose/page.rb', line 628

def self.url(page_id)
  arr = []
  self.url_helper(page_id, arr)
  arr.reverse!
  
  ru = arr.last.redirect_url
  return ru if ru && ru.strip.length > 0

  path = []      
  arr.each do |row|
    if row.alias && row.alias.strip.length > 0        
      path = [row.alias]
    elsif row.slug && row.slug.strip.length > 0
      path << row.slug
    end
  end
  return "/#{path.join('/')}"
end

.url_helper(page_id, arr) ⇒ Object



647
648
649
650
651
652
653
654
655
# File 'app/models/caboose/page.rb', line 647

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, :redirect_url])
  return if p.nil?

  arr << p
  self.url_helper(p.parent_id, arr)
end

Instance Method Details

#blockObject



45
46
47
# File 'app/models/caboose/page.rb', line 45

def block
  Caboose::Block.where("page_id = ? and parent_id is null", self.id).first
end

#cached_cssObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/models/caboose/page.rb', line 106

def cached_css
  str = ''
  return str if self.site.nil? || !self.site.use_caching
  blocks = Caboose::Block.includes(:block_type).where("blocks.page_id = ?",self.id).where("blocks.cached_css IS NOT NULL").where("block_types.is_dynamic = false").order("blocks.id").all
  blocks.each do |b|
    str << b.cached_css
  end

  # Add any CSS for referenced Block Copy blocks
  bt = Caboose::BlockType.where(:name => 'source_block_id').first
  if bt
    ref_ids = Caboose::Block.where("value is not null").where(:block_type_id => bt.id, :page_id => self.id).pluck(:value)
    if ref_ids && ref_ids.count > 0
      Caboose::Block.includes(:block_type).where("blocks.id in (?)", ref_ids).order("blocks.id").all.each do |b|
        str << b.cached_css if !b.cached_css.blank? && !b.block_type.is_dynamic
        b.children.where("name is null").each do |b1|
          str << b1.cached_css if !b1.cached_css.blank? && !b1.block_type.is_dynamic
          b1.children.where("name is null").each do |b2|
            str << b2.cached_css if !b2.cached_css.blank? && !b2.block_type.is_dynamic
            b2.children.where("name is null").each do |b3|
              str << b3.cached_css if !b3.cached_css.blank? && !b3.block_type.is_dynamic
              b3.children.where("name is null").each do |b4|
                str << b4.cached_css if !b4.cached_css.blank? && !b4.block_type.is_dynamic
              end
            end
          end
        end
      end
    end
  end

  return str.blank? ? '' : str.gsub(/<\/style>(\s*)<style>/,'')
end

#cached_jsObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/models/caboose/page.rb', line 140

def cached_js
  str = ''
  return str if self.site.nil? || !self.site.use_caching
  blocks = Caboose::Block.includes(:block_type).where("blocks.page_id = ?",self.id).where("blocks.cached_js IS NOT NULL").where("block_types.is_dynamic = false").order("blocks.id").all
  blocks.each do |b|
    str << b.cached_js
  end

  # Add any JS for referenced Block Copy blocks
  bt = Caboose::BlockType.where(:name => 'source_block_id').first
  if bt
    ref_ids = Caboose::Block.where("value is not null").where(:block_type_id => bt.id, :page_id => self.id).pluck(:value)
    if ref_ids && ref_ids.count > 0
      Caboose::Block.includes(:block_type).where("blocks.id in (?)", ref_ids).order("blocks.id").all.each do |b|
        str << b.cached_js if !b.cached_js.blank? && !b.block_type.is_dynamic
        b.children.where("name is null").each do |b1|
          str << b1.cached_js if !b1.cached_js.blank? && !b1.block_type.is_dynamic
          b1.children.where("name is null").each do |b2|
            str << b2.cached_js if !b2.cached_js.blank? && !b2.block_type.is_dynamic
            b2.children.where("name is null").each do |b3|
              str << b3.cached_js if !b3.cached_js.blank? && !b3.block_type.is_dynamic
              b3.children.where("name is null").each do |b4|
                str << b4.cached_js if !b4.cached_js.blank? && !b4.block_type.is_dynamic
              end
            end
          end
        end
      end
    end
  end

  host = Rails.env.production? ? "https://#{ActionController::Base.asset_host}" : ""
  return str.blank? ? '' : str.gsub('"/assets/',"\"#{host}/assets/")
end

#css_path(digest = self.css_digest) ⇒ Object



516
517
518
# File 'app/models/caboose/page.rb', line 516

def css_path(digest = self.css_digest)
  return "assets/pages/page_#{self.id}_#{digest}.css"
end

#css_urlObject



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'app/models/caboose/page.rb', line 520

def css_url
  rad = rand(999)
  old = "/api/page_#{self.id}.css?#{rad}"
  return old if self.css_digest.blank?
  if Rails.env.production?
    return "https://#{Caboose::cdn_domain}/#{css_path}"
  else
    path = File.join(Rails.root, 'public', "#{css_path}")
    if File.file?(path)
      return "#{ActionController::Base.asset_host}/#{css_path}"
    else
      return "https://#{Caboose::cdn_domain}/#{css_path}"
    end
  end
end

#custom_field_value(key) ⇒ Object



691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
# File 'app/models/caboose/page.rb', line 691

def custom_field_value(key)
  fv = Caboose::PageCustomFieldValue.where(:page_id => self.id, :key => key).first
  if fv.nil?
    f = Caboose::PageCustomField.where(:site_id => self.site_id, :key => key).first
    return nil if f.nil?
    fv = Caboose::PageCustomFieldValue.create(:page_id => self.id, :page_custom_field_id => f.id, :key => key, :value => f.default_value, :sort_order => f.sort_order)
  end
  f = fv.page_custom_field
  return fv.value if f.nil?
  case f.field_type
    when Caboose::PageCustomField::FIELD_TYPE_TEXT     then return fv.value
    when Caboose::PageCustomField::FIELD_TYPE_SELECT   then return fv.value
    when Caboose::PageCustomField::FIELD_TYPE_CHECKBOX then return fv.value == '1'
    when Caboose::PageCustomField::FIELD_TYPE_DATE     then return fv.value ? Date.strptime(fv.value, "%Y-%m-%d") : nil
    when Caboose::PageCustomField::FIELD_TYPE_DATETIME then return fv.value ? DateTime.strptime(fv.value, "%Y-%m-%d %H:%i:%s") : nil
  end  
  return fv.value
end

#duplicate(site_id, parent_id, duplicate_children = false, block_type_id = nil, child_block_type_id = nil, user_id = nil) ⇒ Object



717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
# File 'app/models/caboose/page.rb', line 717

def duplicate(site_id, parent_id, duplicate_children = false, block_type_id = nil, child_block_type_id = nil, user_id = nil)
  
  if parent_id.to_i == -1
    p = Caboose::Page.index_page(site_id)
    p.children.destroy_all        
    #if self.site_id != site_id
    #  self.page_tags.destroy_all    
    #  self.page_custom_field_values.destroy_all          
    #  self.page_permissions.destroy_all      
    #  self.block.destroy
    #end
  else
    p = Caboose::Page.create(:site_id => site_id, :parent_id => parent_id)
  end

  site = Caboose::Site.where(:id => site_id).first

  p.title                = "Copy of " + self.title                 
  p.menu_title           = self.menu_title            
  p.alias                = self.alias                               
  p.redirect_url         = self.redirect_url
  p.hide                 = true                     
  p.custom_css           = self.custom_css            
  p.custom_js            = self.custom_js           
  p.sort_order           = self.sort_order            
  p.custom_sort_children = self.custom_sort_children  
  p.seo_title            = self.seo_title             
  p.meta_keywords        = self.meta_keywords         
  p.meta_description     = self.meta_description      
  p.meta_robots          = self.meta_robots           
  p.canonical_url        = self.canonical_url         
  p.fb_description       = self.fb_description        
  p.gp_description       = self.gp_description 

  i = 0
  begin
    par = Caboose::Page.where(:id => parent_id).first
    p.slug = Caboose::Page.slug(p.title + (i > 0 ? " #{i}" : ""))
    p.uri = par.parent_id == -1 ? p.slug : "#{par.uri}/#{p.slug}"
    i = i+1
  end while (Caboose::Page.where(:uri => p.uri, :site_id => site_id).count > 0 && i < 10)

  p.save

  Caboose::ChangeLog.create(:site_id => site_id, :description => p.title, :user_id => user_id, :page_id => p.id, :timestamp => DateTime.now, :action => 'created') if site && site.use_change_logs
  
  self.page_tags.each{ |tag| Caboose::PageTag.create(:page_id => p.id, :tag => tag.tag) }
  
  self.page_custom_field_values.each do |v|
    f = v.page_custom_field.duplicate(site_id)
    v.duplicate(p.id, f.id)      
  end
  
  self.page_permissions.each do |pp|
    pp.role.duplicate(site_id)      
    r = Caboose::Role.where(:site_id => site_id, :name => pp.role.name).first
    Caboose::PagePermission.create(:page_id => p.id, :role_id => r.id, :action => pp.action)
  end

  self.block.duplicate_page_block(site_id, p.id, block_type_id)
      
  if duplicate_children && !p.is_child_of?(self.id)
    self.children.each do |p2|
      p2.duplicate(site_id, p.id, duplicate_children, child_block_type_id, child_block_type_id, user_id)
    end
  end
  return p
end

#head_titleObject



681
682
683
684
685
# File 'app/models/caboose/page.rb', line 681

def head_title
  str = ""
  str << "#{self.title} | " if !self.title.nil? && self.title.strip.length > 0
  str << self.site.description if self.site && self.site.description
end

#is_child_of?(parent_id) ⇒ Boolean

Returns:

  • (Boolean)


677
678
679
# File 'app/models/caboose/page.rb', line 677

def is_child_of?(parent_id)
  return Caboose::Page.is_child(parent_id, self.id)
end

#is_publishedObject



66
67
68
# File 'app/models/caboose/page.rb', line 66

def is_published
  Caboose::Block.where(:page_id => self.id).where('status != ?','published').count == 0 if !self.id.nil?
end

#js_path(digest = self.js_digest) ⇒ Object



571
572
573
# File 'app/models/caboose/page.rb', line 571

def js_path(digest = self.js_digest)
  return "assets/pages/page_#{self.id}_#{digest}.js"
end

#js_urlObject



575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'app/models/caboose/page.rb', line 575

def js_url
  rad = rand(999)
  old = "/api/page_#{self.id}.js?#{rad}"
  return old if self.js_digest.blank?
  if Rails.env.production?
    return "https://#{Caboose::cdn_domain}/#{js_path}"
  else
    path = File.join(Rails.root, 'public', "#{js_path}")
    if File.file?(path)
      return "#{ActionController::Base.asset_host}/#{js_path}"
    else
      return "https://#{Caboose::cdn_domain}/#{js_path}"
    end
  end
end

#order_titleObject



38
39
40
41
42
43
# File 'app/models/caboose/page.rb', line 38

def order_title
  return "" + menu_title + title unless menu_title.nil? || title.nil?
  return menu_title unless menu_title.nil?
  return title unless title.nil?
  return ""
end

#publish(user_id) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'app/models/caboose/page.rb', line 175

def publish(user_id)
  if !self.id.nil?
    use_caching = self.site.use_caching
    changed_ids = []
    Caboose::Block.where(:page_id => self.id).where('status = ? OR status = ?','edited','added').all.each do |b|
      if self.site && self.site.use_change_logs
        if b.new_parent_id.blank? && b.new_value.blank? && b.new_media_id.blank? && b.sort_order == b.new_sort_order && b.status == 'edited'
          # This one just got moved because something else was added
        else
          cl = Caboose::ChangeLog.create(:site_id => self.site_id, :user_id => user_id, :page_id => self.id, :block_id => b.id, :block_parent_id => b.parent_id, :timestamp => DateTime.now, :action => b.status)
          desc = ""
          desc += b.parent.parent.block_type.description + " -> " if b.parent && b.parent.parent && b.parent.parent.block_type && b.parent.parent.block_type.description != "Content" && !b.parent.parent.block_type.name.include?('layout')
          pos = b.parent && b.parent.block_type && b.parent.block_type.name == 'flex_grid_unit' ? b.parent.sort_order + 1 : nil
          desc += b.parent.block_type.description + " #{pos} -> " if b.parent && b.parent.block_type && b.parent.block_type.description != "Content" && !b.parent.block_type.name.include?('layout')
          desc += b.block_type.description if b.block_type
          cl.description = desc
          cl.is_field = true if !b.name.blank?
          ov = ['page','page_id'].include?(b.name) ? Caboose::Page.title_for_id(b.value) : b.value
          nv = ['page','page_id'].include?(b.name) ? Caboose::Page.title_for_id(b.new_value) : b.new_value
          if b.new_value.blank? && !b.new_media_id.blank?
            ov = Caboose::Media.title_for_id(b.media_id)
            nv = Caboose::Media.title_for_id(b.new_media_id)
          end
          cl.old_value = ov
          cl.new_value = nv
          cl.old_sort_order = b.sort_order
          cl.new_sort_order = b.new_sort_order
          cl.old_parent_id = b.parent_id
          cl.new_parent_id = b.new_parent_id
          cl.save
        end
      end
      if b.new_value == 'EMPTY'
        b.value = nil
      elsif !b.new_value.blank?
        b.value = b.new_value
      end
      if b.new_media_id == 0
        b.media_id = nil
      elsif !b.new_media_id.blank?
        b.media_id = b.new_media_id
      end
      b.sort_order = b.new_sort_order if !b.new_sort_order.blank?
      b.parent_id = b.new_parent_id if !b.new_parent_id.blank?
      b.status = 'published'
      b.new_value = nil
      b.new_media_id = nil
      b.new_sort_order = b.sort_order
      b.new_parent_id = nil
      b.save
      # Update cached value for the closest cached ancestor of this block
      changed_ids << b.cached_ancestor if use_caching
    end
    if use_caching # Catch published blocks that have never been cached before
      content = self.block.child('content')
      Caboose::Block.includes(:block_type).where(:parent_id => content.id, :status => 'published', :cached_value => nil, :name => nil).where("block_types.is_dynamic = false").all.each do |b|
        changed_ids << b.cached_ancestor
      end
    end
    deleted_blocks = Caboose::Block.where(:page_id => self.id, :status => 'deleted').pluck(:id)
    dids = deleted_blocks.blank? ? 0 : deleted_blocks
    if self.site && self.site.use_change_logs
      Caboose::Block.where("id in (?)",dids).all.each do |b|
        cl = Caboose::ChangeLog.create(:site_id => self.site_id, :user_id => user_id, :page_id => self.id, :block_id => b.id, :block_parent_id => b.parent_id, :timestamp => DateTime.now, :action => 'deleted')
        cl.old_value = b.new_value.blank? ? b.value : b.new_value
        cl.description = b.block_type.description if b.block_type
        cl.is_field = true if !b.name.blank?
        cl.save
      end
    end
    Caboose::Block.where("id in (?) or parent_id in (?)",dids,dids).destroy_all
    Caboose::Block.where(:page_id => self.id, :status => nil).update_all(:status => 'published')
    if use_caching
      index_page = Caboose::Page.index_page(self.site_id)
      self.update_cached_blocks(changed_ids)
      self.delay(:queue => 'caching', :priority => 7).update_css_file
      self.delay(:queue => 'caching', :priority => 7).update_js_file
      if index_page && self.id == index_page.id
        self.site.delay(:queue => 'caching', :priority => 8).update_footer_css_file
        self.site.delay(:queue => 'caching', :priority => 8).update_footer_js_file
      end
    end
  end
end

#refresh_cssObject



267
268
269
270
271
272
273
274
275
276
# File 'app/models/caboose/page.rb', line 267

def refresh_css
  begin
    rad = rand(999)
    dom = Rails.env.production? ? self.site.public_domain : self.site.dev_domain.domain
    uri = URI("http://#{dom}/api/page_#{self.id}.css?#{rad}")
    css = Net::HTTP.get(uri)
  rescue
    Caboose.log("Couldn't generate page CSS")
  end
end

#refresh_jsObject



278
279
280
281
282
283
284
285
286
287
# File 'app/models/caboose/page.rb', line 278

def refresh_js
  begin
    rad = rand(999)
    dom = Rails.env.production? ? self.site.public_domain : self.site.dev_domain.domain
    uri = URI("http://#{dom}/api/page_#{self.id}.js?#{rad}")
    css = Net::HTTP.get(uri)
  rescue
    Caboose.log("Couldn't generate page JS")
  end
end

#revertObject



260
261
262
263
264
265
# File 'app/models/caboose/page.rb', line 260

def revert
  if !self.id.nil?
    Caboose::Block.where(:page_id => self.id).where(:status => 'added').destroy_all
    Caboose::Block.where(:page_id => self.id).update_all("status = 'published', new_value = null, new_media_id = null, new_sort_order = sort_order, new_parent_id = null")
  end
end

#top_level_blocksObject



54
55
56
# File 'app/models/caboose/page.rb', line 54

def top_level_blocks
  Caboose::Block.where("page_id = ? and parent_id is null", self.id).reorder(:sort_order).all
end

#update_cached_blocks(block_ids) ⇒ Object



97
98
99
100
101
102
103
104
# File 'app/models/caboose/page.rb', line 97

def update_cached_blocks(block_ids)
  blocks = block_ids && block_ids.count > 0 ? Caboose::Block.where("id in (?)", block_ids).order(:id).all : []
  blocks.each do |b|
    b.use_cache = false
    b.save
    b.delay(:queue => 'caching', :priority => 6).update_cache
  end
end

#update_css_fileObject



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'app/models/caboose/page.rb', line 536

def update_css_file
  str = ""
  css_digest = SecureRandom.urlsafe_base64.to_s
  old_digest = self.css_digest
  css = self.refresh_css
  if css && !css.blank?
    begin
      if Rails.env.production?
        config = YAML.load(File.read(Rails.root.join('config', 'aws.yml')))[Rails.env]
        AWS.config(:access_key_id => config['access_key_id'], :secret_access_key => config['secret_access_key'])
        bucket =  AWS::S3.new.buckets[config['bucket']]                         
        bucket.objects[self.css_path(css_digest)].write(css, :acl => 'public-read', :content_type => 'text/css', :cache_control => 'public, max-age=86400')
        if !old_digest.blank? && old_digest != 'empty'
          key = self.css_path(old_digest)
          obj = bucket.objects[key]
          obj.delete
        end
      else
        file_path = File.join(Rails.root, 'public', 'assets', 'pages')
        FileUtils.mkdir_p(file_path) unless File.directory?(file_path)
        File.open(File.join(Rails.root, 'public', self.css_path(css_digest)), 'wb+') { |f| f.write( css ) }
        if !old_digest.blank? && old_digest != 'empty'
          path = File.join(Rails.root, 'public', self.css_path(old_digest))
          File.delete(path) if File.file?(path)
        end
      end
      self.update_column('css_digest', css_digest)
    rescue
      self.update_column('css_digest', old_digest)
    end
  else
    self.update_column('css_digest', 'empty')
  end
end

#update_js_fileObject



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'app/models/caboose/page.rb', line 591

def update_js_file
  str = ""
  js_digest = SecureRandom.urlsafe_base64.to_s
  old_digest = self.js_digest
  js = self.refresh_js
  if js && !js.blank?
    begin
      if Rails.env.production?
        config = YAML.load(File.read(Rails.root.join('config', 'aws.yml')))[Rails.env]
        AWS.config(:access_key_id => config['access_key_id'], :secret_access_key => config['secret_access_key'])
        bucket =  AWS::S3.new.buckets[config['bucket']]                         
        bucket.objects[self.js_path(js_digest)].write(js, :acl => 'public-read', :content_type => 'text/javascript', :cache_control => 'public, max-age=86400')
        if !old_digest.blank? && old_digest != 'empty'
          key = self.js_path(old_digest)
          obj = bucket.objects[key]
          obj.delete
        end
      else
        file_path = File.join(Rails.root, 'public', 'assets', 'pages')
        FileUtils.mkdir_p(file_path) unless File.directory?(file_path)
        File.open(File.join(Rails.root, 'public', self.js_path(js_digest)), 'wb+') { |f| f.write( js ) } 
        if !old_digest.blank? && old_digest != 'empty'
          path = File.join(Rails.root, 'public', self.js_path(old_digest))
          File.delete(path) if File.file?(path)
        end
      end
      self.update_column('js_digest', js_digest)
    rescue
     self.update_column('js_digest', old_digest)
    end
  else
    self.update_column('js_digest', 'empty')
  end
end

#urlObject



657
658
659
# File 'app/models/caboose/page.rb', line 657

def url
  return Caboose::Page.url(self.id)
end

#verify_custom_field_values_existObject



710
711
712
713
714
715
# File 'app/models/caboose/page.rb', line 710

def verify_custom_field_values_exist
  Caboose::PageCustomField.where(:site_id => self.site_id).all.each do |f|
    fv = Caboose::PageCustomFieldValue.where(:page_id => self.id, :page_custom_field_id => f.id).first                  
    Caboose::PageCustomFieldValue.create(:page_id => self.id, :page_custom_field_id => f.id, :key => f.key, :value => f.default_value, :sort_order => f.sort_order) if fv.nil?
  end
end