Inflector/environment.rb Bug in Rails

Today I needed to get Rails to grok an uncountable noun “media”. Easy you think, right? Just add an inflection rule.

So I went to config/environment.rb, uncommented the Inflection example and modified it with my “media” noun, just like this:

Rails::Initializer.run do |config|
...
...
...

Inflector.inflections do |inflect|
inflect.uncountable %w( media )
end

# See Rails::Configuration for more options
end

# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
# Mime::Type.register "application/x-mobile", :mobile

When I tried to run rails console or tests I saw an error like this:


me@Xbook:testapp$ ./script/console
Loading development environment.
./script/../config/../config/environment.rb:55:NameError: uninitialized constant Inflector
/opt/local/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/action_controller/assertions/selector_assertions.rb:525:NoMethodError: undefined method `camelize' for "top":String
/Users/me3x/Development/DSE/mediacast2/apps/testapp/app/controllers/application.rb:4:NameError: uninitialized constant ActionController::Base

The problem turned out to be in the commented-out rails example in environment.rb. The Inflector.inflections block must not be nested within the Initializer.run block.

This fixes the problem:


Rails::Initializer.run do |config|
...
...
...

# See Rails::Configuration for more options
end

Inflector.inflections do |inflect|
inflect.uncountable %w( media )
end

# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
# Mime::Type.register "application/x-mobile", :mobile

#77 Destroy Without JavaScript

If the user has JavaScript disabled, the “Destroy” link might not work properly. In this episode I will explore a number of ways to work around this issue.

#77 Destroy Without JavaScript

If the user has JavaScript disabled, the “Destroy” link might not work properly. In this episode I will explore a number of ways to work around this issue.

Rob McKinnon: TheyWorkForYou.co.nz – Ruby on Rails Podcast

A London-based developer talks about keeping government accountable in New Zealand and around the world.
Also mentioned:

Rob McKinnon: TheyWorkForYou.co.nz – Ruby on Rails Podcast

A London-based developer talks about keeping government accountable in New Zealand and around the world.
Also mentioned:

World Series Tickets Time line

As many, I tried to buy a couple of tickets to the World Series. Hey, it’s coming to Denver. Go Rockies! Well, yesterday the online ticket sales was quite a debacle. ‘They’ blame it on a vicious attack…hmmm…well…EC2 could have helped, I guess they just didn’t think that nearly everyone in the US would go on their website at 10am MDT, yesterday. Well, today things went well for them, and all the tickets sold in less than 3 hours. So what does this have to do with Rails? We are working on MySpyder.net, which allows to track changes on websites. So I create a watch on the “”http://colorado.rockies.mlb.com/col/ticketing/postseason_2007.jsp">The Official Site of The Colorado Rockies". I was hoping getting an advantage, find out before others that sell are back..Didn’t work out that way. I didn’t get tickets, but find here after how Myspyder.net saw the World Series Tickets online sales.MySpyder.net is not public yet as we need to ensure that we can handle the load πŸ™‚

All times are in UTC. Ok, on to the events..as seen by Myspyder.net

  • 2007-10-23 03:30:17 Tickets go on sales tomorrow at 12pm
  • 2007-10-23 16:30:31 Oops, official press release announced regarding sales of Tickets.
  • 2007-10-23 18:00:17 Sales is back on!
  • 2007-10-23 20:30:22 27th Sold out, 26 Sold out.
  • 2007-10-23 21:01:07 Sold out πŸ™
  • 2007-10-23 21:30:46 Good news! Parking passes still available.

world_series_tickets.gif

#76 scope_out

The scope_out plugin will generate several helpful find methods for you automatically! It’s the best way to move the find conditions into your model. Watch this episode for details.

#76 scope_out

The scope_out plugin will generate several helpful find methods for you automatically! It’s the best way to move the find conditions into your model. Watch this episode for details.

WebSnapshot 0.3 – a simple (and powerful) Adobe AIR application.

I finally got around an recompiled WebSnapshot for AIR beta 2. While doing this I’ve added some cool new features.

websnapshot_3.jpg

Download Info

Check the Auto Save box and start drag and dropping URL’s from your browser into the address bar of WebSnapshot. Quickly you’ll get some pretty cool snapshots of WebSites in png format. You can now have Full Page, Browser Size snapshots in addition to the Thumbnail snapshot like before. You can define the scale of how much the size needs to be reduced before saving and other parameters that define how the snapshot is taken.

I moved the project to the MySpyder.net website and you can download it and find more information here.

RailsConf Europe 2007 – Ruby on Rails Podcast

Interviews with David Chelimsky, Dave Troy, Nick Sieger, and David Heinemeier Hansson.

RailsConf Europe 2007 – Ruby on Rails Podcast

Interviews with David Chelimsky, Dave Troy, Nick Sieger, and David Heinemeier Hansson.

#75 Complex Forms Part 3

In this third and final episode on complex forms I will show you how to edit a project and multiple tasks all in one form. This includes removing and adding tasks dynamically as well. See the show notes for updated code.

#75 Complex Forms Part 3

In this third and final episode on complex forms I will show you how to edit a project and multiple tasks all in one form. This includes removing and adding tasks dynamically as well. See the show notes for updated code.

Michael Koziarski – Ruby on Rails Podcast

Rails core member Michael Koziarski talks about Rails 2.0, the new patch process, and working on the core team.
From RailsConf in Berlin.

Michael Koziarski – Ruby on Rails Podcast

Rails core member Michael Koziarski talks about Rails 2.0, the new patch process, and working on the core team.
From RailsConf in Berlin.

#74 Complex Forms Part 2

See how to use Javascript and RJS to add and remove form fields dynamically. This episode will build upon the previous episode allowing you to create any number of tasks in one form the same time a project is created.

#74 Complex Forms Part 2

See how to use Javascript and RJS to add and remove form fields dynamically. This episode will build upon the previous episode allowing you to create any number of tasks in one form the same time a project is created.

Many Skinny Methods

        This refactoring is based on a topic <a href="http://www.marcelmolina.com/">Marcel</a> and I covered at RailsConf Europe.


<strong>Before</strong>
1
2
3
4
5
6
7
8
9
10
11
12
class Expense < ActiveRecord::Base
  belongs_to :payee
  protected

    # Nice and concise, but what happens as we add more rules
    # and how do we write test cases for the four different possible 
    # validation states?
    def validate
      errors.add("Not enough funds") if payee.balance - amount > 0
      errors.add("Charge is too great") if payee.account.maximum_allowable_charge > amount
    end
end
<strong>After</strong>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Expense < ActiveRecord::Base
  belongs_to  :payee

  # Instead of one large validation method,  Continue reading "Many Skinny Methods"

Many Skinny Methods

This refactoring is based on a topic Marcel and I covered at RailsConf Europe.

Before

1
2
3
4
5
6
7
8
9
10
11
12
class Expense < ActiveRecord::Base
  belongs_to :payee
  protected

    # Nice and concise, but what happens as we add more rules
    # and how do we write test cases for the four different possible 
    # validation states?
    def validate
      errors.add("Not enough funds") if payee.balance - amount > 0
      errors.add("Charge is too great") if payee.account.maximum_allowable_charge > amount
    end
end

After

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Expense < ActiveRecord::Base
  belongs_to  :payee

  # Instead of one large validation method, break each individual
  # rule into methods, and declare them here.
  validate      :ensure_balance_is_sufficient_to_cover_amount
  validate      :amount_does_not_exceed_maximum_allowable_charge

  protected

    # These validation callbacks simply add error messages if a particular 
    # condition is met.   Each of them can be tested and understood on their own
    # without having to understand the entire body of the validate method.
    def ensure_balance_is_sufficient_to_cover_amount
      errors.add("Not enough funds") if insufficient_funds?
    end

    def amount_does_not_exceed_maximum_allowable_charge
      errors.add("Charge is too great") if exceeds_maximum_allowable_charge?
    end

    # By defining separate predicate methods we can test each of them individually
    # and new programmers can see the intent of the code, not just the implementation.


    # Instead of subtracting the amount from the balance and checking if the
    # value is greater than 0,  change the implementation to mirror the intent.
    # There was a bug in the before code,  this is more obvious.
    def insufficient_funds?
      amount > payee.balance
    end

    def exceeds_maximum_allowable_charge?
      payee.account.maximum_allowable_charge > amount
    end
end

While the refactored version may have more lines of code, but don’t let that scare you. It’s far more important for code to be human readable than incredibly concise.

Rails Rocks!

I am trying so see the result of a change in a Java program….5 minutes compilation…5 minutes deployment. Arggggggggggg!

Rails Rocks! Change + refresh = done!

Short term memory is good, hopefully I will forget that experience soon.

πŸ˜‰