Skip to content
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

File-labels #1338

Merged
merged 4 commits into from
Mar 13, 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
Diff view
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ Rails/Output:
Exclude:
- 'lib/spooler.rb'
- 'lib/spooler/**/*'

Rails/UniqueValidationWithoutIndex:
Enabled: false
8 changes: 8 additions & 0 deletions app/models/file_label.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class FileLabel < ApplicationRecord
belongs_to :file_record
belongs_to :label

validates :file_record_id, uniqueness: { scope: :label_id }
end
3 changes: 3 additions & 0 deletions app/models/file_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class FileRecord < ApplicationRecord
has_one_attached :file
has_many :jobs, class_name: 'Printer::Job', as: :executable, dependent: :nullify

has_many :file_labels, dependent: :destroy
has_many :labels, through: :file_labels

validates :file, presence: true

def filename
Expand Down
10 changes: 10 additions & 0 deletions app/models/label.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class Label < ApplicationRecord
validates :name, presence: true, uniqueness: true
validates :color, presence: true,
format: { with: /\A#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\Z/ }

has_many :file_labels, dependent: :destroy
has_many :file_records, through: :file_labels
end
12 changes: 12 additions & 0 deletions db/migrate/20240308014217_create_labels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateLabels < ActiveRecord::Migration[7.1]
def change
create_table :labels do |t|
t.string :name, null: false
t.string :color, null: false

t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions db/migrate/20240311162807_create_file_labels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateFileLabels < ActiveRecord::Migration[7.1]
def change
create_table :file_labels do |t|
t.references :file_record, null: false, foreign_key: true
t.references :label, null: false, foreign_key: true

t.timestamps
end
end
end
20 changes: 19 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@
)
end
end

trait(:with_labels) do
labels { build_list :label, 1 }
end
end
end
12 changes: 12 additions & 0 deletions spec/factories/labels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

FactoryBot.define do
factory :label do
name { 'My red label' }
color { '#ff0000' }

trait(:with_files) do
file_records { build_list :file_record, 1 }
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

it 'has a valid factory' do
expect(file).to be_valid
expect(build(:file_record, :with_labels)).to be_valid
end

describe '#filename' do
Expand Down Expand Up @@ -136,4 +137,18 @@
}.from(Rails.root.join('spec/fixture_files/test.gcode').read).to(content)
end
end

describe '#labels' do
subject { build(:file_record, :with_labels).labels }

it { is_expected.to_not be_nil }
it { is_expected.to_not be_empty }

it 'does not have duplicate labels associated to a single file record' do
file_record = create(:file_record, :with_labels)
file_record.labels.build(file_records: [file_record])

expect(file_record).to be_invalid # (same as to_not be_valid)
end
end
end
63 changes: 63 additions & 0 deletions spec/models/label_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Label, type: :model do
let(:label) { build(:label) }

it 'has a valid factory' do
expect(label).to be_valid
expect(build(:label, :with_files)).to be_valid
end

describe '#name' do
subject { label.name }

it { is_expected.to eq 'My red label' }
end

describe '#color' do
subject { label.color }

it { is_expected.to eq '#ff0000' }
end

it 'has a valid name' do
expect(Label.new(name: nil, color: '#ffffff')).to_not be_valid
expect(Label.new(name: '', color: '#ffffff')).to_not be_valid
expect(Label.new(color: '#ffffff')).to_not be_valid
end

it 'has a unique name' do
Label.create(name: 'My yellow label', color: '#0000ff')
duplicate_label = Label.new(name: 'My yellow label', color: '#00f')
expect(duplicate_label).to_not be_valid
end

it 'has a valid color format' do
expect(Label.new(name: 'my label')).to_not be_valid
expect(Label.new(name: 'my label', color: '')).to_not be_valid
expect(Label.new(name: 'my label', color: nil)).to_not be_valid
expect(Label.new(name: 'my label', color: 'green')).to_not be_valid
expect(Label.new(name: 'my label', color: '123456')).to_not be_valid
expect(Label.new(name: 'my label', color: '#1234')).to_not be_valid
expect(Label.new(name: 'my label', color: '#gg00gg')).to_not be_valid
expect(Label.new(name: 'my label', color: 'rgb(0, 155, 0)')).to_not be_valid
expect(Label.new(name: 'my label', color: '#ff00ff')).to be_valid
expect(Label.new(name: 'my label', color: '#f0f')).to be_valid
end

describe '#file_records' do
subject { build(:label, :with_files).file_records }

it { is_expected.to_not be_nil }
it { is_expected.to_not be_empty }

it 'does not have duplicate files associated to a single label' do
label = create(:label, :with_files)
label.file_records.build(labels: [label])

expect(label).to be_invalid # (same as to_not be_valid)
end
end
end