diff options
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/concerns/nested_tree.rb | 8 | ||||
| -rw-r--r-- | app/models/concerns/rrule_humanizer.rb | 49 | ||||
| -rw-r--r-- | app/models/node.rb | 223 | ||||
| -rw-r--r-- | app/models/node_action.rb | 136 | ||||
| -rw-r--r-- | app/models/page.rb | 17 | ||||
| -rw-r--r-- | app/models/user.rb | 31 |
6 files changed, 372 insertions, 92 deletions
diff --git a/app/models/concerns/nested_tree.rb b/app/models/concerns/nested_tree.rb index 9a2eb0e..befcdb3 100644 --- a/app/models/concerns/nested_tree.rb +++ b/app/models/concerns/nested_tree.rb | |||
| @@ -9,8 +9,6 @@ module NestedTree | |||
| 9 | included do | 9 | included do |
| 10 | belongs_to :parent, class_name: name, foreign_key: :parent_id, optional: true, inverse_of: :children | 10 | belongs_to :parent, class_name: name, foreign_key: :parent_id, optional: true, inverse_of: :children |
| 11 | has_many :children, -> { order(:id) }, class_name: name, foreign_key: :parent_id, inverse_of: :parent | 11 | has_many :children, -> { order(:id) }, class_name: name, foreign_key: :parent_id, inverse_of: :parent |
| 12 | |||
| 13 | before_destroy :delete_descendants | ||
| 14 | end | 12 | end |
| 15 | 13 | ||
| 16 | class_methods do | 14 | class_methods do |
| @@ -77,10 +75,4 @@ module NestedTree | |||
| 77 | def move_to_child_of(new_parent) | 75 | def move_to_child_of(new_parent) |
| 78 | update!(parent_id: new_parent.id) | 76 | update!(parent_id: new_parent.id) |
| 79 | end | 77 | end |
| 80 | |||
| 81 | private | ||
| 82 | |||
| 83 | def delete_descendants | ||
| 84 | self.class.where(id: descendants.pluck(:id)).delete_all | ||
| 85 | end | ||
| 86 | end | 78 | end |
diff --git a/app/models/concerns/rrule_humanizer.rb b/app/models/concerns/rrule_humanizer.rb index 8231de8..07141c1 100644 --- a/app/models/concerns/rrule_humanizer.rb +++ b/app/models/concerns/rrule_humanizer.rb | |||
| @@ -15,8 +15,8 @@ module RruleHumanizer | |||
| 15 | }.freeze | 15 | }.freeze |
| 16 | 16 | ||
| 17 | ORDINAL_NAMES = { | 17 | ORDINAL_NAMES = { |
| 18 | de: { 1=>"ersten", 2=>"zweiten", 3=>"dritten", 4=>"vierten", -1=>"letzten", -2=>"vorletzten" }, | 18 | de: { 1=>"ersten", 2=>"zweiten", 3=>"dritten", 4=>"vierten", 5=>"fünften", -1=>"letzten", -2=>"vorletzten" }, |
| 19 | en: { 1=>"first", 2=>"second", 3=>"third", 4=>"fourth", -1=>"last", -2=>"second-to-last" } | 19 | en: { 1=>"first", 2=>"second", 3=>"third", 4=>"fourth", 5=>"fifth", -1=>"last", -2=>"second-to-last" } |
| 20 | }.freeze | 20 | }.freeze |
| 21 | 21 | ||
| 22 | MONTH_NAMES = { | 22 | MONTH_NAMES = { |
| @@ -35,7 +35,8 @@ module RruleHumanizer | |||
| 35 | ordinals = ORDINAL_NAMES[loc] || ORDINAL_NAMES[:en] | 35 | ordinals = ORDINAL_NAMES[loc] || ORDINAL_NAMES[:en] |
| 36 | months = MONTH_NAMES[loc] || MONTH_NAMES[:en] | 36 | months = MONTH_NAMES[loc] || MONTH_NAMES[:en] |
| 37 | 37 | ||
| 38 | days = byday&.split(",")&.map do |d| | 38 | byday_values = byday&.split(",") |
| 39 | days = byday_values&.map do |d| | ||
| 39 | if d =~ /^(-?\d+)([A-Z]{2})$/ | 40 | if d =~ /^(-?\d+)([A-Z]{2})$/ |
| 40 | "#{ordinals[$1.to_i]} #{weekdays[$2]}" | 41 | "#{ordinals[$1.to_i]} #{weekdays[$2]}" |
| 41 | else | 42 | else |
| @@ -43,6 +44,32 @@ module RruleHumanizer | |||
| 43 | end | 44 | end |
| 44 | end | 45 | end |
| 45 | 46 | ||
| 47 | excluded_monthly_ordinal = nil | ||
| 48 | excluded_monthly_weekday = nil | ||
| 49 | selected_monthly_ordinals = nil | ||
| 50 | selected_monthly_weekday = nil | ||
| 51 | if freq == "MONTHLY" && byday_values.present? | ||
| 52 | ordinal_days = byday_values.map { |d| d.match(/^([1-5])([A-Z]{2})$/) } | ||
| 53 | if ordinal_days.all? | ||
| 54 | positions = ordinal_days.map { |match| match[1].to_i }.uniq.sort | ||
| 55 | weekdays_in_rule = ordinal_days.map { |match| match[2] }.uniq | ||
| 56 | if weekdays_in_rule.size == 1 | ||
| 57 | selected_monthly_ordinals = positions | ||
| 58 | selected_monthly_weekday = weekdays_in_rule.first | ||
| 59 | missing_positions = (1..5).to_a - positions | ||
| 60 | if positions.size == 4 && missing_positions.size == 1 | ||
| 61 | excluded_monthly_ordinal = missing_positions.first | ||
| 62 | excluded_monthly_weekday = selected_monthly_weekday | ||
| 63 | end | ||
| 64 | end | ||
| 65 | end | ||
| 66 | end | ||
| 67 | |||
| 68 | join_ordinals = lambda do |ordinal_values, conjunction| | ||
| 69 | names = ordinal_values.map { |ordinal| ordinals[ordinal] } | ||
| 70 | names.size > 1 ? "#{names[0..-2].join(', ')} #{conjunction} #{names.last}" : names.first | ||
| 71 | end | ||
| 72 | |||
| 46 | base = | 73 | base = |
| 47 | case loc | 74 | case loc |
| 48 | when :de | 75 | when :de |
| @@ -59,14 +86,26 @@ module RruleHumanizer | |||
| 59 | interval == 2 ? "Alle zwei Wochen" : "Wöchentlich" | 86 | interval == 2 ? "Alle zwei Wochen" : "Wöchentlich" |
| 60 | end | 87 | end |
| 61 | when "MONTHLY" | 88 | when "MONTHLY" |
| 62 | days ? "Jeden #{days.join(' und ')} im Monat" : "Monatlich" | 89 | if excluded_monthly_ordinal |
| 90 | "Jeden #{weekdays[excluded_monthly_weekday]} im Monat, außer dem #{ordinals[excluded_monthly_ordinal]} #{weekdays[excluded_monthly_weekday]}" | ||
| 91 | elsif selected_monthly_ordinals | ||
| 92 | "Jeden #{join_ordinals.call(selected_monthly_ordinals, 'und')} #{weekdays[selected_monthly_weekday]} im Monat" | ||
| 93 | else | ||
| 94 | days ? "Jeden #{days.join(' und ')} im Monat" : "Monatlich" | ||
| 95 | end | ||
| 63 | end | 96 | end |
| 64 | else | 97 | else |
| 65 | case freq | 98 | case freq |
| 66 | when "WEEKLY" | 99 | when "WEEKLY" |
| 67 | days ? "#{interval == 2 ? 'Every other' : 'Every'} #{days.join(' and ')}" : (interval == 2 ? "Every other week" : "Weekly") | 100 | days ? "#{interval == 2 ? 'Every other' : 'Every'} #{days.join(' and ')}" : (interval == 2 ? "Every other week" : "Weekly") |
| 68 | when "MONTHLY" | 101 | when "MONTHLY" |
| 69 | days ? "Every #{days.join(' and ')} of the month" : "Monthly" | 102 | if excluded_monthly_ordinal |
| 103 | "Every #{weekdays[excluded_monthly_weekday]} of the month, except the #{ordinals[excluded_monthly_ordinal]} #{weekdays[excluded_monthly_weekday]}" | ||
| 104 | elsif selected_monthly_ordinals | ||
| 105 | "Every #{join_ordinals.call(selected_monthly_ordinals, 'and')} #{weekdays[selected_monthly_weekday]} of the month" | ||
| 106 | else | ||
| 107 | days ? "Every #{days.join(' and ')} of the month" : "Monthly" | ||
| 108 | end | ||
| 70 | end | 109 | end |
| 71 | end | 110 | end |
| 72 | return nil unless base | 111 | return nil unless base |
diff --git a/app/models/node.rb b/app/models/node.rb index 6ca607d..a440c2f 100644 --- a/app/models/node.rb +++ b/app/models/node.rb | |||
| @@ -4,17 +4,23 @@ class Node < ApplicationRecord | |||
| 4 | 4 | ||
| 5 | # Associations | 5 | # Associations |
| 6 | has_many :pages, -> { order("revision ASC") }, :dependent => :destroy | 6 | has_many :pages, -> { order("revision ASC") }, :dependent => :destroy |
| 7 | belongs_to :head, :class_name => "Page", :foreign_key => :head_id, :dependent => :destroy, optional: true | 7 | has_many :node_actions, :dependent => :nullify |
| 8 | belongs_to :draft, :class_name => "Page", :foreign_key => :draft_id, :dependent => :destroy, optional: true | 8 | belongs_to :head, :class_name => "Page", :foreign_key => :head_id, optional: true |
| 9 | belongs_to :draft, :class_name => "Page", :foreign_key => :draft_id, optional: true | ||
| 10 | # Autosave pages carry no node_id, so has_many :pages does not cover | ||
| 11 | # them -- this dependent: :destroy is their only cleanup on node destroy. | ||
| 9 | belongs_to :autosave, :class_name => "Page", :foreign_key => :autosave_id, :dependent => :destroy, optional: true | 12 | belongs_to :autosave, :class_name => "Page", :foreign_key => :autosave_id, :dependent => :destroy, optional: true |
| 13 | |||
| 10 | has_many :permissions, :dependent => :destroy | 14 | has_many :permissions, :dependent => :destroy |
| 11 | has_many :events, :dependent => :destroy | 15 | has_many :events, :dependent => :destroy |
| 12 | belongs_to :lock_owner, :class_name => "User", :foreign_key => :locking_user_id, optional: true | 16 | belongs_to :lock_owner, :class_name => "User", :foreign_key => :locking_user_id, optional: true |
| 13 | 17 | ||
| 14 | # Callbacks | 18 | # Callbacks |
| 15 | after_create :initialize_empty_page | 19 | after_create :initialize_empty_page |
| 16 | before_save :check_for_changed_slug | 20 | before_save :check_for_changed_slug |
| 17 | after_save :update_unique_names_of_children | 21 | after_save :update_unique_names_of_children |
| 22 | before_destroy :refuse_destroy_with_children | ||
| 23 | before_destroy :refuse_destroying_trash_node | ||
| 18 | 24 | ||
| 19 | # Validations | 25 | # Validations |
| 20 | validates_length_of :slug, :within => 1..255, :unless => -> { parent_id.nil? || slug.blank? } | 26 | validates_length_of :slug, :within => 1..255, :unless => -> { parent_id.nil? || slug.blank? } |
| @@ -22,6 +28,21 @@ class Node < ApplicationRecord | |||
| 22 | validates_uniqueness_of :slug, :scope => :parent_id, :unless => -> { parent_id.nil? } | 28 | validates_uniqueness_of :slug, :scope => :parent_id, :unless => -> { parent_id.nil? } |
| 23 | validates_presence_of :parent_id, :unless => -> { Node.root.nil? } | 29 | validates_presence_of :parent_id, :unless => -> { Node.root.nil? } |
| 24 | 30 | ||
| 31 | validate :reserved_slug_stays_reserved | ||
| 32 | validate :no_head_inside_trash | ||
| 33 | validates :default_template_name, | ||
| 34 | :inclusion => { :in => ->(_) { Page.custom_templates } }, | ||
| 35 | :allow_blank => true, | ||
| 36 | :if => :default_template_name_changed? | ||
| 37 | |||
| 38 | # Everything outside the Trash subtree, the Trash node included. | ||
| 39 | # Relies on unique_name being authoritative for tree position -- | ||
| 40 | # the same trust public routing places in it. | ||
| 41 | scope :not_in_trash, -> { | ||
| 42 | where.not(:unique_name => CccConventions::TRASH_SLUG) | ||
| 43 | .where("unique_name NOT LIKE ?", "#{CccConventions::TRASH_SLUG}/%") | ||
| 44 | } | ||
| 45 | |||
| 25 | # Class methods | 46 | # Class methods |
| 26 | 47 | ||
| 27 | # Returns a page for a given node. If no revision is supplied, it returns | 48 | # Returns a page for a given node. If no revision is supplied, it returns |
| @@ -48,6 +69,19 @@ class Node < ApplicationRecord | |||
| 48 | nil | 69 | nil |
| 49 | end | 70 | end |
| 50 | 71 | ||
| 72 | # The Trash container node. Lazily self-creating and idempotent, so | ||
| 73 | # every environment acquires it on first touch. | ||
| 74 | # Never call from validations. the positional predicates below | ||
| 75 | # exist for that. | ||
| 76 | def self.trash | ||
| 77 | root.children.find_by(:slug => CccConventions::TRASH_SLUG) || | ||
| 78 | root.children.create!(:slug => CccConventions::TRASH_SLUG).tap do |node| | ||
| 79 | Globalize.with_locale(I18n.default_locale) do | ||
| 80 | node.draft.update!(:title => "Trash") | ||
| 81 | end | ||
| 82 | end | ||
| 83 | end | ||
| 84 | |||
| 51 | # Instance Methods | 85 | # Instance Methods |
| 52 | 86 | ||
| 53 | # Acquires (or reaffirms) the editing lock without creating a draft or | 87 | # Acquires (or reaffirms) the editing lock without creating a draft or |
| @@ -194,6 +228,10 @@ class Node < ApplicationRecord | |||
| 194 | # Return nil if nothing to publish and no staged changes | 228 | # Return nil if nothing to publish and no staged changes |
| 195 | return nil unless self.draft || staged_slug || staged_parent_id | 229 | return nil unless self.draft || staged_slug || staged_parent_id |
| 196 | 230 | ||
| 231 | if in_trash? || trash_node? | ||
| 232 | raise ActiveRecord::RecordInvalid.new(self), "Cannot publish a node in the Trash" | ||
| 233 | end | ||
| 234 | |||
| 197 | path_before = self.unique_name | 235 | path_before = self.unique_name |
| 198 | 236 | ||
| 199 | ActiveRecord::Base.transaction do | 237 | ActiveRecord::Base.transaction do |
| @@ -258,6 +296,117 @@ class Node < ApplicationRecord | |||
| 258 | end | 296 | end |
| 259 | end | 297 | end |
| 260 | 298 | ||
| 299 | |||
| 300 | # Moves this node and its subtree into the Trash. Demotes every head | ||
| 301 | # in the subtree first (aggregators and search operate on heads | ||
| 302 | # regardless of tree position); where a node has no draft, the former | ||
| 303 | # head becomes its draft so content stays editable and restorable -- | ||
| 304 | # otherwise the former head remains a plain revision. One log entry, | ||
| 305 | # at the root, carrying the leaving-public-view snapshot. | ||
| 306 | def trash! current_user = nil | ||
| 307 | return nil if in_trash? | ||
| 308 | raise ActiveRecord::RecordInvalid.new(self), "The Trash node itself cannot be trashed" if trash_node? | ||
| 309 | |||
| 310 | ActiveRecord::Base.transaction do | ||
| 311 | path_before = unique_name | ||
| 312 | was_published = head_id.present? | ||
| 313 | final_published_at = head&.published_at | ||
| 314 | |||
| 315 | demoted = 0 | ||
| 316 | ([self] + descendants.to_a).each do |node| | ||
| 317 | next unless node.head_id | ||
| 318 | former = node.head | ||
| 319 | node.head = nil | ||
| 320 | node.draft_id = former.id if node.draft_id.nil? | ||
| 321 | node.save! | ||
| 322 | demoted += 1 | ||
| 323 | end | ||
| 324 | |||
| 325 | self.reload | ||
| 326 | move_to_child_of(Node.trash) | ||
| 327 | self.reload | ||
| 328 | update_unique_name | ||
| 329 | send(:update_unique_names_of_children) | ||
| 330 | unlock! | ||
| 331 | |||
| 332 | metadata = { :path => { "from" => path_before, "to" => unique_name } } | ||
| 333 | metadata[:was_published] = true if was_published | ||
| 334 | metadata[:final_published_at] = final_published_at.iso8601 if final_published_at | ||
| 335 | metadata[:demoted_heads] = demoted if demoted > 0 | ||
| 336 | |||
| 337 | NodeAction.record!(:node => self, :user => current_user, :action => "trash", **metadata) | ||
| 338 | self | ||
| 339 | end | ||
| 340 | end | ||
| 341 | |||
| 342 | # Returns the node to the living tree under a chosen parent. The | ||
| 343 | # subtree comes back exactly as it sits in the Trash: all drafts, | ||
| 344 | # nothing published. Republication is a separate, witnessed act | ||
| 345 | # per node. | ||
| 346 | def restore_from_trash! new_parent, current_user = nil | ||
| 347 | return nil unless in_trash? | ||
| 348 | |||
| 349 | if new_parent.nil? || new_parent == self || descendants.include?(new_parent) || | ||
| 350 | new_parent.trash_node? || new_parent.in_trash? | ||
| 351 | raise ActiveRecord::RecordInvalid.new(self), "Restore target must be a living node" | ||
| 352 | end | ||
| 353 | |||
| 354 | ActiveRecord::Base.transaction do | ||
| 355 | path_before = unique_name | ||
| 356 | move_to_child_of(new_parent) | ||
| 357 | self.reload | ||
| 358 | update_unique_name | ||
| 359 | send(:update_unique_names_of_children) | ||
| 360 | |||
| 361 | NodeAction.record!(:node => self, :user => current_user, :action => "restore_from_trash", | ||
| 362 | :path => { "from" => path_before, "to" => unique_name }) | ||
| 363 | self | ||
| 364 | end | ||
| 365 | end | ||
| 366 | |||
| 367 | # Final deletion -- only from inside the Trash. Removes the whole | ||
| 368 | # subtree, deepest first, each node through a real destroy! so every | ||
| 369 | # per-node cascade runs (the categorical difference from the old | ||
| 370 | # delete_all nuke). refuse_destroy_with_children on bare destroy is | ||
| 371 | # untouched | ||
| 372 | # One log entry at the root, per the subtree rule, written before the | ||
| 373 | # rows die. | ||
| 374 | def destroy_from_trash! current_user = nil | ||
| 375 | raise ActiveRecord::RecordInvalid.new(self), "Nodes are only destroyed from the Trash" unless in_trash? | ||
| 376 | |||
| 377 | ActiveRecord::Base.transaction do | ||
| 378 | doomed = self_and_descendants_ordered_with_level | ||
| 379 | .sort_by { |_, level| -level } | ||
| 380 | .map(&:first) | ||
| 381 | |||
| 382 | metadata = { :path => unique_name } | ||
| 383 | metadata[:destroyed_descendants] = doomed.size - 1 if doomed.size > 1 | ||
| 384 | |||
| 385 | NodeAction.record!(:node => self, :user => current_user, :action => "destroy", **metadata) | ||
| 386 | doomed.each(&:destroy!) | ||
| 387 | end | ||
| 388 | end | ||
| 389 | |||
| 390 | # The most recent trash entry. Its path.from is the restore hint. | ||
| 391 | def last_trash_entry | ||
| 392 | node_actions.where(:action => "trash").order(:occurred_at => :desc, :id => :desc).first | ||
| 393 | end | ||
| 394 | |||
| 395 | # The node's pre-trash parent, if a living node still answers to | ||
| 396 | # that path. Nil when the parent was itself trashed (its unique_name | ||
| 397 | # changed) or deleted; a different node that has since taken over | ||
| 398 | # the path is a legitimate suggestion. | ||
| 399 | def suggested_restore_parent | ||
| 400 | from = last_trash_entry&.metadata&.dig("path", "from") | ||
| 401 | return nil unless from | ||
| 402 | |||
| 403 | parent_path = from.rpartition("/").first | ||
| 404 | return Node.root if parent_path.empty? | ||
| 405 | |||
| 406 | candidate = Node.find_by(:unique_name => parent_path) | ||
| 407 | candidate unless candidate.nil? || candidate.in_trash? || candidate.trash_node? | ||
| 408 | end | ||
| 409 | |||
| 261 | # returns an array with all parts of a unique_name rather than a string | 410 | # returns an array with all parts of a unique_name rather than a string |
| 262 | def unique_path | 411 | def unique_path |
| 263 | unique_name.to_s.split("/") | 412 | unique_name.to_s.split("/") |
| @@ -268,9 +417,12 @@ class Node < ApplicationRecord | |||
| 268 | parent.nil? ? [slug] : parent.path_to_root.push(slug) | 417 | parent.nil? ? [slug] : parent.path_to_root.push(slug) |
| 269 | end | 418 | end |
| 270 | 419 | ||
| 420 | def computed_unique_name | ||
| 421 | path_to_root[1..-1].join("/") # excluding root | ||
| 422 | end | ||
| 423 | |||
| 271 | def current_unique_name | 424 | def current_unique_name |
| 272 | path = path_to_root[1..-1] # excluding root | 425 | self.unique_name = computed_unique_name |
| 273 | self.unique_name = path.join("/") | ||
| 274 | end | 426 | end |
| 275 | 427 | ||
| 276 | def update_unique_name | 428 | def update_unique_name |
| @@ -310,6 +462,20 @@ class Node < ApplicationRecord | |||
| 310 | autosave || draft || head | 462 | autosave || draft || head |
| 311 | end | 463 | end |
| 312 | 464 | ||
| 465 | def trash_node? | ||
| 466 | parent&.root? && slug == CccConventions::TRASH_SLUG | ||
| 467 | end | ||
| 468 | |||
| 469 | # Inside the Trash subtree. False for the Trash node itself. | ||
| 470 | def in_trash? | ||
| 471 | current = parent | ||
| 472 | while current | ||
| 473 | return true if current.trash_node? | ||
| 474 | current = current.parent | ||
| 475 | end | ||
| 476 | false | ||
| 477 | end | ||
| 478 | |||
| 313 | # Returns immutable node id for all new nodes so that the atom feed entry ids | 479 | # Returns immutable node id for all new nodes so that the atom feed entry ids |
| 314 | # stay the same eventhough the slug or positions changes. | 480 | # stay the same eventhough the slug or positions changes. |
| 315 | # Can be removed after a year or so ;) | 481 | # Can be removed after a year or so ;) |
| @@ -350,7 +516,7 @@ class Node < ApplicationRecord | |||
| 350 | end | 516 | end |
| 351 | 517 | ||
| 352 | def self.drafts_and_autosaves(current_user_id: nil) | 518 | def self.drafts_and_autosaves(current_user_id: nil) |
| 353 | scope = where("draft_id IS NOT NULL OR autosave_id IS NOT NULL") | 519 | scope = where("draft_id IS NOT NULL OR autosave_id IS NOT NULL").not_in_trash |
| 354 | return scope.order("updated_at DESC") unless current_user_id | 520 | return scope.order("updated_at DESC") unless current_user_id |
| 355 | 521 | ||
| 356 | scope.order( | 522 | scope.order( |
| @@ -358,6 +524,15 @@ class Node < ApplicationRecord | |||
| 358 | ) | 524 | ) |
| 359 | end | 525 | end |
| 360 | 526 | ||
| 527 | # Nodes are never destroyed recursively | ||
| 528 | # Descendants must be removed or reparented individually first. | ||
| 529 | # The Trash feature will be the ordinary path to deletion. | ||
| 530 | def refuse_destroy_with_children | ||
| 531 | return unless children.exists? | ||
| 532 | errors.add(:base, "Cannot destroy a node that still has children") | ||
| 533 | throw :abort | ||
| 534 | end | ||
| 535 | |||
| 361 | def self.recently_changed | 536 | def self.recently_changed |
| 362 | includes(:head).where( | 537 | includes(:head).where( |
| 363 | "pages.updated_at < ? AND pages.updated_at > ? AND nodes.parent_id IS NOT NULL", | 538 | "pages.updated_at < ? AND pages.updated_at > ? AND nodes.parent_id IS NOT NULL", |
| @@ -414,4 +589,36 @@ class Node < ApplicationRecord | |||
| 414 | end | 589 | end |
| 415 | end | 590 | end |
| 416 | end | 591 | end |
| 592 | |||
| 593 | private | ||
| 594 | |||
| 595 | def reserved_slug_stays_reserved | ||
| 596 | if parent&.root? && !trash_node_already_me? | ||
| 597 | errors.add(:slug, "is reserved for the Trash") if slug == CccConventions::TRASH_SLUG | ||
| 598 | errors.add(:staged_slug, "is reserved for the Trash") if staged_slug == CccConventions::TRASH_SLUG | ||
| 599 | end | ||
| 600 | |||
| 601 | if persisted? && slug_was == CccConventions::TRASH_SLUG && Node.find(id).trash_node? | ||
| 602 | errors.add(:slug, "of the Trash node cannot change") if slug_changed? | ||
| 603 | errors.add(:parent_id, "of the Trash node cannot change") if parent_id_changed? | ||
| 604 | errors.add(:staged_slug, "must stay empty on the Trash node") if staged_slug.present? | ||
| 605 | errors.add(:staged_parent_id, "must stay empty on the Trash node") if staged_parent_id.present? | ||
| 606 | end | ||
| 607 | end | ||
| 608 | |||
| 609 | def trash_node_already_me? | ||
| 610 | existing = Node.root&.children&.find_by(:slug => CccConventions::TRASH_SLUG) | ||
| 611 | existing.nil? || existing.id == id | ||
| 612 | end | ||
| 613 | |||
| 614 | def no_head_inside_trash | ||
| 615 | return unless head_id.present? | ||
| 616 | errors.add(:head_id, "cannot exist inside the Trash") if in_trash? || trash_node? | ||
| 617 | end | ||
| 618 | |||
| 619 | def refuse_destroying_trash_node | ||
| 620 | return unless trash_node? | ||
| 621 | errors.add(:base, "The Trash node cannot be destroyed") | ||
| 622 | throw :abort | ||
| 623 | end | ||
| 417 | end | 624 | end |
diff --git a/app/models/node_action.rb b/app/models/node_action.rb index 39935c7..13bd5ba 100644 --- a/app/models/node_action.rb +++ b/app/models/node_action.rb | |||
| @@ -8,95 +8,96 @@ class NodeAction < ApplicationRecord | |||
| 8 | 8 | ||
| 9 | # == Metadata contract == | 9 | # == Metadata contract == |
| 10 | # | 10 | # |
| 11 | # metadata is written once at creation and never updated. It is the | 11 | # metadata is written once at creation, never updated. It is the |
| 12 | # single place anything that must survive deletion of the referenced | 12 | # single place anything that must survive deletion of the referenced |
| 13 | # rows lives; node_id/page_id/user_id are lookup and ordering only. | 13 | # rows lives; node_id/page_id/user_id are lookup and ordering only. |
| 14 | # All keys are strings. Pairs are always {"from" => x, "to" => y} -- | 14 | # All keys are strings. Pairs are always {"from" => x, "to" => y}. |
| 15 | # never _old/_new suffixes. Optional pairs are written only when the | 15 | # Optional pairs only when the sides differ; booleans only when |
| 16 | # two sides differ; required keys are always present. All titles and | 16 | # true; required keys always present. Titles and names are read |
| 17 | # names are read pinned to I18n.default_locale unless inside a | 17 | # pinned to I18n.default_locale unless inside a locale-keyed block. |
| 18 | # locale-keyed block. | ||
| 19 | # | 18 | # |
| 20 | # Baseline, every entry (written here, not by call sites): | 19 | # Baseline, every entry (written here, not by call sites): |
| 21 | # "username" -- actor's login at write time | 20 | # "username" -- actor's login at write time |
| 22 | # "human_readable_node_name" -- node title, default locale | 21 | # "human_readable_node_name" -- node title, default locale |
| 23 | # | 22 | # |
| 24 | # "create": | 23 | # "create": |
| 25 | # "title" -- initial title, default locale | 24 | # "title" -- initial title, flat string (NOT a pair) |
| 26 | # "path" -- unique path at creation time. Historical value only: | 25 | # "path" -- unique path at creation, flat string. Historical |
| 27 | # later renames/moves do NOT update old entries. Never | 26 | # value only; never a join key. |
| 28 | # use as a join key; node_id is the join key while the | ||
| 29 | # node lives. | ||
| 30 | # | 27 | # |
| 31 | # "publish" (any promotion of a page to head; the diff against the | 28 | # "publish" (any promotion to head; diff computed BEFORE head is |
| 32 | # outgoing head is computed BEFORE head is re-pointed, over the | 29 | # re-pointed, over the union of both pages' locales, by head_diff -- |
| 33 | # union of both pages' locales, by the shared diff function also | 30 | # shared with the backfill): |
| 34 | # used by the backfill): | 31 | # "via" -- "draft" | "revision" (rollback). Always written; |
| 35 | # "via" -- "draft" (ordinary publish) | "revision" (rollback | 32 | # absent means a pre-contract entry. |
| 36 | # via restore_revision!). Always written; absent | 33 | # "title" -- pair, always; "from" null on first publish |
| 37 | # means a pre-contract entry. | 34 | # "author" -- pair, when the byline changed (incl. first publish) |
| 38 | # "title" -- pair, always present. "from" is null on a first | 35 | # "tags" -- pair of arrays, when changed |
| 39 | # publish (no outgoing head). | 36 | # "assets_changed", "template_changed", |
| 40 | # "author" -- pair, only when the byline (page.user) changed | ||
| 41 | # "tags" -- pair of arrays, only when changed | ||
| 42 | # "assets_changed", "template_changed" | ||
| 43 | # -- booleans, only when true; page_id links to the | ||
| 44 | # revision for the real diff (never stored here) | ||
| 45 | # "abstract_changed", "body_changed" | 37 | # "abstract_changed", "body_changed" |
| 46 | # -- booleans for the default locale, only when true | 38 | # -- the last two for the default locale; page_id links |
| 47 | # "translation_diff" -- only when any non-default locale differs: | 39 | # to the revision for the real diff (never stored) |
| 40 | # "translation_diff" -- only when a non-default locale differs: | ||
| 48 | # { "<locale>" => { | 41 | # { "<locale>" => { |
| 49 | # "status" -- "added" | "removed" | "changed" | 42 | # "status" -- "added" | "removed" | "changed" |
| 50 | # "title" -- pair; "from" null when added, | 43 | # "title" -- pair, only when it differs; "from" null when |
| 51 | # "to" null when removed | 44 | # added, "to" null when removed |
| 52 | # "abstract_changed", "body_changed" -- booleans, only when | 45 | # "abstract_changed", "body_changed" -- status "changed" only |
| 53 | # true, only for status "changed" | ||
| 54 | # } } | 46 | # } } |
| 55 | # | 47 | # |
| 56 | # "move" (reparenting and/or unique-path change; one entry at the | 48 | # "move" (reparent and/or path change; one entry at the subtree |
| 57 | # subtree root, descendants get none -- a descendant's own zoomed | 49 | # root, descendants get none): |
| 58 | # view will not show path history): | 50 | # "path" -- pair |
| 59 | # "path" -- pair | ||
| 60 | # | 51 | # |
| 61 | # Reserved for the Trash feature, final shape decided there: | 52 | # "trash" (subtree into the Trash; every head in the subtree is |
| 62 | # "demote" (via "trash" | "depublish"; carries the leaving-public- | 53 | # demoted first; one entry at the root; snapshots the |
| 63 | # view snapshot: head presence, final published_at), "trash", | 54 | # leaving-public-view state, since Trash holds no heads and destroy |
| 64 | # "restore_from_trash", "destroy" (final path only; publish-state | 55 | # can no longer know): |
| 65 | # snapshot lives on the entry that removed it from public view). | 56 | # "path" -- pair; "from" doubles as the restore hint |
| 66 | # Whether trash logs one entry or a trash+demote pair is decided | 57 | # "was_published" -- boolean |
| 67 | # with that feature. | 58 | # "demoted_heads" -- integer count, only when positive |
| 59 | # "final_published_at" -- ISO8601 string, only when present | ||
| 68 | # | 60 | # |
| 69 | # Backfilled entries mirror this vocabulary exactly. Their diff | 61 | # "restore_from_trash" (reparent back to a living node; returns as |
| 70 | # content is computed, not guessed (consecutive revisions still | 62 | # drafts, republication is a separate witnessed act): |
| 71 | # exist); only actor (from page.editor) and occurred_at are | 63 | # "path" -- pair |
| 72 | # inferred, and inferred_from names the heuristic per entry | ||
| 73 | # ("from_page_revision", "from_published_at_heuristic"). | ||
| 74 | # inferred_from null = witnessed live. | ||
| 75 | # | 64 | # |
| 76 | # The "locale" column is currently written by no verb (it belonged | 65 | # "destroy" (only from inside the Trash, never with children; the |
| 77 | # to the retired translation_destroy) and is retained for backfill | 66 | # entry is written in the same transaction before the row dies): |
| 78 | # or future draft-scoped verbs. | 67 | # "path" -- final path, flat string (create-symmetric) |
| 68 | # "destroyed_descendants" -- integer, only when positive; one entry | ||
| 69 | # at the root, per the subtree rule. | ||
| 79 | # | 70 | # |
| 80 | # This log records; it does not undo. Reversibility stays in | 71 | # Reserved: "demote" (via "trash" | "depublish") for an explicit |
| 81 | # restore_revision! and the revisions system. No IP, session, or | 72 | # depublish workflow, if ever built. |
| 82 | # user agent, ever. Success only -- rejected attempts are not | 73 | # |
| 83 | # logged. | 74 | # Backfilled entries mirror this vocabulary; diff content is |
| 75 | # computed, only actor (page.editor) and occurred_at are inferred, | ||
| 76 | # inferred_from names the heuristic ("from_node_created_at", | ||
| 77 | # "from_page_revision"). Null = witnessed live. | ||
| 78 | # | ||
| 79 | # The "locale" column is written by no verb; retained. | ||
| 80 | # | ||
| 81 | # This log records; it does not undo. No IP, session, or user | ||
| 82 | # agent, ever. Success only. | ||
| 84 | 83 | ||
| 85 | def self.record!(node:, action:, user: nil, page: nil, locale: nil, **extra) | 84 | def self.record!(node:, action:, user: nil, page: nil, locale: nil, |
| 85 | occurred_at: nil, inferred_from: nil, **extra) | ||
| 86 | create!( | 86 | create!( |
| 87 | :node => node, | 87 | :node => node, |
| 88 | :page => page, | 88 | :page => page, |
| 89 | :user => user, | 89 | :user => user, |
| 90 | :action => action, | 90 | :action => action, |
| 91 | :locale => locale, | 91 | :locale => locale, |
| 92 | :occurred_at => Time.now, | 92 | :occurred_at => occurred_at || Time.now, |
| 93 | :metadata => { | 93 | :inferred_from => inferred_from, |
| 94 | :metadata => { | ||
| 94 | "username" => user&.login, | 95 | "username" => user&.login, |
| 95 | "human_readable_node_name" => Globalize.with_locale(I18n.default_locale) { | 96 | "human_readable_node_name" => Globalize.with_locale(I18n.default_locale) { |
| 96 | node&.head&.title || node&.draft&.title | 97 | node&.head&.title || node&.draft&.title |
| 97 | }, | 98 | }, |
| 98 | }.merge(extra.stringify_keys) | 99 | }.merge(extra.stringify_keys) |
| 99 | ) | 100 | ) |
| 100 | end | 101 | end |
| 101 | 102 | ||
| 102 | # Computes the publish-entry diff between an outgoing head and the | 103 | # Computes the publish-entry diff between an outgoing head and the |
| @@ -113,7 +114,10 @@ class NodeAction < ApplicationRecord | |||
| 113 | title_of = ->(page) { page&.translations&.find_by(:locale => default)&.title } | 114 | title_of = ->(page) { page&.translations&.find_by(:locale => default)&.title } |
| 114 | diff = { :title => { "from" => title_of.call(old_page), | 115 | diff = { :title => { "from" => title_of.call(old_page), |
| 115 | "to" => title_of.call(new_page) } } | 116 | "to" => title_of.call(new_page) } } |
| 116 | return diff unless old_page | 117 | unless old_page |
| 118 | diff[:author] = { "from" => nil, "to" => new_page.user&.login } if new_page.user | ||
| 119 | return diff | ||
| 120 | end | ||
| 117 | 121 | ||
| 118 | old_author, new_author = old_page.user&.login, new_page.user&.login | 122 | old_author, new_author = old_page.user&.login, new_page.user&.login |
| 119 | diff[:author] = { "from" => old_author, "to" => new_author } if old_author != new_author | 123 | diff[:author] = { "from" => old_author, "to" => new_author } if old_author != new_author |
diff --git a/app/models/page.rb b/app/models/page.rb index e5a8d9d..f33d88d 100644 --- a/app/models/page.rb +++ b/app/models/page.rb | |||
| @@ -11,11 +11,21 @@ class Page < ApplicationRecord | |||
| 11 | 11 | ||
| 12 | translates :title, :abstract, :body # Globalize2 | 12 | translates :title, :abstract, :body # Globalize2 |
| 13 | 13 | ||
| 14 | # Template names render as filesystem paths; only names actually | ||
| 15 | # present in the public template directory are acceptable. Validated | ||
| 16 | # only on change so legacy rows whose template file has since | ||
| 17 | # vanished stay saveable -- valid_template already falls back to | ||
| 18 | # standard_template for those at render time. | ||
| 19 | validates :template_name, | ||
| 20 | :inclusion => { :in => ->(_) { Page.custom_templates } }, | ||
| 21 | :allow_blank => true, | ||
| 22 | :if => :template_name_changed? | ||
| 23 | |||
| 14 | # Associations | 24 | # Associations |
| 15 | belongs_to :node, optional: true | 25 | belongs_to :node, optional: true |
| 16 | belongs_to :user, optional: true | 26 | belongs_to :user, optional: true |
| 17 | belongs_to :editor, :class_name => "User", optional: true | 27 | belongs_to :editor, :class_name => "User", optional: true |
| 18 | has_many :related_assets | 28 | has_many :related_assets, :dependent => :destroy |
| 19 | has_many :assets, -> { order("position ASC") }, :through => :related_assets | 29 | has_many :assets, -> { order("position ASC") }, :through => :related_assets |
| 20 | 30 | ||
| 21 | # Named scopes | 31 | # Named scopes |
| @@ -77,7 +87,10 @@ class Page < ApplicationRecord | |||
| 77 | .paginate(:page => page, :per_page => options[:limit]) | 87 | .paginate(:page => page, :per_page => options[:limit]) |
| 78 | end | 88 | end |
| 79 | 89 | ||
| 80 | scope.order("#{options[:order_by]} #{direction}") | 90 | column = options[:order_by].to_s.sub(/\Apages\./, "") |
| 91 | column = "id" unless %w[id published_at created_at updated_at].include?(column) | ||
| 92 | |||
| 93 | scope.order("pages.#{column} #{direction}") | ||
| 81 | .paginate(:page => page, :per_page => options[:limit]) | 94 | .paginate(:page => page, :per_page => options[:limit]) |
| 82 | end | 95 | end |
| 83 | 96 | ||
diff --git a/app/models/user.rb b/app/models/user.rb index 92ac33a..5e47ae7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb | |||
| @@ -1,6 +1,10 @@ | |||
| 1 | require 'digest/sha1' | 1 | require 'digest/sha1' |
| 2 | 2 | ||
| 3 | class User < ApplicationRecord | 3 | class User < ApplicationRecord |
| 4 | has_secure_password(validations: false) | ||
| 5 | |||
| 6 | alias_method :authenticate_bcrypt, :authenticate | ||
| 7 | |||
| 4 | # Mixins and Plugins | 8 | # Mixins and Plugins |
| 5 | include Authentication | 9 | include Authentication |
| 6 | include Authentication::ByPassword | 10 | include Authentication::ByPassword |
| @@ -23,9 +27,30 @@ class User < ApplicationRecord | |||
| 23 | 27 | ||
| 24 | # Authenticates a user by their login name and unencrypted password. Returns the user or nil. | 28 | # Authenticates a user by their login name and unencrypted password. Returns the user or nil. |
| 25 | def self.authenticate(login, password) | 29 | def self.authenticate(login, password) |
| 26 | return nil if login.blank? || password.blank? | 30 | return if login.blank? || password.blank? |
| 27 | u = find_by_login(login) # need to get the salt | 31 | |
| 28 | u && u.authenticated?(password) ? u : nil | 32 | user = find_by(login: login) |
| 33 | return unless user | ||
| 34 | |||
| 35 | user&.authenticate(password) | ||
| 36 | end | ||
| 37 | |||
| 38 | def authenticate(password) | ||
| 39 | if password_digest.present? | ||
| 40 | return self if authenticate_bcrypt(password) | ||
| 41 | return nil | ||
| 42 | end | ||
| 43 | |||
| 44 | return nil unless authenticate_legacy(password) | ||
| 45 | |||
| 46 | transaction do | ||
| 47 | self.password = password | ||
| 48 | self.crypted_password = nil | ||
| 49 | self.salt = nil | ||
| 50 | save!(validate: false) | ||
| 51 | end | ||
| 52 | |||
| 53 | self | ||
| 29 | end | 54 | end |
| 30 | 55 | ||
| 31 | # TODO: Do we really want to have downcase logins only? | 56 | # TODO: Do we really want to have downcase logins only? |
