Topic: Long code reprat its self to many times

the only think that changes is

event.first_staff.to_s
to
event.second_staff.to_s

but i have 5 of them


- whos_on.each do  |event|
      
          %dt
          %dd
            .with-padding
              %p.button-height
                =raw event.genetal_instructions
                %br
                - get_staff.each do  |staff|
                  - if (staff.id.to_s == event.first_staff.to_s)
                    - name =  staff.full_name
                    - if event.first_number == 'cell'
                      - phone = staff.cell
                    - if event.first_number == 'office'
                      - phone = staff.office.to_s + ";" + staff.extention.to_s
                    - if event.first_number == 'home'
                      - phone = staff.home
                    - if event.first_number == 'fax'
                      - phone = staff.fax
                    - if event.first_number == 'email'
                      - phone = staff.email
                    - if event.first_number == 'beeper'
                      - phone = staff.beeper
                    - if event.first_number == 'smsemail'
                      - phone = staff.smsemail
                    = hidden_field_tag 'text-to-copy1'+event.id.to_s, phone
                    %a#copy-button1{:href => "#", :class=> 'click2copy button with-tooltip anthracite-gradient glossy icon-pages', :id=> event.id.to_s , :title=>t(:copy) }
                    %strong=t(:name)
                    = name-
                    %strong= phone
                = event.first_notes
                %br
                - get_staff.each do  |staff|
                  - if (staff.id.to_s == event.second_staff.to_s)
                    - name =  staff.full_name
                    - if event.second_number == 'cell'
                      - phone = staff.cell
                    - if event.second_number == 'office'
                      - phone = staff.office.to_s + ";" + staff.extention.to_s
                    - if event.second_number == 'home'
                      - phone = staff.home
                    - if event.second_number == 'fax'
                      - phone = staff.fax
                    - if event.second_number == 'email'
                      - phone = staff.email
                    - if event.second_number == 'beeper'
                      - phone = staff.beeper
                    - if event.second_number == 'smsemail'
                      - phone = staff.smsemail
                    = hidden_field_tag 'text-to-copy1'+event.id.to_s, phone
                    %a#copy-button1{:href => "#", :class=> 'click2copy button with-tooltip anthracite-gradient glossy icon-pages', :id=> event.id.to_s , :title=>t(:copy) }
                    %strong=t(:name)
                    = name-
                    %strong= phone
                = event.second_notes

Re: Long code reprat its self to many times

If you'll change office getter on:
def office
staff.office.to_s + ...
end

Or change the name of event on something like "office_with_extension", then you can make it shorter using 'send' method:
phone = staff.send(event.first_staff)

Last edited by KindBug (2013-04-15 09:24:26)

Re: Long code reprat its self to many times

Thanks

Re: Long code reprat its self to many times

Also you can use get method_prefix like this:
method_prefix = ['first', 'second', 'third', 'fourth', 'fifth'].detect{|number| staff.id.to_s == event.send("#{number}_staff").to_s}

Then you'll be able to have one cycle with:
staff_method_name = event.send("#{method_prefix}_number")
phone = staff.send(staff_method_name)

Re: Long code reprat its self to many times

- get_staff.each do  |staff|
- staff_method_name = event.send("#{method_prefix}_number")
- phone = staff.send(staff_method_name)

And changes into your model or event names