Skip to content

Instantly share code, notes, and snippets.

@josephcc
Created January 14, 2014 20:09
Show Gist options
  • Select an option

  • Save josephcc/8424760 to your computer and use it in GitHub Desktop.

Select an option

Save josephcc/8424760 to your computer and use it in GitHub Desktop.
class Task < ActiveRecord::Base
attr_accessible :name, :taskable_id, :taskable_type, :question_id, :task_role_id, :task_order_id
belongs_to :taskable, polymorphic: true
belongs_to :task_role
belongs_to :task_status
belongs_to :task_order
belongs_to :question
has_many :task_assignments
after_create :pub_create, :send_turkee_task, if: Rails.configuration.ka.tasks_launch_hits == true
after_update :pub_update
after_destroy :pub_destroy
belongs_to :open_after_role, :class_name => "TaskRole"
validates_presence_of :name#, :taskable_id, :taskable_type
scope :open_tasks, includes(:task_assignments).where("task_assignments.id IS NULL")
scope :assigned_tasks, includes(:task_assignments).where("task_assignments.id IS NOT NULL")
scope :assigned_to, lambda{|user| joins(:task_assignments).where("task_assignments.user_id = ?",user.id)}
def question
parent = self.taskable_type.capitalize.constantize.find(self.taskable_id)
if self.taskable_type == 'question'
question = parent
else
question = parent.question
end
question
end
def send_turkee_task#(num_assignments, reward, lifetime)
hit_title = self.name+" (Chrome/Firefox/Safari)"
hit_description = self.name#+"Given a problem, help us find websites that would be useful for solving it"
host = Phase0::Application.config.hostaddress
task = Turkee::TurkeeTask.create_hit(host, hit_title, hit_description, "", 1, 0.15, 2, 2, {},{:question_id => self.question.id},{:form_url => "#{host}"+parse_view_url})
end
def parse_view_url
view_url = self.task_role.view_url
if view_url.present?
#
# substitute #{:references}
#
view_url.sub! '#{:taskable_type}', self.taskable_type.downcase.pluralize
view_url.sub! '#{:taskable_id}', self.taskable_id.to_s
else
false
end
end
def percent_options
return {
'None; start now'=>0,
'10%'=>10,
'20%'=>20,
'30%'=>30,
'40%'=>40,
'50%'=>50,
'60%'=>60,
'70%'=>70,
'80%'=>80,
'90%'=>90,
'100%'=>100
}
end
def referent_excerpt(length=50)
@field = [
'name',
'title',
'text'
]
obj = self.taskable_type.capitalize.constantize.find(self.taskable_id)
@field.each do |f|
if obj[f].present?
excerpt = obj[f][0..length]
if excerpt.length > 49
excerpt = excerpt+'...'
end
return excerpt
end
end
end
def prep_json
tmp = self
#tmp["count"] = self.task_assignments.count
tmp.as_json(:except => :updated_at, :include => [:task_assignments])
end
def pub_create
PrivatePub.publish_to('/task/'+self.taskable_type+'/'+self.taskable_id.to_s+'/create', task: self.prep_json)
end
def pub_update
PrivatePub.publish_to('/task/'+self.taskable_type+'/'+self.taskable_id.to_s+'/update', task: self.prep_json, changes: self.changes)
end
def pub_destroy
PrivatePub.publish_to('/task/'+self.taskable_type+'/'+self.taskable_id.to_s+'/destroy', task: self.prep_json)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment