Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should calculation logic go in a Rails app?

I have an app that models a House. The House has_many Rooms, Rooms has_many Lights and Small_appliances, etc. I also have a controller called Calculator that is how the app is accessed. Data is added to the house (and its rooms) using the Calculator controller. Then a report is generated, which is located at app/views/calculator/report.html.erb.

My question is where should all the calculations and logic for the report go? Currently I have it all in the view, with some things in calculator_helper. Normally this would go in the model, right? But Calculator doesn't have a model that was generated. What is the standard for this?

Here is the calculator controller.

class CalculatorController < ApplicationController
  def index
  end

  def save_house
    @house = House.new(params[:house])
    respond_to do |format|
      if @house.save
        format.html { render :action => 'add_rooms', :id => @house }
        format.xml { render :xml => @house, :status => :created, :location => @house }
      else
        format.html { render :action => 'index' }
        format.xml  { render :xml => @house.errors, :status => :unprocessable_entity }
      end
    end
  end

  def add_rooms
    @house = House.find(params[:id])
    @rooms = Room.find_by_house_id(@house.id)

  rescue ActiveRecord::RecordNotFound
    logger.error("Attempt to access invalid house #{params[:id]}")
    flash[:notice] = "You must create a house before adding rooms"
    redirect_to :action => 'index'
  end

  def add_room
    @room = Room.new(params[:room])
    @house = @room.house

    respond_to do |format|
      if @room.save
        flash[:notice] = "Room \"#{@room.name}\" was successfully added."
        format.html { render :action => 'add_rooms' }
        format.xml { render :xml => @room, :status => :created, :location => @room }
      else
        format.html { render :action => 'add_rooms' }
        format.xml  { render :xml => @room.errors, :status => :unprocessable_entity }
      end
    end
  rescue ActiveRecord::RecordNotFound
    logger.error("Attempt to access invalid house #{params[:id]}")
    flash[:notice] = "You must create a house before adding a room"
    redirect_to :action => 'index'
  end

  def report
    flash[:notice] = nil
    @house = House.find(params[:id])
    @rooms = Room.find_by_house_id(@house.id)
  rescue ActiveRecord::RecordNotFound
    logger.error("Attempt to access invalid house #{params[:id]}")
    flash[:notice] = "You must create a house before generating a report"
    redirect_to :action => 'index'
  end
end
like image 761
Ryan Avatar asked Nov 07 '09 03:11

Ryan


People also ask

Where do I put business logic rails?

You can put business logic anywhere you want (even in views! though that's a bad idea). I'd say if the logic is tied to a real-world object, then put it on the model. Otherwise, use the controller.

What kind of logic doesn't belong to the model in Rails?

A model is the M in MVC. Models contain business logic that interacts with the database. Business logic that doesn't interact with a database doesn't belong in a model. That's it.


3 Answers

There are a few ways to approach it, but the logic certainly does not belong in the view. You have the various models associated with one another in a clear hierarchy with the top of the hierarchy being the House model, if I am reading your description correctly. That being the case, I would add an appropriate method of set of methods to the House model that may be composed of calls to calculation methods in the Room models associated with a given House instance and on down the line of association. That ways the relevant calculation can be performed at each level and through composing one or more methods at the House model level you are able to have a clean, expressive and maintainable way to deal with calculations.

One thing to do, as well, would be to make sure that any calculations that can be performed by the DB are. For example, if there is a calculation that a Room model can do by simply querying it's own data then by all means push that computational burden to the DB using the ability of ActiveRecord to invoke such lower level calculation logic. Check out the API docs for the details.

I would look very carefully at the logic you want and see how it can be pushed into the model since that is probably where it belongs, close to the actual data of the calculations, and within the class structures that represent that data specifically; I would not create a model just to handle the calculation logic unless you really need to store the calculations persistently for some reason.

like image 181
plainprogrammer Avatar answered Oct 29 '22 07:10

plainprogrammer


I would create a class in RAILS_ROOT/lib/ called, for example, Calculator and put the code in there.

Classes in /lib/ should be loaded an available anywhere in your app.

You can also create a plain ruby object in /app/models/. There's no reason they all have to inherit from ActiveRecord::Base

like image 44
BJ Clark Avatar answered Oct 29 '22 06:10

BJ Clark


Ok, now I can see the code posted. I can see the calculator_controller actually has no calculations in it, are they in the views?. Try this approach:

  1. Write a test that sets up an object which will return the results you need to return to the user of the web page, given a house, rooms or whatever else it needs.
  2. Build a model (in models) to make that test pass.
  3. Modify your controller code above to use your new calculator model
  4. Modify the tests of your controller so they also pass. These tests, of course, do not need to test any business logic.

My prior respose:

If the business logic is fairly simple and only used behind this web app, then you can put it in your app/models folder.

class MyCoolClass
  def initialize(clues)
    @other_things = OtherThing.all
  end
  def do_cool_thing; end
  def calculate_coolness
    @other_things.length 
  end
end

Then in your controller, create an instance of your model

def index
  @mcc = MyCoolClass "A clue as to what I want"
  render
end

Then in your templates you can access it

<%=h @mcc.calculate_coolness %>

Note that @other_things is an instance__variable of MyCoolClass and generally not accessible to the templates without accessor methods being defined

like image 25
wentbackward Avatar answered Oct 29 '22 07:10

wentbackward