diff --git a/Gemfile b/Gemfile index 3e3ace6..f61ed2f 100644 --- a/Gemfile +++ b/Gemfile @@ -15,6 +15,7 @@ gem 'stimulus-rails' gem 'turbo-rails' gem 'tzinfo-data', platforms: %i[windows jruby] +gem 'faker' gem 'jsonapi-rails' gem 'rack-cors' gem 'react-rails' @@ -23,7 +24,6 @@ gem 'rubytree' group :development, :test do gem 'debug', platforms: %i[mri windows] gem 'factory_bot_rails' - gem 'faker' gem 'pry' gem 'rspec-rails', '~> 7.0.0' end diff --git a/app/controllers/tables_arrangements_controller.rb b/app/controllers/tables_arrangements_controller.rb index a1d7420..80935fd 100644 --- a/app/controllers/tables_arrangements_controller.rb +++ b/app/controllers/tables_arrangements_controller.rb @@ -2,7 +2,7 @@ class TablesArrangementsController < ApplicationController def index - render json: TablesArrangement.all.order(discomfort: :asc).limit(3).as_json(only: %i[id discomfort]) + render json: TablesArrangement.all.order(discomfort: :asc).limit(3).as_json(only: %i[id name discomfort]) end def show diff --git a/app/models/tables_arrangement.rb b/app/models/tables_arrangement.rb index 0ceb0cd..d1eba03 100644 --- a/app/models/tables_arrangement.rb +++ b/app/models/tables_arrangement.rb @@ -3,4 +3,12 @@ class TablesArrangement < ApplicationRecord has_many :seats has_many :guests, through: :seats + + before_create :assign_name + + private + + def assign_name + self.name = "#{Faker::Adjective.positive} #{Faker::Creature::Animal.name}".capitalize + end end diff --git a/db/migrate/20241103072808_add_name_to_tables_arrangements.rb b/db/migrate/20241103072808_add_name_to_tables_arrangements.rb new file mode 100644 index 0000000..53dd4fa --- /dev/null +++ b/db/migrate/20241103072808_add_name_to_tables_arrangements.rb @@ -0,0 +1,7 @@ +# Copyright (C) 2024 Manuel Bustillo + +class AddNameToTablesArrangements < ActiveRecord::Migration[7.2] + def change + add_column :tables_arrangements, :name, :string, null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index a300416..5a7912b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -185,6 +185,7 @@ ActiveRecord::Schema[7.2].define(version: 2024_11_03_093955) do t.integer "discomfort" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "name", null: false end add_foreign_key "groups", "groups", column: "parent_id" diff --git a/spec/models/tables_arrangement_spec.rb b/spec/models/tables_arrangement_spec.rb index ab4677c..c15971a 100644 --- a/spec/models/tables_arrangement_spec.rb +++ b/spec/models/tables_arrangement_spec.rb @@ -3,5 +3,9 @@ require 'rails_helper' RSpec.describe TablesArrangement, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + describe 'callbacks' do + it 'assigns a name before creation' do + expect(described_class.create!.name).to be_present + end + end end