Create model and controller

This commit is contained in:
Manuel Bustillo 2024-08-11 16:29:10 +02:00
parent 716b819de2
commit 3bc7a0c58b
10 changed files with 54 additions and 1 deletions

View File

@ -0,0 +1,5 @@
class GroupsController < ApplicationController
def index
render jsonapi: Group.all
end
end

View File

@ -0,0 +1,2 @@
module GroupsHelper
end

4
app/models/group.rb Normal file
View File

@ -0,0 +1,4 @@
class Group < ApplicationRecord
validates :name, uniqueness: true
validates :name, :order, presence: true
end

View File

@ -0,0 +1,5 @@
class SerializableGroup < JSONAPI::Serializable::Resource
type 'group'
attributes :name, :icon
end

View File

@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :groups, only: :index
resources :guests do
post :import, on: :collection
end

View File

@ -0,0 +1,13 @@
class CreateGroups < ActiveRecord::Migration[7.1]
def change
create_table :groups, id: :uuid do |t|
t.string :name, null: false
t.string :icon
t.integer :order, null: false, default: 1
t.timestamps
end
add_index :groups, :name, unique: true
end
end

11
db/schema.rb generated
View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2024_07_24_181853) do
ActiveRecord::Schema[7.1].define(version: 2024_08_11_142121) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -26,6 +26,15 @@ ActiveRecord::Schema[7.1].define(version: 2024_07_24_181853) do
t.datetime "updated_at", null: false
end
create_table "groups", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "name", null: false
t.string "icon"
t.integer "order", default: 1, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["name"], name: "index_groups_on_name", unique: true
end
create_table "guests", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "first_name"
t.string "last_name"

View File

@ -5,6 +5,7 @@ Expense.delete_all
Guest.delete_all
ActsAsTaggableOn::Tagging.delete_all
ActsAsTaggableOn::Tag.delete_all
Group.delete_all
Expense.create!(name: 'Photographer', amount: 3000, pricing_type: 'fixed')
Expense.create!(name: 'Country house', amount: 6000, pricing_type: 'fixed')
@ -43,6 +44,7 @@ samples = {
football_team_a: 15,
dance_club: 10
}.each_with_object([]) do |(affinity_group, count), acc|
Group.find_or_create_by!(name: affinity_group.to_s.humanize)
count.times { acc << affinity_group }
end

7
spec/factories/groups.rb Normal file
View File

@ -0,0 +1,7 @@
FactoryBot.define do
factory :group do
name { "MyString" }
icon { "MyString" }
order { 1 }
end
end

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Group, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end