0
1 2 3 4 5 6 7 8 9 10 11 12 13
# TODO: выбросить нахер метод with_company_and_state и правильно написать
# этот named scope
named_scope :with_company_and_state_non_uniq, lambda { |company, state|
{ :conditions => [ 'products.company_id = :company_id AND ' +
'state_mask = :state_mask', { :company_id => company,
:state_mask => OrderSet::STATES.index(state) || 0 } ],
:order => 'order_sets.created_at DESC',
:joins => 'INNER JOIN products' }
}
def self.with_company_and_state(company, state)
with_company_and_state_non_uniq(company, state).uniq
end
Бывает, что SELECT DISTINCT сделать не всегда возможно. На самом деле, такая выборка сосёт.
eveel ,
28.03.2010 (Updated 24.03.2018 )
0
1 2 3 4 5
class Vector
def -@
map(&:-@)
end
end
Код мой, говнокодом бы не назвал, но без улыбки точно не взглянешь на такое =)
(тут определение унарного минуса через вызов того же унарного минуса у всех элементов вектора, Кэп)
rakoth3d ,
11.03.2010 (Updated 24.03.2018 )
0
1 2 3
from app/controllers/test_controller.rb:13:in `index'
from (irb):4
from ♥:0>>
Сразу предупреждаю: не говнокод (и даже не код), можно минусовать.
День Святого Валентина, Interactive Ruby выдал такое, типа с праздником :)
PS: незнаю где здесь руби.
hardcoder ,
15.02.2010 (Updated 24.03.2018 )
0
1 2 3 4 5 6 7 8
def has_currency_rate?
val = false
if self.currency.id == self.client.company.currency.id or self.currency_rate.blank?
else
val = true
end
val
end
тяжело же жилось людям...
rakoth3d ,
09.02.2010 (Updated 24.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 39 40 41 42 43 44 45 46 47 48 49 50 51 52
class FinancialEventObserver < ActiveRecord::Observer
observe Payment, Invoice
def before_save(model)
event = nil
if model.class == Payment
if model.new_record?
event = FinancialEvent.new(:event => FinancialEvent::Event::PAYMENT_INVOICE,
:arguments => {:client_name => model.invoice.client.short_name, :invoice_number => model.invoice.invoice_number},
:company_id=>model.invoice.client.company.id)
end
elsif model.class == Invoice
i = Invoice.find_by_id model.id
if model.new_record? or i.status != model.status
if model.status == Invoice::Status::ESTIMATE
event = FinancialEvent.new(:event => FinancialEvent::Event::ESTIMATE_SEND,
:arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},
:company_id=>model.client.company.id)
elsif model.status == Invoice::Status::APPROVED
event = FinancialEvent.new(:event => FinancialEvent::Event::ESTIMATE_APPROVED,
:arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},
:company_id=>model.client.company.id)
elsif model.status == Invoice::Status::REJECTED
event = FinancialEvent.new(:event => FinancialEvent::Event::ESTIMATE_REJECTED,
:arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},
:company_id=>model.client.company.id)
elsif model.status == Invoice::Status::SEND
event = FinancialEvent.new(:event => FinancialEvent::Event::INVOICE_SEND,
:arguments => {:client_name => model.client.short_name, :invoice_number => model.invoice_number},
:company_id=>model.client.company.id)
end
elsif !model.new_record? and i.state != model.state
if model.state == Invoice::State::DELETED
event = FinancialEvent.new(:event => FinancialEvent::Event::INVOICE_DELETED,
:arguments => {:invoice_number => model.invoice_number},
:company_id=>model.client.company.id)
end
end
end
event.eventable = model.requester unless event.blank?
event.save unless event.blank?
end
def before_destroy(model)
if model.class == Payment
event = FinancialEvent.new(:event => FinancialEvent::Event::PAYMENT_DELETED,
:arguments => {:invoice_number => model.invoice.invoice_number},
:company_id=>model.invoice.client.company.id)
event.eventable = model.requester
event.save
end
end
end
о боже, зачем я открыл этот файл?
rakoth3d ,
15.01.2010 (Updated 24.03.2018 )
0
1
some_variable = (some_condition && some_other_condition) ? true : false
Иногда встречаю ГК подобный этому но реализованный в виде case..when. Еще более впечатляет :)
shine ,
15.01.2010 (Updated 24.03.2018 )
0
1
<li class="list_item primary_card_<%= index+1 %> <%= primary_card.patient.sex == 'male' ? 'boy' : 'girl' %> critical">
А можно было изменить имя класса в CSS
nimnull ,
26.12.2009 (Updated 24.03.2018 )
0
1 2 3
def true_or_nil bool
!!bool || nil
end
eveel ,
31.10.2009 (Updated 24.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
case @demand.status #зависит также от временных групп
when 1,4
@status_list[2]=@@statuses[2]
when 2,7,8
@status_list[2]=@@statuses[2]
@status_list[3]=@@statuses[3]
@status_list[7]=@@statuses[7]
@status_list[8]=@@statuses[8]
when 3
@status_list[3]=@@statuses[3]
if @is_admin || @demand.watchers.include?(session[:user]) || @demand.heads.include?(session[:user]) ||@see_later_demands_watchers || @see_later_demands_heads
@status_list[4]=@@statuses[4]
end
if @is_admin || @demand.heads.include?(session[:user]) || @see_later_demands_heads
@status_list[6]=@@statuses[6]
end
when 6
if @is_admin || @demand.watchers.include?(session[:user]) || @demand.heads.include?(session[:user]) ||@see_later_demands_watchers || @see_later_demands_heads
@status_list[4]=@@statuses[4]
end
else
end
магия
Rommel ,
18.10.2009 (Updated 24.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
@ids.each_index do |di|
if @ids[di] then
@ids[di].each_index do |li|
@employ[di][li] = Hash.new
@employ[di][li][:subject] = Subject.find(:first, :conditions => "id = #{
Lesson.find(:first, :conditions => "id = #{@ids[di][li]}")[:subject_id]
}")[:title] if @ids[di][li]
@employ[di][li][:class] = SchoolClass.find(:first, :conditions => "id = #{
Lesson.find(:first, :conditions => "id = #{@ids[di][li]}")[:school_class_id]
}")[:number].to_s +
SchoolClass.find(:first, :conditions => "id = #{
Lesson.find(:first, :conditions => "id = #{@ids[di][li]}")[:school_class_id]
}")[:letter] if @ids[di][li]
@employ[di][li][:teacher] = Teacher.find(:first, :conditions => "id = #{
Lesson.find(:first, :conditions => "id = #{@ids[di][li]}")[:teacher_id]
}")[:name] + ' ' +
@employ[di][li][:teacher] = Teacher.find(:first, :conditions => "id = #{
Lesson.find(:first, :conditions => "id = #{@ids[di][li]}")[:teacher_id]
}")[:surname] + ' ' +
@employ[di][li][:teacher] = Teacher.find(:first, :conditions => "id = #{
Lesson.find(:first, :conditions => "id = #{@ids[di][li]}")[:teacher_id]
}")[:patronymic] if @ids[di][li]
end
end
end
Небольшая утренняя ревизия кода раскрыла заговор по свержению власти и захвату мозга остальных кодеров проекта.
Сидим, рефакторим.
eveel ,
26.08.2009 (Updated 24.03.2018 )