I use "integrate_views" to combine controller and view specs. The thing I discovered just now is that if you have nested describes, you have to place "integrate_views" inside the inner describe or "have_tag" won't work.
The error you typically get otherwise (even when the element exists) is:
Expected at least 1 element matching "div#content", found 0.
For this particular project I'm using RSpec 1.1.3.
Tuesday, May 20, 2008
Monday, March 31, 2008
In Place Editor using Scriptaculous and Prototype in Rails 2.0
I've been using the scriptaculous InPlaceEditor and thought I would share some useful snippets.
The code is setup to save on blur and being able to handle empty fields.
The creation script:
In the view I have a bit of script that binds the callbacks for success and failure.
The validation errors is displayed in the error notice, this isn't exactly ideal, but works as an example of how to handle validation.
The controller code looks something like this:
And finally I create them using something like this:
Though I recommend wrapping it in a helper to keep it nice and DRY...
The code is setup to save on blur and being able to handle empty fields.
The creation script:
var inplace_editor_edit_hint = 'Click to edit...';
function createInplaceEditor(field, update_path, highlightcolor,
highlightendcolor, width)
{
fillIfEmpty($(field));
new Ajax.InPlaceEditor(field, update_path, {
highlightcolor: highlightcolor,
highlightendcolor: highlightendcolor,
okButton: false,
cancelLink: false,
submitOnBlur: true,
cols: width,
callback: function(form) {
input_field = form.elements[0];
// This is to ensure we don't save the edit hint if
// the user accidentally clicked an empty field
if(input_field.value == inplace_editor_edit_hint)
input_field.value = '';
return Form.serialize(form);
},
onComplete: function(transport, element)
{
fillIfEmpty(element);
if(transport.statusText != "Internal Server Error")
onEditorSuccess(); // cb
new Effect.Highlight(element, {
startcolor: this.options.highlightcolor,
endcolor: this.options.highlightendcolor
});
},
onFailure: function(element, transport) {
onEditorFailure(transport.responseText); // cb
}
});
}
function fillIfEmpty(element)
{
if(element.innerHTML == '')
element.innerHTML = inplace_editor_edit_hint;
}
In the view I have a bit of script that binds the callbacks for success and failure.
The validation errors is displayed in the error notice, this isn't exactly ideal, but works as an example of how to handle validation.
function onEditorSuccess()
{
$('error').innerHTML = '';
$('notice').innerHTML = 'Model was successfully updated.';
}
function onEditorFailure(response)
{
response = response.evalJSON();
var lines = new Array();
for(var i = 0; i < response.length; i++)
lines.push(response[i][0] + ' ' + response[i][1]);
$('notice').innerHTML = '';
$('error').innerHTML = 'Error: ' + lines.join('. ')
}
The controller code looks something like this:
def update_field
model = Model.find(params[:id])
if model.update_attributes(field_to_update => params[:value])
render :text => params[:value]
else
render :text => model.errors.to_json, :status => 500
end
end
private
def field_to_update
params[:editorId].split('_')[1]
end
And finally I create them using something like this:
<p id="prefix_title"><%= model.title %></p>
<script type="text/javascript">
createInplaceEditor("prefix_title", '/path/to/action',
"#FFFFFF", "#AAAAAA", 10);
</script>
Though I recommend wrapping it in a helper to keep it nice and DRY...
Tuesday, February 5, 2008
HABTM issue with Rails 2.0.2 and SQLite3
When using SQLite3 with rails you must make sure not to add an id column to your join tables (probably a good idea in any case).
The error I got when I did have an id column where something like this:
To avoid this, make sure you include :id => false when creating the join table:
The error I got when I did have an id column where something like this:
SQL logic error or missing database: INSERT INTO
podcasts_tags("podcast_id", "id", "tag_id") VALUES (3, 3, 1)
To avoid this, make sure you include :id => false when creating the join table:
create_table :podcast_tags, :id => false do |t|
t.integer :podcast_id, :null => false
t.integer :tag_id, :null => false
end
Monday, January 28, 2008
NCover in a relative path
Just a small note for anyone trying to get NCover to run in a relative path.
Make sure you include CoverLib.dll and register it (run Regsvr32 CoverLib.dll).
I had errors where it would run on one computer but not on another and that when it couldn't find the dll it's simply froze after the unit tests had run. I'm using v1.5.8.
Make sure you include CoverLib.dll and register it (run Regsvr32 CoverLib.dll).
I had errors where it would run on one computer but not on another and that when it couldn't find the dll it's simply froze after the unit tests had run. I'm using v1.5.8.
Monday, November 26, 2007
Interaction design, UCD and Agile
Today I was at yet another seminar, this time at Agical about how UCD combined with Agile methods does work and how it can be beneficial.
It began with Illugi Ljótsson och Eric Idebro describing the "write a big document and throw it over the wall"-problem and that UCD and Agile development methods weren't that far apart if they where only allowed to work together, iteratively.
To allow for this they would have an interaction designer in their scrum. In a 4 week scrum the interaction designer helped with usability and each friday he did user testing to get feedback for the coming week.
I got the impression that this kind of feedback would work and that it would help a great deal to have that kind of expertise at hand during development.
Do we really need a 4 step publication routine with document comparison if all the users really need is a way to get text out on the web quickly?
They also made the point that what the product owner asked for and what the users actually needed where quite some way apart. When using this method you have more material to help guide your decisions on what to build in addition to making the application more user friendly.
It began with Illugi Ljótsson och Eric Idebro describing the "write a big document and throw it over the wall"-problem and that UCD and Agile development methods weren't that far apart if they where only allowed to work together, iteratively.
To allow for this they would have an interaction designer in their scrum. In a 4 week scrum the interaction designer helped with usability and each friday he did user testing to get feedback for the coming week.
I got the impression that this kind of feedback would work and that it would help a great deal to have that kind of expertise at hand during development.
Do we really need a 4 step publication routine with document comparison if all the users really need is a way to get text out on the web quickly?
They also made the point that what the product owner asked for and what the users actually needed where quite some way apart. When using this method you have more material to help guide your decisions on what to build in addition to making the application more user friendly.
Thursday, November 15, 2007
Andy Hunt: ”How Hard Can it Be?” at Valtech
Today I was at a seminar by Andy Hunt called "How Hard Can It Be?" at Valtech (Stockholm, Sweden).
It was a great and very funny seminar and I recommend anyone that has a chance to go to the next one.
One thing that got to me was the discussion about complexity... that is accidental complexity and essential complexity. One example he used was the way a language like Java could isn't very intuitive for people new to development compared to a language like Ruby due to accidental complexity.
In Java you would need "public static void Main(.... System.out.println(..." for the basic Hello, World example, whereas in Ruby you would type 'puts "Hello, World!"'.
IDE's do us a great disservice in hiding accidental complexity by using macros to autogenerate code that is required by the language, but probably isn't of any value for the task at hand.
The point of it all is that when the domain is complex, we have essential complexity, but we should avoid accidental complexity, extra code that creates noice, making the code less readable without adding any value.
He also hinted on the irony of the waterfall method that it wasn't supposed to be used the way it has and in fact the document describing the method was a example of a flawed, non-working model of development.
Some time ago I found a really nice book that uses this way of avoiding accidental complexity that Ruby does for introducing newcomers to programming, Learn to program (pragmatic bookshelf).
On the way home I listened to a podcast about RoR, the Ruby on Rails Podcast episode featuring Stuart Halloway. I've only listened to one episode so far but it was great :), got me thinking of contributing a bit more to Open Source.
It was a great and very funny seminar and I recommend anyone that has a chance to go to the next one.
One thing that got to me was the discussion about complexity... that is accidental complexity and essential complexity. One example he used was the way a language like Java could isn't very intuitive for people new to development compared to a language like Ruby due to accidental complexity.
In Java you would need "public static void Main(.... System.out.println(..." for the basic Hello, World example, whereas in Ruby you would type 'puts "Hello, World!"'.
IDE's do us a great disservice in hiding accidental complexity by using macros to autogenerate code that is required by the language, but probably isn't of any value for the task at hand.
The point of it all is that when the domain is complex, we have essential complexity, but we should avoid accidental complexity, extra code that creates noice, making the code less readable without adding any value.
He also hinted on the irony of the waterfall method that it wasn't supposed to be used the way it has and in fact the document describing the method was a example of a flawed, non-working model of development.
Some time ago I found a really nice book that uses this way of avoiding accidental complexity that Ruby does for introducing newcomers to programming, Learn to program (pragmatic bookshelf).
On the way home I listened to a podcast about RoR, the Ruby on Rails Podcast episode featuring Stuart Halloway. I've only listened to one episode so far but it was great :), got me thinking of contributing a bit more to Open Source.
Thursday, November 8, 2007
Degrading link_to_remote
I've been learning RubyOnRails and I noticed that the 'link_to_remote' helper didn't degrade gracefully...
Here's the fix I used (Inspired by this blog post).
(in helpers/application_helper.rb)
Just keep in mind that this uses a HTTP GET when javascript is disabled. A HTTP GET should not have side effects. I can't think of a good way to do a HTTP POST from a link (trigger a form-post) without using javascript.
Here's the fix I used (Inspired by this blog post).
(in helpers/application_helper.rb)
def link_to_remote(name, options = {}, html_options = {})
unless html_options[:href]
html_options[:href] = url_for(options[:url])
end
link_to_function(name, remote_function(options),
html_options)
end
Just keep in mind that this uses a HTTP GET when javascript is disabled. A HTTP GET should not have side effects. I can't think of a good way to do a HTTP POST from a link (trigger a form-post) without using javascript.
Labels:
code,
frameworks,
Javascript,
learning,
Ruby,
RubyOnRails,
Web
Subscribe to:
Posts (Atom)