diff options
| author | hukl <contact@smyck.org> | 2012-02-18 16:36:03 +0100 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2012-02-18 16:36:03 +0100 |
| commit | 220c6f7bdfc0da33d4284495d6954b2b89f224f6 (patch) | |
| tree | 27ed500b083ead8b86d82fe1f386c857f159ac90 | |
| parent | 2f80dfa82be421742506f3762bf68c83a5654f30 (diff) | |
Bugfix: Finally after all those years, asset urls are not rewritten anymore with a locale prefix! Hoorray!
| -rw-r--r-- | app/models/page.rb | 111 |
1 files changed, 56 insertions, 55 deletions
diff --git a/app/models/page.rb b/app/models/page.rb index 360601d..c5da386 100644 --- a/app/models/page.rb +++ b/app/models/page.rb | |||
| @@ -1,51 +1,51 @@ | |||
| 1 | require 'xml' | 1 | require 'xml' |
| 2 | 2 | ||
| 3 | class Page < ActiveRecord::Base | 3 | class Page < ActiveRecord::Base |
| 4 | 4 | ||
| 5 | PUBLIC_TEMPLATE_PATH = File.join(%w(custom page_templates public)) | 5 | PUBLIC_TEMPLATE_PATH = File.join(%w(custom page_templates public)) |
| 6 | FULL_PUBLIC_TEMPLATE_PATH = File.join(RAILS_ROOT, 'app', 'views', PUBLIC_TEMPLATE_PATH) | 6 | FULL_PUBLIC_TEMPLATE_PATH = File.join(RAILS_ROOT, 'app', 'views', PUBLIC_TEMPLATE_PATH) |
| 7 | 7 | ||
| 8 | # named scopes | 8 | # named scopes |
| 9 | 9 | ||
| 10 | named_scope( | 10 | named_scope( |
| 11 | :drafts, | 11 | :drafts, |
| 12 | :joins => :node, | 12 | :joins => :node, |
| 13 | :include => [:translations], | 13 | :include => [:translations], |
| 14 | :conditions => ["nodes.draft_id = pages.id"] | 14 | :conditions => ["nodes.draft_id = pages.id"] |
| 15 | ) | 15 | ) |
| 16 | 16 | ||
| 17 | named_scope( | 17 | named_scope( |
| 18 | :heads, | 18 | :heads, |
| 19 | :joins => :node, | 19 | :joins => :node, |
| 20 | :include => [:translations], | 20 | :include => [:translations], |
| 21 | :conditions => ["nodes.head_id = pages.id"] | 21 | :conditions => ["nodes.head_id = pages.id"] |
| 22 | ) | 22 | ) |
| 23 | 23 | ||
| 24 | # Mixins and Plugins | 24 | # Mixins and Plugins |
| 25 | acts_as_taggable | 25 | acts_as_taggable |
| 26 | acts_as_list :column => :revision, :scope => :node_id | 26 | acts_as_list :column => :revision, :scope => :node_id |
| 27 | 27 | ||
| 28 | translates :title, :abstract, :body # Globalize2 | 28 | translates :title, :abstract, :body # Globalize2 |
| 29 | 29 | ||
| 30 | # Associations | 30 | # Associations |
| 31 | belongs_to :node | 31 | belongs_to :node |
| 32 | belongs_to :user | 32 | belongs_to :user |
| 33 | belongs_to :editor, :class_name => "User" | 33 | belongs_to :editor, :class_name => "User" |
| 34 | has_many :related_assets | 34 | has_many :related_assets |
| 35 | has_many :assets, :through => :related_assets, :order => "position ASC" | 35 | has_many :assets, :through => :related_assets, :order => "position ASC" |
| 36 | 36 | ||
| 37 | # Filter | 37 | # Filter |
| 38 | before_create :set_page_title | 38 | before_create :set_page_title |
| 39 | before_create :set_template | 39 | before_create :set_template |
| 40 | before_save :rewrite_links_in_body | 40 | before_save :rewrite_links_in_body |
| 41 | 41 | ||
| 42 | # Security | 42 | # Security |
| 43 | attr_accessible :title, :abstract, :body, :template_name, :published_at, :user_id | 43 | attr_accessible :title, :abstract, :body, :template_name, :published_at, :user_id |
| 44 | 44 | ||
| 45 | # Class Methods | 45 | # Class Methods |
| 46 | 46 | ||
| 47 | # This method is most likely called from the ContentHelper.render_collection | 47 | # This method is most likely called from the ContentHelper.render_collection |
| 48 | # method which aggregates pages into a collection, based on parameters it | 48 | # method which aggregates pages into a collection, based on parameters it |
| 49 | # recieves. This method then calls Page.aggregate with these parameters. | 49 | # recieves. This method then calls Page.aggregate with these parameters. |
| 50 | # The Page.aggregate method comes with a defaults hash. These options are | 50 | # The Page.aggregate method comes with a defaults hash. These options are |
| 51 | # partially or entirely overwritten by the options hash. Afterwards the merged | 51 | # partially or entirely overwritten by the options hash. Afterwards the merged |
| @@ -59,9 +59,9 @@ class Page < ActiveRecord::Base | |||
| 59 | :order_by => "pages.id", | 59 | :order_by => "pages.id", |
| 60 | :order_direction => "ASC" | 60 | :order_direction => "ASC" |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | options = defaults.merge options | 63 | options = defaults.merge options |
| 64 | 64 | ||
| 65 | Page.heads.paginate( | 65 | Page.heads.paginate( |
| 66 | find_options_for_find_tagged_with( | 66 | find_options_for_find_tagged_with( |
| 67 | options[:tags].gsub(/\s/, ","), :match_all => true | 67 | options[:tags].gsub(/\s/, ","), :match_all => true |
| @@ -74,17 +74,17 @@ class Page < ActiveRecord::Base | |||
| 74 | end | 74 | end |
| 75 | 75 | ||
| 76 | def self.custom_templates | 76 | def self.custom_templates |
| 77 | files = Dir.entries(FULL_PUBLIC_TEMPLATE_PATH).select do |x| | 77 | files = Dir.entries(FULL_PUBLIC_TEMPLATE_PATH).select do |x| |
| 78 | x if x.gsub!(".html.erb", "") | 78 | x if x.gsub!(".html.erb", "") |
| 79 | end | 79 | end |
| 80 | end | 80 | end |
| 81 | 81 | ||
| 82 | def self.untranslated(options = {:locale => :de}) | 82 | def self.untranslated(options = {:locale => :de}) |
| 83 | PageTranslation.all.group_by(&:page_id).select do |k,v| | 83 | PageTranslation.all.group_by(&:page_id).select do |k,v| |
| 84 | v.size == 1 && v.map{|x| x.locale}.include?(options[:locale]) | 84 | v.size == 1 && v.map{|x| x.locale}.include?(options[:locale]) |
| 85 | end | 85 | end |
| 86 | end | 86 | end |
| 87 | 87 | ||
| 88 | # Returns only those pages that have outdated translations. See | 88 | # Returns only those pages that have outdated translations. See |
| 89 | # outdated_translations? for more information. | 89 | # outdated_translations? for more information. |
| 90 | # Takes :locale => <locale> and :delta_time => 12.hours as options | 90 | # Takes :locale => <locale> and :delta_time => 12.hours as options |
| @@ -93,81 +93,81 @@ class Page < ActiveRecord::Base | |||
| 93 | page.outdated_translations? options | 93 | page.outdated_translations? options |
| 94 | end | 94 | end |
| 95 | end | 95 | end |
| 96 | 96 | ||
| 97 | # Instance Methods | 97 | # Instance Methods |
| 98 | 98 | ||
| 99 | def public_template_path | 99 | def public_template_path |
| 100 | File.join(PUBLIC_TEMPLATE_PATH, template_name) | 100 | File.join(PUBLIC_TEMPLATE_PATH, template_name) |
| 101 | end | 101 | end |
| 102 | 102 | ||
| 103 | def full_public_template_path | 103 | def full_public_template_path |
| 104 | File.join(FULL_PUBLIC_TEMPLATE_PATH, template_name) | 104 | File.join(FULL_PUBLIC_TEMPLATE_PATH, template_name) |
| 105 | end | 105 | end |
| 106 | 106 | ||
| 107 | def template_exists? | 107 | def template_exists? |
| 108 | File.exists? "#{full_public_template_path}.html.erb" | 108 | File.exists? "#{full_public_template_path}.html.erb" |
| 109 | end | 109 | end |
| 110 | 110 | ||
| 111 | def valid_template | 111 | def valid_template |
| 112 | 112 | ||
| 113 | if template_name && template_exists? | 113 | if template_name && template_exists? |
| 114 | public_template_path | 114 | public_template_path |
| 115 | else | 115 | else |
| 116 | File.join(PUBLIC_TEMPLATE_PATH, 'standard_template') | 116 | File.join(PUBLIC_TEMPLATE_PATH, 'standard_template') |
| 117 | end | 117 | end |
| 118 | end | 118 | end |
| 119 | 119 | ||
| 120 | def public_link | 120 | def public_link |
| 121 | "/#{node.unique_name}" | 121 | "/#{node.unique_name}" |
| 122 | end | 122 | end |
| 123 | 123 | ||
| 124 | def clone_attributes_from page | 124 | def clone_attributes_from page |
| 125 | return nil unless page | 125 | return nil unless page |
| 126 | 126 | ||
| 127 | self.reload | 127 | self.reload |
| 128 | 128 | ||
| 129 | # Clone untranslated attributes | 129 | # Clone untranslated attributes |
| 130 | self.tag_list = page.tag_list | 130 | self.tag_list = page.tag_list |
| 131 | self.template_name ||= page.template_name | 131 | self.template_name ||= page.template_name |
| 132 | self.published_at = page.published_at | 132 | self.published_at = page.published_at |
| 133 | 133 | ||
| 134 | # Getting rid of the auto-generated empty translations | 134 | # Getting rid of the auto-generated empty translations |
| 135 | self.translations.delete_all | 135 | self.translations.delete_all |
| 136 | 136 | ||
| 137 | # Clone translated attributes | 137 | # Clone translated attributes |
| 138 | page.translations.each do |translation| | 138 | page.translations.each do |translation| |
| 139 | self.translations.create!(translation.attributes) | 139 | self.translations.create!(translation.attributes) |
| 140 | end | 140 | end |
| 141 | 141 | ||
| 142 | # Clone asset references | 142 | # Clone asset references |
| 143 | self.assets = page.assets | 143 | self.assets = page.assets |
| 144 | 144 | ||
| 145 | self.save | 145 | self.save |
| 146 | end | 146 | end |
| 147 | 147 | ||
| 148 | def public? | 148 | def public? |
| 149 | published_at.nil? ? true : published_at < Time.now | 149 | published_at.nil? ? true : published_at < Time.now |
| 150 | end | 150 | end |
| 151 | 151 | ||
| 152 | # Returns true if a page has translations where one of them is significantly | 152 | # Returns true if a page has translations where one of them is significantly |
| 153 | # older than the other. | 153 | # older than the other. |
| 154 | # Takes the I18n.default locale and a second :locale to test if the | 154 | # Takes the I18n.default locale and a second :locale to test if the |
| 155 | # translations for the given locales exist and if their updated_at attributes | 155 | # translations for the given locales exist and if their updated_at attributes |
| 156 | # have a delta time that is greater than the specified :delta_time | 156 | # have a delta time that is greater than the specified :delta_time |
| 157 | def outdated_translations? options = {} | 157 | def outdated_translations? options = {} |
| 158 | 158 | ||
| 159 | default_options = { | 159 | default_options = { |
| 160 | :locale => :en, | 160 | :locale => :en, |
| 161 | :delta_time => 23.hours | 161 | :delta_time => 23.hours |
| 162 | } | 162 | } |
| 163 | 163 | ||
| 164 | options = default_options.merge options | 164 | options = default_options.merge options |
| 165 | 165 | ||
| 166 | translations = self.translations | 166 | translations = self.translations |
| 167 | 167 | ||
| 168 | default = *(translations.select {|x| x.locale == I18n.default_locale}) | 168 | default = *(translations.select {|x| x.locale == I18n.default_locale}) |
| 169 | custom = *(translations.select {|x| x.locale == options[:locale]}) | 169 | custom = *(translations.select {|x| x.locale == options[:locale]}) |
| 170 | 170 | ||
| 171 | if translations.size > 1 && default && custom | 171 | if translations.size > 1 && default && custom |
| 172 | difference = (default.updated_at - custom.updated_at).to_i.abs | 172 | difference = (default.updated_at - custom.updated_at).to_i.abs |
| 173 | return (options[:delta_time].to_i.abs < difference) | 173 | return (options[:delta_time].to_i.abs < difference) |
| @@ -175,12 +175,12 @@ class Page < ActiveRecord::Base | |||
| 175 | return false | 175 | return false |
| 176 | end | 176 | end |
| 177 | end | 177 | end |
| 178 | 178 | ||
| 179 | def update_assets image_ids | 179 | def update_assets image_ids |
| 180 | 180 | ||
| 181 | transaction do | 181 | transaction do |
| 182 | self.related_assets.delete_all | 182 | self.related_assets.delete_all |
| 183 | 183 | ||
| 184 | if image_ids | 184 | if image_ids |
| 185 | image_ids.each_with_index do |id, index| | 185 | image_ids.each_with_index do |id, index| |
| 186 | asset = Asset.find(id) | 186 | asset = Asset.find(id) |
| @@ -188,23 +188,23 @@ class Page < ActiveRecord::Base | |||
| 188 | end | 188 | end |
| 189 | end | 189 | end |
| 190 | end | 190 | end |
| 191 | 191 | ||
| 192 | end | 192 | end |
| 193 | 193 | ||
| 194 | private | 194 | private |
| 195 | 195 | ||
| 196 | def set_page_title | 196 | def set_page_title |
| 197 | if title.nil? | 197 | if title.nil? |
| 198 | title = "Untitled" | 198 | title = "Untitled" |
| 199 | end | 199 | end |
| 200 | end | 200 | end |
| 201 | 201 | ||
| 202 | def set_template | 202 | def set_template |
| 203 | if node && node.update? | 203 | if node && node.update? |
| 204 | self.template_name = "update" | 204 | self.template_name = "update" |
| 205 | end | 205 | end |
| 206 | end | 206 | end |
| 207 | 207 | ||
| 208 | def rewrite_links_in_body | 208 | def rewrite_links_in_body |
| 209 | begin | 209 | begin |
| 210 | if self.body | 210 | if self.body |
| @@ -212,13 +212,14 @@ class Page < ActiveRecord::Base | |||
| 212 | xml_string = XML::Parser.string( tmp_body ) | 212 | xml_string = XML::Parser.string( tmp_body ) |
| 213 | xml_doc = xml_string.parse | 213 | xml_doc = xml_string.parse |
| 214 | links = xml_doc.find("//a[not(starts-with(@href, 'http://'))]") | 214 | links = xml_doc.find("//a[not(starts-with(@href, 'http://'))]") |
| 215 | links = links.reject { |l| l[:href] =~ /system\/uploads/ } | ||
| 215 | locales = I18n.available_locales.reject {|l| l == :root} | 216 | locales = I18n.available_locales.reject {|l| l == :root} |
| 216 | 217 | ||
| 217 | if xml_doc.find("//p/aggregate")[0] | 218 | if xml_doc.find("//p/aggregate")[0] |
| 218 | aggregate_tags = xml_doc.find("//aggregate") | 219 | aggregate_tags = xml_doc.find("//aggregate") |
| 219 | aggregate_tags[0].parent.replace_with aggregate_tags[0] | 220 | aggregate_tags[0].parent.replace_with aggregate_tags[0] |
| 220 | end | 221 | end |
| 221 | 222 | ||
| 222 | links.each do |link| | 223 | links.each do |link| |
| 223 | unless locales.include? link[:href].slice(1,2).to_sym | 224 | unless locales.include? link[:href].slice(1,2).to_sym |
| 224 | unless link[:href] =~ /sytem\/uploads/ | 225 | unless link[:href] =~ /sytem\/uploads/ |
| @@ -226,15 +227,15 @@ class Page < ActiveRecord::Base | |||
| 226 | end | 227 | end |
| 227 | end | 228 | end |
| 228 | end | 229 | end |
| 229 | 230 | ||
| 230 | tmp_body = xml_doc.to_s.gsub(/(\n\<div\>|\<\/div\>\n)/, "") | 231 | tmp_body = xml_doc.to_s.gsub(/(\n\<div\>|\<\/div\>\n)/, "") |
| 231 | tmp_body.gsub!("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "") | 232 | tmp_body.gsub!("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "") |
| 232 | 233 | ||
| 233 | self.body = tmp_body | 234 | self.body = tmp_body |
| 234 | end | 235 | end |
| 235 | rescue | 236 | rescue |
| 236 | nil | 237 | nil |
| 237 | end | 238 | end |
| 238 | end | 239 | end |
| 239 | 240 | ||
| 240 | end \ No newline at end of file | 241 | end \ No newline at end of file |
