0
1 2 3 4 5 6 7
before_create :build_client_info_attributes
def build_client_info_attributes
CLIENT_INFO_ATTRIBUTES.each do |attr|
eval("def #{attr}();return read_attribute(:#{attr}).present? ? read_attribute(:#{attr}) : subject.try(:owner).try(:#{attr});end;")
end
eval("def country_id;read_attribute(:country).present? ? read_attribute(:country) : subject.try(:owner).try(:country).try(:id);end;")
end
Шедевр метапрограммирования.
maxk ,
18.04.2016 (Updated 29.03.2018 )
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
prepared_items = @cart.prepare_sort
errors = []
orders = []
ActiveRecord::Base.transaction do
address_create = create_new_address address_params if order_params[:address_attributes].present?
prepared_items.each do |type, items|
next unless items.present?
user = current_user.class.name == 'User' ? { user_id: current_user.id } : { user_id: nil }
case type
when :order then @order = Order.new(order_params.merge(user))
when :preorder then @order = Order.new(order_params.merge(user.merge(status: 'preorder')))
when :b_y then @order = Order.new(order_params.merge(user.merge(delivery: 'pick_up', store_id: Settings.store.for_by)))
end
@order.is_mobile = mobile_device?
@order.promo_code = @cart.promo_code
@order.promotion = Promotion.for_line_items(@cart)
good_promotion = Promotion.good_promotion_with_bonus(@cart)
if good_promotion && @cart.used_promotion_discount > 0
@order.bonus_promotion = good_promotion.promotion
@order.stored_bonus_promotion_discount = @cart.used_promotion_discount
end
items.each{ |item| @order.add_line_item item }
address_create.orders << @order if address_create.present?
if @order.save
orders << @order
else
errors += @order.errors.full_messages
end
# Are you fucking serious?
end if errors.empty?
чувак спутал цикл и each с блоком, печалька. код из реального проекта
tribals ,
07.04.2016 (Updated 29.03.2018 )
0
1 2 3 4 5 6 7 8 9 10 11
begin
@project = Admin::Project.find(params[:project_id])
rescue ActiveRecord::RecordNotFound => ex
raise ActiveRecord::RecordNotFound, "project with id = #{params[:project_id]} not found"
end
begin
user = Admin::User.find(params[:user_id])
rescue ActiveRecord::RecordNotFound => ex
raise ActiveRecord::RecordNotFound, "user with id = #{params[:user_id]} not found"
end
strax ,
07.04.2016 (Updated 29.03.2018 )
0
1 2 3 4 5 6
def events_format(e)
m = I18n.l e.event_start, :format=> '%m'
m = m.sub!(/^0/, "") if m[0] == "0"
m_result = "#{I18n.l e.event_start, :format=> '%Y-%m-%d'}"
return [m_result + " • " + e.name + " • " + e.try(:venue).try(:city).try(:name).to_s, e.id]
end
american_spy ,
06.04.2016 (Updated 29.03.2018 )
0
1 2 3 4 5
def self.any_value_to_boolean(val)
val = (((val.is_a? String) and (val.downcase != 'false') and (val.downcase != 'no') and (val != '0')) \
or ((val.is_a? Numeric) and (val != 0)))
val
end
Привидение типов хендмейд. Найдено здесь https://github.com/Novator/Pandora
Вообще каждый из 21к строк дойстойна быть здесь
SqREL ,
05.04.2016 (Updated 29.03.2018 )
0
А что с руби категорией стало? Ни одного поста.
SDK ,
25.02.2016 (Updated 29.03.2018 )
0
1
column 'Плательщик' do |task| (task.booking && task.booking.invoice) && task.booking.invoice.full_name || '' end
darkdemiurge ,
23.02.2016 (Updated 29.03.2018 )
0
1 2 3 4 5 6 7
def show
if (params[:id].scan(/-/).length == 1)
redirect_to transfers_path(game_id: params[:game_id], period: 'days', start_date: params[:id] + '-01', end_date: Date.parse(params[:id] + '-01').end_of_month.strftime('%Y-%m-%d'))
return
end
...
end
А всего лишь нужно было в действие show принимать дату
Sandwich ,
19.02.2016 (Updated 29.03.2018 )
0
1
(link_to company.email, 'mailto:' + company.email) if !company.email.nil?
В одном из проектов
gururuby ,
17.02.2016 (Updated 29.03.2018 )
0
1 2 3 4
data = []
10000.times do |i|
data.push ({ :shipper => 'ЗАО "НЕФТЕТРАНССЕРВИС" ' + i, :shipper_id => i })
end
Когда одного НЕФТЕТРАНССЕРВИСа просто мало...
SDK ,
09.02.2016 (Updated 29.03.2018 )