- 1
- 2
- 3
- 4
- 5
if user.sex == true
image_path 'no-photo-male.jpg'
else
image_path 'no-photo-female.jpg'
end
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
Всего: 3
−36
if user.sex == true
image_path 'no-photo-male.jpg'
else
image_path 'no-photo-female.jpg'
end
Это Оскар!
−32
# We have strange bug: when company's fresher salary is updated, CompanyAnnouncement.track_update
# method is called.
# This method helps to see, was object changed or not.
# This is workaround. We should get rid of this method in future.
def self.object_changed?(obj)
# Warning: We can't have "return true if obj.changed?" here, because sometimes changed? method
# returns true when object was not really changed.
return false if !obj.changed?
obj.changes.each do |k, v|
if v[0] != v[1] && k != 'updated_at'
return true
end
end
false
end
Открываю индусский проект, который надо спасать. app/controllers - 60 контроллеров, ок. User.rb - 650 строк кода, ок. А вот и самый сок!
−89
# Да, да, захардкоженные id!
# .where(id: [id_arrays]) ? Не, не слышал
@cities = [52, 21, 9, 18, 196, 27, 106, 4, 40, 71].collect { |c| City.find(c) }
# Это все в одном экшене, ага.
session[:latitude] = params[:my_latitude]
session[:longitude] = params[:my_longitude]
session[:my_address] = params[:my_address]
session[:fuel] = params[:fuel][:fuel_id]
session[:range] = params[:station][:range]
# А потом кто-нибудь развернет базу из бэкапа без сохранения первичных ключей и все перестанет работать.
@fuels = Fuel.where(:id=>[1, 2, 3, 5])
# Типичный экшен
session[:view_sub_page] = action_name
id = params[:station_chain].split("-")
s_id = id[id.size - 2]
station = Station.where(:id=>s_id)
@fuels_level = AppUtils.calculate_price(station)
@prices = station.last.prices.order("created_at").group_by { |item| item.fuel_id }
@fuel_id = id[id.size - 1].to_i
@station_lists = Station.get_station_detail_map(station, @fuels_level, @fuel_id, @prices)
@station = station.last
@station_logo = ''
@station_logo_chain = ManageStationChain.where(:brand_id=>@station.brand_id).last
@station_logo = PromoLogo.find_by_station_id(@station.id)
@end = @station.address
@fuels = Fuel.where(:id=>[1, 2, 3, 5])
@sub_station = ""
@cities = Station.get_static_cities()
# Какие еще Rails, у меня свой лисопед
def City.find_by_name(name)
city = City.where("name = ?", name).first
if city.nil?
city = City.new
city.name = name
city.save
end
return city
end
# Именно так, в одну строчку. station.open_time и close_time - типа :time в базе
if !station.open_time.nil? and !station.close_time.nil? and (Time.parse(station.open_time.to_s)...Time.parse(station.close_time.to_s)).cover?(Time.now.utc.change(year: 2000, month: 1, day: 1, hour: Time.now.hour, min: Time.now.min, second: 0)) == false
return "PRICE_LEVEL_CLOSE"
end
# Компьютер купил, программировать не купил
# В таблице Points - тысячи записей
def get_points
points = Points.all
render json: {:success => true, :data => {:points => points.as_json(:only => [:action, :points, :points_new])}} and return
end
# И под занавес - код логина
# Гемы? Devise? OAuth? А чой энта?
def login
register = false
url = "https://graph.facebook.com/me/?access_token="+params[:facebook_token]
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.path + "?" + uri.query)
response = http.request(request)
if response.code == "200"
facebook_response = JSON.parse(response.body)
user = User.where("facebook_id = ?", facebook_response["id"]).first
if !user
user = User.new
user.facebook_id = facebook_response["id"]
user.nick = facebook_response["username"]
user.email = facebook_response["email"]
register = true
end
user.email = facebook_response["email"]
user.generate_token
user.save
if register == true
history = History.new
history.user = user
points = Points.where(:action => "REGISTER").first
history.points_action = points
history.points = points.points
user.points+=points.points
user.save
history.save
end
logger.debug("Calculating point");
points = Points.where('action != ?', 'FACEBOOK_INVITES')
logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
logger.debug("Testing for points");
logger.debug(points.as_json(:only => [:action, :points]))
logger.debug(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
if user.disabled == true
render json: {:success => false, :error => {:message => "Invalid facebook token", :error_code => "INVALID_TOKEN"}}, status: :unauthorized and return
else
render json: {:success => true, :data => {:auth_token => user.auth_token, :points => points.as_json(:only => [:action, :points])}} and return#
...
Почему никогда нельзя нанимать индусских программистов. Выдержки из избранного.