Created
February 27, 2009 18:36
-
-
Save AndrewO/71629 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CheckAssociationProcessor < ETL::Processor::RowProcessor | |
attr_reader :ids, :foreign_key | |
def initialize(control, configuration) | |
super | |
@foriegn_key = configuration[:foreign_key] || raise ArgumentError, ":foreign_key option required" | |
model = configuration[:model] || raise ArgumentError, ":model option required" | |
# Optional :conditions or :find_options | |
find_options = cond = (configuration[:conditions] ? {:conditions => cond} : configuration[:find_options]) || {} | |
@ids = model.find(:all, {:select => "id"}.merge(find_options)).map {|m| m.id} | |
end | |
def process(row) | |
self.ids.include?(row[foreign_key.to_sym].to_i) ? row : [] | |
end | |
end | |
before_write CheckAssociationProcessor, :model => EnterprisePricing::Route, :foreign_key => "route_id" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment