class Re<%=rule_class%> < ActiveRecord::Base set_table_name :re_<%=rule_name%> has_many :words, :source=>:re_<%=rule_name%>_words, :foreign_key=>'writer_id', :dependent => :destroy end class Re<%=rule_class%>Words < ActiveRecord::Base set_table_name :re_<%=rule_name%>_words belongs_to :writer, :class_name => 'Re<%=rule_class%>', :foreign_key => 'writer_id' has_many :counts, :source=>:re_<%=rule_name%>_count, :foreign_key=>'word_id', :dependent => :destroy def self.find_or_create(writer_id, word) re_word = find(:first, :conditions => ["writer_id = ? AND word = ?", writer_id, word]) unless re_word re_word = create(:writer_id => writer_id, :word => word, :ignore_word => 0) end re_word.id end end class Re<%=rule_class%>Count < ActiveRecord::Base set_table_name :re_<%=rule_name%>_count belongs_to :word, :class_name => 'Re<%=rule_class%>Words', :foreign_key => 'word_id' def self.update_time_codes(word_id) now = Time.now; [now.strftime("%Y%m%d%H"), now.strftime("%Y%m%d"), now.strftime("%Y%m"), now.strftime("%Y")].each do |time_code| re_word_count = find(:first, :conditions => ["time_code = ? AND word_id = ?", time_code, word_id]) if re_word_count re_word_count.update_attributes(:word_count => re_word_count.word_count + 1) else create(:time_code => time_code, :word_id => word_id, :word_count => 1) end end end end module RulesEngine module Rule class <%=rule_class%> < RulesEngine::Rule::Definition attr_reader :re_<%=rule_name%>_id ################################################################## # class options self.options = { :group => 'Twitter', :display_name => 'Tweet Word Writer', :help_partial => '/re_rules/<%=rule_name%>/help', :new_partial => '/re_rules/<%=rule_name%>/new', :edit_partial => '/re_rules/<%=rule_name%>/edit' } ################################################################## # set the rule data def data= data if data.nil? @title = nil @re_<%=rule_name%>_id = nil else @title, data_id = ActiveSupport::JSON.decode(data) @re_<%=rule_name%>_id = data_id.to_i end end ################################################################## # get the rule attributes def title @title end def summary "Write the twitter words to the database" end def data [title, re_<%=rule_name%>_id.to_s].to_json end def expected_outcomes [:outcome => RulesEngine::Rule::Outcome::NEXT] end ################################################################## # set the rule attributes def attributes=(params) param_hash = params.symbolize_keys @title = param_hash[:<%=rule_name%>_title] end ################################################################## # validation and errors def valid? @errors = {} @errors[:<%=rule_name%>_title] = "Title required" if title.blank? return @errors.empty? end ################################################################## # callbacks when the rule is added and removed from a workflow def before_create() @re_<%=rule_name%>_id = Re<%=rule_class%>.create(:title => title).id end def before_update() re_<%=rule_name%> = Re<%=rule_class%>.find_by_id(re_<%=rule_name%>_id) if re_<%=rule_name%> re_<%=rule_name%>.update_attributes(:title => title) else @re_<%=rule_name%>_id = Re<%=rule_class%>.create(:title => title).id end end def before_destroy() re_<%=rule_name%> = Re<%=rule_class%>.find_by_id(re_<%=rule_name%>_id) re_<%=rule_name%>.destroy if re_<%=rule_name%> end ################################################################## # execute the rule # this rule does nothing def process(process_id, plan, data) (data[:tweet_words] || []).each do |word| word_id = Re<%=rule_class%>Words.find_or_create(re_<%=rule_name%>_id, word) Re<%=rule_class%>Count.update_time_codes(word_id) end RulesEngine::Rule::Outcome.new(RulesEngine::Rule::Outcome::NEXT) end end end end