Sha256: 5da79a62b8a93de592356433f0c21ca46b92e20516a1c449a890c324524f5eff

Contents?: true

Size: 1.88 KB

Versions: 15

Compression:

Stored size: 1.88 KB

Contents

module Gluttonberg
  class PageObserver < ActiveRecord::Observer

    observe Page

    # Generate a series of content models for this page based on the specified
    # template. These models will be empty, but ready to be displayed in the 
    # admin interface for editing.
    def after_create(page)
      puts("Generating page localizations")
      Locale.all.each do |locale|
          loc = page.localizations.create(
            :name     => page.name,
            :locale_id   => locale.id
          )
      end
        
      unless page.description.sections.empty?
        puts("Generating stubbed content for new page")
        page.description.sections.each do |name, section|
          # Create the content
              association = page.send(section[:type].to_s.pluralize)
              content = association.create(:section_name => name)
              # Create each localization
               if content.class.localized?
                   page.localizations.all.each do |localization|
                     content.localizations.create(:parent => content, :page_localization => localization)
                   end
              end
        end
      end
    end
    
    
    def before_update(page)    
      # This checks to make see if we need to regenerate paths for child-pages
      # and adds a flag if it does.
      if page.parent_id_changed? || page.slug_changed? 
        page.paths_need_recaching = true
      end
    end

    def after_update(page)
      # This has the page localizations regenerate their path if the slug or 
      # parent for this page has changed.
      if page.paths_need_recaching?
        page.localizations.each { |l| l.regenerate_path! }
      end      
    end
    
    # If parent page is removed then make sure its children either orphaned or child of their grandfather
    def after_destroy(page)
        Page.delete_all(:parent_id => page.id)  
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
gluttonberg-core-2.6.4 app/observers/gluttonberg/page_observer.rb
gluttonberg-core-2.6.3 app/observers/gluttonberg/page_observer.rb
gluttonberg-core-2.6.2 app/observers/gluttonberg/page_observer.rb
gluttonberg-core-2.6.1 app/observers/gluttonberg/page_observer.rb
gluttonberg-core-2.6.0 app/observers/gluttonberg/page_observer.rb
gluttonberg-core-2.5.9 app/observers/gluttonberg/page_observer.rb
gluttonberg-core-2.5.8 app/observers/gluttonberg/page_observer.rb
gluttonberg-core-2.5.7 app/observers/gluttonberg/page_observer.rb
gluttonberg-core-2.5.6 app/observers/gluttonberg/page_observer.rb
gluttonberg-core-2.5.5 app/observers/gluttonberg/page_observer.rb
gluttonberg-core-2.5.4 app/observers/gluttonberg/page_observer.rb
gluttonberg-core-2.5.3 app/observers/gluttonberg/page_observer.rb
gluttonberg-core-2.5.2 app/observers/gluttonberg/page_observer.rb
gluttonberg-core-2.5.1 app/observers/gluttonberg/page_observer.rb
gluttonberg-core-2.5 app/observers/gluttonberg/page_observer.rb