require 'polygallery/factories' module Polygallery module CapybaraHelpers def polyphoto_tr_selector(table_id='photos') ".polygallery-cocoon-table##{table_id} tbody tr" end def polyphoto_trs(table_id='photos') all polyphoto_tr_selector(table_id) end def select_polyphoto_tr(i=0, table_id='photos') if i.is_a? String table_id = i; i = 0 end polyphoto_trs(table_id).to_a[i] end def add_polyphoto_btn_selector(table_id='photos') '.add_fields[data-association-insertion-node="#' << table_id << ' .photo-receptacle"]' end def add_polyphoto_to_form(table_id='photos', attrs=nil) unless table_id.is_a? String attrs = table_id; table_id = 'photos' end attrs ||= build :polygallery_photo trs = polyphoto_trs(table_id); last_tr = trs.last unless last_tr[:class].include?('new-polyphoto') && last_tr.find('input[id$="_photo"]').value.blank? page.execute_script "$('#{add_polyphoto_btn_selector table_id}').click();" last_tr = polyphoto_trs(table_id).last end within last_tr do find('input[id$="_photo"]').set get_random_photo find('input[id$="_caption"]').set(attrs.caption) yield if block_given? end end def change_polyphoto_in_form(photo_index=0, table_id='photos', file_to_use=nil) if photo_index.is_a? File file_to_use = photo_index; photo_index = 0 end if table_id.is_a? File file_to_use = table_id; table_id = 'photos' end if photo_index.is_a? String table_id = photo_index; photo_index = 0 end file_to_use ||= get_random_photo within select_polyphoto_tr(photo_index, table_id) do find('input[id$="_photo"]').set file_to_use yield if block_given? end end def remove_polyphoto_from_form(photo_index=0, table_id='photos') if photo_index.is_a? String table_id = photo_index; photo_index = 0 end tr_selector = polyphoto_tr_selector(table_id) + ':visible' page.execute_script <<-SCRIPT setTimeout(function(){ $($('#{tr_selector}')[#{photo_index}]).find('.remove_fields').click(); }, 1); SCRIPT sleep 0.005 end end end