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

#current_localizationObject

Returns the value of attribute current_localization.



28
29
30
# File 'app/models/gluttonberg/page.rb', line 28

def current_localization
  @current_localization
end

#locale_idObject

Returns the value of attribute locale_id.



28
29
30
# File 'app/models/gluttonberg/page.rb', line 28

def locale_id
  @locale_id
end

#paths_need_recachingObject

Returns the value of attribute paths_need_recaching.



28
29
30
# File 'app/models/gluttonberg/page.rb', line 28

def paths_need_recaching
  @paths_need_recaching
end

Class Method Details

.find_by_path(path, locale = nil, domain_name = nil) ⇒ Object

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.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/gluttonberg/page.rb', line 57

def self.find_by_path(path, locale = nil , domain_name=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? #looking for home
    locale = Gluttonberg::Locale.first_default if locale.blank?
    if !Rails.configuration.multisite.blank?
      page_desc = PageDescription.all.find{|key , val|  val.home_for_domain?(domain_name) }
      page_desc = page_desc.last unless page_desc.blank?
      unless page_desc.blank?
        pages = joins(:localizations).where("locale_id = ? AND description_name = ?", locale.id, page_desc.name)
      end
    end

    if pages.blank?
      pages = joins(:localizations).where("locale_id = ? AND home = ?", locale.id, true)
    end

    page = pages.first unless pages.blank?
    unless page.blank?
      page.current_localization = page.localizations.where("locale_id = ? ", locale.id).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

.find_by_previous_path(path, locale = nil, domain_name = nil) ⇒ Object

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.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/gluttonberg/page.rb', line 100

def self.find_by_previous_path(path, locale = nil , domain_name=nil)
  path = path.match(/^\/(\S+)/)
  if( !locale.blank? && !path.blank?)
    path = path[1]
    page = joins(:localizations).where("locale_id = ? AND gb_page_localizations.previous_path LIKE ? ", locale.id, path).first
    unless page.blank?
      page.current_localization = page.localizations.where("locale_id = ? AND previous_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.previous_path LIKE ? ", locale.id, path).first
     unless page.blank?
       page.current_localization = page.localizations.where("locale_id = ? AND previous_path LIKE ? ", locale.id, path).first
     end
     page
  end
end

.home_pageObject



229
230
231
# File 'app/models/gluttonberg/page.rb', line 229

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

.home_page_nameObject



233
234
235
236
237
238
239
240
# File 'app/models/gluttonberg/page.rb', line 233

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

.repair_pages_structureObject



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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'app/models/gluttonberg/page.rb', line 261

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

#create_default_template_fileObject

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



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'app/models/gluttonberg/page.rb', line 245

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("= @page.easy_contents(:#{content.section_name})")
        end
        file.close
      end
    end
  end
end

#descriptionObject

Returns the PageDescription associated with this page.



140
141
142
143
# File 'app/models/gluttonberg/page.rb', line 140

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

#duplicateObject



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'app/models/gluttonberg/page.rb', line 320

def duplicate
  ActiveRecord::Base.transaction do
    duplicated_page = self.dup
    duplicated_page.state = "draft"
    duplicated_page.created_at = Time.now
    duplicated_page.published_at = nil
    duplicated_page.position = nil

    if duplicated_page.save
      self.localizations.each do |localization|
        dup_loc = duplicated_page.localizations.where(:locale_id => localization.locale_id).first
        dup_loc_contents = dup_loc.contents
        unless dup_loc.blank?
          localization.contents.each do |content|
             if content.respond_to?(:parent) && content.parent.localized
                dup_content = dup_loc_contents.find{|c| c.respond_to?(:page_localization_id) &&  c.page_localization_id == dup_loc.id && c.parent.section_name ==  content.parent.section_name}
                dup_content.update_attributes(:text => content.text)
                dup_content = nil
             else
                dup_content = dup_loc_contents.find{|c| c.respond_to?(:page_id) && c.page_id == duplicated_page.id && c.section_name ==  content.section_name}
                dup_content.update_attributes(:asset_id => content.asset_id)
                dup_content = nil
             end
          end

        end
      end

      duplicated_page
    else
      nil
    end
  end
end

#easy_contents(section_name, opts = {}) ⇒ Object



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

def easy_contents(section_name, opts = {})
  begin
    section_name = section_name.to_sym
    load_localization
    content = localized_contents.pluck {|c| c.section[:name] == section_name}
    if content.class.name == "Gluttonberg::ImageContent"
      if opts[:url_for].blank?
        content.asset.url
      else
        content.asset.url_for(opts[:url_for].to_sym)
      end
    elsif content.class.name == "Gluttonberg::HtmlContent"
      content.current_localization.text.html_safe
    elsif content.class.name == "Gluttonberg::PlainTextContent"
      content.current_localization.text
    else
      nil
    end
  rescue
    nil
  end
end

#generate_rewrite_path(path) ⇒ Object

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



135
136
137
# File 'app/models/gluttonberg/page.rb', line 135

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

#home=(state) ⇒ Object



224
225
226
227
# File 'app/models/gluttonberg/page.rb', line 224

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

#is_public?Boolean

Returns:

  • (Boolean)


304
305
306
# File 'app/models/gluttonberg/page.rb', line 304

def is_public?
  groups.blank?
end

#layoutObject

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



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

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

#load_default_localizationsObject



308
309
310
# File 'app/models/gluttonberg/page.rb', line 308

def load_default_localizations
  self.current_localization = Gluttonberg::PageLocalization.find(:first , :conditions => { :page_id => id , :locale_id => Gluttonberg::Locale.first_default.id } )
end

#load_localization(locale = nil) ⇒ Object

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



216
217
218
219
220
221
222
# File 'app/models/gluttonberg/page.rb', line 216

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

#localized_contentsObject

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



205
206
207
208
209
210
211
# File 'app/models/gluttonberg/page.rb', line 205

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.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/models/gluttonberg/page.rb', line 161

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

#pathObject

Delegates to the current_localization



183
184
185
186
187
188
189
# File 'app/models/gluttonberg/page.rb', line 183

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

#paths_need_recaching?Boolean

Returns:

  • (Boolean)


200
201
202
# File 'app/models/gluttonberg/page.rb', line 200

def paths_need_recaching?
  @paths_need_recaching
end

#public_pathObject



191
192
193
194
195
196
197
# File 'app/models/gluttonberg/page.rb', line 191

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

#published?Boolean

Returns:

  • (Boolean)


312
313
314
315
316
317
318
# File 'app/models/gluttonberg/page.rb', line 312

def published?
  if publishing_status == "Published"
    return true
  else
    return false
  end
end

#redirect_required?Boolean

Returns:

  • (Boolean)


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

def redirect_required?
  self.description.redirect?
end

#redirect_urlObject



123
124
125
# File 'app/models/gluttonberg/page.rb', line 123

def redirect_url
  self.description.redirect_url(self,{})
end

#rewrite_required?Boolean

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

Returns:

  • (Boolean)


129
130
131
# File 'app/models/gluttonberg/page.rb', line 129

def rewrite_required?
  self.description.rewrite_required?
end

#titleObject

Returns the localized title for the page or a default



178
179
180
# File 'app/models/gluttonberg/page.rb', line 178

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

#viewObject

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



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

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