Skip to content

Instantly share code, notes, and snippets.

@pjammer
Created September 10, 2009 01:14
Show Gist options
  • Save pjammer/184222 to your computer and use it in GitHub Desktop.
Save pjammer/184222 to your computer and use it in GitHub Desktop.
Trying to move controller code to model code
#Model Code
def self.calculate_state_revenue(state)
@affiliates = select_state(state) # gets all affiliates from the state
@orders_unpaginated = self.orders
#use inject to add up their order totals.
@state_total = @affiliates.inject {|result, affiilate|
result = @orders_unpaginated.calculate(:sum, "order_total", :conditions => ['affiliate_code = ?', affiliate.affiliate_code])
result
}
@state_total
end
#select the state, duh!
def select_state(arg)
#find all affiliates by the status state_active, which means they are one of the taxation states.
your_shop.users.find_all_by_status("#{arg}_State_Active") #unless your_shop.users.blank?
end
#controller code
def sales_tax_states
@shop = your_shop
end
# View Code
<h1>Total sales for the trailing 4 quarters by state</h1>
<h2>Rhode Island</h2>
<p><%= @shop.calculate_state_revenue("RI") %></p>
<h2>New York</h2>
<p><%= @shop.calculate_state_revenue("NY") %></p>
<h2>North Carolina</h2>
<p><%= @shop.calculate_state_revenue("NC") %></p>
## Error:
undefined method `calculate_state_revenue' for #<Shop:0x105be6490>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment