Feed on
Posts
Comments
Problem: You wish to populate a “collection_select” box with an Array.

I recently had an issue where I wanted to create a select box populated from an array of continuous years. I wanted to use thecollection_select method so it would have a minimal “code footprint” and play nice with the form that I had built. The application that I’m working on is in Rails2 and I didn’t want to divert heavily or make to many changes, and after some fiddling I came up with a nice solution: In the environment.rb file I created an Array constant to contain the year range that I wanted to use:

#Constant Values
YEARS_ARRAY = Array.new(89) {|i| 1920+i}

This code is a shortcut to create an array of values between 1920 and 2008 (88 years). It creates a new array with a size of 88, and then uses the i value to populate each year incrementally. I was pleased to find a method like this!

The form declaration contained the object that I wanted to populate with the form values using the Rails2 syntax:

<table class=”new”>
<% form_for(@movie_poster) do |f| %>
 <tr>
<th>Title:</th>
  <td><%= f.text_field :title %></td>
 </tr>
…..
<% end %>
</table>

*note the newer Rails2 syntax for the form…

I will populate my select box with an Array of Objects. I need to create my Object first to populate my Labels and Values, and I used an “old trick” from Struts by creating a Ruby LabelValue object that mimics Struts LabelValueBean utility POJO. The object has a label and value accessor, which works extremely well for any set of arrays since any kind of mapping scheme would consume duplicate keys and often select boxes, radio buttons, etc may need some duplication. Here is my label_value.rb class, located in my model directory:

# Creates a Label and value for select boxes and
# forms without clearly delinieated# objects, such as Arrays.
class LabelValue
# name the accessors. Label and Value
attr_accessor :label, :value
end

Now that I have a model to work with and an Array Constant to use, I created the “helper” to use with the form tag in the application_helper.rb file, so I might use it throughout the application’s forms. There are two methods, one public, and one private. Note the “Select year” prompt:

# selection for a year value
# ‘f’ represents the passed in form value
def year_select(f)
f.collection_select(:year,year_lookup,:label,:value,{:prompt=>”Select year”})
end
# ———— PRIVATE METHODS —————–

private

def year_lookup
#create an emptycollection to hold the LabelValue Objects
years = Array.new()#populate the ArrayYEARS_ARRAY.each do |yr| y = LabelValue.new
y.label = yr
y.value = yr
years.push(y)
end
years
end

Finally, we need to call the _helper method from the form:

<tr>
<th>Grade:</th>
<td><%= grade_select(f) %></td>
</tr>

The resulting code renders a select box:

<select id=”movie_poster_year” name=”movie_poster[year]“>
<option value=”">Select year</option>
<option value=”1920″>1920</option>
<option value=”1921″>1921</option>
<option value=”1922″>1922</option>
<option value=”1923″>1923</option>
<option value=”1924″>1924</option>

# continues…

<option value=”2005″>2005</option>
<option value=”2006″>2006</option>
<option value=”2007″>2007</option>
<option value=”2007″>2007</option>
</select>

The nice thing is reusability of the LabelValue class, the YEAR Constant and the collection_select itelf. Everything can be accessed as it is needed, whether you want to use the Constant Array for something else, the LabelValue object for a different array or tuple, or the actual rendered select box in different views.

UPDATE, 1/15:

See the comments below for a nice alternative and discussion!

Create PDF    Send article as PDF   

Cocoon has been around for years with an active development community. I tried to work with it in 2002 using the books and resources available at the time, but I was using Windows at that point and the books really focused more on a Unix/Linux deployment, and at the time I didn’t have the chops to really transfer this knowledge cleanly and was really never able to set up a coherent development environment for it. Struts, on the other hand, had the resources available to get up and running. Although the documentation wasn’t that great, I could “get home” on most issues, and when the level of frustration on completing some of the things that I needed to get done was at a boiling point, I did something about it and wrote a book with George Franciscus to share what I had learned with the larger community. I wasn’t alone with this either; many great books on the subject came out. As the framework diversified and solved many problems, so did the available web and print documentation from many wonderful people.

Step forward to today. Struts 1 is ubiquitous; nearly every Java Web Application Programmer has had some experience with Struts, building upon the base and working to create other Frameworks as well extend Struts1 into Struts2. Lately, I’ve been working with projects that consume a great deal of XML services and need to be available to many different viewing platforms in a coherent manner. Not only that, but there is a tremendous amount of conditional logic that must be matched depending upon input. Cocoon handles these things very well — it’s dispatch system allows for various URL matching which gives it’s configurations a very similar feel to a lightweight Business Rule engine. I decided to give it another shot and go through the tutorials and documentation — trying to evaluate whether or not it could fit into the way that my company does business.

I proceeded to find tutorials on the Cocoon Site, DeveloperWorks and other places. What I found was at best adequate, but at worst quite disappointing. The Cocoon Community insists that you download their source code and use their scripts to Continue Reading »

The bar was set high before last Christmas dinner. We had spent a week in Rome, Siena and Florence at this point, and had driven between these towns, stopping at every espresso bar for coffee, and every restaurant to sample one thing — Spaghetti Carbonara.

It’s nothing like what you get at most run-of-the-mill restaurants, and even some so-called authentic establishments just fix you a plate of spaghetti and slather their “Alfredo Sauce” with some peas and quickly fried ham pieces in it. It’s going to take serious therapy to get out of my head what these authentic Italian Restaurants in my San Fernando Valley have prostituted themselves into . But, in our story, we were in Italy, were we not?

Sheila (my darling, patient wife) and I adore Carbonara, one of the simplest pasta dishes. Simple, but when made well, will blow the winter out of your system like a Breva in the Alps, fill your stomach with childhood memories and your eyes with adoring love for humanity. Spaghetti Carbonara is Italian Heroin — A really good portion will just make you feel so good that absolutely nothing else matters, and your craving for it will never end.

During last winter’s trip to Italy, we had sampled Carbonara in multiple locations in Rome, Siena, a small family diner in Montefiascone, and now Firenze. Continue Reading »

I often find myself writing lightweight performance tests as part of a suite of the unit tests, to make sure that my code hasn’t been written in a way to destroy the performance of an application. I also create performance tests as a standalone Application to test (for instance) how many HttpServletRequest/Response calls can be made over a certain period of time.

My requirements are normally simple, and rarely have a strong need to be COMPLETELY perfect; i.e. the actual calls to and from the timer can cause a slight time loss in the actual application, but frankly I’m willing to put up with this in most circumstances. The “TesterTimer” class featured below is pretty damned efficient, especially if you never call the “elapsed(id)” method, since the start and stop happen pretty much outside the application being tested. Here is the fully commented code with a demonstration “main” class:

Continue Reading »

“As soon as I put a man in command of the army, they all wanted ME to be the general. Now it isn’t so with Grant. He hasn’t told me what his plans are. I don’t know and I don’t want to know. I am glad to find a man who can go ahead without me. He doesn’t ask impossibilities of me, and he’s the first general I’ve had that didn’t.”

– Abraham Lincoln, upon appointing Grant to overall command of the Union Army

 

Constraints are things that we live with in our daily lives, accept them, and move on. With software engineering groups that are well-established and have strong leadership, constraints can get legs of their own and become excuses for not delivering projects on time, not building something correctly, or even stepping outside Enterprise Goals because “they do not fit within our design.” Many times the inability for an engineering team to confront a constraint will go so far as to create blame, cast dispersion and create a poison atmosphere to anyone that “gets in their way”. Continue Reading »

I fly at least every 6 weeks, and I try very hard to be a good neighbor in what usually ends up being a quiet, uncrowded flight. I try to fly during the week and early in the morning because the planes are not as full, and if they are, they usually contain like-minded individuals that travel a lot and have a halfway-decent standard of Passenger Etiquette.

Passenger Etiquette

Passenger Etiquette is how you treat the people around you, how you allow for “personal space” and live within a “do unto others” envelop of manners. I often see persons that get on planes that do things that portray them in a light in a range from uncaring up to and including total douche-baggery.

The following are a few points to contemplate before you step on another crowded plane; they allow for others to enjoy a flight as much as humanly possible, which is often at the very edge of human endurance anyway: Continue Reading »

Is your Engineering Development project stalled in QA? Have expectations throughout your organization been lowered to the point where extremely long, ponderous QA cycles are planned for and expected? This could be the result of an Involuntary Prototyping process that can become a trap that is expensive, not only from a product time line/opportunity cost basis, but also in employee morale and resource turnover.

Projects in general are date-driven, that is the business goals are to deploy something in a specified time-frame and with necessary features with reasonable quality. Planning around this is difficult but not impossible — often QA takes a back seat to development with respect to time-weight concerns, causing any delays in development to be thrown on the backs of QA teams, demanding that they complete the same amount of testing with less time. As development is delayed, the reluctance to accurately modify the schedule to a reasonable period decreases — stakeholders become more and more optimistic with their times, and consequently, any shock to the system will result in blown schedules and scrambling to “make the date”. This scrambling can bring on Involuntary Prototyping — when this happens, be prepared for a bear to eat your schedule and poop it off a cliff.

Continue Reading »

IntelliJ’s support of JBoss-rules in no way compares to Eclipse, but even that isn’t enough to get me off my butt and move from my fave. That said, I created a semi-literate FileType for the drl files so that IntelliJ will recognize them and you will get a decent amount of syntax highlighting. Is it perfect?

No.

But it will probably get you home. Here’s what you do. Create a new filetype and make sure that it includes ?*.drl files. Once that is created, it makes a filetype with the name you gave it in the

%User%/Library/Preferences/IntellijIDEA%your-release-version-here%/filetypes/%name_of_file.xml%

directory (at least that’s what it looks like on my Mac – I haven’t touched a windows box in years, but it should have some parallels.

Open the XML file and replace anything between the <filetype> nodes with:

Continue Reading »

I have to admit that I don’t write as much Java as I used to. I spend a ton of time in design, using UML tools (Visual Paradigm is my favorite). Lately, I’ve spent some time digging into the JBoss-rules framework – really getting to understand it and draw comparisons between it and JESS, CLIPS and other rule engines. So I’ve had the joy of coming back to my IDE of choice for the last 6 years – IntelliJ – now about to release its 7th incarnation.

I have to admit a serious bias, but I don’t get all hung up on the “my tool is better than yours” debate. I’ve used the others, and I have a lot of smart friends that love Eclipse and NetBeans as well. I even have some very smart friends that prefer tabs to spaces, but frankly, why go down the path of flames? It’s important to talk about what works for me, and if you’ve never used IntelliJ, maybe it’s worth the effort. Yeah, it costs money, but frankly if you’re in the business of making money with your code, it’s trivial, especially when you start looking at the features that come “free” with it, and start looking at the extra cost of plugins for some of the competitors.

Why do I like it? Probably familiarity, definitely because it allows me to effortlessly get what’s in my head onto the screen in a seamless manner. I also credit this IDE for making me a better code-writer and giving me the confidence in my abilities to make the next steps in my career, challenge myself with new libraries and frameworks, and make my code professional and complete with documentation and unit tests.

Is it for everyone? Frankly, I don’t care if it is or isn’t. If you’re new to Java, or have no particular fondness for your IDE, I would heartily recommend IntelliJ immediately, and encourage complete understanding of its capabilities. If you are fine with your IDE but don’t particularly like Java, I would also recommend IntelliJ, as I found a fondness for the language and what I can accomplish with it all over again. Also if I quality, ease of use and completeness of the product are key considerations, you get what you pay for with IntelliJ and their plugins. Eclipse, in my opinion, tends to have a little “anarchy” associated with it and it’s overall “finish”.

If you love your IDE, well, tools that feel comfortable in your hand make you productive, so have fun with that.

I like the fact that there is Eclipse, NetBeans and IntelliJ to play back and forth with. They end up making each other better, and since there is enough of a user-base for them to stay around for awhile, this will continue.

BTW, if you are a Ruby on Rails supporter, the latest versions of IntelliJ support it out of the box, and they do it VERY WELL. I just wished that they had a JBoss-rules plugin like Eclipse does, because it has chapped me to no end…

As a Post-Script, Bob Congdon writes back in 2004: “Personally I think it’s healthy that there are a number of Java IDE alternatives. But you have to wonder how long IDE vendors will be able to survive against Eclipse.” Asked and answered?

PDF    Send article as PDF   

eCommerce and Revenue-Generating Site stakeholders often worry about traffic; they think that if they don’t get enough they aren’t going to make any money. There is a little truth to that, but more important is getting good quality traffic before you go after lots of users.

If you get 10,000 hits per week with a conversion rate of 5% with a dollar in revenue per conversion, that’s $500 per week you’ll make. If you rely on more users, doubling your income means doubling the number of hits, which means increasing your user base to 20,000. If you want to double your income with the same number of users, then your conversion rate will need to be 10%. Pretty simple, right?
Continue Reading »

« Newer Posts - Older Posts »

Bad Behavior has blocked 159 access attempts in the last 7 days.