diff --git a/app/models/collection.rb b/app/models/collection.rb index d39e276a2..cb4873bba 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -2,6 +2,7 @@ class Collection < ApplicationRecord include Followable include CaberObject include Linkable + include Sluggable acts_as_federails_actor username_field: :slug, name_field: :name, profile_url_method: :url_for, actor_type: "Collection", include_in_user_count: false @@ -9,9 +10,6 @@ class Collection < ApplicationRecord has_many :collections, dependent: :nullify belongs_to :collection, optional: true validates :name, uniqueness: {case_sensitive: false} - validates :slug, uniqueness: true - - before_validation :slugify_name, if: :name_changed? default_scope { order(:name) } # returns all collections at and below given ids @@ -74,10 +72,4 @@ def self.ransackable_attributes(_auth_object = nil) def self.ransackable_associations(_auth_object = nil) ["collection", "collections", "links", "models"] end - - private - - def slugify_name - self.slug = name.parameterize - end end diff --git a/app/models/concerns/sluggable.rb b/app/models/concerns/sluggable.rb new file mode 100644 index 000000000..3cb000b01 --- /dev/null +++ b/app/models/concerns/sluggable.rb @@ -0,0 +1,14 @@ +module Sluggable + extend ActiveSupport::Concern + + included do + validates :slug, uniqueness: true + before_validation :slugify_name, if: :name_changed? + end + + private + + def slugify_name + self.slug = name.parameterize + end +end diff --git a/app/models/creator.rb b/app/models/creator.rb index 768c6da04..8c2440ba6 100644 --- a/app/models/creator.rb +++ b/app/models/creator.rb @@ -2,17 +2,15 @@ class Creator < ApplicationRecord include Followable include CaberObject include Linkable + include Sluggable acts_as_federails_actor username_field: :slug, name_field: :name, profile_url_method: :url_for, include_in_user_count: false has_many :models, dependent: :nullify validates :name, uniqueness: {case_sensitive: false} - validates :slug, uniqueness: true default_scope { order(:name) } - before_validation :slugify_name, if: :name_changed? - def self.ransackable_attributes(_auth_object = nil) ["caption", "created_at", "id", "name", "notes", "slug", "updated_at"] end @@ -20,10 +18,4 @@ def self.ransackable_attributes(_auth_object = nil) def self.ransackable_associations(_auth_object = nil) ["links", "models"] end - - private - - def slugify_name - self.slug = name.parameterize - end end diff --git a/app/models/model.rb b/app/models/model.rb index c43e38e8f..971a66fba 100644 --- a/app/models/model.rb +++ b/app/models/model.rb @@ -5,6 +5,7 @@ class Model < ApplicationRecord include Followable include CaberObject include Linkable + include Sluggable acts_as_federails_actor username_field: :slug, name_field: :name, profile_url_method: :url_for, actor_type: "Document", include_in_user_count: false @@ -19,7 +20,6 @@ class Model < ApplicationRecord acts_as_taggable_on :tags before_validation :strip_separators_from_path, if: :path_changed? - before_validation :slugify_name, if: :name_changed? before_validation :normalize_license # In Rails 7.1 we will be able to do this instead: @@ -169,8 +169,4 @@ def move_files # Remove the old folder if it's still there previous_library.storage.delete_prefixed(previous_path) end - - def slugify_name - self.slug = name.parameterize - end end