Skip to content

Commit 4f6f318

Browse files
committed
Add better default for file_field :label
Added josiahivey's change to add a more reasonable default for cms_file_field. Had to rework slightly so that <label for=> will correctly match the file. Update features.
1 parent 87f75b5 commit 4f6f318

File tree

9 files changed

+23
-9
lines changed

9 files changed

+23
-9
lines changed

app/helpers/cms/form_builder.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,16 @@ def cms_label(field, label_value)
6666
#
6767
# @param [Symbol] method The name of the model this form upload is associated with
6868
# @param [Hash] options
69-
# @option options [String] :label (Data)
69+
# @option options [String] :label (method) If no label is specified, the human readable name for method will be used.
7070
# @option options [String] :instructions (blank) Helpful tips for the person entering the field, appears blank if nothing is specified.
7171
# @option options [Boolean] :edit_path (false) If true, render a text field to allow users to edit path for this file.
7272
# @option options [Boolean] :edit_section (false) If true, render a select box which allows users to choose which section this attachment should be placed in.
7373
def cms_file_field(method, options={})
7474
@object.ensure_attachment_exists if @object.respond_to?(:ensure_attachment_exists)
75+
76+
unless options[:label]
77+
options[:label] = method.to_s.humanize
78+
end
7579
render_form_field("file_field", method, options)
7680
end
7781

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<%= f.cms_text_field :name %>
2-
<%= f.cms_file_field :file, :label => "File", :instructions=>"Select a file to upload.", edit_path: true, edit_section: true %>
2+
<%= f.cms_file_field :file, :instructions=>"Select a file to upload.", edit_path: true, edit_section: true %>
33
<%= f.cms_tag_list %>
44

55

app/views/cms/form_builder/_cms_file_field.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<% if a.object.attachment_name == method.to_s %>
1111
<%= a.hidden_field :attachment_name, :value => method.to_s %>
1212
<div class="fields file_fields">
13-
<%= a.cms_label method, cms_options[:label] %>
13+
<%= a.cms_label :data, cms_options[:label] %>
1414
<div id="<%= object_name %>_<%= method %>_div" class="file_inputs">
1515
<%= a.file_field :data, options.merge('data-purpose'=>"cms_file_field") %>
1616
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<%= f.cms_text_field :name %>
2-
<%= f.cms_file_field :file, :label => "File", :instructions=>"Select a file to upload.", edit_path: true, edit_section: true %>
2+
<%= f.cms_file_field :file, :label => "Image", :instructions=>"Select a file to upload.", edit_path: true, edit_section: true %>
33
<%= f.cms_tag_list %>

features/content_blocks/add_images.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Feature: Image Blocks
1212

1313
Scenario: Creating image block
1414
When I fill in "Name" with "Giraffe"
15-
And I attach the file "test/fixtures/giraffe.jpeg" to "File"
15+
And I upload an image named "test/fixtures/giraffe.jpeg"
1616
And I select "My Site" from "section_id"
1717
And I fill in "Path" with "/giraffe.jpeg"
1818
And I Save And Publish
@@ -32,15 +32,15 @@ Feature: Image Blocks
3232

3333
Scenario: Missing Path
3434
When I fill in "Name" with "Giraffe"
35-
When I attach the file "test/fixtures/giraffe.jpeg" to "File"
35+
And I upload an image named "test/fixtures/giraffe.jpeg"
3636
And I Save And Publish
3737
Then I should see "Path can't be blank"
3838

3939
Scenario: With Existing Path
4040
Given an image with path "/giraffe.jpeg" exists
4141
And I am adding a New Image
4242
When I fill in "Name" with "Giraffe"
43-
And I attach the file "test/fixtures/giraffe.jpeg" to "File"
43+
And I upload an image named "test/fixtures/giraffe.jpeg"
4444
And I fill in "Path" with "/giraffe.jpeg"
4545
When I Save And Publish
4646
Then I should see "Path must be unique."

features/content_blocks/form_controls.feature

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ Feature: Form Controls
66
Background:
77
Given I am logged in as a Content Editor
88

9-
Scenario: File Field Helper
9+
Scenario: Default File Field
1010
Given I am adding a new File
1111
Then I should see a label named "File"
1212
And I should see a file upload button
1313
And I should see the following instructions:
1414
| Select a file to upload |
1515

16+
Scenario: Setting a label named 'Name' for File Field
17+
Given I am adding a New Image
18+
Then I should see a label named "Image"
19+
1620
Scenario: Multiple Individual Attachments
1721
Given I am creating a new block which has two attachments
1822
Then I should see two file uploads
@@ -35,3 +39,4 @@ Feature: Form Controls
3539

3640

3741

42+

features/step_definitions/form_control_steps.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ def create_new_product(upload_both_files=true)
3939
@block = Product.last
4040
end
4141

42+
When /^I upload an image named "([^"]*)"$/ do |path|
43+
attach_file("Image", File.expand_path(path))
44+
end
45+
4246
When /^I edit that block$/ do
4347
visit "/cms/#{@block.class.path_name}/#{@block.id}/edit"
4448
end

features/step_definitions/image_steps.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
visit '/cms/image_blocks/new'
7070
fill_in "Name", :with => "Giraffe"
7171
fill_in "Path", :with => path
72-
attach_file "File", "test/fixtures/giraffe.jpeg"
72+
attach_file "Image", "test/fixtures/giraffe.jpeg"
7373
click_button "true"
7474
end
7575

features/step_definitions/web_steps.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def with_scope(locator)
102102
attach_file(field, File.expand_path(path))
103103
end
104104

105+
105106
Then /^(?:|I )should see "([^"]*)"$/ do |text|
106107
if page.respond_to? :should
107108
page.should have_content(text)

0 commit comments

Comments
 (0)