diff options
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/asset.rb | 8 | ||||
| -rw-r--r-- | app/models/asset_license.rb | 15 | ||||
| -rw-r--r-- | app/models/concerns/file_attachment.rb | 11 | ||||
| -rw-r--r-- | app/models/node_action.rb | 8 | ||||
| -rw-r--r-- | app/models/related_asset.rb | 9 |
5 files changed, 49 insertions, 2 deletions
diff --git a/app/models/asset.rb b/app/models/asset.rb index aca0ee8..8bea1b3 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb | |||
| @@ -1,12 +1,18 @@ | |||
| 1 | class Asset < ApplicationRecord | 1 | class Asset < ApplicationRecord |
| 2 | IMAGE_CONTENT_TYPES = ["image/gif", "image/jpeg", "image/png", "image/webp"] | ||
| 2 | 3 | ||
| 3 | include FileAttachment | 4 | include FileAttachment |
| 4 | 5 | ||
| 5 | has_many :related_assets, :dependent => :destroy | 6 | has_many :related_assets, :dependent => :destroy |
| 6 | has_many :pages, :through => :related_assets | 7 | has_many :pages, :through => :related_assets |
| 7 | 8 | ||
| 8 | scope :images, -> { where(:upload_content_type => ["image/gif", "image/jpeg", "image/png", "image/webp"]) } | 9 | scope :images, -> { where(:upload_content_type => IMAGE_CONTENT_TYPES) } |
| 9 | scope :documents, -> { where(:upload_content_type => ["application/pdf", "text/plain", "text/rtf"]) } | 10 | scope :documents, -> { where(:upload_content_type => ["application/pdf", "text/plain", "text/rtf"]) } |
| 10 | scope :audio, -> { where(:upload_content_type => ["audio/mpeg", "audio/x-m4a", "audio/wav", "audio/x-wav"]) } | 11 | scope :audio, -> { where(:upload_content_type => ["audio/mpeg", "audio/x-m4a", "audio/wav", "audio/x-wav"]) } |
| 11 | 12 | ||
| 13 | validates :license_key, inclusion: { in: -> { AssetLicense.keys } }, allow_blank: true | ||
| 14 | |||
| 15 | def image? | ||
| 16 | IMAGE_CONTENT_TYPES.include?(upload_content_type) | ||
| 17 | end | ||
| 12 | end | 18 | end |
diff --git a/app/models/asset_license.rb b/app/models/asset_license.rb new file mode 100644 index 0000000..ca66d40 --- /dev/null +++ b/app/models/asset_license.rb | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | class AssetLicense | ||
| 2 | Entry = Struct.new(:key, :url, :requires_attribution, :style, keyword_init: true) | ||
| 3 | |||
| 4 | DICTIONARY = YAML.load_file(Rails.root.join("config", "asset_licenses.yml")).freeze | ||
| 5 | |||
| 6 | def self.keys | ||
| 7 | DICTIONARY.keys | ||
| 8 | end | ||
| 9 | |||
| 10 | def self.find(key) | ||
| 11 | entry = DICTIONARY[key.to_s] if key.present? | ||
| 12 | return nil unless entry | ||
| 13 | Entry.new(key: key.to_s, url: entry["url"], requires_attribution: entry["requires_attribution"], style: entry["style"]) | ||
| 14 | end | ||
| 15 | end | ||
diff --git a/app/models/concerns/file_attachment.rb b/app/models/concerns/file_attachment.rb index 613a6f0..e7a33c5 100644 --- a/app/models/concerns/file_attachment.rb +++ b/app/models/concerns/file_attachment.rb | |||
| @@ -52,6 +52,14 @@ module FileAttachment | |||
| 52 | build_upload_proxy | 52 | build_upload_proxy |
| 53 | end | 53 | end |
| 54 | 54 | ||
| 55 | def has_variant?(style) | ||
| 56 | upload_file_name.present? && File.exist?(file_path(style)) | ||
| 57 | end | ||
| 58 | |||
| 59 | def previewable? | ||
| 60 | has_variant?(:medium) | ||
| 61 | end | ||
| 62 | |||
| 55 | private | 63 | private |
| 56 | 64 | ||
| 57 | def build_upload_proxy | 65 | def build_upload_proxy |
| @@ -106,7 +114,8 @@ module FileAttachment | |||
| 106 | end | 114 | end |
| 107 | 115 | ||
| 108 | def sanitize_filename(filename) | 116 | def sanitize_filename(filename) |
| 109 | File.basename(filename).gsub(/[^\w\.\-]/, '_') | 117 | name = File.basename(filename).unicode_normalize(:nfc) |
| 118 | name.gsub(/(?u)[^\w\.\-]/, '_') | ||
| 110 | end | 119 | end |
| 111 | 120 | ||
| 112 | # Proxy object returned by asset.upload, providing the Paperclip-compatible | 121 | # Proxy object returned by asset.upload, providing the Paperclip-compatible |
diff --git a/app/models/node_action.rb b/app/models/node_action.rb index 13bd5ba..63a99ae 100644 --- a/app/models/node_action.rb +++ b/app/models/node_action.rb | |||
| @@ -166,4 +166,12 @@ class NodeAction < ApplicationRecord | |||
| 166 | def subject_name | 166 | def subject_name |
| 167 | metadata["human_readable_node_name"] || node&.unique_name || "deleted node" | 167 | metadata["human_readable_node_name"] || node&.unique_name || "deleted node" |
| 168 | end | 168 | end |
| 169 | |||
| 170 | def diff_link_params | ||
| 171 | prev = NodeAction.where(node_id: node_id, action: "publish") | ||
| 172 | .where("id < ?", id) | ||
| 173 | .order(id: :desc).first | ||
| 174 | return nil unless prev&.page && page | ||
| 175 | { start_revision: prev.page.revision, end_revision: page.revision } | ||
| 176 | end | ||
| 169 | end | 177 | end |
diff --git a/app/models/related_asset.rb b/app/models/related_asset.rb index 8f16460..62000cc 100644 --- a/app/models/related_asset.rb +++ b/app/models/related_asset.rb | |||
| @@ -5,4 +5,13 @@ class RelatedAsset < ApplicationRecord | |||
| 5 | acts_as_list :scope => :page_id | 5 | acts_as_list :scope => :page_id |
| 6 | 6 | ||
| 7 | default_scope -> { order("position ASC") } | 7 | default_scope -> { order("position ASC") } |
| 8 | |||
| 9 | validate :headline_only_for_images | ||
| 10 | |||
| 11 | private | ||
| 12 | |||
| 13 | def headline_only_for_images | ||
| 14 | return unless asset | ||
| 15 | errors.add(:headline, "can only be set on image assets") if headline? && !asset.image? | ||
| 16 | end | ||
| 8 | end | 17 | end |
