Skip to content
Browse files

Add processor determination to units

  • Loading branch information...
1 parent 96ce62e commit b241d0b6e6e1bf3ac46705c83be1962ab9cfb21e @rwdaigle committed Jun 1, 2012
Showing with 50 additions and 44 deletions.
  1. +2 −31 app/models/subject.rb
  2. +33 −5 app/models/unit.rb
  3. +9 −0 db/migrate/20120602003006_add_processor_to_units.rb
  4. +6 −8 db/schema.rb
View
33 app/models/subject.rb
@@ -37,15 +37,9 @@ def process!(subject_id, overwrite=false)
def process!(overwrite = false)
(overwrite ? units : units.empty).each do |unit|
- $queue.enqueue("WordnikProcessor.process!", unit.id)
+ unit.process!(overwrite)
end
end
-
- # # Processors are now unit-specific
- # def with_processor(&block)
- # processor_klass = resolve_processor_class
- # yield processor_klass if processor_klass
- # end
# Override the param field to use in URIs
def to_param; permalink; end
@@ -57,27 +51,4 @@ def to_log
{ 'subject_id' => self.id, 'subject_title' => self.title }
end
- private
-
- def resolve_processor_class
- if(unit_processor_type)
- begin
- @resolved_processor_class ||= "#{unit_processor_type}_processor".classify.constantize
- rescue
- # log({ 'status' => error, self, "message=\"Unable to resolve processor class '#{unit_processor_type}' #{$!}\"", "error=#{$!}")
- end
- end
- end
-
-end
-
- # Old implementation
- #
- # # Import the question and answers from the given file into this
- # # subject
- # def import(file)
- # CSV::Reader.parse(File.open(file)).each do |row|
- # logger.info "Importing #{row[0]} -> #{row[1]}"
- # units.find_or_create_by_question_and_answer(row[0], row[1])
- # end
- # end
+end
View
38 app/models/unit.rb
@@ -5,7 +5,7 @@ class Unit < ActiveRecord::Base
validates_presence_of :question, :subject
validates_uniqueness_of :question, :scope => :subject_id
- before_save :normalize_question
+ before_save :normalize_question, :set_processor_type
after_create :process!
belongs_to :subject
@@ -43,9 +43,8 @@ def clean(scope = :all, opts = {})
find(scope, {:select => 'units.*'}.merge(opts))
end
- def process!(unit_id, overwrite=false)
- unit = self.find(unit_id)
- unit.process!(overwrite) if unit
+ def process!(unit_id, overwrite = false)
+ find(unit_id).try(:process!, overwrite)
end
end
@@ -54,8 +53,16 @@ def <=>(other)
self.position <=> other.position
end
+ def empty?
+ answer.blank?
+ end
+
def process!(overwrite = false)
- $queue.enqueue("WordnikProcessor.process!", id) if answer.blank? || overwrite
+ if overwrite || empty?
+ with_processor do |processor_klass|
+ $queue.enqueue("#{processor_klass}.process!", id)
+ end
+ end
end
def to_log
@@ -65,4 +72,25 @@ def to_log
def normalize_question
self.question = question.strip.downcase if question
end
+
+ def set_processor_type
+ self.processor_type = "Wordnik" unless processor_type
+ end
+
+ def with_processor(&block)
+ processor_klass = resolve_processor_class
+ yield processor_klass if processor_klass
+ end
+
+ private
+
+ def resolve_processor_class
+ if(processor_type)
+ begin
+ @resolved_processor_class ||= "#{processor_type}_processor".classify.constantize
+ rescue
+ # log({ 'status' => error, self, "message=\"Unable to resolve processor class '#{unit_processor_type}' #{$!}\"", "error=#{$!}")
+ end
+ end
+ end
end
View
9 db/migrate/20120602003006_add_processor_to_units.rb
@@ -0,0 +1,9 @@
+class AddProcessorToUnits < ActiveRecord::Migration
+
+ def change
+ change_table :units do |t|
+ t.column :processor_type, :string
+ end
+ remove_column :subjects, :unit_processor_type, :from, :to
+ end
+end
View
14 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120601202329) do
+ActiveRecord::Schema.define(:version => 20120602003006) do
create_table "associations", :force => true do |t|
t.integer "user_id", :null => false
@@ -63,26 +63,24 @@
add_index "reviews", ["user_id"], :name => "index_reviews_on_user_id"
create_table "subjects", :force => true do |t|
- t.string "permalink", :null => false
- t.string "from"
- t.string "to"
+ t.string "permalink", :null => false
t.datetime "created_at"
t.datetime "updated_at"
- t.string "title", :null => false
+ t.string "title", :null => false
t.integer "owner_id"
- t.string "unit_processor_type"
end
add_index "subjects", ["owner_id"], :name => "index_subjects_on_owner_id"
add_index "subjects", ["permalink"], :name => "index_subjects_on_permalink"
create_table "units", :force => true do |t|
- t.string "question", :null => false
+ t.string "question", :null => false
t.text "answer"
- t.integer "subject_id", :null => false
+ t.integer "subject_id", :null => false
t.integer "position"
t.datetime "created_at"
t.datetime "updated_at"
+ t.string "processor_type"
end
add_index "units", ["position"], :name => "index_units_on_position"

0 comments on commit b241d0b

Please sign in to comment.
Something went wrong with that request. Please try again.