Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add base functionality #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ gem "bootsnap", require: false
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

gem "httpx"

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri windows ]
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ GEM
erubi (1.12.0)
globalid (1.2.1)
activesupport (>= 6.1)
http-2-next (1.0.1)
httpx (1.1.1)
http-2-next (>= 1.0.1)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
importmap-rails (1.2.3)
Expand Down Expand Up @@ -254,6 +257,7 @@ DEPENDENCIES
bootsnap
capybara
debug
httpx
importmap-rails
jbuilder
pg (~> 1.1)
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/quotes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class QuotesController < ApplicationController
def show
@quote = Quote.sample
@rating = @quote.ratings.new
end
end
19 changes: 19 additions & 0 deletions app/controllers/ratings_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class RatingsController < ApplicationController
def create
@quote = Quote.find(params[:quote_id])
@rating = @quote.ratings.new(rating_params)
@rating.ip = request.remote_ip

if @rating.save
redirect_to root_url
else
render "quotes/show", status: :unprocessable_entity
end
end

private

def rating_params
params.require(:rating).permit(:number)
end
end
5 changes: 5 additions & 0 deletions app/lib/ron_swanson_quotes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module RonSwansonQuotes
def self.get
HTTPX.get("https://ron-swanson-quotes.herokuapp.com/v2/quotes").json.first
end
end
9 changes: 9 additions & 0 deletions app/models/quote.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Quote < ApplicationRecord
has_many :ratings, dependent: :destroy

validates :text, presence: true, uniqueness: true

def self.sample
find_or_create_by!(text: RonSwansonQuotes.get)
end
end
8 changes: 8 additions & 0 deletions app/models/rating.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Rating < ApplicationRecord
NUMBER_RANGE = 1..5

belongs_to :quote

validates :number, presence: true, numericality: {in: NUMBER_RANGE}
validates :ip, presence: true, uniqueness: true
end
14 changes: 14 additions & 0 deletions app/views/quotes/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h2>
<%= @quote.text %>
</h2>

<p>
Average rating:
<%= @quote.ratings.average(:number) %>
</p>

<%= form_with model: [@quote, @rating] do |f| %>
<%= f.object.errors.full_messages.to_sentence %>
<%= f.number_field :number, in: Rating::NUMBER_RANGE %>
<%= f.submit %>
<% end %>
6 changes: 4 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show", as: :rails_health_check

# Defines the root path route ("/")
# root "posts#index"
resources :quotes, only: :show do
resources :ratings, only: :create
end
root "quotes#show"
end
15 changes: 15 additions & 0 deletions db/migrate/20231109024110_initial_migration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class InitialMigration < ActiveRecord::Migration[7.1]
def change
create_table :quotes do |t|
t.text :text, null: false, index: { unique: true }
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's not many Ron Swanson quotes (I checked via https://ron-swanson-quotes.herokuapp.com/v2/quotes/1000), so this will scale.

t.timestamps
end

create_table :ratings do |t|
t.belongs_to :quote, foreign_key: true
t.integer :number, null: false
t.string :ip, null: false, index: { unique: true }
t.timestamps
end
end
end
35 changes: 35 additions & 0 deletions db/schema.rb

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