Skip to content

Commit

Permalink
Merge pull request #16 from mikeheft/2-create-entities
Browse files Browse the repository at this point in the history
2 create entities
  • Loading branch information
mikeheft authored Jul 2, 2024
2 parents 4726de1 + 5a042db commit 7c90a06
Show file tree
Hide file tree
Showing 25 changed files with 483 additions and 12 deletions.
13 changes: 12 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ require:
AllCops:
TargetRubyVersion: 3.3.0
Exclude:
- "spec/**/*.rb"
- "db/**/*"
- "config/**/*"
- "bin/**/*"
Expand All @@ -17,20 +16,32 @@ Custom/PrivateMethodStyle:
###################
# End Custom Cops #
###################
Layout/ArgumentAlignment:
EnforcedStyle: with_fixed_indentation
Layout/IndentationWidth:
Enabled: true
Width: 2
Layout/SpaceInsideBlockBraces:
EnforcedStyle: space
Lint/UnusedMethodArgument:
AutoCorrect: false
Metrics/AbcSize:
Exclude:
- "app/models/application_record.rb"
Metrics/CyclomaticComplexity:
Exclude:
- "lib/rubocop/cop/custom/*.rb"
Metrics/MethodLength:
Exclude:
- "lib/rubocop/cop/custom/*.rb"
- "app/models/application_record.rb"
Metrics/PerceivedComplexity:
Exclude:
- "lib/rubocop/cop/custom/*.rb"
Naming/VariableNumber:
EnforcedStyle: snake_case
Rails/InverseOf:
Enabled: false
Style/AccessModifierDeclarations:
EnforcedStyle: inline
Style/Documentation:
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ gem "rails", "~> 7.1.3", ">= 7.1.3.4"
gem "pg", "~> 1.1"

# Use the Puma web server [https://github.com/puma/puma]
gem "faker"
gem "money-rails"
gem "puma", ">= 5.0"

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ GEM
factory_bot_rails (4.11.1)
factory_bot (~> 4.11.1)
railties (>= 3.0.0)
faker (3.3.1)
i18n (>= 1.8.11, < 2)
faraday (2.9.0)
faraday-net_http (>= 2.0, < 3.2)
faraday-net_http (3.1.0)
Expand Down Expand Up @@ -137,6 +139,15 @@ GEM
method_source (1.1.0)
mini_mime (1.1.5)
minitest (5.24.1)
monetize (1.13.0)
money (~> 6.12)
money (6.19.0)
i18n (>= 0.6.4, <= 2)
money-rails (1.15.0)
activesupport (>= 3.0)
monetize (~> 1.9)
money (~> 6.13)
railties (>= 3.0)
msgpack (1.7.2)
mutex_m (0.2.0)
net-http (0.4.1)
Expand Down Expand Up @@ -295,8 +306,10 @@ DEPENDENCIES
database_cleaner
debug
factory_bot_rails (~> 4.0)
faker
faraday
figaro
money-rails
pg (~> 1.1)
pry-rails
puma (>= 5.0)
Expand Down
13 changes: 13 additions & 0 deletions app/models/address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class Address < ApplicationRecord
has_many :driver_addresses, dependent: :destroy
has_many :ride_origins, class_name: "Ride", foreign_key: "from_address_id", dependent: nil, inverse_of: :from_address
has_many :ride_destinations, class_name: "Ride", foreign_key: "to_address_id", dependent: nil, inverse_of: :to_address

validates :line_1, :city, :state, :zip_code, :place_id, :latitude, :longitude, presence: true

def rides
Ride.by_address(id)
end
end
13 changes: 13 additions & 0 deletions app/models/driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class Driver < ApplicationRecord
has_many :driver_addresses, dependent: :destroy
has_one :current_driver_address,
-> { where(current: true) },
class_name: "DriverAddress",
dependent: :destroy,
inverse_of: :driver
has_one :current_address, through: :current_driver_address, source: :driver, dependent: :destroy

validates :first_name, :last_name, presence: true
end
14 changes: 14 additions & 0 deletions app/models/driver_address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class DriverAddress < ApplicationRecord
belongs_to :driver
belongs_to :address

validates :current,
if: -> { current },
uniqueness: {
scope: :driver_id,
message: "can only have one current Driver"
},
inclusion: { in: [true, false] }
end
19 changes: 19 additions & 0 deletions app/models/ride.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class Ride < ApplicationRecord
belongs_to :driver, optional: true
belongs_to :from_address, class_name: "Address"
belongs_to :to_address, class_name: "Address"

validates :duration, :distance, :commute_duration, :amount_cents, presence: true
monetize :amount_cents,
as: :amount,
allow_nil: false,
numericality: {
greater_than_or_equal_to: 0
}

scope :by_address, ->(address_id) {
where(from_address_id: address_id).or(where(to_address_id: address_id))
}
end
115 changes: 115 additions & 0 deletions config/initializers/money.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# encoding : utf-8

MoneyRails.configure do |config|

# To set the default currency
#
config.default_currency = :usd

# Set default bank object
#
# Example:
# config.default_bank = EuCentralBank.new

# Add exchange rates to current money bank object.
# (The conversion rate refers to one direction only)
#
# Example:
# config.add_rate "USD", "CAD", 1.24515
# config.add_rate "CAD", "USD", 0.803115

# To handle the inclusion of validations for monetized fields
# The default value is true
#
config.include_validations = true

# Default ActiveRecord migration configuration values for columns:
#
# config.amount_column = { prefix: '', # column name prefix
# postfix: '_cents', # column name postfix
# column_name: nil, # full column name (overrides prefix, postfix and accessor name)
# type: :integer, # column type
# present: true, # column will be created
# null: false, # other options will be treated as column options
# default: 0
# }
#
# config.currency_column = { prefix: '',
# postfix: '_currency',
# column_name: nil,
# type: :string,
# present: true,
# null: false,
# default: 'USD'
# }

# Register a custom currency
#
# Example:
# config.register_currency = {
# priority: 1,
# iso_code: "EU4",
# name: "Euro with subunit of 4 digits",
# symbol: "€",
# symbol_first: true,
# subunit: "Subcent",
# subunit_to_unit: 10000,
# thousands_separator: ".",
# decimal_mark: ","
# }

# Specify a rounding mode
# Any one of:
#
# BigDecimal::ROUND_UP,
# BigDecimal::ROUND_DOWN,
# BigDecimal::ROUND_HALF_UP,
# BigDecimal::ROUND_HALF_DOWN,
# BigDecimal::ROUND_HALF_EVEN,
# BigDecimal::ROUND_CEILING,
# BigDecimal::ROUND_FLOOR
#
# set to BigDecimal::ROUND_HALF_EVEN by default
#
# config.rounding_mode = BigDecimal::ROUND_HALF_UP

# Set default money format globally.
# Default value is nil meaning "ignore this option".
# Example:
#
# config.default_format = {
# no_cents_if_whole: nil,
# symbol: nil,
# sign_before_symbol: nil
# }

# If you would like to use I18n localization (formatting depends on the
# locale):
# config.locale_backend = :i18n
#
# Example (using default localization from rails-i18n):
#
# I18n.locale = :en
# Money.new(10_000_00, 'USD').format # => $10,000.00
# I18n.locale = :es
# Money.new(10_000_00, 'USD').format # => $10.000,00
#
# For the legacy behaviour of "per currency" localization (formatting depends
# only on currency):
# config.locale_backend = :currency
#
# Example:
# Money.new(10_000_00, 'USD').format # => $10,000.00
# Money.new(10_000_00, 'EUR').format # => €10.000,00
#
# In case you don't need localization and would like to use default values
# (can be redefined using config.default_format):
# config.locale_backend = nil

# Set default raise_error_on_money_parsing option
# It will be raise error if assigned different currency
# The default value is false
#
# Example:
# config.raise_error_on_money_parsing = false
end
19 changes: 19 additions & 0 deletions db/migrate/20240702182238_create_addresses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

class CreateAddresses < ActiveRecord::Migration[7.1]
def change
create_table :addresses do |t|
t.string :line_1, null: false
t.string :line_2, null: true
t.string :city, null: false
t.string :state, index: true, null: false
t.string :zip_code, index: true, null: false
t.float :latitude, null: false
t.float :longitude, null: false
t.string :place_id, null: true, index: true

t.timestamps
end
add_index :addresses, %i[city state]
end
end
10 changes: 10 additions & 0 deletions db/migrate/20240702184830_create_drivers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateDrivers < ActiveRecord::Migration[7.1]
def change
create_table :drivers do |t|
t.string :first_name, null: false
t.string :last_name, null: false

t.timestamps
end
end
end
14 changes: 14 additions & 0 deletions db/migrate/20240702193305_create_rides.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateRides < ActiveRecord::Migration[7.1]
def change
create_table :rides do |t|
t.float :duration, index: true, null: false
t.float :distance, index: true, null: false
t.float :commute_duration, index: true, null: false
t.monetize :amount
t.references :driver, null: true, index: true
t.references :from_address, foreign_key: {to_table: :addresses}, index: true, null: false
t.references :to_address, foreign_key: {to_table: :addresses}, index: true, null: false
t.timestamps
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20240702210002_create_driver_addresses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateDriverAddresses < ActiveRecord::Migration[7.1]
def change
create_table :driver_addresses do |t|
t.boolean :current, null: false, default: false
t.references :driver, null: false, foreign_key: true
t.references :address, null: false, foreign_key: true

t.timestamps
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddUniqIndexToDriverAddresses < ActiveRecord::Migration[7.1]
def change
add_index :driver_addresses, %i[current driver_id], unique: true, where: "(current IS TRUE)"
end
end
Loading

0 comments on commit 7c90a06

Please sign in to comment.