SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Zealot ThetaWaveRider's Avatar
    Join Date
    Aug 2004
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Low-level customization in Rails

    Hello all,

    The more I read about Rails, the more I'm considering using it for my next project. There's one thing I'm not sure of, though, and it will make/break my decision to go that route.

    Since Rails is a framework, how easy is it to override classes that do not do exactly what I want? If this is possible, how much Ruby would I need to know in order to do so (beyond what would be taught in a basic Rails book)?

    The reason I ask is because while frameworks are great for getting work done fast, they can be somewhat constricting if something outside of the functionality of the default framework is required. That's why PHP is great....it's highly customizable at a low level. It just takes forever to build things (when not using PEAR or someone else's horrible coding from hotscripts). If I had the freedom of making low-level changes while at the same time having the option of using conventionalized classes (to the language itself, not as an extension) to get common things done, this would be ideal.

    So the question, then, is - does Rails allow me to do this?

    Thanks,
    Aaron

  2. #2
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ThetaWaveRider
    Since Rails is a framework, how easy is it to override classes that do not do exactly what I want?
    On a technical level, Ruby has open classes, so it is simple to override behaviour in classes after the fact.

    More practically, if you want to change the framework, you're going to have to know enough Ruby to understand how the framework works. The amount you need to know about the framework will depend on how much you want to change.

    What sort of customizations do you have in mind?

    You'll probably find that many things which would require changing the framework itself are much easier to do in Rails without editing the framework itself. For example, some people have complained that they would rather have Model classes which don't depend on ActiveRecord. The solution is simple: delete the words "ActiveRecord" from the model class, and Rails will treat your Model class like any other; you don't need to go into the framework to change things.

    The same is true for templates. If you don't like the default ERB templates, you can easily use a different template engine without modification to the Rails code. Liquid or Amrita are examples of template engines wrapped up as Rails plugins.

    Douglas
    Hello World

  3. #3
    SitePoint Addict Brak's Avatar
    Join Date
    Jul 2004
    Location
    Central Coast, CA
    Posts
    321
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Rails actually makes customizing the framework easier than any other framework I've worked with (not every framework, mind you). You can write a few timely coded plugins that you can carry along with all of your applications. You could write a plugin that lets you call acts_as_herd_of_sheep if you like, and it would be completely separate from the framework - all packaged up in a plugin.

    So, yes, rails more than allows you to customize the framework - it even has features that make it easier
    Studio Rockstar's Blog - A journey to quitting the dayjob.

  4. #4
    SitePoint Addict
    Join Date
    Nov 2001
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't mean to sound like an echo, but I will anway: Rails is very simple to work around whenever you choose to do so.

    For instance, while my current project fits nicely into Rails for CRUD operations and for displaying every page, I need a very advanced search mechanims. No problem... I just create my own SQL statements in the controller and use a couple of non-ActiveRecord model classes and it works great. Rails lets you slip in and out of it so gracefully you hardly notice when you're not using it.

  5. #5
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the ability to alter classes after they are loaded makes things eay in Ruby:
    Code:
    class Smelly
      def odor
        puts 'bad'
      end
      def whatever
        puts 'blargggg'
      end
    end
    
    s = Smelly.new
    s.odor
    
    class Smelly
      def odor
        puts 'horid'
      end
    end
    
    s.odor
    s.whatever
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the ability to edit classes when they are already defined is just great in itself. The fact you can do that after they've been initiated is just a big bonus
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  7. #7
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just create my own SQL statements in the controller
    ???

    SQL belongs in the model ;-). If you put the SQL in the controller, then you can only use the search function in that controller. So I think it is better to put it in the model.

    Rails is customizable, but you shouldn't customize it too much. If you want to do everything your way, it might be better to code your own framework. Remember that a good portion of Rails' productivity boost depends on doing things the Rails way.

    And remember that you might break Rails if you override classes. If class A uses class B, and you override class B, class A might not work anymore.

    But you can always add methods to classes, even to builtin Ruby classes:

    Code:
    class String
      def say_hi
        print "Hello, #{self}!"
      end
    end
    
    "ThetaWaveRider".say_hi()
    
    =>
    
    Hello, ThetaWaveRider!
    So you can do the same thing with all Rails' classes.

  8. #8
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by shakin
    I don't mean to sound like an echo, but I will anway: Rails is very simple to work around whenever you choose to do so.

    For instance, while my current project fits nicely into Rails for CRUD operations and for displaying every page, I need a very advanced search mechanims. No problem... I just create my own SQL statements in the controller and use a couple of non-ActiveRecord model classes and it works great. Rails lets you slip in and out of it so gracefully you hardly notice when you're not using it.

    SQL really has no place in the controller. It should be in a model, somewhere.
    Last edited by Luke Redpath; Mar 28, 2006 at 18:25.

  9. #9
    SitePoint Zealot ThetaWaveRider's Avatar
    Join Date
    Aug 2004
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the feedback everyone, you've been helpful. I really like the simplicity of the Rails syntax, and, from what I hear in this thread, the framework is flexible enough to do anything that I'll need it to.

    In fact, I REALLY like the simplicity of the Rails syntax. Seems like it is self-documenting and short enough to allow me to focus more on the actual design than I usually have time to do. And since I currently wind up with some hybrid semi-object-oriented beast when I'm done coding, being forced to code in an OO fashion is a welcome change.

    Two more questions:

    1) Speed. Since a framework has alot more overhead than something more fundamental (like PHP, which I love for it's speed), how much of a speed price am I going to have to pay?

    2) Web Services. How difficult are these to implement w/ Rails?

  10. #10
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ThetaWaveRider
    1) Speed. Since a framework has alot more overhead than something more fundamental (like PHP, which I love for it's speed), how much of a speed price am I going to have to pay?
    Rails is pretty slow compared to PHP unless you're using FastCGI. It's even faster if you use lighttpd over Apache as your web server. Speed shouldn't be too much of an issue if you have a good setup, and developer time is usually much more expensive than adding a server or two when the load increases later.
    Quote Originally Posted by ThetaWaveRider
    2) Web Services. How difficult are these to implement w/ Rails?
    Check out ActionWebService in the Rails API. It's pretty easy.

  11. #11
    SitePoint Addict Brak's Avatar
    Join Date
    Jul 2004
    Location
    Central Coast, CA
    Posts
    321
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Luke Redpath
    SQL really has no place in the controller. It should be in a model, somewhere.
    Where the F did you read SQL in a controller?
    Studio Rockstar's Blog - A journey to quitting the dayjob.

  12. #12
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Brak
    Where the F did you read SQL in a controller?
    Quoted the wrong person.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •