Class: Gluttonberg::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Content::Publishable, Content::SlugManagement
Defined in:
app/models/gluttonberg/page.rb

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Content::SlugManagement

included

Methods included from Content::Publishable

included

Instance Attribute Details

- (Object) current_localization

Returns the value of attribute current_localization



24
25
26
# File 'app/models/gluttonberg/page.rb', line 24

def current_localization
  @current_localization
end

- (Object) locale_id

Returns the value of attribute locale_id



24
25
26
# File 'app/models/gluttonberg/page.rb', line 24

def locale_id
  @locale_id
end

- (Object) paths_need_recaching

Returns the value of attribute paths_need_recaching



24
25
26
# File 'app/models/gluttonberg/page.rb', line 24

def paths_need_recaching
  @paths_need_recaching
end

Class Method Details

+ (Object) find_by_path(path, locale = nil)

A custom finder used to find a page + locale combination which most closely matches the path specified. It will also optionally limit it's search to the specified locale, otherwise it will fall back to the default.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/gluttonberg/page.rb', line 31

def self.find_by_path(path, locale = nil)
  path = path.match(/^\/(\S+)/)
  if( !locale.blank? && !path.blank?)
    path = path[1]
    page = joins(:localizations).where("locale_id = ? AND gb_page_localizations.path LIKE ? ", locale.id, path).first
    unless page.blank? 
      page.current_localization = page.localizations.where("locale_id = ? AND path LIKE ? ", locale.id, path).first
    end  
    page
  elsif path.blank?
    locale = Gluttonberg::Locale.first_default if locale.blank?
    pages = joins(:localizations).where("locale_id = ? AND home = ?", locale.id, true)
    page = pages.first unless pages.blank?
    unless page.blank? 
      page.current_localization = page.localizations.where("locale_id = ? AND path LIKE ? ", locale.id, path).first
    end
    page
  else # default locale
     path = path[1]
     locale = Gluttonberg::Locale.first_default
     page = joins(:localizations).where("locale_id = ? AND gb_page_localizations.path LIKE ? ", locale.id, path).first
     unless page.blank? 
       page.current_localization = page.localizations.where("locale_id = ? AND path LIKE ? ", locale.id, path).first
     end  
     page 
  end  
end

+ (Object) home_page



149
150
151
# File 'app/models/gluttonberg/page.rb', line 149

def self.home_page
  Page.find( :first ,  :conditions => [ "home = ? " , true ] )
end

+ (Object) home_page_name



153
154
155
156
157
158
159
160
# File 'app/models/gluttonberg/page.rb', line 153

def self.home_page_name
  home_temp = self.home_page
  if home_temp.blank?
    "Not Selected"
  else
    home_temp.name
  end
end

+ (Object) repair_pages_structure



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
# File 'app/models/gluttonberg/page.rb', line 181

def self.repair_pages_structure
  pages = Page.all

  pages.each do |page|
    
    if page.description.blank?
      puts "Page description '#{page.description_name}' for '#{page.name}' page  does not exist in page descriptions file. #{page.id}"
    elsif !page.description.sections.blank?
      puts("Generating stubbed content for new page #{page.id}")
      
      [PlainTextContent , HtmlContent , ImageContent].each do |klass|   
        list = klass.find(:all , :conditions => { :page_id => page.id})
        list.each do |item|
          found = page.description.contains_section?(item.section_name , item.class.to_s.demodulize.underscore)
          puts found
          if(!found)
              item.destroy
          end
        end
      end
      

      page.description.sections.each do |name, section|
        # Create the content
            association = page.send(section[:type].to_s.pluralize)
            content = association.find(:first , :conditions => {:section_name => name})
            if content.blank?
              content = association.create(:section_name => name)
            end
            # Create each localization
             if content.class.localized?
                 page.localizations.all.each do |localization|
                   if content.localizations.find(:first , :conditions => { "#{section[:type]}_id" => content.id, :page_localization_id => localization.id }).blank?
                     content.localizations.create(:parent => content, :page_localization => localization)
                   end 
                 end
            end
      end
    end
  end # pages loop end  
  puts "completed"
end

Instance Method Details

- (Object) create_default_template_file

if page type is not redirection. then create default view files for all localzations of the page. file will be created in host appliation/app/views/pages/template_name.locale-slug.html.haml



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/models/gluttonberg/page.rb', line 165

def create_default_template_file
  unless self.description.redirection_required?
    self.localizations.each do |page_localization|
      file_path = File.join(Rails.root, "app", "views" , "pages" , "#{self.view}.#{page_localization.locale.slug}.html.haml"  )
      unless File.exists?(file_path)
        file = File.new(file_path, "w")
    
        page_localization.contents.each do |content|
          file.puts("= render_content_for(:#{content.section_name})")
        end
        file.close
      end  
    end  
  end  
end

- (Object) description

Returns the PageDescription associated with this page.



72
73
74
75
# File 'app/models/gluttonberg/page.rb', line 72

def description
  @description = PageDescription[self.description_name.to_sym] if self.description_name
  @description
end

- (Object) generate_rewrite_path(path)

Takes a path and rewrites it to point at an alternate route. The idea being that this path points to a controller.



67
68
69
# File 'app/models/gluttonberg/page.rb', line 67

def generate_rewrite_path(path) 
  path.gsub(current_localization.path, self.description.rewrite_route)
end

- (Object) home=(state)



144
145
146
147
# File 'app/models/gluttonberg/page.rb', line 144

def home=(state)
  write_attribute(:home, state)
  @home_updated = state
end

- (Object) layout

Returns the name of the layout template specified for this page — determined via the associated PageDescription



86
87
88
89
# File 'app/models/gluttonberg/page.rb', line 86

def layout
  self.description if @description.blank? 
  @description[:layout] if @description
end

- (Object) load_localization(locale = nil)

Load the matching localization as specified in the options TODO Write spec for it



136
137
138
139
140
141
142
# File 'app/models/gluttonberg/page.rb', line 136

def load_localization(locale = nil)
  if locale.blank?
     @current_localization = localizations.first
  else  
    @current_localization = localizations.where("locale_id = ? AND path LIKE ?", locale.id, "#{path}%").first
  end   
end

- (Object) localized_contents

Just palms off the request for the contents to the current localization



125
126
127
128
129
130
131
# File 'app/models/gluttonberg/page.rb', line 125

def localized_contents
  @contents ||= begin
    Content.content_associations.inject([]) do |memo, assoc|
      memo += send(assoc).all_with_localization(:page_localization_id => current_localization.id)
    end
  end
end

Returns the localized navigation label, or falls back to the page for a the default.



93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/gluttonberg/page.rb', line 93

def nav_label
  if current_localization.blank? || current_localization.navigation_label.blank?
    if navigation_label.blank?
      name
    else
      navigation_label
    end
  else
    current_localization.navigation_label
  end
end

- (Object) path

Delegates to the current_localization



111
112
113
114
115
116
117
# File 'app/models/gluttonberg/page.rb', line 111

def path
  unless current_localization.blank?
    current_localization.path
  else
    localizations.first.path
  end  
end

- (Boolean) paths_need_recaching?

Returns:

  • (Boolean)


120
121
122
# File 'app/models/gluttonberg/page.rb', line 120

def paths_need_recaching?
  @paths_need_recaching
end

- (Boolean) rewrite_required?

Indicates if the page is used as a mount point for a public-facing controller, e.g. a blog, message board etc.

Returns:

  • (Boolean)


61
62
63
# File 'app/models/gluttonberg/page.rb', line 61

def rewrite_required?
  self.description.rewrite_required?
end

- (Object) title

Returns the localized title for the page or a default



106
107
108
# File 'app/models/gluttonberg/page.rb', line 106

def title
  (current_localization.blank? || current_localization.name.blank?) ? self.name : current_localization.name
end

- (Object) view

Returns the name of the view template specified for this page — determined via the associated PageDescription



79
80
81
82
# File 'app/models/gluttonberg/page.rb', line 79

def view
  self.description if @description.blank? 
  @description[:view] if @description
end