Testing a rails before_filter method

I finally figured out a kind of cool way in Ruby using Rspec to test a controller’s before_filter method a few days ago.

While trying to figure this out, I came across all kinds of posts saying why you shouldn’t test the before_filter because it means looking too closely at the implementation by doing that.  (I disagree; I think this, like any other method, can and should be unit tested.)

I came across other posts that told how to skip the before_filter altogether and test everything else. (Valuable in some cases I’m sure, but not what I was trying to do.)

And I came across many other posts that had examples that did not work for me (perhaps different versions of rspec/ruby/rails or any number of things).

I came across some posts said to test the functionality of the before_filter method in one of the subclasses of ApplicationController.  I could do that, but then I have to pick a subclass at random.  And since the method I’m trying to test is defined ON ApplicationController, I feel like it is more intuitive to test that IN application_controller_spec.rb, not some other, random test.  And if the randomly chosen controller happened to ever get removed from the app in the future, the spec for that would also get removed, and thus inadvertently removing any coverage of the before_filter method in app controller.

I found out that Rspec has a cool way of defining anonymous controllers.  And this worked out well for testing ApplicationController.  So I wanted to share how I used anonymous controllers, as well as some things I tried along the way that did NOT work (almost, but not quite).

(These examples use Rails 3.1.1 and Rspec 2.12.0)

The application code:

application_controller.rb

# The before_filter method "check_permission" is defined here.  It is
# not specified as a before_filter in ApplicationController because
# some controllers in the app will want to use it, and some will not.
1  class ApplicationController
2    def check_permission
3      @current_user = session[:user]
4
5      #Check the permissions of the logged in user in the session
6      if @current_user does not have permission #pseudocode
7        render( :file => File.join(Rails.root, 'public/403.html'),
8                :status => 403,
9                :layout => false )
10     #else don’t do anything. The app will continue as usual
11   end
12 end

members_controller.rb

# There are rules around who can view members.
# Different logged in users can view different members.
1 class MembersController < ApplicationController
2   before_filter :check_permission
3
4   def show
5     member_id = params[:id]
6     # get the details for the given member id
7     # etc....
8   end
9 end

The test code:

I came across some suggestions that said to just write a test that calls the method directly.  But in this case, the before_filter method uses the HTTP session.  If you call a method outside of the context of an  HTTP get/post, you don’t have access to HTTP things like session, request or response.  So calling the method directly did not work:

application_controller_spec.rb

 1  require 'spec_helper'
 2
 3  describe ApplicationController do
 4    before do
 5      # Put a user in the session
 6      # do any other rspec mocking/stubbing necessary
 7    end
 8
 9    it 'should allow viewing of users if the user has permission' do
 10     ApplicationController.new.check_permission
 11     # then test stuff here ...
 12   end
 13 end

Running this gave the following error on the line 11.

RuntimeError:
 ActionController::Metal#session delegated to @_request.session, but @_request is nil: 
 #<ApplicationController:0x007fbac2aed3f8 @_routes=nil, @_action_has_layout=true, 
 @_view_context_class=nil, @_headers={"Content-Type"=>"text/html"}, @_status=200, 
 @_request=nil, @_response=nil>

I tried defining my own anonymous controller like this:

application_controller_spec.rb

 1  require 'spec_helper'
 2
 3  class AnonymousController < ApplicationController
 4    before_filter :check_permission
 5    def show
 6      render :text => 'Hello'
 7    end
 8  end
 9
 10 describe AnonymousController do
 11   before do
 12     # Put a user in the session
 13     # Any other rspec mocking/stubbing that needs done
 14   end
 15
 16   it 'should allow viewing of users if the user has permission' do
 17     get :show , :id => 123456
 18     response.code.should eq "200"
 19     response.body.should include "Hello"
 20   end
 21 end

And got this error on line 17:

ActionController::RoutingError:
 No route matches {:id=>"111111", :controller=>"anonymous", :action=>"show"}

But there’s a more Rspec-y way to do anonymous controllers.  And it magically sets up the routes for you to avoid the error above.

application_controller_spec.rb

 1  require 'spec_helper'
 2
 3  describe ApplicationController do
 4
 5    before do
 6      # put a user in the session
 7      # do any other rspec mocking/stubbing necessary
 8    end
 9
 10   # This is the anonymous controller:
 11   controller do
 12     before_filter :check_permission
 13
 14     def show
 15       render :text => "Hello"
 16     end
 17   end
 18
 19   it 'should allow viewing of users if the user has permission' do
 20     get :show , :id => 123456
 21     response.code.should eq "200"
 22     response.body.should include "Hello"
 23   end
 24
 25   it 'should not allow viewing of users if the user does not have permission' do
 26     get :show , :id => 111111
 27     response.code.should eq "403"
 28   end
 29 end
Advertisements
Posted in code. Tags: , . 2 Comments »

RubyConf 2010, keynotes, and maihem. Oh my!

It’s now a little over a month after RubyConf 2010, but I still want to finish and publish a blog post that I started right after getting back from RubyConf about some of the keynote talks and some of my experiences and thoughts.


Whenever I go to a tech conference, I’m often in the minority as one of few women at the conference. And inevitably a few people are interested in hearing my thoughts and opinions about being one of few women at the conference, and in IT in general. It’s not one of my favorite topics, but I appreciate people’s interest in talking to me, so I indulge.

After a collection of conversations at RubyConf, and some mini-controversy on Twitter following RubyConf (seriously, can’t Rubyists have a conference without some level of controversial aftershock?) regarding David Heinemeier Hansson’s keynote, I really feel like I’m no longer qualified to give opinions on women in technology, and maybe I never was. I start to feel like a freak among women. I *liked* David’s talk. I even went up to him later that day and told him so. But now I feel like I’m “supposed” to be offended in some way. I just don’t seem to have the same opinions about things as most women. Not only am I a minority at the conferences I attend, I am a minority among the minority. I never did really fit in much of anywhere.

Diversity is a great thing. But I never understand why people make such a big deal about women in technology when I rarely (if ever) hear people talk about small numbers of African American’s in technology, about Jewish people in tech, you name it. (Though to his credit, Dave Thomas did briefly include that topic in his talk. [DT 39:40] More on his talk later.) I just don’t think much about whether I’m one of few women around or not. I guess I just try to see something positive wherever I’m at, and whatever I’m doing; so if being one of few women around is “supposed” to be bad, I just don’t see it that way. If that’s how I’m supposed to feel, then I start to wonder if I’m just not good at being a woman. I don’t feel that way all the time. But sometimes being singled out for being different makes me feel worse.

Dave Thomas started off RubyConf with an inspiring keynote recapping his last 10 years in Ruby, and offering 3 challenges to the audience (inspire someone, diversify, get out of the rut.) Challenge 1 [DT 33:03] started off with a comparison of the percentage of women in the workforce (47%) vs. women in computing/math (25%), vs. women at RubyConf (5.6%), vs. women in open source (1.5%). Then he talked mostly about how the community can inspire women and avoid discriminatory behavior, some of which “you don’t always know you’re doing….” [DT 39:08] On one hand I admire a speaker addressing a topic he feels is an issue. And while I have mixed feelings on the subject, I don’t disagree that there’s an issue. On the other hand, as soon as he brought up the subject of women, I knew that was about all people would want to talk about with me the rest of the day (which I kind of dreaded). And that’s pretty much how my next couple of days went.

“How did you push through?” they asked.

I never really felt like I had to push through much of anything to enter IT. If there was any barrier to entry in any subset of IT, it might have been into the Ruby community. For one, it feels awkward going to a Ruby meeting with a Windows machine. And I don’t think we should make people feel that way. You shouldn’t have to buy a Mac before you attend your first Ruby meeting, nor worry about people telling you to “get a real machine.” I think it’s a bit much to expect people to spend the money on a Mac (about twice as much money as equivalent non-Mac hardware whenever I’ve priced it out) just to do Ruby. Not that I have any problem with Macs either. But I do like my Window 7. By choice. And I’m really scared to say that. On my blog. I shouldn’t be, but I am. At RubyConf, during one of the conversations I had where someone asked me about Women in IT, I mentioned this fear. And in an indirect way, the person basically called me an idiot for that choice. Backpedaling… “ummm,” he says… “it’s just that most of the people I’ve met that ‘choose’ windows are idiots.” Thanks dude. That felt great </sarcasm>.  But I’m trying not to be ashamed of who I am anymore.

David Heinemeier Hansson gave his keynote on the 2nd morning of RubyConf. His talk, titled “Why Ruby?” was about what he loves so much about Ruby, and why he hasn’t felt a need to look at another programming language in many years. He talked about the freedom that Ruby gives you over statically-typed languages such as Java or C#. Within that freedom, some acts are harmless, and others are much worse, but freedom should be used with appropriate moderation. [DHH 49:00] And he dropped 3 F-bombs. And he talked about having one’s balls fondled by the TSA. [DHH 40:30-40:47]

Some people said these things were “distracting” or did not add value to his talk. While I wouldn’t get up in front of a group and talk that way, (I also wouldn’t get up in front of a group and talk) if he wants to get up and talk that way, that’s his choice. At this point in his career, he doesn’t really have to worry whether or not people think he’s unprofessional when he gets up and talks. But was anything in his talk something that you wouldn’t see or hear in an R-rated movie? No. Was it distracting? I don’t think so. 3 F-bombs. And the majority of the ball fondling discussion lasted only 17 seconds, not counting 1 fleeting side remark referencing back to it later on [DHH 41:41], for a total of about 19 seconds. Is 19 seconds, out of an hour-long talk, really a distraction to someone? If anything I thought it was funny. And funny holds my attention a lot longer than most standard talks. When he expressed this disgust at recent TSA practices and as he put it, having one’s balls fondled, I wanted to turn to the person beside me and say “Yeah, I hate when the TSA fondles my balls!” If I’d been less tired, I probably would have said that. But people were fired up against his talk anyway. I guess you can’t really convince people who are fired up over something to settle down. And I’m simply not qualified to say if people are over-reacting or not. I’m not qualified to say whether women (in general) are uncomfortable about this stuff, or offended. I just don’t know. It’s up to individuals to form their own opinions.

I do know it’s up the mature ones who can handle the freedom to lead by example, whatever that means to them. To me, it means not dwelling on little things people say, but rather try to step back see the big picture of what message someone is trying to get across and take it for what it is. I know, that’s really tough sometimes. People get offended at things. It’s hard not to take something too personally sometimes. And I think anyone trying to effectively get their message across needs to be considerate of that. We’re all free to say what we want, and speak to people however we want. But just because you CAN do something, doesn’t mean you SHOULD. (Just like we’re all free to write complex logic in one convoluted line of Ruby code, just to be proud to say you did something in only one line. Clever, but it doesn’t mean you should do that. Can someone else easily understand what it does? I think readability is more important than cleverness. But some people will do that anyway. I can lead by example there too. At the end of the day, I have to figure out what they were trying to do and move on. I digress. 🙂

Dave Thomas, when talking about ways the community may exclude women, he said “The exclusion happens among people who often do not mean to appear, and do not interpret their own actions, as hostile to women.” [DT 36:50] That is probably true. Though I think there’s a lot of actions are hostile in many ways, not to women specifically. For example, unintentional hostility against people with less confidence.

“Why’d you do it THAT way?”

Perhaps they just asked the question because they want to see where the person’s coming from. Maybe they like to question decisions in order to spawn discussion. Maybe they like to challenge and argue and see where it goes. Depending on tone of voice, “Why’d you do it THAT way?” can easily imply “That was a dumb way to do it.” instead. And the person with low confidence takes it further to mean “YOU are dumb.” Are you aware of this? But sometimes people with low confidence have great ideas, too! Are we missing out on those? Are we including them? Or are we pushing THEM away?

Dave Thomas also said “if you do find something offensive… do something about it. If you’re in a talk and it’s offensive, stand up and get out. Or blog about it… tweet the guy into oblivion…. by reacting, you’re saying ‘yeah it happened, but you know what? We’re not going to let it happen again.'” [DT 40:12] Not bad advice! Yet just because you can do these things doesn’t necessarily mean you should. For one thing, you can’t please everyone all the time. Chances are someone will not like something that any speaker has to say. Be careful to use this kind of power of persuasion when it’s really important, not on every little thing. Else it becomes meaningless. It becomes “oh yeah, there go those rubyists again, always someone pissed off at the end of the conference.” May we all have the wisdom to discern the important from the trivial.

But as for the male/female ratio thing, I think it goes deeper than being less “inflammatory” [DT 37:40] in order to not deter women. How do you think it makes a person feel to hear someone who’s trying to have kids say “I hope we don’t have a girl,” or to hear someone say “can you imagine him trying to raise a *girl*? Oh my!” Are girls really that much more trouble? Maybe we are. Maybe it makes us feel like we’re not worth the trouble. It makes me feel like I’m not worth the trouble. I think people (not women, but anyone) with lower self-esteem have a harder time convincing themselves that they can do something that’s “supposed” to be hard, like Math, Science, Computers. What have the experiences in your life taught you about what you can and can’t do? What opinions have the years of your life helped you form? For me, I’ve “learned” (note I put “learned” in quotes):

* that having daughters is a bad thing,

* that most of what women do is annoying and excessive (shopping, talking too much, taking too long to get ready, over-packing for a trip),

* that all wives are good for is nagging and telling their husbands what they can and cannot do. I’m exaggerating mostly (however, I did see someone tweet recently “I don’t have an opinion, I have a wife.” That’s a horrible way to have to feel in life. And if women are to blame for that, then yes, I do feel bad.)

These are thoughts I have to consciously put out of my head when I’m feeling down. It’s really hard sometimes.

So what now? If I’m offended by one little keynote speech, I can leave that talk. I could stop attending community events. I can still code in Ruby and Rails and not be a part of the community at all. But I lose out. I lose out on other people’s passion, their knowledge, their ideas. But I’m not offended by much. I liked Dave Thomas’s talk, AND I liked David Heinemeier Hansson’s talk. I particularly liked his analogy with rope. “People say things like ‘…just enough rope to hang himself’ but does that mean we should outlaw rope? You shouldn’t be allowed to have rope, because what if you hung yourself? Is that the main use case for rope? … you can do a lot of things with rope.” [DHH 37:30]

I don’t have to condone all the individual choices people make [DHH 56:20]; We don’t all have the same sense of humor but we can still get along [DHH 34:00]. Can’t we all just get along? [Rodney King :)]

I don’t know the first thing about why there’s not more women in IT. I don’t have any clue what to do about it. Sometimes I wonder why it’s really a “problem.” If women aren’t interested in it, why force it? There’s lots of opinions on that. But I think the best thing to do is make anyone interested in IT feel welcome; instead of singling out people who represent some statistical group, just treat them like they belong in the group. I really liked Dave Thomas’ challenge to inspire someone, and his suggestion that we need to be talking more to kids and inspiring them about software development. [DT 41:05] (Kids. Not girls, not boys. Kids.) “Fewer and fewer people are entering software as a profession…. Mentoring is a really important part of what we do. [41:16] And that I agree with. I’ve had plenty of mentors over my career, and giving back by mentoring others makes a lot of sense. “Go inspire someone” is something that I can do. Can you?

References:

Video of David Heinemeier Hansson’s RubyConf Keynote (DHH* http://confreaks.net/videos/431-rubyconf2010-keynote-why-ruby)

Video of Dave Thomas’ RubyConf Keynote (DT* http://confreaks.net/videos/368-rubyconf2010-keynote)

Never be ashamed of where you came from!

I am frequently asked my opinion on how to encourage more people people to get into software development. My response frequently is:

“if people aren’t interested in software development, why force them to be?”

I enjoy hearing people’s various responses to my often unexpected follow-up question. All the while, I have my own answer to the question: I don’t think we want to force it, but we do want to make absolutely sure we’re not discouraging people either.

Lately, I have heard way too many people actually apologize for languages, platforms or technologies they once used. I’ve heard it in one-on-one conversations, in groups, even from a speaker addressing a local user group. Soon it struck me: YOU SHOULD NEVER HAVE TO BE ASHAMED OF WHERE YOU CAME FROM. It’s what makes you uniquely you, it gives you the unique perspective that only you have to bring to the table. I even hear people apologize for the languages, platforms or technologies they currently use. And I also feel YOU SHOULD NEVER HAVE TO BE ASHAMED OF WHO YOU ARE.

Please, let’s make sure we’re not making people feel ashamed of who they were, and of who they are.

I think a lot of people in IT tend to be very opinionated, er, I mean very passionate about their craft. Passion is a great thing when used for good and not for evil. But on more than one occasion I’ve heard people say that they stopped attending a local user group because whenever they went, they were made to feel bad about being “the [insert technology here] guy” (the .Net guy, the Java guy, the Python guy, the PHP guy, whatever) and sometimes to the point of feeling they have to defend who and what they are. That became very tiring after awhile, and they quit attending the group. Do we want to share the Ruby love or not? We all lose if we don’t. This person is no longer learns about Ruby and Rails at their local user group every month. And that Ruby community no longer has the unique point of view that only that person could have contributed if only people would listen.

Please, let’s welcome people who are different from us, and not try to change them but embrace them.

Dave Thomas spoke in his keynote at RubyConf 2010 that we, as a community, may be unaware of what we may do or say to discourage people into joining us. I hate to say it, but I think the Ruby community not seen as the most welcoming bunch. We have good intentions of welcoming. On the surface, we are. When it comes down to it, I get a feeling that we unknowingly making people who do not use Ruby, own Macs, and have iPhones feel very out of place. That is not a good feeling. It’s not a warm feeling. It’s not a welcoming feeling. I’m not saying this kind of thing isn’t prevalent elsewhere, but the Ruby community is what I know best right now, and it’s where I see it right now.

What can we do? I encourage and challenge everyone, the Ruby community especially, to be more welcoming.

  • Next time you are talking to a fellow technologist, and you hear someone says they are a .Net or Java or whatever developer, resist the temptation to say (or even think) “ohhh, I’m sorry!” You may be joking. And it may be appropriate to joke in that way with someone you know very well who knows without a doubt you are joking. But what you may not realize is who else is listening. There may be a person over in the corner who doesn’t know anyone at the user group yet, but is within ear shot of hearing you. That’s the impression they will get of how this Ruby community welcomes n00bs and people of other technological backgrounds.
  • Lead by example. Don’t criticize or look down on someone who uses a completely different language than you, or uses a different operating system, or chooses a different editor. Don’t strong-arm someone into seeing things your way. Never TELL someone that they should love something. Show them what YOU love about it, the rest will follow. Or it may not. But that’s all you can do.
  • Next time you’re at a user group meeting, make a point of talking to someone you don’t know. Maybe it’s someone who’s been coming for awhile but you have been too busy to notice. But maybe it’s someone who’s new and will appreciate that someone went out of their way to be nice. Find out what they do, but never judge. Find out how they got interested in Ruby and encourage that. Tell them what made you get into Ruby, but make sure they never feel they have to have the same reasons as you.

Go forth and ENcourage!

RubyConf 2010 Highlights

This year, I attended my first RubyConf on Nov. 11-13. It was a blast! I’ve attended lots of local and regional conferences in the last few years, but it’s been awhile since I’ve been to a larger conference.

I went to a lot of great sessions, & learned lots of things from lots of people. I won’t be able to cover everything I learned, as even now it’s all still soaking into my brain-sponge. Nor could I possibly name all the great people I met & talked to, & all the great places we went. So here’s a few highlights:

Top 5 favorite things of a non-technical nature

:hanging out with Marc Peabody => Marc is my peer, my cousin (b/c he married my cousin), & my friend.  I’m happy we got to know each other better as a result of heading out to Bourbon St. w/ the group, & staying out way too late & having good conversations that usually don’t happen among people at a conference until later in the week.

:beignets => a wonderful, delicious, donut-like pastry piled with tons of powdered sugar that we experienced at a place called Café Du Monde in New Orleans.  Funny thing is I spotted powdered sugar as I stepped onto the escaltor at the hotel on my way to my room that night! I found powdered sugar still clinging to my jeans on my way home, too! It’s everywhere!

:making new friends => including getting to meet Sarah Mei in person, who I’ve followed on Twitter for awhile now, & going out dancing the last night.

:getting up EARLY to run in the rubyconf 5k => mornings are hard enough for me, even more so after getting only 3 hours of sleep the night before.  But I did it!  And if that’s not positive enough, I finished the race in less than 50 minutes, including time to stop or slow down to take pictures! Bonus! I’m not a runner, so that’s a pretty good time for me.

:meeting david heinemeier hansson => and having a normal-person conversation about Rails, dynamic typing in Ruby, & Rails in mobile browsers.  Well, it was a normal-person conversation until I asked him to sign my conference t-shirt, haha.  He kindly obliged without making me feel ridiculous for it. And because of that, I decided to go get Matz to sign my shirt too!  How cool is that?

 

Top 5 things of a more technical nature

:Knocking Ruby’s Date & DateTime Performance Out of the Park with home_run – Jeremy Evans
=> This talk was about why the Date & DateTime classes in Ruby are slow, & about the ruby library “home_run” & how it works to speed that up. I’m really hoping to get time this week to try this out at work, run some benchmarks, and hopefully it will help some slower parts of our app.

:Writing games with Ruby – Mike Moore
=> This talk introduced us to the Gosu library for game programming in Ruby. Now I just have to figure out what kind of shenanigans I can get into with this new knowledge! 🙂

:RedCar – It’s time for a Ruby editor – Daniel Lucraft
=> This is an editor that installs as a gem. My understanding is that you can run Ruby commands, like you would in IRB, to extend the editor, make plugins or even edit your code if you want to. I like my RubyMine, but am curious how something like RedCar handles a very large rails project.

:Concurrency: Rubies, plural – Eleanor McHugh, Elise Huard
=> In talk the speakers discussed things like threads & fibers in Ruby that are used for concurrency. Unfortunately I was sitting way in the back & could not see the slides or code examples. However, I was able to follow along enough to get some ideas about some specific things I’d like to look into later.

:a cool introduction
=> At one point in the week, I was introduced by a former co-worker Jeff Lembeck to someone he knows as follows: “This is Gayle, she taught me Rails.” It was a really a good feeling to be regarded in that way.  Having trouble expressing in words how that makes me feel. Good. Worthwhile. Valued. Something like that.

 

Keynotes
:I’m not going to talk about this here => there’s been quite a bit of talk|debate|arguing on Twitter after RubyConf this year about the diversity of the various keynote talks. I’ve got a chaos of thoughts bouncing around in my head on that. If I can get my thoughts organized, maybe there will be another blog post coming soon.

 

One thing I’d like to take away from all the inspiration I’ve gotten…
:I really need to write in my blog more regularly!
=> I have a growing list of ideas & things I’ve learned that I’d like to share. I really don’t like to get up & speak in front of people. This – blog – is a much better way for me to do just that.

OSU Ruby on Rails

8 months after stepping so totally out of my comfort zone, speaking at Columbus Ruby Brigade, not remembering most of it because it was such a nervous blur, and swearing off public speaking again for a long time (if ever) – I surprised myself recently.

When the OSU Ruby on Rails group sent out an open invite to the Columbus Ruby Brigade (CRB) mailing list, inviting anyone who might like to speak, a little voice in me said maybe I should give it another try. A bit surprised the thought even crossed my mind, I talked myself into it.

For whatever variety of reasons, I don’t think I ever 100% convinced myself that I was qualified to speak at CRB. With some hard work I could probably get over the fear and insecurity. And yet, I’ve said before, public speaking has never been a passion of mine so I see little motivation to actually overcome it.

For whatever variety of other reasons, I didn’t have as much trouble convincing myself I am qualified to speak to a small group of college students. Small group being the biggest factor. I suppose knowing that I have more experience than people in my audience was something else to ease my mind too, not necessarily so at CRB.  And for this time anyway, they were OK with me re-using my previous talk on sorting in Ruby, so it helped that it was a talk I’d given before.

Three things to put me at ease. And put  me at ease it did. I didn’t incessantly rehearse/memorize what I was going to say this time. As a result I have a feeling my speech was more fluid and conversational. The guys were great and asked good questions, too.

So, I did it. I’m still not sure if/when I’ll do it again, but I’m not running for the hills either.  So we’ll see where it takes me.

Model – View – Controller (MVC). Where do you put your business logic?

When I first started doing Rails development over a year ago, I often found other developers suggesting that I refactor my code to move the business logic out of the controller, and into the model.  After this happened several times, I wondered why this seemed so obvious to them, and why that wasn’t the way I was naturally doing things.  It wasn’t just me and another developer approaching problems in different ways.  It was multiple people approaching problems in a similar way — and then me.  I wondered why there was this general difference in thought processes and solutions?

I began to passively wonder if my background in Java was contributing in some way to my way of thinking here.  Since then, I have also heard other Rails developers who are working with former Java developers say things like

"these are good developers but they keep putting business logic in their controllers!"

That made me think more seriously about what a person’s Java background might have to do with this.

VIEW 

In earlier days of PHP, classic ASP, and early JSP, it was OK to put business logic in the view.  Back in the day, it was common to find <% %> tags in JSPs with all kinds of logic in the Java code.  I think we all now agree that business logic does not belong here. 

let the view deal only with DISPLAYING what it has.

MODEL 

It seems like I’ve spent a lot of time seeing a model as simply a data-holder class.  Something like a Struct in C.  Something like a Bean in Java, with private attributes, and with get/set methods for each attribute.  It’s just a data transfer object, nothing more.  But why?  Perhaps it comes from my EJB days.  That might explain why so many other former Java developers do the same thing.  To us,

the model carries data but is dumb. 

CONTROLLER 

I always thought of the controller as being the one smacking around the model objects to get them to do what the controller wants – and

what the controller wants the model to do, is the business logic. 

A RAILS WAY OF THINKING 

Give the model more credit.

The model isn’t so dumb after all.  It doesn’t just hold state.  It contains the business logic behind the behavior of that model.  A model knows better than any other code how itself behaves, what it does or does not do, and how it interacts with the outside world.  I can try to predict how my brother will behave in a given situation; I can take a guess based on what I know about him.  But only he knows for sure, and the only way I can know for sure is to interact directly with him and find out.

Let the controller control the flow of information.

This leaves the controller with only the responsibility of collecting information and routing it to the right place.  It’s the electronic curator that finds and obtains art, and hands it off to the decorators to display.  He decides what pieces go in which rooms.  However, the decorators (analogous to the view) are the ones that decide how to arrange the art within that room for all to see.

I still need to be reminded sometimes to not put so much logic in my controller.  It is starting to come more naturally to me, but old habits die hard I suppose.  This way of thinking makes more sense to me now that I understand where my past way of thinking comes from.  I like this new way because it really makes sense now, and also because

I find that code re-use comes much easier when needed business logic is already available in the model.

.

Posted in code. Tags: , . 7 Comments »

Ruby Sorting [2] – Common Mistakes When Sorting With Blocks


This sorting technique is one I’ve had a chance to use at work more lately. But what keeps tripping me up is when you use the block to sorting primarily by one field, with a secondary sort on another field. Let’s say Fish has species and type, and we have these fish in our database:

species type
Platy Sunset
Platy Calico
Molly Dalmation
Platy Rainbow
Guppy Fancy Tail
Platy Mickey Mouse

When I sort first by species, then by type, I keep accidentally doing the following, which gives the wrong sorted results:

>> fishes = Fish.find(:all) 

>> fishes.sort do |a,b|
?>   a.species <=> b.species
>>   a.type <=> b.type
>> end

Doing that, I end up with a list like:

species type
Platy Calico
Molly Dalmation
Guppy Fancy Tail
Platy Mickey Mouse
Platy Rainbow
Platy Sunset

It ignores my first sort on species, and ends up sorting only by type!

Why? What’s wrong with that?  Well, the <=> comparator function (also known informally as the “spaceship operator”) returns either -1, 0 or 1, depending on whether the first value is less than, equal to, or greater than the other.  The block will return the last statement evaluated.  So what happens is we compare species, then we then compare type, and it is always the result of the type comparison, -1, 0 or 1, is returned from the block.  The problem is, if species is not equal, then we want to stop there and return -1 or 1 accordingly and not evaluate type at all.

A simple way to do this is to add “if result==0” to the end of the type comparison, and only evaluate type if species was equal.

>> fishes = Fish.find(:all) 

>> fishes.sort do |a,b|
?>   result = a.species <=> b.species
>>   result = a.type <=> b.type if result == 0 
>>   result
>> end

This way, it will perform the first search by species, then only continue to perform the secondary search if the result of the first search was zero, that is they were equal values. And so I end up with a list like:

species type
Guppy Fancy Tail
Molly Dalmation
Platy Calico
Platy Mickey Mouse
Platy Rainbow
Platy Sunset
Posted in code. Tags: , . 2 Comments »