Skip to content

Commit

Permalink
move slug generation into concern
Browse files Browse the repository at this point in the history
  • Loading branch information
Floppy committed Sep 4, 2024
1 parent 9f6ce54 commit af1607d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 23 deletions.
10 changes: 1 addition & 9 deletions app/models/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ 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

has_many :models, dependent: :nullify
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
Expand Down Expand Up @@ -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
14 changes: 14 additions & 0 deletions app/models/concerns/sluggable.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 1 addition & 9 deletions app/models/creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,20 @@ 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

def self.ransackable_associations(_auth_object = nil)
["links", "models"]
end

private

def slugify_name
self.slug = name.parameterize
end
end
6 changes: 1 addition & 5 deletions app/models/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit af1607d

Please sign in to comment.