Skip to content
Browse files

data_file as an activemodel so we can have easy sweet validations

  • Loading branch information...
1 parent 76761ce commit edd4906fc9d216448440b600f3c6db4d8407bd1d @arnab committed Mar 22, 2012
View
13 app/assets/stylesheets/custom.css.scss
@@ -117,3 +117,16 @@ input, textarea, select, .uneditable-input {
margin-bottom: 15px;
@include box_sizing;
}
+
+#error_explanation {
+ color:#f00;
+ ul {
+ list-style: none;
+ margin: 0 0 18px 0;
+ }
+}
+
+.field_with_errors {
+ @extend .control-group;
+ @extend .error;
+}
View
10 app/helpers/data_files_helper.rb
@@ -1,2 +1,12 @@
module DataFilesHelper
+ def example_input_file_link
+ 'https://raw.github.com/lschallenges/data-engineering/master/example_input.tab'
+ end
+
+ def data_file_label_content_with_link
+ raw(
+ "Please upload a tab-delimited file, like " +
+ "<a href='#{example_input_file_link}'>this</a>."
+ )
+ end
end
View
6 app/views/data_files/new.html.haml
@@ -4,11 +4,9 @@
.row
.span6.offset4
= form_for(@data_file) do |f|
+ = render 'shared/error_messages', :object => f.object
.span6
- = f.label :data, 'Please upload a tab-delimited file'
- %span.help-block
- example:
- = link_to 'here', 'https://raw.github.com/lschallenges/data-engineering/master/example_input.tab'
+ = f.label :data, data_file_label_content_with_link
= f.file_field :data, :class => 'input-xlarge'
.span6
= f.submit "Upload", :class => "btn btn-large btn-primary"
View
9 app/views/shared/_error_messages.html.haml
@@ -0,0 +1,9 @@
+- if object.errors.any?
+ #error_explanation
+ .alert.alert-error
+ The form contains
+ = pluralize(object.errors.count, "error")
+ \.
+ %ul
+ - object.errors.full_messages.each do |msg|
+ %li= msg
View
1 features/file_upload.feature
@@ -8,7 +8,6 @@ Scenario: Home Page should be the file import page
Then I should see "Import Your Data"
Then I should see a "Upload" button
-@wip
Scenario: Not uploading a file
When I go to the file import page
And I click "Upload"
View
2 features/step_definitions/common_steps.rb
@@ -22,7 +22,7 @@
end
Then /^I should see (\d+) validation errors?$/ do |num|
- expected_str = "The file contains #{num} error"
+ expected_str = "The form contains #{num} error"
expected_str += "s" if num.to_i > 1
page.should have_content(expected_str)
end
View
15 spec/helpers/data_files_helper_spec.rb
@@ -1,15 +0,0 @@
-require 'spec_helper'
-
-# Specs in this file have access to a helper object that includes
-# the FilesHelper. For example:
-#
-# describe FilesHelper do
-# describe "string concat" do
-# it "concats two strings with spaces" do
-# helper.concat_strings("this","that").should == "this that"
-# end
-# end
-# end
-describe DataFilesHelper do
- pending "add some examples to (or delete) #{__FILE__}"
-end
View
5 spec/models/data_file_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe DataFile do
+ it_should_behave_like "ActiveModel"
+end
View
19 spec/support/active_model_lint.rb
@@ -0,0 +1,19 @@
+# From https://gist.github.com/639089
+
+shared_examples_for "ActiveModel" do
+ require 'test/unit/assertions'
+ require 'active_model/lint'
+ include Test::Unit::Assertions
+ include ActiveModel::Lint::Tests
+
+ # to_s is to support ruby-1.9
+ ActiveModel::Lint::Tests.public_instance_methods.map{|m| m.to_s}.grep(/^test/).each do |m|
+ example m.gsub('_',' ') do
+ send m
+ end
+ end
+
+ def model
+ subject
+ end
+end

0 comments on commit edd4906

Please sign in to comment.
Something went wrong with that request. Please try again.