Skip to content
This repository has been archived by the owner on Mar 12, 2023. It is now read-only.
/ kulinar Public archive

Commit

Permalink
Restaurants (#2)
Browse files Browse the repository at this point in the history
* 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
SumLare authored and dankimio committed Aug 4, 2016
1 parent 53354e2 commit 40c7465
Show file tree
Hide file tree
Showing 47 changed files with 755 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
# Ignore Byebug command history file.
.byebug_history

# Ignore uploads
/public/uploads

# macOS
.DS_Store
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ gem 'jbuilder', '~> 2.5'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

gem 'bootstrap-sass'
gem 'slim-rails'
gem 'carrierwave'
gem 'mini_magick'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
Expand Down
26 changes: 26 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,19 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (7.1.0)
autoprefixer-rails (6.3.7)
execjs
bootstrap-sass (3.3.7)
autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4)
builder (3.2.2)
byebug (9.0.5)
carrierwave (0.11.2)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
json (>= 1.7)
mime-types (>= 1.16)
mimemagic (>= 0.3.0)
coffee-rails (4.2.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.2.x)
Expand All @@ -63,6 +74,7 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (2.0.2)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -74,6 +86,8 @@ GEM
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mimemagic (0.3.1)
mini_magick (4.5.1)
mini_portile2 (2.1.0)
minitest (5.9.0)
multi_json (1.12.1)
Expand Down Expand Up @@ -121,6 +135,13 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
slim (3.0.7)
temple (~> 0.7.6)
tilt (>= 1.3.3, < 2.1)
slim-rails (3.1.0)
actionpack (>= 3.1)
railties (>= 3.1)
slim (~> 3.0)
spring (1.7.2)
spring-watcher-listen (2.0.0)
listen (>= 2.7, < 4.0)
Expand All @@ -132,6 +153,7 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
temple (0.7.7)
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.5)
Expand All @@ -155,15 +177,19 @@ PLATFORMS
ruby

DEPENDENCIES
bootstrap-sass
byebug
carrierwave
coffee-rails (~> 4.2)
jbuilder (~> 2.5)
jquery-rails
listen (~> 3.0.5)
mini_magick
pg (~> 0.18)
puma (~> 3.0)
rails (~> 5.0.0)
sass-rails (~> 5.0)
slim-rails
spring
spring-watcher-listen (~> 2.0.0)
turbolinks (~> 5)
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .
15 changes: 0 additions & 15 deletions app/assets/stylesheets/application.css

This file was deleted.

2 changes: 2 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import 'bootstrap-sprockets';
@import 'bootstrap';
55 changes: 55 additions & 0 deletions app/controllers/admin/recipes_controller.rb
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
55 changes: 55 additions & 0 deletions app/controllers/admin/restaurants_controller.rb
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
9 changes: 9 additions & 0 deletions app/controllers/api/recipes_controller.rb
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
9 changes: 9 additions & 0 deletions app/controllers/api/restaurants_controller.rb
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
9 changes: 9 additions & 0 deletions app/models/recipe.rb
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
25 changes: 25 additions & 0 deletions app/models/restaurant.rb
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
29 changes: 29 additions & 0 deletions app/uploaders/application_uploader.rb
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
5 changes: 5 additions & 0 deletions app/uploaders/icon_uploader.rb
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
5 changes: 5 additions & 0 deletions app/uploaders/image_uploader.rb
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
40 changes: 40 additions & 0 deletions app/views/admin/recipes/_form.html.slim
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'
3 changes: 3 additions & 0 deletions app/views/admin/recipes/edit.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 Изменить рецепт

= render 'form'
Loading

0 comments on commit 40c7465

Please sign in to comment.