Module: Globalize2::PageExtensions::InstanceMethods

Defined in:
lib/globalize2/page_extensions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/globalize2/page_extensions.rb', line 4

def self.included(base)
  base.validate.delete_if { |v| v.options[:scope] == :parent_id }
  base.send(:validate, :unique_slug)
  base.reflections[:children].options[:order] = 'pages.virtual DESC'


  base.class_eval do
    extend Globalize2::LocalizedContent
    
    def self.locale
      I18n.locale
    end
    #eigenclass = class << self; self; end
    
    def self.find_by_path_with_globalize(path, live = true)
      raise MissingRootPageError unless root
      if path[/^(js|css)/]
        case $1
        when "js" then JavascriptPage.root.find_by_path(path, live)
        when "css" then StylesheetPage.root.find_by_path(path, live)
        end
      else
        root.find_by_path(path, live)
      end
    end
    translates :title, :slug, :breadcrumb, :description, :keywords
    localized_content_for :title, :slug, :breadcrumb
    
    attr_accessor :reset_translations
    alias_method_chain 'tag:link', :globalize
    alias_method_chain 'tag:children:each', :globalize
    alias_method_chain :path, :globalize
    alias_method_chain :save_translations!, :reset
    alias_method_chain :find_by_path, :globalize
    class << self
      alias_method_chain :find_by_path, :globalize
    end
    
    
    def self.scope_locale(locale, &block)
      with_scope(:find => { :joins => "INNER JOIN page_translations ptrls ON ptrls.page_id = pages.id", :conditions => ['ptrls.locale = ?', locale] }) do
        yield
      end
    end
  end
end

Instance Method Details

#cloneObject



113
114
115
116
117
118
119
# File 'lib/globalize2/page_extensions.rb', line 113

def clone
  new_page = super
  translations.each do |t|
    new_page.translations << t.clone
  end
  new_page
end

#find_by_path_with_globalize(path, live = true, clean = true) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/globalize2/page_extensions.rb', line 102

def find_by_path_with_globalize(path, live = true, clean = true)
  if clean_path(path)[/^(js|css)/]
    case $1
    when "js" then JavascriptPage.root.find_by_path(path, live)
    when "css" then StylesheetPage.root.find_by_path(path, live)
    end
  else
    find_by_path_without_globalize(path, live, clean)
  end
end

#path_with_globalizeObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/globalize2/page_extensions.rb', line 90

def path_with_globalize
  unless parent || Globalize2Extension.locales.size <= 1 
    '/' + Globalize2Extension.content_locale.to_s + path_without_globalize
  else
    if ["text/css", "text/javascript"].include?(headers['Content-Type']) && parent? && parent == Page.root
      clean_path(slug) + '/'
    else
      path_without_globalize
    end
  end
end

#save_translations_with_reset!Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/globalize2/page_extensions.rb', line 79

def save_translations_with_reset!
  if reset_translations && I18n.locale.to_s != Globalize2Extension.default_language
    self.translations.find_by_locale(I18n.locale.to_s).destroy
    parts.each do |part|
      part.translations.find_by_locale(I18n.locale.to_s).destroy
    end
  else
    save_translations_without_reset!
  end
end

#unique_slugObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/globalize2/page_extensions.rb', line 52

def unique_slug
  options = {
    "pages.parent_id = ?" => self.parent_id,
    "ptrls.slug = ?" => self.slug,
    "ptrls.locale = ?" => self.class.locale.to_s,
    "ptrls.page_id <> ?" => self.id
  }
  conditions_str = []
  conditions_arg = []
  
  options.each do |key, value|
    if value != nil
      conditions_str << key
      conditions_arg << value
    else
      conditions_str << "ptrls.page_id IS NOT NULL"
    end
  end
  
  conditions = [conditions_str.join(" AND "), *conditions_arg]
  if self.class.find(:first, :joins => "INNER JOIN page_translations ptrls ON ptrls.page_id = pages.id", :conditions => conditions )
    errors.add('slug', "must be unique")
  end
  
end