Forum: Ruby on Rails Best way to organize non-controller logic???

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-03-21 00:46
I don't want to put certain code in my controller because I think it
makes things more confusing.

What is the best way to manage business logic such that it doesn't end
up in the controller?

Basically, where should I put my regular utility classes and backing
objects that may not be models?

Thanks,
Wes
F3dc06f587d1ff4c7366b102bfda9204?d=identicon&s=25 David Mitchell (Guest)
on 2006-03-21 00:55
(Received via mailing list)
application.rb can be a good spot for general purpose stuff - I put
e.g. my date formatting stuff in there to ensure dates are shown
consistently across an entire site.

Regards

Dave M.
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-03-21 01:32
(Received via mailing list)
you could create modules and put them in the lib directory.
24e30ea2f4bfda89a9a915dd18247d05?d=identicon&s=25 Jeremy Huffman (Guest)
on 2006-03-21 03:54
(Received via mailing list)
Can you give an example of business logic that doesn't interact with a
model?

On 3/20/06, Chris Hall <christopher.k.hall@gmail.com> wrote:
> >
> > Rails@lists.rubyonrails.org
>
--
Jeremy Huffman
http://www.jeremyhuffman.com
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-03-21 13:03
(Received via mailing list)
for me, I have to access external information on an AS/400 system.  My
only
access to the system is via ODBC, for which there is no AR Adapter
avaliable.

so what i did was create a module for making connections to and
extracting
information from the AS/400 and put it in the lib directory.

AS400.rb

require 'dbi'
module AS400
  def AS400.get_some_data(args)
    # do the AS/400 stuff here
    # massage the data if needed (strip whitespaces, do some
calculations,
etc)
    return data
  end
end

then where i need it, typically a controller, i just require 'AS400' and
do
AS400.get_some_data(12345) .

Chris
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-03-21 16:23
(Received via mailing list)
Personally, I'd say that looks like a model.  You can just put it in
your models directory, and use it from controllers the way you
normally would.  You'll need to set up some mocks in order to unit
test it, since most back-end systems like that don't provide usable
test regions. (At least not for the meaning of the word that OO
programmers are comfortable with.)

You can use the "model" and "service" controller class methods to make
sure it gets 'required' in the appropriate place.
0091f92762685860109bbcb02edfdf27?d=identicon&s=25 Alain Ravet (Guest)
on 2006-03-21 20:01
(Received via mailing list)
> you could create modules and put them in the lib directory.

It's  cleaner BUT it's cached in development mode => if you change it,
you need to restart the server.

Alain
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-03-21 20:18
I have a filename that is an attribute of one of my model objects.  It
references a file in the file system.

I need to figure out if that file is an Excel file, a CSV file, or
neither.

Wes


Jeremy Huffman wrote:
> Can you give an example of business logic that doesn't interact with a
> model?
>
> On 3/20/06, Chris Hall <christopher.k.hall@gmail.com> wrote:
>> >
>> > Rails@lists.rubyonrails.org
>>
> --
> Jeremy Huffman
> http://www.jeremyhuffman.com
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-03-28 14:16
(Received via mailing list)
class MyObject < ActiveRecord::Base
  ...
  validates_each :filename do |fname|
    unless [".xls", ".csv"].include?(fname.extname)
      model.errors.add(fname, "File does not appear to be an excel or
csv
file")
    end
end
This topic is locked and can not be replied to.