This repository has been archived by the owner on Mar 12, 2023. It is now read-only.
-
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.
* Add slim, bootstrap and carrierwave to Gemfile * Create restaurant * Restaurants * Restaurant tests * Ignore uploads in gitignore * Create image and icon uploaders * Add api and admin panel views * Change favourited to featured * Recipes (#1) * Create recipe * Add API inflection * Recipes * Create recipe views * Recipe tests
- Loading branch information
Showing
47 changed files
with
755 additions
and
19 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -16,5 +16,8 @@ | |
# Ignore Byebug command history file. | ||
.byebug_history | ||
|
||
# Ignore uploads | ||
/public/uploads | ||
|
||
# macOS | ||
.DS_Store |
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
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 was deleted.
Oops, something went wrong.
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 @@ | ||
@import 'bootstrap-sprockets'; | ||
@import 'bootstrap'; |
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,55 @@ | ||
class Admin::RecipesController < ApplicationController | ||
before_action :set_recipe, only: [:show, :edit, :update, :destroy] | ||
|
||
def index | ||
@recipes = Recipe.all | ||
end | ||
|
||
def new | ||
@recipe = Recipe.new | ||
end | ||
|
||
def show | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def create | ||
@recipe = Recipe.new(recipe_params) | ||
if @recipe.save | ||
redirect_to admin_recipes_url, notice: 'Рецепт успешно создан.' | ||
else | ||
render 'new' | ||
end | ||
end | ||
|
||
def update | ||
if @recipe.update(recipe_params) | ||
redirect_to admin_recipes_url, notice: 'Рецепт успешно обновлен.' | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
def destroy | ||
@recipe.destroy | ||
redirect_to admin_recipes_url, notice: 'Рецепт успешно удален.' | ||
end | ||
|
||
private | ||
|
||
def set_recipe | ||
@recipe = Recipe.find(params[:id]) | ||
end | ||
|
||
def recipe_params | ||
params | ||
.require(:recipe) | ||
.permit( | ||
:name, :description, :image, :cooking_time, :calories, | ||
:proteins, :fats, :carbohydrates, :restaurant_id | ||
) | ||
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,55 @@ | ||
class Admin::RestaurantsController < ApplicationController | ||
before_action :set_restaurant, only: [:show, :edit, :update, :destroy] | ||
|
||
def index | ||
@restaurants = Restaurant.all | ||
end | ||
|
||
def new | ||
@restaurant = Restaurant.new | ||
end | ||
|
||
def show | ||
end | ||
|
||
def edit | ||
end | ||
|
||
def create | ||
@restaurant = Restaurant.new(restaurant_params) | ||
if @restaurant.save | ||
redirect_to admin_restaurants_url, notice: 'Ресторан был успешно создан.' | ||
else | ||
render 'new' | ||
end | ||
end | ||
|
||
def update | ||
if @restaurant.update(restaurant_params) | ||
redirect_to admin_restaurants_url, notice: 'Ресторан успешно обновлен.' | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
def destroy | ||
@restaurant.destroy | ||
redirect_to admin_restaurants_url, notice: 'Ресторан был успешно удален.' | ||
end | ||
|
||
private | ||
|
||
def set_restaurant | ||
@restaurant = Restaurant.find(params[:id]) | ||
end | ||
|
||
def restaurant_params | ||
params | ||
.require(:restaurant) | ||
.permit( | ||
:name, :description, :address, :phone_number, | ||
:site, :image, :icon, :featured | ||
) | ||
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,9 @@ | ||
class API::RecipesController < ApplicationController | ||
def index | ||
@recipes = Recipe.all | ||
end | ||
|
||
def show | ||
@recipe = Recipe.find(params[:id]) | ||
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,9 @@ | ||
class API::RestaurantsController < ApplicationController | ||
def index | ||
@restaurants = Restaurant.featured | ||
end | ||
|
||
def show | ||
@restaurant = Restaurant.find(params[:id]) | ||
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,9 @@ | ||
class Recipe < ApplicationRecord | ||
belongs_to :restaurant, optional: true | ||
|
||
validates :name, :description, :image, :cooking_time, presence: true | ||
validates :proteins, :fats, :carbohydrates, numericality: { greater_than_or_equal_to: 0 } | ||
validates :calories, numericality: { greater_than: 0 } | ||
|
||
mount_uploader :image, ImageUploader | ||
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,25 @@ | ||
class Restaurant < ApplicationRecord | ||
MAX_FEATURED = 5 | ||
|
||
has_many :recipes, dependent: :destroy | ||
|
||
validates :name, :description, :image, :icon, | ||
:address, :phone_number, :site, presence: true | ||
validate :featured_count | ||
|
||
scope :featured, -> { where(featured: true) } | ||
|
||
mount_uploader :image, ImageUploader | ||
mount_uploader :icon, IconUploader | ||
|
||
def self.max_featured? | ||
featured.count >= MAX_FEATURED | ||
end | ||
|
||
private | ||
|
||
def featured_count | ||
return unless featured? | ||
errors.add(:base, 'Нельзя добавить больше 5 избранных') if Restaurant.max_featured? | ||
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,29 @@ | ||
class ApplicationUploader < CarrierWave::Uploader::Base | ||
# Include RMagick or MiniMagick support: | ||
include CarrierWave::MiniMagick | ||
|
||
# Override the directory where uploaded files will be stored. | ||
# This is a sensible default for uploaders that are meant to be mounted: | ||
def store_dir | ||
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" | ||
end | ||
|
||
# Add a white list of extensions which are allowed to be uploaded. | ||
# For images you might use something like this: | ||
def extension_white_list | ||
%w(jpg jpeg gif png) | ||
end | ||
|
||
# Override the filename of the uploaded files: | ||
# Avoid using model.id or version_name here, see uploader/store.rb for details. | ||
def filename | ||
"#{secure_token}.#{file.extension}" if original_filename | ||
end | ||
|
||
protected | ||
|
||
def secure_token | ||
var = :"@#{mounted_as}_secure_token" | ||
model.instance_variable_get(var) || model.instance_variable_set(var, SecureRandom.hex) | ||
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,5 @@ | ||
class IconUploader < ApplicationUploader | ||
version :thumb do | ||
process resize_to_limit: [100, 100] | ||
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,5 @@ | ||
class ImageUploader < ApplicationUploader | ||
version :thumb do | ||
process resize_to_limit: [250, 200] | ||
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,40 @@ | ||
= form_for [:admin, @recipe] do |f| | ||
|
||
.form-group | ||
= f.label :name | ||
= f.text_field :name, class: 'form-control' | ||
|
||
.form-group | ||
= f.label :description | ||
= f.text_field :description, class: 'form-control' | ||
|
||
.form-group | ||
= f.label :restaurant | ||
= f.collection_select :restaurant_id, Restaurant.all, :id, :name, { include_blank: true }, { class: 'form-control' } | ||
|
||
.form-group | ||
= f.label :calories | ||
= f.number_field :calories, class: 'form-control' | ||
|
||
.form-group | ||
= f.label :cooking_time | ||
= f.number_field :cooking_time, class: 'form-control' | ||
|
||
.form-group | ||
= f.label :proteins | ||
= f.number_field :proteins, class: 'form-control' | ||
|
||
.form-group | ||
= f.label :fats | ||
= f.number_field :fats, class: 'form-control' | ||
|
||
.form-group | ||
= f.label :carbohydrates | ||
= f.number_field :carbohydrates, class: 'form-control' | ||
|
||
.form-group | ||
= f.label :image | ||
= f.file_field :image | ||
|
||
.actions | ||
= f.submit class: 'btn btn-default' |
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,3 @@ | ||
h1 Изменить рецепт | ||
|
||
= render 'form' |
Oops, something went wrong.