Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fallback to default partial if one in custom directory not found
  • Loading branch information
bogdan committed Apr 21, 2015
1 parent c62247b commit a7c6269
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/views/datagrid/_form.html.erb
Expand Up @@ -2,7 +2,7 @@
<% grid.filters.each do |filter| %>
<div class="datagrid-filter filter">
<%= f.datagrid_label filter %>
<%= f.datagrid_filter filter %>
<%= f.datagrid_filter filter, partials: options[:partials] %>
</div>
<% end %>
<div class="datagrid-actions">
Expand Down
4 changes: 4 additions & 0 deletions lib/datagrid/filters/base_filter.rb
Expand Up @@ -99,6 +99,10 @@ def default_filter(value, scope, grid)
end
end

def supports_range?
self.class.ancestors.include?(::Datagrid::Filters::RangedFilter)
end

def format(value)
value.nil? ? nil : value.to_s
end
Expand Down
22 changes: 18 additions & 4 deletions lib/datagrid/form_builder.rb
Expand Up @@ -7,6 +7,8 @@ module FormBuilder
def datagrid_filter(filter_or_attribute, options = {}, &block)
filter = datagrid_get_filter(filter_or_attribute)
options = add_html_classes(options, filter.name, datagrid_filter_html_class(filter))
# Prevent partials option from appearing in HTML attributes
options.delete(:partials) unless supports_partial?(filter)
self.send(filter.form_builder_helper_name, filter, options, &block)
end

Expand Down Expand Up @@ -48,17 +50,17 @@ def datagrid_default_filter(attribute_or_filter, options = {})
end

def datagrid_enum_filter(attribute_or_filter, options = {}, &block)
options = options.clone
filter = datagrid_get_filter(attribute_or_filter)
if filter.checkboxes?
partial = partial_path(options, 'enum_checkboxes')
options = add_html_classes(options, 'checkboxes')
elements = object.select_options(filter).map do |element|
text, value = @template.send(:option_text_and_value, element)
checked = enum_checkbox_checked?(filter, value)
[value, text, checked]
end
@template.render(
:partial => partial_path(options, 'enum_checkboxes'),
:partial => partial,
:locals => {
:elements => elements,
:form => self,
Expand Down Expand Up @@ -128,6 +130,7 @@ def datagrid_dynamic_filter(attribute_or_filter, options = {})
def datagrid_range_filter(type, attribute_or_filter, options = {})
filter = datagrid_get_filter(attribute_or_filter)
if filter.range?
partial = partial_path(options, 'range_filter')
options = options.merge(:multiple => true)


Expand All @@ -150,7 +153,7 @@ def datagrid_range_filter(type, attribute_or_filter, options = {})
I18n.t(format_key, :from_input => from_input, :to_input => to_input).html_safe
else
# More flexible way to render via partial
@template.render :partial => partial_path(options, 'range_filter'), :locals => {
@template.render :partial => partial, :locals => {
:from_options => from_options, :to_options => to_options, :filter => filter, :form => self
}
end
Expand Down Expand Up @@ -214,7 +217,18 @@ def add_html_classes(options, *classes)
end

def partial_path(options, name)
File.join(options.delete(:partials) || 'datagrid', name)
if partials = options.delete(:partials)
partial_name = File.join(partials, name)
# Second argument is []: no magical namespaces to lookup added from controller
if @template.lookup_context.template_exists?(partial_name, [], true)
return partial_name
end
end
File.join('datagrid', name)
end

def supports_partial?(filter)
(filter.supports_range? && filter.range?) || (filter.type == :enum && filter.checkboxes?)
end

class Error < StandardError
Expand Down
29 changes: 29 additions & 0 deletions spec/datagrid/form_builder_spec.rb
Expand Up @@ -58,6 +58,13 @@ class MyTemplate
it { should equal_to_dom(
'<input class="group_id integer_filter" id="report_group_id" name="report[group_id]" size="30" type="text"/>'
)}

context "when partials option is passed for filter that don't support range" do
let(:_filter_options) { {partials: 'anything' } }
it { should equal_to_dom(
'<input class="group_id integer_filter" id="report_group_id" name="report[group_id]" size="30" type="text"/>'
)}
end
end

context "with date filter type" do
Expand Down Expand Up @@ -130,6 +137,23 @@ class MyTemplate
)}
end

context "with custom partials option and template exists" do
let(:_filter_options) { { :partials => 'custom_range' } }
let(:_range) { nil }
it { should equal_to_dom(
"custom_range_partial"
) }
end

context "when custom partial doesn't exist" do
let(:_filter_options) { { :partials => 'not_existed' } }
let(:_range) { nil }
it { should equal_to_dom(
'<input class="group_id integer_filter from" multiple name="report[group_id][]" size="30" type="text"><span class="separator integer"> - </span><input class="group_id integer_filter to" multiple name="report[group_id][]" size="30" type="text">'
) }

end

context "when deprecated format translation specified" do
let(:_range) { nil }
around(:each) do |example|
Expand Down Expand Up @@ -314,6 +338,11 @@ class MyTemplate


end

context "when partials option passed and partial exists" do
let(:_filter_options) { {partials: 'custom_checkboxes'} }
it { should equal_to_dom('custom_enum_checkboxes') }
end
end
end

Expand Down
23 changes: 23 additions & 0 deletions spec/datagrid/helper_spec.rb
Expand Up @@ -455,6 +455,29 @@ def param_name
"form.datagrid-form input[name='g[id]']" => 1,
)
end

it "takes default partials if custom doesn't exist" do
class PartialDefaultGrid
include Datagrid
scope {Entry}
filter(:id, :integer, :range => true)
filter(:group_id, :enum, :multiple => true, :checkboxes => true, :select => [1,2])
def param_name
'g'
end
end
rendered_form = subject.datagrid_form_for(PartialDefaultGrid.new, {
:url => '',
:partials => 'custom_form'
})
expect(rendered_form).to include 'form_partial_test'
expect(rendered_form).to match_css_pattern([
'input.integer_filter.from',
'input.integer_filter.to',
".enum_filter input[value='1']",
".enum_filter input[value='2']",
])
end
end


Expand Down
5 changes: 5 additions & 0 deletions spec/support/matchers.rb
Expand Up @@ -42,6 +42,11 @@ def force_encoding(text)
class CssPattern
def initialize(pattern)
@css_pattern = pattern
unless @css_pattern.is_a?(Hash)
@css_pattern = Array(@css_pattern).map do |key|
[key, 1]
end
end
end

def error!(message)
Expand Down
@@ -0,0 +1 @@
custom_enum_checkboxes
7 changes: 7 additions & 0 deletions spec/support/test_partials/custom_form/_form.html.erb
@@ -0,0 +1,7 @@
<%= form_for grid, options do |f| -%>
<p>form_partial_test</p>
<% grid.filters.each do |filter| %>
<%= f.datagrid_label filter %>
<%= f.datagrid_filter filter, partials: options[:partials] %>
<% end %>
<% end -%>
@@ -0,0 +1 @@
custom_range_partial

0 comments on commit a7c6269

Please sign in to comment.