Skip to content
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ gem 'sprockets-rails'
gem 'sqlite3', '~> 1.4'

# Use the Puma web server [https://github.com/puma/puma]
gem 'pg'
gem 'puma', '~> 5.0'

# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ GEM
nio4r (2.5.9)
nokogiri (1.15.3-x64-mingw-ucrt)
racc (~> 1.4)
pg (1.5.3-x64-mingw-ucrt)
public_suffix (5.0.3)
puma (5.6.6)
nio4r (~> 2.0)
Expand Down Expand Up @@ -218,6 +219,7 @@ DEPENDENCIES
debug
importmap-rails
jbuilder
pg
puma (~> 5.0)
rails (~> 7.0.6)
selenium-webdriver
Expand Down
16 changes: 16 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Category < ApplicationRecord
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
has_many :categories_entities, dependent: :destroy
has_many :entities, through: :categories_entities

validates :name, presence: true, length: { minimum: 3, maximum: 60 }
validates :icon, presence: true

def self.default_icon
'fas fa-question-circle'
end

def total_amount
entities.sum(:amount)
end
end
7 changes: 7 additions & 0 deletions app/models/category_entity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CategoryEntity < ApplicationRecord
belongs_to :category
belongs_to :entity

validates :category_id, presence: true
validates :entity_id, presence: true
end
8 changes: 8 additions & 0 deletions app/models/entity.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Entity < ApplicationRecord
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
has_many :categories_entities, dependent: :destroy
has_many :categories, through: :categories_entities

validates :name, presence: true, length: { minimum: 3, maximum: 60 }
validates :amount, presence: true, numericality: { greater_than: 0 }
end
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class User < ApplicationRecord
has_many :categories, dependent: :destroy
has_many :entities, dependent: :destroy

validates :name, presence: true
validates :name, length: { minimum: 3, maximum: 50 }
end
81 changes: 71 additions & 10 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,86 @@
# SQLite. Versions 3.8.0 and up are supported.
# gem install sqlite3
# PostgreSQL. Versions 9.3 and up are supported.
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem "sqlite3"
# Install the pg driver:
# gem install pg
# On macOS with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On macOS with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem "pg"
#
default: &default
adapter: sqlite3
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000

development:
<<: *default
database: db/development.sqlite3
database: budget_app_development

# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user running Rails.
username: postgres

# The password associated with the postgres role (username).
password: postgres#13579

# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost

# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432

# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public

# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
database: budget_app_test
username: postgres
password: postgres#13579

# As with config/credentials.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password or a full connection URL as an environment
# variable when you boot the app. For example:
#
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# If the connection URL is provided in the special DATABASE_URL environment
# variable, Rails will automatically merge its configuration values on top of
# the values provided in this file. Alternatively, you can specify a connection
# URL environment variable explicitly:
#
# production:
# url: <%= ENV["MY_APP_DATABASE_URL"] %>
#
# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full overview on how database connection configuration can be specified.
#
production:
<<: *default
database: db/production.sqlite3
<<: *default
url: <%= ENV['DATABASE_URL'] %>
9 changes: 9 additions & 0 deletions db/migrate/20230727190447_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateUsers < ActiveRecord::Migration[7.0]
def change
create_table :users do |t|
t.string :name

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20230727191140_create_categories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateCategories < ActiveRecord::Migration[7.0]
def change
create_table :categories do |t|
t.string :name
t.string :icon

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20230727191201_create_entities.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateEntities < ActiveRecord::Migration[7.0]
def change
create_table :entities do |t|
t.string :name
t.decimal :amount

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20230727200438_create_categories_entities.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateCategoriesEntities < ActiveRecord::Migration[7.0]
def change
create_table :categories_entities do |t|
t.references :category, null: false, foreign_key: true
t.references :entity, null: false, foreign_key: true

t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddAuthorsReferencestoEntities < ActiveRecord::Migration[7.0]
def change
add_reference :entities, :author, foreign_key: { to_table: :users } do
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddAuthorsReferencestoCategories < ActiveRecord::Migration[7.0]
def change
add_reference :categories, :author, foreign_key: { to_table: :users } do
end
end
end
54 changes: 54 additions & 0 deletions db/schema.rb

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

30 changes: 30 additions & 0 deletions test/fixtures/categories.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
category_one:
name: Category 1
description: Category 1 description
user_id: 1
entity_id: 1
created_at: <%= Time.now %>
updated_at: <%= Time.now %>

category_two:
name: Category 2
description: Category 2 description
user_id: 1
entity_id: 1
created_at: <%= Time.now %>
updated_at: <%= Time.now %>

category_three:
name: Category 3
description: Category 3 description
user_id: 2
entity_id: 2
created_at: <%= Time.now %>
updated_at: <%= Time.now %>
```
24 changes: 24 additions & 0 deletions test/fixtures/category_entities.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
category_entity_one:
category_id: 1
entity_id: 1
created_at: <%= Time.now %>
updated_at: <%= Time.now %>

category_entity_two:
category_id: 2
entity_id: 1
created_at: <%= Time.now %>
updated_at: <%= Time.now %>

category_entity_three:
category_id: 3
entity_id: 2
created_at: <%= Time.now %>
updated_at: <%= Time.now %>
```
26 changes: 26 additions & 0 deletions test/fixtures/entities.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
entity_one:
name: Entity 1
description: Entity 1 description
user_id: 1
created_at: <%= Time.now %>
updated_at: <%= Time.now %>

entity_two:
name: Entity 2
description: Entity 2 description
user_id: 2
created_at: <%= Time.now %>
updated_at: <%= Time.now %>

entity_three:
name: Entity 3
description: Entity 3 description
user_id: 2
created_at: <%= Time.now %>
updated_at: <%= Time.now %>
13 changes: 13 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
user_one:
name: User 1
email: admin@localhost

user_two:
name: User 2
email: user@localhost
Loading