Class: Gluttonberg::PageLocalization

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/gluttonberg/page_localization.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#content_needs_savingObject

Returns the value of attribute content_needs_saving.



22
23
24
# File 'app/models/gluttonberg/page_localization.rb', line 22

def content_needs_saving
  @content_needs_saving
end

#current_pathObject

Returns the value of attribute current_path.



10
11
12
# File 'app/models/gluttonberg/page_localization.rb', line 10

def current_path
  @current_path
end

#paths_need_recachingObject

Returns the value of attribute paths_need_recaching.



22
23
24
# File 'app/models/gluttonberg/page_localization.rb', line 22

def paths_need_recaching
  @paths_need_recaching
end

Instance Method Details

#contentsObject

Returns an array of content localizations



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/gluttonberg/page_localization.rb', line 31

def contents
  @contents ||= begin
    # First collect the localized content
    contents_data = Gluttonberg::Content.localization_associations.inject([]) do |memo, assoc|
      memo += send(assoc).all
    end
    # Then grab the content that belongs directly to the page
    Gluttonberg::Content.non_localized_associations.inject(contents_data) do |memo, assoc|
      contents_data += page.send(assoc).all
    end
    contents_data = contents_data.sort{|a,b| a.section_position <=> b.section_position}
  end
  @contents
end

#contents=(params) ⇒ Object

Updates each localized content record and checks their validity



47
48
49
50
51
52
53
# File 'app/models/gluttonberg/page_localization.rb', line 47

def contents=(params)
  self.content_needs_saving = true
  contents.each do |content|
    update = params[content.association_name][content.id.to_s]
    content.attributes = update if update
  end
end

#name_and_codeObject



59
60
61
# File 'app/models/gluttonberg/page_localization.rb', line 59

def name_and_code
  "#{name} (#{locale.name})"
end

#paths_need_recaching?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/models/gluttonberg/page_localization.rb', line 55

def paths_need_recaching?
  @paths_need_recaching
end

#public_pathObject



63
64
65
66
67
68
69
# File 'app/models/gluttonberg/page_localization.rb', line 63

def public_path
    if Gluttonberg.localized?
      "/#{self.locale.slug}/#{self.path}"
    else
      "/#{self.path}"
    end
end

#regenerate_pathObject

Forces the localization to regenerate it’s full path. It will firstly look to see if there is a parent page that it need to derive the path prefix from. Otherwise it will just use the slug, with a fall-back to it’s page’s default.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/gluttonberg/page_localization.rb', line 76

def regenerate_path

  self.current_path = self.path

  page.reload #forcing that do not take cached page object
  slug = nil if slug.blank?
  if page.parent_id && page.parent.home != true
    localization = page.parent.localizations.find(:first,
      :conditions => {
        :locale_id  => locale_id
      }
    )
    new_path = "#{localization.path}/#{self.slug || page.slug}"
  else
    new_path = "#{self.slug || page.slug}"
  end

  # check duplication: add id at the end if its duplicated
  already_exist = self.class.where([ "path = ? AND page_id != ? ", new_path, page.id]).all
  if !already_exist.blank?
    if already_exist.length > 1 || (already_exist.length == 1 && already_exist.first.id != self.id )
      new_path= "#{new_path}_#{already_exist.length+1}"
    end
  end
  self.previous_path = self.current_path
  write_attribute(:path, new_path)
end

#regenerate_path!Object

Regenerates and saves the path to this localization.



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

def regenerate_path!
  regenerate_path
  save
end

#slug=(new_slug) ⇒ Object

Write an explicit setter for the slug so we can check it’s not a blank value. This stops it being overwritten with an empty string.



26
27
28
# File 'app/models/gluttonberg/page_localization.rb', line 26

def slug=(new_slug)
  write_attribute(:slug, new_slug) unless new_slug.blank?
end