Skip to content

Commit e1fe9d2

Browse files
committed
[browsermedia#623] Make path automatically detected
If a file_picker needs to render a path field, it will.
1 parent ed1e72a commit e1fe9d2

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

app/inputs/file_picker_input.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# @param [Hash] options
55
# @option options [String] :label (method) If no label is specified, the human readable name for method will be used.
66
# @option options [String] :hint (blank) Helpful tips for the person entering the field, appears blank if nothing is specified.
7-
# @option options [Boolean] :edit_path (false) If true, render a text field to allow users to edit path for this file.
87
# @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.
98
class FilePickerInput < SimpleForm::Inputs::Base
109

@@ -13,7 +12,7 @@ def input
1312
object.ensure_attachment_exists if object.respond_to?(:ensure_attachment_exists)
1413

1514
html = ""
16-
if options[:edit_section]
15+
if render_section_picker?
1716
sections = sections_with_full_paths
1817
sections.each do |s|
1918
html << template.tag(:span, :class => "section_id_map", style: 'display: hidden', :data => {:id => s.id, :path => s.prependable_path})
@@ -22,10 +21,10 @@ def input
2221
@builder.simple_fields_for :attachments do |a|
2322
html << a.hidden_field("attachment_name", value: attribute_name.to_s)
2423
html << a.file_field(:data, input_html_options.merge('data-purpose' => "cms_file_field"))
25-
if options[:edit_section]
24+
if render_section_picker?
2625
html << a.input(:section_id, collection: sections, label_method: :full_path, include_blank: false, label: "Section", input_html: {'data-purpose' => "section_selector"})
2726
end
28-
if options[:edit_path]
27+
if render_path_input?
2928
klass = object.new_record? ? "suggest_file_path" : "keep_existing_path"
3029
html << a.input(:data_file_path, label: "Path", input_html: {class: klass})
3130
end
@@ -34,6 +33,15 @@ def input
3433

3534
end
3635

36+
def render_path_input?
37+
object.respond_to?(:set_attachment_path)
38+
end
39+
40+
def render_section_picker?
41+
#object.respond_to? :set_attachment_section
42+
options[:edit_section]
43+
end
44+
3745

3846
def sections_with_full_paths
3947
root = Cms::Section.root.first
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<%= f.input :name %>
2-
<%= f.input :file, hint: "Select a file to upload.", as: :file_picker, edit_path: true, edit_section: true %>
2+
<%= f.input :file, hint: "Select a file to upload.", as: :file_picker, edit_section: true %>
33
<%= f.input :tag_list, as: :tag_list, label: "Tags", input_html: {autocomplete: 'off'}, hint: "A space separated list of tags." %>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<%= f.input :name %>
2-
<%= f.input :file, :label => "Image", :hint=>"Select a file to upload.", as: :file_picker, edit_path: true, edit_section: true %>
2+
<%= f.input :file, :label => "Image", :hint=>"Select a file to upload.", as: :file_picker, edit_section: true %>
33
<%= f.input :tag_list, as: :tag_list, label: "Tags", input_html: {autocomplete: 'off'}, hint: "A space separated list of tags." %>

lib/cms/behaviors/attaching.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ def validates_attachment_content_type(name, options = {})
140140
end
141141
end
142142

143-
# Define at :set_attachment_path if you would like to override the way file_path is set
143+
# Define the #set_attachment_path method if you would like to override the way file_path is set.
144+
# A path input will be rendered for content types having #set_attachment_path.
144145
def handle_setting_attachment_path
145146
if self.respond_to? :set_attachment_path
146147
set_attachment_path

0 commit comments

Comments
 (0)