Forum: Ruby on Rails MVC architecture

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.
Af3cecc8af253b5acd3c09c5b67c0074?d=identicon&s=25 Valentino Lun (on9west)
on 2009-01-06 05:10
Dear all

In the MVC architecture framework, the Model is actually communicate
with database. In my rails project, there is a model/ssh.rb that is not
connect with database. Is it a good practice for this arrangement in my
project? Am I violate the rules of MVC architecture? Or is it better to
move all the codes from models to controllers? What do you think? Thank
you.

models/ssh.rb
$LOADED_FEATURES[$LOADED_FEATURES.length] =
'net/ssh/authentication/pageant.rb'
require 'net/ssh'

class Ssh

  def self.execute(cmd)
    ssh=Net::SSH.start("ahnibm71", "testlib", :port => 19207, :paranoid
=> false, :auth_methods => ["publickey"], :passphrase => "hellokitty",
:keys => ["C:/testing/id_rsa"])
    output = ssh.exec!(cmd)
    ssh.close
    if output.nil?
      return "NULL"
    else
      return output.gsub("\n", "<br>")
    end
  end

end

controllers/ssh_controller.rb
class SshController < ApplicationController
  def main
  end

  def test
    render :text => Ssh.execute(params[:command])
  end

end
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-01-06 06:13
(Received via mailing list)
On Jan 5, 11:10 pm, Valentino Lun <rails-mailing-l...@andreas-s.net>
wrote:
> Dear all
>
> In the MVC architecture framework, the Model is actually communicate
> with database. In my rails project, there is a model/ssh.rb that is not
> connect with database.

That's fine.  The model is the only part of the MVC architecture that
is *allowed* to communicate with the database, but it certainly does
not *have* to do so.  The job of the model is simply to model the
data.  If it needs access to the DB to do that, fine.  If it doesn't
need access to the DB to model the data effectively, also fine.

> Is it a good practice for this arrangement in my
> project?

Probably, although I'm not certain about your particular case.

> Am I violate the rules of MVC architecture?

Not at all.

> Or is it better to
> move all the codes from models to controllers?

Probably not.  The trend in Rails development at the moment seems to
be "skinny controller, fat model".

In other words, you're doing fine.  Don't worry.

Best,
--
Marnen Laibow-Koser
marnen@marnen.org
http://www.marnen.org
6fabb84e727dee98d1277df56458cbac?d=identicon&s=25 Andrew Bloom (Guest)
on 2009-01-06 06:31
(Received via mailing list)
It would also be appropriate to place the ssh.rb file in the lib/
directory. Placing it in app/models/ is fine. It's just a matter of
preference.

On Jan 5, 10:10 pm, Valentino Lun <rails-mailing-l...@andreas-s.net>
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-01-07 02:46
(Received via mailing list)
Valentino Lun wrote:

 > In the MVC architecture framework, the Model is actually communicate
 > with database. In my rails project, there is a model/ssh.rb that is
not
 > connect with database. Is it a good practice for this arrangement in
my
 > project? Am I violate the rules of MVC architecture? Or is it better
to
 > move all the codes from models to controllers? What do you think?
Thank
 > you.

MVC has two meanings. The most important meaning is a set of Guidelines
(_not_
"Rules") for where to put what logic. For example, if you put HTML code
in the
Controller or Model, you had better be prepared to answer why.

MVC's other meaning is the answer to these questions for most GUIs:

  1. do all your tests pass?
  2. is your code simple and easy to read?
  3. are you as DRY as possible without violating 2 (or 1)?
  4. is your code minimal, without run-on modules?

The game is to apply each item, in order, while not breaking the
previous items.
Don't DRY your code (meaning Don't Repeat Yourself) if you break tests,
or make
your code unreadable. Don't remove lines of code if tests break? (That
is
"refactoring".)

The ideal of MVC is that a good design that follows those guidelines
ought to
automatically fall out as three layers, with the data accessors in one
layer;
the "switchboard operators" in another layer, plugging everything
together, and
the views in another layer, stretching it all out for the users. So, in
theory,
if you apply those Simplicity Guidelines an MVC pattern ought to emerge
for you.

Put anything you like in the models folder, so long as it's something
that
controllers mix and match together.

 > models/ssh.rb
 > $LOADED_FEATURES[$LOADED_FEATURES.length] =
 > 'net/ssh/authentication/pageant.rb'
 > require 'net/ssh'
 >
 > class Ssh
 >
 >   def self.execute(cmd)
 >     ssh=Net::SSH.start("ahnibm71", "testlib", :port => 19207,
:paranoid
 > => false, :auth_methods => ["publickey"], :passphrase =>
"hellokitty",
 > :keys => ["C:/testing/id_rsa"])
 >     output = ssh.exec!(cmd)
 >     ssh.close
 >     if output.nil?
 >       return "NULL"
 >     else
 >       return output.gsub("\n", "<br>")

Firstly, this mixes HTML and Model. That <br> is not DRY, because it
repeats the
concept of a view down here. Remove that gsub, and render your output
like this:

  <pre><%=h output %></pre>

Nextly, all your code should be pure XHTML (google assert_xpath for more
on this
subject!), so you should have used <br/>. XHTML makes your pages
super-easy to
test, and this, in turn, keeps your code quality extremely high. Lack of
tests
is a much, much bigger problem than any variations in MVC ideals!

 >     end
 >   end
 >
 > end
 >
 > controllers/ssh_controller.rb
 > class SshController < ApplicationController

Does this control SSHs? What is the concept on the SSH wire that's so
important?
Is this a "RemoteCommandController"?

 >   def main

If you have a main.html.erb file, and if it takes care of itself, you
don't need
this action in here.

 >   end
 >
 >   def test
 >     render :text => Ssh.execute(params[:command])

Exactly. Models are typically things that respond to Hash options, and
yours
does. But put the <pre> tag up here, like this:

      render :inline => "<pre><%=h Ssh.execute(params[:command])
%></pre>"

Why did I use :inline, instead of :text with a #{} masher? Because h()
is not
visible at this level, and :inline opens up a little bitty View inside
your
Controller.

Yet your existing design was already very DRY, so these are only
suggestions.
The only technical fix is the h().

Further, shouldn't some other Model object intermediate between the raw
SSH
layer and the Controller? What concept is is the SSH object?

--
   Phlip
This topic is locked and can not be replied to.