Skip to content

Commit

Permalink
Merge pull request #2451 from manyfold3d/following
Browse files Browse the repository at this point in the history
Refactor list code to be more easily reusable
  • Loading branch information
Floppy committed Jul 27, 2024
2 parents 995ba70 + e74bd72 commit 926ef9b
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/controllers/model_files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def create

def update
if @file.update(file_params)
@file.set_printed_by_user(current_user, params[:model_file][:printed] === "1")
current_user.set_list_state(@file, :printed, params[:model_file][:printed] === "1")
redirect_to [@library, @model, @file], notice: t(".success")
else
render :edit, alert: t(".failure")
Expand All @@ -52,7 +52,7 @@ def bulk_update
params[:model_files].each_pair do |id, selected|
if selected == "1"
file = @model.model_files.find(id)
file.set_printed_by_user(current_user, params[:printed] === "1")
current_user.set_list_state(file, :printed, params[:printed] === "1")
if file.update(hash)
file.save
end
Expand Down
11 changes: 11 additions & 0 deletions app/models/concerns/listable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Listable
extend ActiveSupport::Concern

included do
acts_as_favoritable
end

def listers(list_name)
favoritors(scope: list_name)
end
end
27 changes: 27 additions & 0 deletions app/models/concerns/lister.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Lister
extend ActiveSupport::Concern

included do
acts_as_favoritor
end

def list(object, list_name)
favorite(object, scope: list_name)
end

def delist(object, list_name)
unfavorite(object, scope: list_name)
end

def set_list_state(object, list_name, listed)
if listed
list(object, list_name)
else
delist(object, list_name)
end
end

def listed?(object, list_name)
favorited?(object, scope: list_name)
end
end
11 changes: 1 addition & 10 deletions app/models/model_file.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ModelFile < ApplicationRecord
include LibraryUploader::Attachment(:attachment)
include Listable

extend Memoist

Expand All @@ -23,8 +24,6 @@ class ModelFile < ApplicationRecord
# Explicitly explain serialization for MariaDB
attribute :attachment_data, :json

acts_as_favoritable

SUPPORT_KEYWORDS = %w[
presupported
presup
Expand Down Expand Up @@ -139,14 +138,6 @@ def delete_from_disk_and_destroy
destroy
end

def set_printed_by_user(user, printed)
if printed
user.favorite(self, scope: :printed)
else
user.unfavorite(self, scope: :printed)
end
end

def self.ransackable_attributes(_auth_object = nil)
["caption", "created_at", "digest", "filename", "id", "notes", "presupported", "size", "updated_at", "y_up", "presupported_version_id", "unsupported_version_id"]
end
Expand Down
6 changes: 3 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require "uri"

class User < ApplicationRecord
include Lister

rolify
devise :database_authenticatable,
:registerable, :zxcvbnable,
:rememberable, :recoverable,
:lockable, :timeoutable

acts_as_favoritor

validates :username,
presence: true,
uniqueness: {case_sensitive: false},
Expand Down Expand Up @@ -38,7 +38,7 @@ def to_param
end

def printed?(file)
favorited?(file, scope: :printed)
listed?(file, scope: :printed)
end

def self.ransackable_attributes(auth_object = nil)
Expand Down
16 changes: 16 additions & 0 deletions spec/models/concerns/listable_shared.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
shared_examples "Listable" do
let(:thing) { create(described_class.to_s.underscore.to_sym) }
let(:user) { create(:user) }

before do
user.list(thing, :printed)
end

it "shows as listed" do
expect(user.listed?(thing, :printed)).to be true
end

it "can access listers" do
expect(thing.listers(:printed)).to eq [user]
end
end
2 changes: 2 additions & 0 deletions spec/models/model_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require "support/mock_directory"

RSpec.describe ModelFile do
it_behaves_like "Listable"

it "is not valid without a filename" do
expect(build(:model_file, filename: nil)).not_to be_valid
end
Expand Down
1 change: 1 addition & 0 deletions spec/support/shared_examples.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dir[Rails.root.join("spec/**/*_shared.rb")].each { |f| require f }

0 comments on commit 926ef9b

Please sign in to comment.