Guest User

Untitled

a guest
Mar 2nd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.66 KB | None | 0 0
  1. module HelperMethods
  2.  
  3.   def upload_track(options = {})
  4.     case options
  5.       when :mp3
  6.         attachment = File.new(File.join(Rails.root, 'spec', 'support', 'files', 'track.mp3'))
  7.       when :image
  8.         attachment = File.new(File.join(Rails.root, 'spec', 'support', 'files', 'image.jpg'))
  9.     end
  10.     track = Track.upload(@user, attachment.to_path, attachment)
  11.   end
  12.  
  13.   def destroy_from_collection(track, album)
  14.     page.driver.delete("/album_collections/#{album.id}?album_id=#{album.id}&track_id=#{track.id}")
  15.     visit page.driver.response.location
  16.   end
  17.  
  18.   def create_album_collection(count, user, album)
  19.     count.times { Factory.create(:album_collection, :track => Factory.create(:track, :user => user), :album => album)}
  20.   end
  21.  
  22.   def remote_ip_digest
  23.     Digest::SHA1.hexdigest("request.remote_addr")
  24.   end
  25.  
  26.   def generate_authorize_key
  27.     Digest::SHA1.hexdigest("#{Time.now.strftime("%H%M")}#{remote_ip_digest}")
  28.   end
  29.  
  30.   def authorize_track
  31.     cookies[:authorize_track] = generate_authorize_key
  32.   end
  33.  
  34.   def fill_user_form(name, email, password = 'foobar', confirmation = false)
  35.     (fill_in 'Name', :with => name) if name.present?
  36.     (fill_in "Email", :with => email) if email.present?
  37.     (fill_in "Password", :with => password) if password.present?
  38.     (fill_in "Password confirmation", :with => password) if confirmation == true
  39.   end
  40.  
  41.   def sign_in(email = nil, password = nil)
  42.     @user = Factory(:user)
  43.     visit root_path
  44.     click_link "Sign in"
  45.     fill_user_form(nil, (email.blank? ? @user.email : email), (password.blank? ? 'foobar' : password))
  46.     click_button "Sign In"
  47.   end
  48.  
  49.   def page_should_have(array)
  50.     array.map {|album| page.should have_content album.title}
  51.   end
  52.  
  53.   def truncate_title(album, length)
  54.     title = album.title
  55.     title = title.chop while title.length > (length - 3)
  56.     split = title.split.delete_if {|word| !album.title.split.include?(word) if title.split.last.length > 3}
  57.     return "#{split.join(" ")}..."
  58.   end
  59.  
  60.   def start_new_album(options = {:title => false, :image => false}, album = false)
  61.     unless album
  62.       visit new_album_path
  63.     else
  64.       visit edit_album_path(album)
  65.     end
  66.     fill_in "Title", :with => (options[:title] ? 'Album collection' : '')
  67.     case options[:image]
  68.     when :medium
  69.       attach_file('Cover', 'spec/support/files/image.jpg')
  70.     when :small
  71.       attach_file('Cover', 'spec/support/files/small_image.jpg')
  72.     when :not_image
  73.       attach_file('Cover', 'spec/support/files/track.mp3')
  74.     end
  75.     yield if block_given?
  76.     click_button (album ? 'Update Album' : 'Create Album')
  77.   end
  78. end
  79.  
  80. RSpec.configuration.include HelperMethods
Add Comment
Please, Sign In to add comment