Permalink
Browse files

Form helper is successfully working.

  • Loading branch information...
1 parent d189ca2 commit 2991c4e1e1910e9d4b461e694234a518b78ce21a @aantix committed Sep 24, 2010
Showing with 199 additions and 120 deletions.
  1. +3 −0 .gitignore
  2. +2 −0 Rakefile
  3. +125 −118 lib/turkee.rb
  4. +0 −2 rails/init.rb
  5. +69 −0 turkee.gemspec
View
@@ -1,6 +1,9 @@
## MAC OS
.DS_Store
+*.gem
+build.sh
+
## TEXTMATE
*.tmproj
tmtags
View
@@ -1,6 +1,8 @@
require 'rubygems'
require 'rake'
+$LOAD_PATH.unshift('lib')
+
begin
INSTALL_MESSAGE = %q{
========================================================================
View
@@ -1,161 +1,168 @@
-class TurkeeTask < ActiveRecord::Base
- belongs_to :task, :polymorphic => true
-
- HIT_MAX_ASSIGNMENTS = 5
- HIT_REWARD = 0.05
- HIT_LIFETIME = 5.days.seconds.to_i # in seconds
- HIT_FRAMEHEIGHT = 1000
-
- named_scope :unprocessed_hits, :conditions => ['completed is null']
-
- def logger
- @logger ||= Logger.new($stderr)
- end
+module Turkee
- # Use this method to go out and retrieve the data for all of the posted Turk Tasks.
- # Each specific TurkTask object (determined by task_type field) is in charge of
- # accepting/rejecting the assignment and importing the data into their respective tables.
- def self.process_hits
- turks = TurkTask.unprocessed_hits
+ class TurkeeTask < ActiveRecord::Base
+ belongs_to :task, :polymorphic => true
- turks.each do |turk|
- hit = RTurk::Hit.new(turk.hit_id)
+ HIT_MAX_ASSIGNMENTS = 5
+ HIT_REWARD = 0.05
+ HIT_LIFETIME = 5.days.seconds.to_i
+ # in seconds
+ HIT_FRAMEHEIGHT = 1000
- hit.assignments.each do |assignment|
- next if assignment.status != 'Submitted' # Only process submitted assignments
- Object::const_get(turk.task_type).eval_assignment(assignment)
- end
+ named_scope :unprocessed_hits, :conditions => ['completed is null']
- turk.completed = true
- turk.save
-
- # hit.dispose!
+ def logger
+ @logger ||= Logger.new($stderr)
end
-
- end
- # Creates a new Mechanical Turk task on AMZN with the given title, desc, etc
- def self.create_turk(hit_title, hit_description, url)
+ # Use this method to go out and retrieve the data for all of the posted Turk Tasks.
+ # Each specific TurkeeTask object (determined by task_type field) is in charge of
+ # accepting/rejecting the assignment and importing the data into their respective tables.
+ def self.process_hits
+ turks = TurkeeTask.unprocessed_hits
+
+ turks.each do |turk|
+ hit = RTurk::Hit.new(turk.hit_id)
+
+ hit.assignments.each do |assignment|
+ next if assignment.status != 'Submitted'
+ # Only process submitted assignments
+ Object::const_get(turk.task_type).eval_assignment(assignment)
+ end
+
+ turk.completed = true
+ turk.save
- h = RTurk::Hit.create(:title => hit_title) do |hit|
- hit.assignments = HIT_MAX_ASSIGNMENTS
- hit.description = hit_description
- hit.reward = HIT_REWARD
- hit.lifetime = HIT_LIFETIME
- hit.question(url, :frame_height => HIT_FRAMEHEIGHT) # pixels for iframe
+ # hit.dispose!
+ end
- # hit.qualifications.add :approval_rate, { :gt => 80 }
end
- end
+ # Creates a new Mechanical Turk task on AMZN with the given title, desc, etc
+ def self.create_turk(hit_title, hit_description, url)
- # Finds all of the tasks that haven't been posted to Mechanical Turk and creates HITs for them.
- def self.dequeue_turks(storyboard)
- turks = TurkeeTask.unqueued_tasks_for_storyboard(storyboard.id)
+ h = RTurk::Hit.create(:title => hit_title) do |hit|
+ hit.assignments = HIT_MAX_ASSIGNMENTS
+ hit.description = hit_description
+ hit.reward = HIT_REWARD
+ hit.lifetime = HIT_LIFETIME
+ hit.question(url, :frame_height => HIT_FRAMEHEIGHT)
+ # pixels for iframe
- turks.each do |turk|
-
- hit = nil
- #HIT_MAX_ASSIGNMENTS.times do
- hit = TurkeeTask.create_turk(turk.hit_title, turk.hit_description, turk.form_url)
- #end
+ # hit.qualifications.add :approval_rate, { :gt => 80 }
+ end
- # Can't update the turk object with a save since it was retrieved
- # using a 'join' so we can use the class level TurkTask update method.
- TurkeeTask.update(turk.id, {:hit_url => hit.url, :hit_id => hit.id}) unless hit.nil?
end
- end
+ # Finds all of the tasks that haven't been posted to Mechanical Turk and creates HITs for them.
+ def self.dequeue_turks(storyboard)
+ turks = TurkeeTask.unqueued_tasks_for_storyboard(storyboard.id)
- # Queue a turkee task. All tasks get paired to a storyboard location.
- # E.g. an 'eye-witness account' or a location specific photo.
- def self.queue_turk(typ, title, description, form_url)
+ turks.each do |turk|
- task = TurkeeTask.create(:sandbox => TURKTASK_SANDBOX,
- :hit_title => title,
- :hit_description => description,
- :form_url => form_url,
- :task_type => typ.to_s)
+ hit = nil
+ #HIT_MAX_ASSIGNMENTS.times do
+ hit = TurkeeTask.create_turk(turk.hit_title, turk.hit_description, turk.form_url)
+ #end
- # Need to append the turk_task_id to the form url and update the task with it.
- new_form_url = form_url =~ /\?/ ? "#{form_url}&turk_task_id=#{task.id}" : "#{form_url}?turk_task_id=#{task.id}"
+ # Can't update the turk object with a save since it was retrieved
+ # using a 'join' so we can use the class level TurkeeTask update method.
+ TurkeeTask.update(turk.id, {:hit_url => hit.url, :hit_id => hit.id}) unless hit.nil?
+ end
- if !new_form_url.nil? && !task.nil?
- task.form_url = new_form_url
- task.save
end
- end
- ##########################################################################################################
- # DON'T PUSH THIS BUTTON UNLESS YOU MEAN IT. :)
- def self.clear_all_turks
- # Do NOT execute this function if we're in production mode
- raise "You can only clear turks in the sandbox/development environment." if RAILS_ENV == 'production'
+ # Queue a turkee task. All tasks get paired to a storyboard location.
+ # E.g. an 'eye-witness account' or a location specific photo.
+ def self.queue_turk(typ, title, description, form_url)
- hits = RTurk::Hit.all_reviewable
+ task = TurkeeTask.create(:sandbox => TURKEETASK_SANDBOX,
+ :hit_title => title,
+ :hit_description => description,
+ :form_url => form_url,
+ :task_type => typ.to_s)
- logger.info "#{hits.size} reviewable hits. \n"
+ # Need to append the turk_task_id to the form url and update the task with it.
+ new_form_url = form_url =~ /\?/ ? "#{form_url}&turk_task_id=#{task.id}" : "#{form_url}?turk_task_id=#{task.id}"
- unless hits.empty?
- logger.info puts "Approving all assignments and disposing of each hit."
+ if !new_form_url.nil? && !task.nil?
+ task.form_url = new_form_url
+ task.save
+ end
+ end
+
+ ##########################################################################################################
+ # DON'T PUSH THIS BUTTON UNLESS YOU MEAN IT. :)
+ def self.clear_all_turks
+ # Do NOT execute this function if we're in production mode
+ raise "You can only clear turks in the sandbox/development environment." if RAILS_ENV == 'production'
+
+ hits = RTurk::Hit.all_reviewable
+
+ logger.info "#{hits.size} reviewable hits. \n"
+
+ unless hits.empty?
+ logger.info puts "Approving all assignments and disposing of each hit."
+
+ hits.each do |hit|
+ #hit.expire!
+ begin
+ hit.expire! if (hit.status == "Assignable" || hit.status == 'Unassignable')
+
+ hit.assignments.each do |assignment|
- hits.each do |hit|
- #hit.expire!
- begin
- hit.expire! if (hit.status == "Assignable" || hit.status == 'Unassignable')
-
- hit.assignments.each do |assignment|
+ logger.info "Assignment status : #{assignment.status}"
- puts "Assignment status : #{assignment.status}"
-
- assignment.approve!('__clear_all_turks__approved__') if assignment.status == 'Submitted'
+ assignment.approve!('__clear_all_turks__approved__') if assignment.status == 'Submitted'
+ end
+ hit.dispose!
+ rescue Exception => e
+ # Probably a service unavailable
+ logger.error "Exception : #{e.to_s}"
end
- hit.dispose!
- rescue Exception => e
- # Probably a service unavailable
- logger.error "Exception : #{e.to_s}"
end
end
+
end
end
-end
+ module TurkeeFormHelper
+ # Rails 2.3.8 form_for implementation with the exception of what url it posts to.
+ def turkee_form_for(record_or_name_or_array, *args, &proc)
+ raise ArgumentError, "Missing block" unless block_given?
+ options = args.extract_options!
+ case record_or_name_or_array
+ when String, Symbol
+ object_name = record_or_name_or_array
+ when Array
+ object = record_or_name_or_array.last
+ object_name = ActionController::RecordIdentifier.singular_class_name(object)
+ apply_form_for_options!(record_or_name_or_array, options)
+ args.unshift object
+ else
+ object = record_or_name_or_array
+ object_name = ActionController::RecordIdentifier.singular_class_name(object)
+ apply_form_for_options!([object], options)
+ args.unshift object
+ end
-module TurkeeFormBuilder
- def mturk_url
- RAILS_ENV == 'development' ? "https://workersandbox.mturk.com/mturk/externalSubmit" : "https://www.mturk.com/mturk/externalSubmit"
- end
+ # concat(form_tag(options.delete(:url) || {}, options.delete(:html) || {}))
+ concat(form_tag(mturk_url))
+ fields_for(object_name, *(args << options), &proc)
+ concat('</form>'.html_safe)
+ end
- # Rails 2.3.8 form_for implementation with the exception of what url it posts to.
- def turkee_form_for(record_or_name_or_array, *args, &proc)
- raise ArgumentError, "Missing block" unless block_given?
-
- options = args.extract_options!
-
- case record_or_name_or_array
- when String, Symbol
- object_name = record_or_name_or_array
- when Array
- object = record_or_name_or_array.last
- object_name = ActionController::RecordIdentifier.singular_class_name(object)
- apply_form_for_options!(record_or_name_or_array, options)
- args.unshift object
- else
- object = record_or_name_or_array
- object_name = ActionController::RecordIdentifier.singular_class_name(object)
- apply_form_for_options!([object], options)
- args.unshift object
+ private
+ def mturk_url
+ RAILS_ENV == 'development' ? "https://workersandbox.mturk.com/mturk/externalSubmit" : "https://www.mturk.com/mturk/externalSubmit"
end
- # concat(form_tag(options.delete(:url) || {}, options.delete(:html) || {}))
- concat(form_tag(mturk_url))
- fields_for(object_name, *(args << options), &proc)
- concat('</form>'.html_safe)
end
-
end
+
+ActionView::Base.send :include, Turkee::TurkeeFormHelper
View
@@ -1,2 +0,0 @@
-# coding: utf-8
-require File.expand_path(File.join(File.dirname(__FILE__), "..", "init"))
View
@@ -0,0 +1,69 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{turkee}
+ s.version = "1.0.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Jim Jones"]
+ s.date = %q{2010-09-23}
+ s.description = %q{Turkee will help you to create your Rails forms, post the HITs, and retrieve the user entered values from Mechanical Turk.}
+ s.email = %q{jjones@aantix.com}
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.rdoc"
+ ]
+ s.files = [
+ "Rakefile",
+ "generators/turkee/templates/turkee.js",
+ "generators/turkee/templates/turkee_migration.rb.erb",
+ "generators/turkee/turkee_generator.rb",
+ "lib/turkee.rb",
+ "rails/init.rb",
+ "spec/spec.opts",
+ "spec/spec_helper.rb",
+ "spec/turkee_spec.rb"
+ ]
+ s.homepage = %q{http://github.com/aantix/turkee}
+ s.post_install_message = %q{
+ ========================================================================
+ Turkee Installation Complete.
+ ------------------------------------------------------------------------
+ Please run the generator to copy the needed javascript file
+ into your application:
+ rails generator turkee:install # Rails 3
+ ./script/generate turkee # Rails 2
+
+ For instructions on gem usage, visit:
+ http://github.com/aantix/turkee#readme
+ ========================================================================
+ }
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.7}
+ s.summary = %q{Turkee makes dealing with Amazon's Mechnical Turk a breeze.}
+ s.test_files = [
+ "spec/spec_helper.rb",
+ "spec/turkee_spec.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
+ s.add_development_dependency(%q<rturk>, [">= 2.3.0"])
+ else
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
+ s.add_dependency(%q<rturk>, [">= 2.3.0"])
+ end
+ else
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
+ s.add_dependency(%q<rturk>, [">= 2.3.0"])
+ end
+end
+

0 comments on commit 2991c4e

Please sign in to comment.