diff options
| author | erdgeist <erdgeist@erdgeist.org> | 2026-06-27 22:52:50 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@erdgeist.org> | 2026-06-27 22:52:50 +0200 |
| commit | 9a19a0494ef51cdac9a78e24d517ca48ba44c453 (patch) | |
| tree | 8eaae12d8047a40e29d3ea7ff3116b5c869e04bd /app/models/page.rb | |
| parent | 85a01e35274b8d4d4165a7b26bd7986e211246bb (diff) | |
| parent | 1853082fcd8c067390c246f9daa01a9b47387497 (diff) | |
Migration from Rails 2.3.5 to Rails 8.1 successful.
Merging dev branch.
Diffstat (limited to 'app/models/page.rb')
| -rw-r--r-- | app/models/page.rb | 104 |
1 files changed, 58 insertions, 46 deletions
diff --git a/app/models/page.rb b/app/models/page.rb index c5da386..e6baf20 100644 --- a/app/models/page.rb +++ b/app/models/page.rb | |||
| @@ -1,25 +1,9 @@ | |||
| 1 | require 'xml' | 1 | require 'xml' |
| 2 | 2 | ||
| 3 | class Page < ActiveRecord::Base | 3 | class Page < ApplicationRecord |
| 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 = Rails.root.join('app', 'views', PUBLIC_TEMPLATE_PATH) |
| 7 | |||
| 8 | # named scopes | ||
| 9 | |||
| 10 | named_scope( | ||
| 11 | :drafts, | ||
| 12 | :joins => :node, | ||
| 13 | :include => [:translations], | ||
| 14 | :conditions => ["nodes.draft_id = pages.id"] | ||
| 15 | ) | ||
| 16 | |||
| 17 | named_scope( | ||
| 18 | :heads, | ||
| 19 | :joins => :node, | ||
| 20 | :include => [:translations], | ||
| 21 | :conditions => ["nodes.head_id = pages.id"] | ||
| 22 | ) | ||
| 23 | 7 | ||
| 24 | # Mixins and Plugins | 8 | # Mixins and Plugins |
| 25 | acts_as_taggable | 9 | acts_as_taggable |
| @@ -28,20 +12,21 @@ class Page < ActiveRecord::Base | |||
| 28 | translates :title, :abstract, :body # Globalize2 | 12 | translates :title, :abstract, :body # Globalize2 |
| 29 | 13 | ||
| 30 | # Associations | 14 | # Associations |
| 31 | belongs_to :node | 15 | belongs_to :node, optional: true |
| 32 | belongs_to :user | 16 | belongs_to :user, optional: true |
| 33 | belongs_to :editor, :class_name => "User" | 17 | belongs_to :editor, :class_name => "User", optional: true |
| 34 | has_many :related_assets | 18 | has_many :related_assets |
| 35 | has_many :assets, :through => :related_assets, :order => "position ASC" | 19 | has_many :assets, -> { order("position ASC") }, :through => :related_assets |
| 20 | |||
| 21 | # Named scopes | ||
| 22 | scope :drafts, -> { joins(:node).includes(:translations).where("nodes.draft_id = pages.id") } | ||
| 23 | scope :heads, -> { joins(:node).includes(:translations).where("nodes.head_id = pages.id") } | ||
| 36 | 24 | ||
| 37 | # Filter | 25 | # Filter |
| 38 | before_create :set_page_title | 26 | before_create :set_page_title |
| 39 | before_create :set_template | 27 | before_create :set_template |
| 40 | before_save :rewrite_links_in_body | 28 | before_save :rewrite_links_in_body |
| 41 | 29 | ||
| 42 | # Security | ||
| 43 | attr_accessible :title, :abstract, :body, :template_name, :published_at, :user_id | ||
| 44 | |||
| 45 | # Class Methods | 30 | # Class Methods |
| 46 | 31 | ||
| 47 | # This method is most likely called from the ContentHelper.render_collection | 32 | # This method is most likely called from the ContentHelper.render_collection |
| @@ -51,8 +36,8 @@ class Page < ActiveRecord::Base | |||
| 51 | # partially or entirely overwritten by the options hash. Afterwards the merged | 36 | # partially or entirely overwritten by the options hash. Afterwards the merged |
| 52 | # parameters are used to query the DB for Pages matching these parameters. | 37 | # parameters are used to query the DB for Pages matching these parameters. |
| 53 | # The aggregation only takes published pages into account. | 38 | # The aggregation only takes published pages into account. |
| 54 | def self.aggregate options, page=1 | ||
| 55 | 39 | ||
| 40 | def self.aggregate options, page=1 | ||
| 56 | defaults = { | 41 | defaults = { |
| 57 | :tags => "", | 42 | :tags => "", |
| 58 | :limit => 25, | 43 | :limit => 25, |
| @@ -62,15 +47,24 @@ class Page < ActiveRecord::Base | |||
| 62 | 47 | ||
| 63 | options = defaults.merge options | 48 | options = defaults.merge options |
| 64 | 49 | ||
| 65 | Page.heads.paginate( | 50 | scope = Page.heads |
| 66 | find_options_for_find_tagged_with( | 51 | unless options[:tags].blank? |
| 67 | options[:tags].gsub(/\s/, ","), :match_all => true | 52 | tag_names = options[:tags].gsub(/\s/, ",").split(",").map(&:strip).map(&:downcase).uniq.reject(&:blank?) |
| 68 | ).merge( | 53 | |
| 69 | :page => page, | 54 | unless tag_names.empty? |
| 70 | :per_page => options[:limit], | 55 | scope = scope |
| 71 | :order => "#{options[:order_by]} #{options[:order_direction]}" | 56 | .joins("JOIN taggings ON taggings.taggable_id = pages.id |
| 72 | ) | 57 | AND taggings.taggable_type = 'Page' |
| 73 | ) | 58 | AND taggings.context = 'tags'") |
| 59 | .joins("JOIN tags ON tags.id = taggings.tag_id") | ||
| 60 | .where("LOWER(tags.name) IN (?)", tag_names) | ||
| 61 | .group("pages.id") | ||
| 62 | .having("COUNT(DISTINCT tags.id) = ?", tag_names.length) | ||
| 63 | end | ||
| 64 | end | ||
| 65 | |||
| 66 | scope.order("#{options[:order_by]} #{options[:order_direction]}") | ||
| 67 | .paginate(:page => page, :per_page => options[:limit]) | ||
| 74 | end | 68 | end |
| 75 | 69 | ||
| 76 | def self.custom_templates | 70 | def self.custom_templates |
| @@ -89,11 +83,24 @@ class Page < ActiveRecord::Base | |||
| 89 | # outdated_translations? for more information. | 83 | # outdated_translations? for more information. |
| 90 | # Takes :locale => <locale> and :delta_time => 12.hours as options | 84 | # Takes :locale => <locale> and :delta_time => 12.hours as options |
| 91 | def self.find_with_outdated_translations options = {} | 85 | def self.find_with_outdated_translations options = {} |
| 92 | Page.all(:include => :translations).select do |page| | 86 | Page.includes(:translations).select do |page| |
| 93 | page.outdated_translations? options | 87 | page.outdated_translations? options |
| 94 | end | 88 | end |
| 95 | end | 89 | end |
| 96 | 90 | ||
| 91 | # Is used to compare a node's head with the node's draft | ||
| 92 | |||
| 93 | def has_changes_to? draft | ||
| 94 | return true unless node == draft.node | ||
| 95 | return true unless assets == draft.assets | ||
| 96 | return true unless tag_list == draft.tag_list | ||
| 97 | return true unless template_name == draft.template_name | ||
| 98 | return true unless translated_locales.sort_by(&:to_s) == draft.translated_locales.sort_by(&:to_s) | ||
| 99 | changed = false | ||
| 100 | translated_locales.each { |locale| I18n.with_locale(locale) { changed = true unless title == draft.title && abstract == draft.abstract && body == draft.body } } | ||
| 101 | return changed | ||
| 102 | end | ||
| 103 | |||
| 97 | # Instance Methods | 104 | # Instance Methods |
| 98 | 105 | ||
| 99 | def public_template_path | 106 | def public_template_path |
| @@ -105,7 +112,7 @@ class Page < ActiveRecord::Base | |||
| 105 | end | 112 | end |
| 106 | 113 | ||
| 107 | def template_exists? | 114 | def template_exists? |
| 108 | File.exists? "#{full_public_template_path}.html.erb" | 115 | File.exist? "#{full_public_template_path}.html.erb" |
| 109 | end | 116 | end |
| 110 | 117 | ||
| 111 | def valid_template | 118 | def valid_template |
| @@ -136,7 +143,7 @@ class Page < ActiveRecord::Base | |||
| 136 | 143 | ||
| 137 | # Clone translated attributes | 144 | # Clone translated attributes |
| 138 | page.translations.each do |translation| | 145 | page.translations.each do |translation| |
| 139 | self.translations.create!(translation.attributes) | 146 | self.translations.create!(translation.attributes.except("id", "page_id", "created_at", "updated_at")) |
| 140 | end | 147 | end |
| 141 | 148 | ||
| 142 | # Clone asset references | 149 | # Clone asset references |
| @@ -149,6 +156,16 @@ class Page < ActiveRecord::Base | |||
| 149 | published_at.nil? ? true : published_at < Time.now | 156 | published_at.nil? ? true : published_at < Time.now |
| 150 | end | 157 | end |
| 151 | 158 | ||
| 159 | def effective_lang | ||
| 160 | if translated_locales.empty? | ||
| 161 | return 'de' | ||
| 162 | elsif translated_locales.include?(I18n.locale) | ||
| 163 | return I18n.locale | ||
| 164 | else | ||
| 165 | return translated_locales.first | ||
| 166 | end | ||
| 167 | end | ||
| 168 | |||
| 152 | # Returns true if a page has translations where one of them is significantly | 169 | # Returns true if a page has translations where one of them is significantly |
| 153 | # older than the other. | 170 | # older than the other. |
| 154 | # Takes the I18n.default locale and a second :locale to test if the | 171 | # Takes the I18n.default locale and a second :locale to test if the |
| @@ -165,8 +182,8 @@ class Page < ActiveRecord::Base | |||
| 165 | 182 | ||
| 166 | translations = self.translations | 183 | translations = self.translations |
| 167 | 184 | ||
| 168 | default = *(translations.select {|x| x.locale == I18n.default_locale}) | 185 | default = translations.find {|x| x.locale.to_s == I18n.default_locale.to_s } |
| 169 | custom = *(translations.select {|x| x.locale == options[:locale]}) | 186 | custom = translations.find {|x| x.locale.to_s == options[:locale].to_s } |
| 170 | 187 | ||
| 171 | if translations.size > 1 && default && custom | 188 | if translations.size > 1 && default && custom |
| 172 | difference = (default.updated_at - custom.updated_at).to_i.abs | 189 | difference = (default.updated_at - custom.updated_at).to_i.abs |
| @@ -215,11 +232,6 @@ class Page < ActiveRecord::Base | |||
| 215 | links = links.reject { |l| l[:href] =~ /system\/uploads/ } | 232 | links = links.reject { |l| l[:href] =~ /system\/uploads/ } |
| 216 | locales = I18n.available_locales.reject {|l| l == :root} | 233 | locales = I18n.available_locales.reject {|l| l == :root} |
| 217 | 234 | ||
| 218 | if xml_doc.find("//p/aggregate")[0] | ||
| 219 | aggregate_tags = xml_doc.find("//aggregate") | ||
| 220 | aggregate_tags[0].parent.replace_with aggregate_tags[0] | ||
| 221 | end | ||
| 222 | |||
| 223 | links.each do |link| | 235 | links.each do |link| |
| 224 | unless locales.include? link[:href].slice(1,2).to_sym | 236 | unless locales.include? link[:href].slice(1,2).to_sym |
| 225 | unless link[:href] =~ /sytem\/uploads/ | 237 | unless link[:href] =~ /sytem\/uploads/ |
| @@ -238,4 +250,4 @@ class Page < ActiveRecord::Base | |||
| 238 | end | 250 | end |
| 239 | end | 251 | end |
| 240 | 252 | ||
| 241 | end \ No newline at end of file | 253 | end |
