Skip to content

Instantly share code, notes, and snippets.

View aya-soft's full-sized avatar
🎯
Focusing

Anton Ageev aya-soft

🎯
Focusing
  • iTechArt
  • Minsk, Belarus
View GitHub Profile
@aya-soft
aya-soft / xls_spec.rb
Last active February 13, 2019 18:42
Spreadsheet: wrong value from formula
#encoding: utf-8
require 'spreadsheet'
xls_file_name = "/tmp/status_data.xls"
xls_book = Spreadsheet.open(xls_file_name, "rb")
xls_book.worksheet(0).each do |row|
puts row[6].value if row[6] and row[6].is_a?(Spreadsheet::Formula)
@aya-soft
aya-soft / service_station_comments_controller.rb
Created May 27, 2016 20:08
1) Зачем вводить лишнюю переменную @Spare, если есть @comment.spare?
def edit
@comment = ServiceStationComment.find(params[:id])
@spare = @comment.spare
end
@aya-soft
aya-soft / service_station_comments_controller.rb
Created May 27, 2016 20:11
1) Почему comment, а не @comment? Стремись к однообразию! 2) Зачем искать @Spare таким извращенческим способом? Запчасть же у тебя есть в comment.spare!
def update
@spare = Spare.find(params[:service_station_comment][:spare_id])
comment = ServiceStationComment.find(params[:id])
if comment.update permit params[:service_station_comment]
redirect_to admin_service_station_path(comment.service_station)
else
render :edit
end
end
@aya-soft
aya-soft / service_station_comments_controller.rb
Created May 27, 2016 20:34
Подобные методы лучше писать по-другому
def permit(params)
params.permit(:spare_id, :service_station_id, :installing_current_part_price, :time_of_installing_current_part, :labor_times_price,:blocked_at, :comment)
end
@aya-soft
aya-soft / edit.html.haml
Created May 27, 2016 20:45
1) @spare.number нужно заменить на @comment.spare.number 2) hidden_field убрать СРОЧНО!!! его можно легко подделать
%h1 Редактировать комментарий об установки детали № #{@spare.number}
%h2 Сервисный центр #{@comment.service_station.name}
= simple_form_for [:admin, @comment], :html => { id: "comment_form", class: "form-horizontal", role: "form" } do |f|
= f.hidden_field :spare_id, value: @spare.id
= f.input :time_of_installing_current_part, label: "Ориентировочное время установки (ч.)"
= f.input :installing_current_part_price, label: "Стоимость установки (р.)"
= f.input :comment, label: "Комментарий", as: :text
.form-group
@aya-soft
aya-soft / profile\service_station_comments_controller.rb
Last active May 30, 2016 11:12
Контроллеры в пространстве имен Profile предназначены для работы с данными конкретных пользователей!Как же ты тут выбираешь из базы все комменты без разбора?
class Profile::ServiceStationCommentsController < ApplicationController
before_filter :authenticate_user!
before_filter :load_spare, only: [:index, :new]
def index
@comments = ServiceStationComment.all
end
@aya-soft
aya-soft / profile\service_station_comments_controller.rb
Last active May 30, 2016 09:40
Unless не используют с else, т.к. он и так трудно понятный, а тут станет вообще ненужным, потому что его можно легко превратить в легко понимаемый if-else
def new
unless current_user.service_station.present?
redirect_to new_profile_service_station_path
else
service = current_user.service_station.service_station_comments.where("spare_id = ?", params[:spare_id])
if service.present?
redirect_to "/profile/service_station_comments/#{service.first.id}/edit"
end
@comment = ServiceStationComment.new
gon.labor_times_price = current_user.service_station.labor_times_price
@aya-soft
aya-soft / service_station.rb
Created May 27, 2016 21:22
В каждой строчке и так есть результат вычисления выражения, поэтому последнюю строчку метода нужно убрать
def self.new_autoservice_requests_qty
qty ||= unregistered.count
qty
end
@aya-soft
aya-soft / new.html.haml
Created May 27, 2016 23:41
gon.labor_times_price - это же тупая константа, а ты целую библиотеку ненужную припахал :(
:javascript
$("#service_station_comment_time_of_installing_current_part").change(function() {
var a = $("#service_station_comment_time_of_installing_current_part").val();
$("#service_station_comment_installing_current_part_price").val(a * gon.labor_times_price)
});
@aya-soft
aya-soft / profile\service_station_comments_controller.rb
Last active May 30, 2016 08:21
Как ты не видишь где что лежит из данных - я не понимаю! Все ж на своих местах!
def new
unless current_user.service_station.present?
redirect_to new_profile_service_station_path
else
service = current_user.service_station.service_station_comments.where("spare_id = ?", params[:spare_id])
if service.present?
redirect_to "/profile/service_station_comments/#{service.first.id}/edit"
end
@comment = ServiceStationComment.new
gon.labor_times_price = current_user.service_station.labor_times_price