-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from Nemwel-Boniface/eggs_resource
My kuku Milestone 10 : Eggs resource
- Loading branch information
Showing
21 changed files
with
367 additions
and
61 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* Eggs Index Page Styles */ | ||
.eggs { | ||
padding: 2rem; | ||
} | ||
|
||
.newEggLink { | ||
text-align: center; | ||
margin-bottom: 2rem; | ||
} | ||
|
||
.newEggLink .btn { | ||
background-color: #28a745; | ||
color: #fff; | ||
padding: 0.5rem 1rem; | ||
border-radius: 5px; | ||
text-decoration: none; | ||
} | ||
|
||
.newEggLink .btn:hover { | ||
background-color: #218838; | ||
} | ||
|
||
.eggStatsWrapper { | ||
display: grid; | ||
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); | ||
gap: 1.5rem; | ||
margin-top: 2rem; | ||
} | ||
|
||
.card { | ||
background-color: #f8f9fa; | ||
border: 1px solid #e9ecef; | ||
border-radius: 8px; | ||
padding: 1.5rem; | ||
text-align: center; | ||
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); | ||
} | ||
|
||
.card h3 { | ||
font-size: 1.5rem; | ||
margin-bottom: 1rem; | ||
color: #495057; | ||
} | ||
|
||
.card p { | ||
font-size: 1.25rem; | ||
color: #6c757d; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class EggsController < ApplicationController | ||
# List all eggs and stats (index remains unchanged) | ||
def index | ||
@eggs = Egg.all | ||
@egg_stats = Egg.total_eggs_and_prices | ||
end | ||
|
||
# Display the form for creating a new egg record | ||
def new | ||
@egg = Egg.new | ||
end | ||
|
||
# Create a new egg record | ||
def create | ||
@egg = Egg.new(egg_params) | ||
if @egg.save | ||
redirect_to eggs_path, notice: 'Egg was successfully recorded.' | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
# Display the form for editing an existing egg record | ||
def edit | ||
@egg = Egg.find(params[:id]) | ||
end | ||
|
||
# Update an existing egg record | ||
def update | ||
@egg = Egg.find(params[:id]) | ||
if @egg.update(egg_params) | ||
redirect_to eggs_path, notice: 'Egg was successfully updated.' | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
# Delete an egg record | ||
def destroy | ||
@egg = Egg.find(params[:id]) | ||
@egg.destroy | ||
redirect_to eggs_path, notice: 'Egg was successfully deleted.' | ||
end | ||
|
||
private | ||
|
||
# Strong parameters for egg attributes | ||
def egg_params | ||
params.require(:egg).permit(:egg_count, :egg_size, :poultry_type, :laid_on, :price_per_egg) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module EggsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Egg < ApplicationRecord | ||
# Enums | ||
EGG_SIZE = %w[Small Medium Large].freeze | ||
POULTRY_TYPES = %w[Chicken Duck Goose Turkey Quail].freeze | ||
|
||
# Validations | ||
validates :poultry_type, presence: true, inclusion: { in: POULTRY_TYPES } | ||
validates :egg_size, presence: true, inclusion: { in: EGG_SIZE } | ||
validates :egg_count, presence: true, numericality: { greater_than_or_equal_to: 0 } | ||
validates :price_per_egg, presence: true, numericality: { greater_than_or_equal_to: 0 } | ||
validates :laid_on, presence: true | ||
|
||
# Methods to calculate statistics | ||
def self.total_eggs_and_prices | ||
POULTRY_TYPES.each_with_object({}) do |poultry_type, stats| | ||
eggs = where(poultry_type:) | ||
total_eggs = eggs.sum(:egg_count) | ||
total_price = eggs.sum('egg_count * price_per_egg') | ||
average_price_per_egg = eggs.average(:price_per_egg) | ||
|
||
stats[poultry_type] = { | ||
total_eggs:, | ||
total_price:, | ||
average_price_per_egg: | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,26 @@ | ||
<h1>Editing chicken</h1> | ||
<p style="color: green"><%= notice %></p> | ||
|
||
<%= render "form", chicken: @chicken %> | ||
<header class="aboutheader"> | ||
<h2>Edit Poultry</h2> | ||
<h3><%= link_to "Home", root_path %> <span>-</span>Poultry</h3> | ||
</header> | ||
|
||
<br> | ||
<main class="edit-chicken"> | ||
<h2 class="darkgreentext">Add More to Your Poultry Inventory</h2> | ||
|
||
<div> | ||
<%= link_to "Show this chicken", @chicken %> | | ||
<%= link_to "Back to chickens", chickens_path %> | ||
</div> | ||
<%= form_with(model: @chicken, url: chicken_path(@chicken), method: :patch, local: true) do |form| %> | ||
<div> | ||
<strong>Poultry Type:</strong> | ||
<p><%= @chicken.poultry_type.capitalize %></p> | ||
</div> | ||
|
||
<div> | ||
<%= form.label :no_of_poultry, "Number of additional Poultry:" %> | ||
<%= form.number_field :no_of_poultry, min: 1, value: 1 %> | ||
</div> | ||
|
||
<div> | ||
<%= form.submit "Update Poultry Inventory" %> | ||
</div> | ||
<% end %> | ||
</main> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.