Created
August 10, 2009 18:31
-
-
Save zhhz/165338 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
def generate_daily_pos_report | |
# create a fake request | |
fake_request = Merb::Request.new({}) | |
# init the controller | |
controller = Purchasing::Reports.new(fake_request) | |
# gets all the pos created on today | |
pos = PurchaseOrder.all(:deleted => false, :po_date => date) | |
results = "No purchase orders created on #{date}" | |
if pos.size > 0 | |
## set the instance varialbe for the html.erb, :@pos see report/deaily_pos.html.erb | |
controller.instance_variable_set(:@pos, pos) | |
# gets the html string | |
results = controller.render :_daily_pos_results, :layout => :email | |
end | |
# if you want to re-use the logic in controll, you have to use _dispatch | |
# but this is not a good solution, you have embed in some extra logic to | |
# handle the render method in controller, the best way to re-use the logic | |
# in controller is to move the logic out of controller, put it some services | |
# layer, then you can re-use it. | |
# result = controller._dispatch(:daily_pos) | |
file = purchasing_dir + "/#{date}.html" | |
File.open(file, 'w') {|f| f.puts results} | |
file | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment