8000 DRY up some older code with concerns by Floppy · Pull Request #2611 · manyfold3d/manyfold · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

DRY up some older code with concerns #2611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
13 changes: 2 additions & 11 deletions app/models/collection.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
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
has_many :links, as: :linkable, dependent: :destroy
belongs_to :collection, optional: true
accepts_nested_attributes_for :links, reject_if: :all_blank, allow_destroy: 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 @@ -75,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
8 changes: 8 additions & 0 deletions app/models/concerns/linkable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Linkable
extend ActiveSupport::Concern

included do
has_many :links, as: :linkable, dependent: :destroy
accepts_nested_attributes_for :links, reject_if: :all_blank, allow_destroy: true
end
end
13 changes: 13 additions & 0 deletions app/models/concerns/sluggable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Sluggable
extend ActiveSupport::Concern

included do
before_validation :slugify_name, if: :name_changed?
end

private

def slugify_name
self.slug = name.parameterize
end
end
13 changes: 2 additions & 11 deletions app/models/creator.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
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
has_many :links, as: :linkable, dependent: :destroy
accepts_nested_attributes_for :links, reject_if: :all_blank, allow_destroy: true
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
10 changes: 2 additions & 8 deletions app/models/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class Model < ApplicationRecord
include PathParser
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 @@ -14,14 +16,10 @@ class Model < ApplicationRecord
belongs_to :collection, optional: true
belongs_to :preview_file, class_name: "ModelFile", optional: true
has_many :model_files, dependent: :destroy
has_many :links, as: :linkable, dependent: :destroy
has_many :problems, as: :problematic, dependent: :destroy
acts_as_taggable_on :tags

accepts_nested_attributes_for :links, reject_if: :all_blank, allow_destroy: true

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 @@ -171,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
6 changes: 1 addition & 5 deletions spec/models/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@
RSpec.describe Collection do
it_behaves_like "Followable"
it_behaves_like "Caber::Object"

it "automatically generates a slug from the name" do
collection = create(:collection, name: "Spın̈al Tap")
expect(collection.slug).to eq "spin-al-tap"
end
it_behaves_like "Sluggable"
end
6 changes: 6 additions & 0 deletions spec/models/concerns/sluggable_shared.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
shared_examples "Sluggable" do
it "automatically generates a slug from the name" do
thing = create(described_class.name.parameterize.to_sym, name: "Spın̈al Tap")
expect(thing.slug).to eq "spin-al-tap"
end
end
6 changes: 1 addition & 5 deletions spec/models/creator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@
RSpec.describe Creator do
it_behaves_like "Followable"
it_behaves_like "Caber::Object"

it "automatically generates a slug from the name" do
creator = create(:creator, name: "Spın̈al Tap")
expect(creator.slug).to eq "spin-al-tap"
end
it_behaves_like "Sluggable"
end
6 changes: 1 addition & 5 deletions spec/models/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
RSpec.describe Model do
it_behaves_like "Followable"
it_behaves_like "Caber::Object"
it_behaves_like "Sluggable"

it "is not valid without a path" do
expect(build(:model, path: nil)).not_to be_valid
Expand Down Expand Up @@ -71,11 +72,6 @@
expect(model.path).to eq "models/car"
end

it "automatically generates a slug from the name" do
model = create(:model, name: "Spın̈al Tap")
expect(model.slug).to eq "spin-al-tap"
end

context "with a library on disk" do
around do |ex|
MockDirectory.create([
Expand Down
Loading
0