Skip to content

Commit 5ce60d2

Browse files
committed
feat: add url model
1 parent 09858a1 commit 5ce60d2

8 files changed

Lines changed: 190 additions & 3 deletions

File tree

Gemfile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,16 @@ group :development, :test do
5454
gem "rspec-rails", "~> 8.0.0"
5555

5656
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
57-
gem 'byebug', '~> 12.0', platforms: %i[mri mingw x64_mingw]
57+
gem "byebug", "~> 12.0", platforms: %i[mri mingw x64_mingw]
5858

5959
# Combine 'pry' with 'byebug'. Adds 'step', 'next', 'finish', 'continue' and 'break' commands to control execution.
60-
gem 'pry-byebug', '~> 3.11'
60+
gem "pry-byebug", "~> 3.11"
6161

6262
# Autoload dotenv in Rails.
63-
gem 'dotenv-rails', '~> 3.1', '>= 3.1.8'
63+
gem "dotenv-rails", "~> 3.1", ">= 3.1.8"
64+
65+
# factory_bot is a fixtures replacement with a straightforward definition syntax, support for multiple build strategies (saved
66+
gem "factory_bot_rails", "~> 6.5", ">= 6.5.1"
6467
end
6568

6669
group :development do

Gemfile.lock

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ GEM
8585
brakeman (7.1.1)
8686
racc
8787
builder (3.3.0)
88+
byebug (12.0.0)
8889
capybara (3.40.0)
8990
addressable
9091
matrix
@@ -94,20 +95,30 @@ GEM
9495
rack-test (>= 0.6.3)
9596
regexp_parser (>= 1.5, < 3.0)
9697
xpath (~> 3.2)
98+
coderay (1.1.3)
9799
concurrent-ruby (1.3.5)
98100
connection_pool (2.5.4)
99101
crass (1.0.6)
100102
date (3.5.0)
101103
debug (1.11.0)
102104
irb (~> 1.10)
103105
reline (>= 0.3.8)
106+
diff-lcs (1.6.2)
104107
dotenv (3.1.8)
108+
dotenv-rails (3.1.8)
109+
dotenv (= 3.1.8)
110+
railties (>= 6.1)
105111
drb (2.2.3)
106112
ed25519 (1.4.0)
107113
erb (6.0.0)
108114
erubi (1.13.1)
109115
et-orbi (1.4.0)
110116
tzinfo
117+
factory_bot (6.5.6)
118+
activesupport (>= 6.1.0)
119+
factory_bot_rails (6.5.1)
120+
factory_bot (~> 6.5)
121+
railties (>= 6.1.0)
111122
fugit (1.12.1)
112123
et-orbi (~> 1.4)
113124
raabro (~> 1.4)
@@ -153,6 +164,7 @@ GEM
153164
net-smtp
154165
marcel (1.1.0)
155166
matrix (0.4.3)
167+
method_source (1.1.0)
156168
mini_mime (1.1.5)
157169
minitest (5.26.1)
158170
msgpack (1.8.0)
@@ -196,6 +208,12 @@ GEM
196208
actionpack (>= 7.0.0)
197209
activesupport (>= 7.0.0)
198210
rack
211+
pry (0.15.2)
212+
coderay (~> 1.1)
213+
method_source (~> 1.0)
214+
pry-byebug (3.11.0)
215+
byebug (~> 12.0)
216+
pry (>= 0.13, < 0.16)
199217
psych (5.2.6)
200218
date
201219
stringio
@@ -252,6 +270,23 @@ GEM
252270
reline (0.6.3)
253271
io-console (~> 0.5)
254272
rexml (3.4.4)
273+
rspec-core (3.13.6)
274+
rspec-support (~> 3.13.0)
275+
rspec-expectations (3.13.5)
276+
diff-lcs (>= 1.2.0, < 2.0)
277+
rspec-support (~> 3.13.0)
278+
rspec-mocks (3.13.6)
279+
diff-lcs (>= 1.2.0, < 2.0)
280+
rspec-support (~> 3.13.0)
281+
rspec-rails (8.0.2)
282+
actionpack (>= 7.2)
283+
activesupport (>= 7.2)
284+
railties (>= 7.2)
285+
rspec-core (~> 3.13)
286+
rspec-expectations (~> 3.13)
287+
rspec-mocks (~> 3.13)
288+
rspec-support (~> 3.13)
289+
rspec-support (3.13.6)
255290
rubocop (1.81.7)
256291
json (~> 2.3)
257292
language_server-protocol (~> 3.17.0.2)
@@ -364,14 +399,19 @@ PLATFORMS
364399
DEPENDENCIES
365400
bootsnap
366401
brakeman
402+
byebug (~> 12.0)
367403
capybara
368404
debug
405+
dotenv-rails (~> 3.1, >= 3.1.8)
406+
factory_bot_rails (~> 6.5, >= 6.5.1)
369407
importmap-rails
370408
jbuilder
371409
kamal
372410
propshaft
411+
pry-byebug (~> 3.11)
373412
puma (>= 5.0)
374413
rails (~> 8.0.4)
414+
rspec-rails (~> 8.0.0)
375415
rubocop-rails-omakase
376416
selenium-webdriver
377417
solid_cable

app/models/url.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Url < ApplicationRecord
2+
validates :long_url, presence: true
3+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateUrls < ActiveRecord::Migration[8.0]
2+
def change
3+
create_table :urls do |t|
4+
t.string :short_code
5+
t.string :long_url
6+
7+
t.timestamps
8+
end
9+
end
10+
end

db/schema.rb

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/factories/urls.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# spec/factories/urls.rb
2+
3+
FactoryBot.define do
4+
factory :url do
5+
long_url { 'http://www.default-valid-url.com/test' }
6+
# Add other attributes here if they exist in your model, like 'short_code'
7+
end
8+
end

spec/models/url_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Url, type: :model do
4+
context "is valid" do
5+
it "with a present long_url" do
6+
url = FactoryBot.build(:url, long_url: "https://a-long-but-valid-url.com/path")
7+
expect(url).to be_valid
8+
end
9+
end
10+
11+
context "is invalid" do
12+
it "without a long_url" do
13+
url = FactoryBot.build(:url, long_url: nil)
14+
15+
url.valid?
16+
17+
expect(url).to_not be_valid
18+
expect(url.errors[:long_url]).to include("can't be blank")
19+
end
20+
21+
it "if long_url is an empty string" do
22+
url = FactoryBot.build(:url, long_url: "")
23+
url.valid?
24+
expect(url).to_not be_valid
25+
expect(url.errors[:long_url]).to include("can't be blank")
26+
end
27+
end
28+
end

spec/rails_helper.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# This file is copied to spec/ when you run 'rails generate rspec:install'
2+
require 'spec_helper'
3+
ENV['RAILS_ENV'] ||= 'test'
4+
require_relative '../config/environment'
5+
# Prevent database truncation if the environment is production
6+
abort("The Rails environment is running in production mode!") if Rails.env.production?
7+
# Uncomment the line below in case you have `--require rails_helper` in the `.rspec` file
8+
# that will avoid rails generators crashing because migrations haven't been run yet
9+
# return unless Rails.env.test?
10+
require 'rspec/rails'
11+
# Add additional requires below this line. Rails is not loaded until this point!
12+
13+
# Requires supporting ruby files with custom matchers and macros, etc, in
14+
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
15+
# run as spec files by default. This means that files in spec/support that end
16+
# in _spec.rb will both be required and run as specs, causing the specs to be
17+
# run twice. It is recommended that you do not name files matching this glob to
18+
# end with _spec.rb. You can configure this pattern with the --pattern
19+
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
20+
#
21+
# The following line is provided for convenience purposes. It has the downside
22+
# of increasing the boot-up time by auto-requiring all files in the support
23+
# directory. Alternatively, in the individual `*_spec.rb` files, manually
24+
# require only the support files necessary.
25+
#
26+
# Rails.root.glob('spec/support/**/*.rb').sort_by(&:to_s).each { |f| require f }
27+
28+
# Ensures that the test database schema matches the current schema file.
29+
# If there are pending migrations it will invoke `db:test:prepare` to
30+
# recreate the test database by loading the schema.
31+
# If you are not using ActiveRecord, you can remove these lines.
32+
begin
33+
ActiveRecord::Migration.maintain_test_schema!
34+
rescue ActiveRecord::PendingMigrationError => e
35+
abort e.to_s.strip
36+
end
37+
RSpec.configure do |config|
38+
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
39+
config.fixture_paths = [
40+
Rails.root.join('spec/fixtures')
41+
]
42+
43+
# If you're not using ActiveRecord, or you'd prefer not to run each of your
44+
# examples within a transaction, remove the following line or assign false
45+
# instead of true.
46+
config.use_transactional_fixtures = true
47+
48+
# You can uncomment this line to turn off ActiveRecord support entirely.
49+
# config.use_active_record = false
50+
51+
# RSpec Rails uses metadata to mix in different behaviours to your tests,
52+
# for example enabling you to call `get` and `post` in request specs. e.g.:
53+
#
54+
# RSpec.describe UsersController, type: :request do
55+
# # ...
56+
# end
57+
#
58+
# The different available types are documented in the features, such as in
59+
# https://rspec.info/features/8-0/rspec-rails
60+
#
61+
# You can also this infer these behaviours automatically by location, e.g.
62+
# /spec/models would pull in the same behaviour as `type: :model` but this
63+
# behaviour is considered legacy and will be removed in a future version.
64+
#
65+
# To enable this behaviour uncomment the line below.
66+
# config.infer_spec_type_from_file_location!
67+
68+
# Filter lines from Rails gems in backtraces.
69+
config.filter_rails_from_backtrace!
70+
# arbitrary gems may also be filtered via:
71+
# config.filter_gems_from_backtrace("gem name")
72+
73+
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
74+
config.global_fixtures = :all
75+
end

0 commit comments

Comments
 (0)