diff options
| author | hukl <contact@smyck.org> | 2009-03-29 12:09:03 +0200 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2009-03-29 12:09:03 +0200 |
| commit | 14eae971ba9a385442d5950288f2a74511c09baa (patch) | |
| tree | 6325d55450a4fec6ff5bf24a088142912c7d1916 | |
| parent | fb25bab7dd0ac761702d23ff60decbca9d03dd56 (diff) | |
refactored the outdated translations functionality. also adding flexibility to allow filtering with custom values.
| -rw-r--r-- | app/models/page.rb | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/app/models/page.rb b/app/models/page.rb index 5f0d389..ee1e70a 100644 --- a/app/models/page.rb +++ b/app/models/page.rb | |||
| @@ -72,15 +72,12 @@ class Page < ActiveRecord::Base | |||
| 72 | end | 72 | end |
| 73 | end | 73 | end |
| 74 | 74 | ||
| 75 | # Returns only those pages that have outdated translations. See | ||
| 76 | # outdated_translations? for more information. | ||
| 77 | # Takes :locale => <locale> and :delta_time => 12.hours as options | ||
| 75 | def self.find_with_outdated_translations options = {} | 78 | def self.find_with_outdated_translations options = {} |
| 76 | defaults_options = { | 79 | Page.all(:include => :globalize_translations).select do |page| |
| 77 | :include => :globalize_translations | 80 | page.outdated_translations? options |
| 78 | } | ||
| 79 | |||
| 80 | options.merge! defaults_options | ||
| 81 | |||
| 82 | Page.all(options).select do |page| | ||
| 83 | page.outdated_translations? | ||
| 84 | end | 81 | end |
| 85 | end | 82 | end |
| 86 | 83 | ||
| @@ -133,10 +130,23 @@ class Page < ActiveRecord::Base | |||
| 133 | end | 130 | end |
| 134 | 131 | ||
| 135 | def public? | 132 | def public? |
| 136 | published_at.nil? ? true : published_at < Time.now | 133 | published_at.nil? ? true : published_at < Time.now |
| 137 | end | 134 | end |
| 138 | 135 | ||
| 139 | def outdated_translations? options = {:locale => :en} | 136 | # Returns true if a page has translations where one of them is significantly |
| 137 | # older than the other | ||
| 138 | # Takes the I18n.default locale and a second :locale to test if the | ||
| 139 | # translations for the given locales exist and if their updated_at attributes | ||
| 140 | # have a delta time that is greater than the specified :delta_time | ||
| 141 | def outdated_translations? options = {} | ||
| 142 | |||
| 143 | default_options = { | ||
| 144 | :locale => :en, | ||
| 145 | :delta_time => 23.hours | ||
| 146 | } | ||
| 147 | |||
| 148 | options = default_options.merge options | ||
| 149 | |||
| 140 | translations = self.globalize_translations | 150 | translations = self.globalize_translations |
| 141 | locales = translations.map {|l| l.locale} | 151 | locales = translations.map {|l| l.locale} |
| 142 | 152 | ||
| @@ -145,12 +155,9 @@ class Page < ActiveRecord::Base | |||
| 145 | 155 | ||
| 146 | if translations.size > 1 && default && custom | 156 | if translations.size > 1 && default && custom |
| 147 | time = default.updated_at - custom.updated_at | 157 | time = default.updated_at - custom.updated_at |
| 148 | difference = (time/3600).to_i.abs | 158 | difference = time.to_i.abs |
| 149 | if 23 < difference | 159 | |
| 150 | return true | 160 | return (options[:delta_time].abs < difference) |
| 151 | else | ||
| 152 | return false | ||
| 153 | end | ||
| 154 | else | 161 | else |
| 155 | return false | 162 | return false |
| 156 | end | 163 | end |
