Module: Turkee::TurkeeFormHelper

Defined in:
lib/turkee.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.disable_form_fields?(assignment) ⇒ Boolean

Returns whether the form fields should be disabled or not (based on the assignment_id)

Returns:

  • (Boolean)


313
314
315
316
# File 'lib/turkee.rb', line 313

def self.disable_form_fields?(assignment)
  assignment_id = assignment.is_a?(Hash) ? assignment[:assignmentId] : assignment
  (assignment_id.nil? || assignment_id == 'ASSIGNMENT_ID_NOT_AVAILABLE')
end

Instance Method Details

#mturk_urlObject

Returns the external Mechanical Turk url used to post form data based on whether RTurk is cofigured

for sandbox use or not.


308
309
310
# File 'lib/turkee.rb', line 308

def mturk_url
  RTurk.sandbox? ? "https://workersandbox.mturk.com/mturk/externalSubmit" : "https://www.mturk.com/mturk/externalSubmit"
end

#turkee_form_for(record, params, options = {}, &proc) ⇒ Object

Rails 3.1.1 form_for implementation with the exception of the form action url will always point to the Amazon externalSubmit interface and you must pass in the assignment_id parameter.

Raises:

  • (ArgumentError)


272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/turkee.rb', line 272

def turkee_form_for(record, params, options = {}, &proc)
  raise ArgumentError, "Missing block" unless block_given?
  raise ArgumentError, "turkee_form_for now requires that you pass in the entire params hash, instead of just the assignmentId value. " unless params.is_a?(Hash)
  options[:html] ||= {}

  case record
  when String, Symbol
    object_name = record
    object      = nil
  else
    object      = record.is_a?(Array) ? record.last : record
    object_name = options[:as] || ActiveModel::Naming.param_key(object)
    apply_form_for_options!(record, options)
  end

  options[:html][:remote] = options.delete(:remote) if options.has_key?(:remote)
  options[:html][:method] = options.delete(:method) if options.has_key?(:method)
  options[:html][:authenticity_token] = options.delete(:authenticity_token)

  builder = options[:parent_builder] = instantiate_builder(object_name, object, options, &proc)
  fields_for = fields_for(object_name, object, options, &proc)
  default_options = builder.multipart? ? { :multipart => true } : {}

  output = form_tag(mturk_url, default_options.merge!(options.delete(:html)))
  params.each do |k,v|
    unless k =~ /^action$/i || k =~ /^controller$/i || v.class != String
      output.safe_concat("<input type=\"hidden\" id=\"#{k}\" name=\"#{CGI.escape(k)}\" value=\"#{CGI.escape(v)}\"/>")
    end
  end
  options[:disabled] = true if params[:assignmentId].nil? || Turkee::TurkeeFormHelper::disable_form_fields?(params[:assignmentId])
  output << fields_for
  output.safe_concat('</form>')
end